Loading...
Searching...
No Matches
gpr::ConditionalKernelProjection Class Reference

#include <gpr/covariance_functions/ConditionedKernel.h>

Classes

struct  Factor

Public Member Functions

 ConditionalKernelProjection ()
 ~ConditionalKernelProjection ()
 ConditionalKernelProjection (ConditionalKernelProjection &&) noexcept
ConditionalKernelProjection & operator= (ConditionalKernelProjection &&) noexcept
bool prepare (const Eigen::MatrixXd &joint, Eigen::Index anchor_rows, Eigen::Index query_rows, Eigen::Index dimensions, const Eigen::VectorXd &anchor_noise, Eigen::MatrixXd &covariance, const std::shared_ptr< linalg::DistributedState > &execution={})
bool prepare (const Eigen::MatrixXd &joint, const Eigen::MatrixXd &fixed, Eigen::Index anchor_rows, Eigen::Index query_rows, Eigen::Index dimensions, const Eigen::VectorXd &anchor_noise, Eigen::MatrixXd &covariance, const std::shared_ptr< linalg::DistributedState > &execution={})
bool prepare (const KernelOperator &kernel, const Coord &anchors, const Coord &query, const Eigen::VectorXd &anchor_noise, Eigen::MatrixXd &covariance, const std::shared_ptr< linalg::DistributedState > &execution={})
Eigen::MatrixXd derivative (const Eigen::MatrixXd &matrix) const
Eigen::MatrixXd adjoint (const Eigen::MatrixXd &conditional_weight) const
Eigen::MatrixXd adjointRows (const Eigen::MatrixXd &conditional_weight, Eigen::Index first, Eigen::Index count) const
Eigen::MatrixXd observationLiftRows (Eigen::Index first, Eigen::Index count) const
std::shared_ptr< const KernelOperator > intoKernel (std::shared_ptr< const KernelOperator > kernel, Coord anchors) &&

Detailed Description

A Schur-complement projection at one hyperparameter point. Parameter derivatives can be consumed in bounded batches using the same weights.

Definition at line 20 of file ConditionedKernel.h.

Constructor & Destructor Documentation

◆ ConditionalKernelProjection() [1/2]

gpr::ConditionalKernelProjection::ConditionalKernelProjection ( )
default

◆ ~ConditionalKernelProjection()

gpr::ConditionalKernelProjection::~ConditionalKernelProjection ( )
default

◆ ConditionalKernelProjection() [2/2]

gpr::ConditionalKernelProjection::ConditionalKernelProjection ( ConditionalKernelProjection && )
defaultnoexcept

Member Function Documentation

◆ adjoint()

Eigen::MatrixXd gpr::ConditionalKernelProjection::adjoint ( const Eigen::MatrixXd & conditional_weight) const
nodiscard

Frobenius adjoint on symmetric joint covariance perturbations. Applying it once to a likelihood weight avoids a projection per parameter.

Definition at line 744 of file ConditionedKernel.cpp.

746{
747 const Eigen::Index nq = query_rows_ * (dimensions_ + 1);
748 if (!valid_ || conditional_weight.rows() != nq ||
749 conditional_weight.cols() != nq || !conditional_weight.allFinite())
750 throw std::invalid_argument("invalid conditioned kernel weight");
751 const Eigen::MatrixXd qq = symmetric(conditional_weight);
752 const Eigen::MatrixXd aq = -weights_ * qq;
753 const Eigen::Index rows = anchor_rows_ + query_rows_;
754 const Eigen::Index channels = rows * (dimensions_ + 1);
755 Eigen::MatrixXd result(channels, channels);
756 for (Eigen::Index a = 0; a <= dimensions_; ++a) {
757 // A channel of anchor rows bounds the product workspace without
758 // retaining another full anchor matrix beside the joint adjoint.
759 const Eigen::MatrixXd aa_rows =
760 -aq.middleRows(a * anchor_rows_, anchor_rows_) *
761 weights_.transpose();
762 for (Eigen::Index b = 0; b <= dimensions_; ++b) {
763 result.block(a * rows, b * rows, anchor_rows_, anchor_rows_) =
764 aa_rows.middleCols(b * anchor_rows_, anchor_rows_);
765 result.block(a * rows, b * rows + anchor_rows_, anchor_rows_,
766 query_rows_) =
767 aq.block(a * anchor_rows_, b * query_rows_, anchor_rows_,
768 query_rows_);
769 result.block(a * rows + anchor_rows_, b * rows, query_rows_,
770 anchor_rows_) =
771 aq.block(b * anchor_rows_, a * query_rows_, anchor_rows_,
772 query_rows_)
773 .transpose();
774 result.block(a * rows + anchor_rows_, b * rows + anchor_rows_,
775 query_rows_, query_rows_) =
776 qq.block(a * query_rows_, b * query_rows_, query_rows_,
777 query_rows_);
778 }
779 }
780 for (Eigen::Index j = 0; j < channels; ++j)
781 for (Eigen::Index i = j + 1; i < channels; ++i) {
782 const double value = 0.5 * (result(i, j) + result(j, i));
783 result(i, j) = value;
784 result(j, i) = value;
785 }
786 return result;
787}

◆ adjointRows()

Eigen::MatrixXd gpr::ConditionalKernelProjection::adjointRows ( const Eigen::MatrixXd & conditional_weight,
Eigen::Index first,
Eigen::Index count ) const
nodiscard

Rows of the adjoint for a contiguous range of geometry samples. Each channel contains count rows, while columns retain the full channel-major joint layout. Storage is proportional to count.

Definition at line 815 of file ConditionedKernel.cpp.

818{
819 const Eigen::Index rows = anchor_rows_ + query_rows_;
820 const Eigen::Index nq = query_rows_ * (dimensions_ + 1);
821 if (!valid_ || first < 0 || count < 0 || first > rows ||
822 count > rows - first || conditional_weight.rows() != nq ||
823 conditional_weight.cols() != nq || !conditional_weight.allFinite())
824 throw std::invalid_argument("invalid conditioned kernel weight rows");
825 const Eigen::MatrixXd qq = symmetric(conditional_weight);
826 const Eigen::Index local_rows = count * (dimensions_ + 1);
827 Eigen::MatrixXd query_weight(local_rows, nq);
828 const Eigen::Index anchor_count =
829 first < anchor_rows_ ? std::min(count, anchor_rows_ - first) : 0;
830 const Eigen::Index query_count = count - anchor_count;
831 const Eigen::Index query_first = first + anchor_count - anchor_rows_;
832 for (Eigen::Index a = 0; a <= dimensions_; ++a) {
833 if (anchor_count)
834 query_weight.middleRows(a * count, anchor_count).noalias() =
835 -weights_.middleRows(a * anchor_rows_ + first, anchor_count) *
836 qq;
837 if (query_count)
838 query_weight.middleRows(a * count + anchor_count, query_count) =
839 qq.middleRows(a * query_rows_ + query_first, query_count);
840 }
841 Eigen::MatrixXd result(local_rows, rows * (dimensions_ + 1));
842 for (Eigen::Index b = 0; b <= dimensions_; ++b) {
843 result.middleCols(b * rows, anchor_rows_).noalias() =
844 -query_weight *
845 weights_.middleRows(b * anchor_rows_, anchor_rows_).transpose();
846 result.middleCols(b * rows + anchor_rows_, query_rows_) =
847 query_weight.middleCols(b * query_rows_, query_rows_);
848 }
849 return result;
850}

◆ derivative()

Eigen::MatrixXd gpr::ConditionalKernelProjection::derivative ( const Eigen::MatrixXd & matrix) const
nodiscard

Definition at line 729 of file ConditionedKernel.cpp.

731{
732 const Eigen::Index channels =
733 (anchor_rows_ + query_rows_) * (dimensions_ + 1);
734 if (!valid_ || matrix.rows() != channels || matrix.cols() != channels ||
735 !matrix.allFinite())
736 throw std::invalid_argument("invalid conditioned kernel derivative");
737 Eigen::MatrixXd aa, aq, qq;
738 blocks(matrix, aa, aq, qq);
739 const Eigen::MatrixXd cross = aq.transpose() * weights_;
740 return symmetric(qq - cross - cross.transpose() +
741 weights_.transpose() * aa * weights_);
742}

◆ intoKernel()

std::shared_ptr< const KernelOperator > gpr::ConditionalKernelProjection::intoKernel ( std::shared_ptr< const KernelOperator > kernel,
Coord anchors ) &&
nodiscard

Consume this fitted projection as a rank-local prediction operator. Distributed factors transfer collectively once on their process group. The kernel and anchors must define the joint matrix supplied to prepare.

Definition at line 671 of file ConditionedKernel.cpp.

673{
674 if (!valid_ || !factor_ || !kernel || anchors.rows() != anchor_rows_ ||
675 anchors.cols() != dimensions_ || !anchors.allFinite())
676 throw std::invalid_argument("invalid fitted kernel projection");
677 auto state = std::make_unique<ConditionedKernel::State>();
678 state->observation_major = factor_->observation_major;
679#ifdef GPR_LINALG_SCALAPACK
680 if (factor_->distributed)
681 state->lower = factor_->distributed->lowerFactorPanels();
682 else
683#endif
684 state->lower = linalg::CholeskyPanels::fromLower(factor_->lower);
685 state->kernel = std::move(kernel);
686 state->anchors = std::move(anchors);
687 state->anchor_covariance =
688 makeKernelCrossCovariance(state->kernel, state->anchors);
689 auto result = std::shared_ptr<const KernelOperator>(
690 new ConditionedKernel(std::move(state)));
691 factor_.reset();
692 weights_.resize(0, 0);
693 valid_ = false;
694 return result;
695}
static CholeskyPanels fromLower(const Eigen::MatrixXd &lower)
KernelOperator::CrossCovarianceFunction makeKernelCrossCovariance(std::shared_ptr< const KernelOperator > kernel, const Eigen::MatrixXd &training)

◆ observationLiftRows()

Eigen::MatrixXd gpr::ConditionalKernelProjection::observationLiftRows ( Eigen::Index first,
Eigen::Index count ) const
nodiscard

Rows of the observation lift T with anchor rows -Caa^-1 Caq and query rows I, so dC = T^T dK T for fixed anchor noise. The requested geometry range appears in each channel; columns are query channels.

Definition at line 789 of file ConditionedKernel.cpp.

791{
792 const Eigen::Index rows = anchor_rows_ + query_rows_;
793 const Eigen::Index nq = query_rows_ * (dimensions_ + 1);
794 if (!valid_ || first < 0 || count < 0 || first > rows ||
795 count > rows - first)
796 throw std::invalid_argument(
797 "invalid conditioned observation lift rows");
798 Eigen::MatrixXd lift = Eigen::MatrixXd::Zero(count * (dimensions_ + 1), nq);
799 const Eigen::Index anchor_count =
800 first < anchor_rows_ ? std::min(count, anchor_rows_ - first) : 0;
801 const Eigen::Index query_count = count - anchor_count;
802 const Eigen::Index query_first = first + anchor_count - anchor_rows_;
803 for (Eigen::Index channel = 0; channel <= dimensions_; ++channel) {
804 if (anchor_count)
805 lift.middleRows(channel * count, anchor_count) =
806 -weights_.middleRows(channel * anchor_rows_ + first,
807 anchor_count);
808 for (Eigen::Index row = 0; row < query_count; ++row)
809 lift(channel * count + anchor_count + row,
810 channel * query_rows_ + query_first + row) = 1.0;
811 }
812 return lift;
813}
T lift(const Value &value)

◆ operator=()

ConditionalKernelProjection & gpr::ConditionalKernelProjection::operator= ( ConditionalKernelProjection && )
defaultnoexcept

◆ prepare() [1/3]

bool gpr::ConditionalKernelProjection::prepare ( const Eigen::MatrixXd & joint,
const Eigen::MatrixXd & fixed,
Eigen::Index anchor_rows,
Eigen::Index query_rows,
Eigen::Index dimensions,
const Eigen::VectorXd & anchor_noise,
Eigen::MatrixXd & covariance,
const std::shared_ptr< linalg::DistributedState > & execution = {} )
nodiscard

Condition the sum of two covariances without storing their joint sum. An empty fixed covariance contributes zero.

Definition at line 382 of file ConditionedKernel.cpp.

387{
388 if (!validOnAllRanks(fixed.size() == 0 || (fixed.rows() == joint.rows() &&
389 fixed.cols() == joint.cols()),
390 execution)) {
391 valid_ = false;
392 factor_.reset();
393 return false;
394 }
395 if (fixed.size() == 0)
396 return prepare(joint, anchor_rows, query_rows, dimensions, anchor_noise,
397 covariance, execution);
398 return prepareJoint(joint + fixed, anchor_rows, query_rows, dimensions,
399 anchor_noise, covariance, execution);
400}
bool prepare(const Eigen::MatrixXd &joint, Eigen::Index anchor_rows, Eigen::Index query_rows, Eigen::Index dimensions, const Eigen::VectorXd &anchor_noise, Eigen::MatrixXd &covariance, const std::shared_ptr< linalg::DistributedState > &execution={})

◆ prepare() [2/3]

bool gpr::ConditionalKernelProjection::prepare ( const Eigen::MatrixXd & joint,
Eigen::Index anchor_rows,
Eigen::Index query_rows,
Eigen::Index dimensions,
const Eigen::VectorXd & anchor_noise,
Eigen::MatrixXd & covariance,
const std::shared_ptr< linalg::DistributedState > & execution = {} )
nodiscard

Definition at line 372 of file ConditionedKernel.cpp.

377{
378 return prepareJoint(joint, anchor_rows, query_rows, dimensions,
379 anchor_noise, covariance, execution);
380}

◆ prepare() [3/3]

bool gpr::ConditionalKernelProjection::prepare ( const KernelOperator & kernel,
const Coord & anchors,
const Coord & query,
const Eigen::VectorXd & anchor_noise,
Eigen::MatrixXd & covariance,
const std::shared_ptr< linalg::DistributedState > & execution = {} )
nodiscard

Assemble anchor, cross, and query blocks separately. The anchor block becomes the factor storage without retaining a full joint matrix.

Definition at line 402 of file ConditionedKernel.cpp.

406{
407 valid_ = false;
408 factor_.reset();
409 if (!validOnAllRanks(
410 anchors.rows() > 0 && query.rows() > 0 && anchors.cols() > 0 &&
411 anchors.cols() == query.cols() && anchors.allFinite() &&
412 query.allFinite() &&
413 anchor_noise.size() == anchors.rows() * (anchors.cols() + 1) &&
414 anchor_noise.allFinite() && (anchor_noise.array() >= 0.0).all(),
415 execution))
416 return false;
417 anchor_rows_ = anchors.rows();
418 query_rows_ = query.rows();
419 dimensions_ = anchors.cols();
420#ifdef GPR_LINALG_SCALAPACK
421 if (execution) {
422 // Whole observation tiles share kernel geometry and belong to their
423 // factor owners. No rank materializes the dense anchor covariance.
424 const Eigen::Index channels = dimensions_ + 1;
425 const Eigen::Index order = anchor_rows_ * channels;
426 if (order > std::numeric_limits<int>::max()) return false;
429 PhaseTimers::projection_max_anchor_rows, static_cast<long>(order));
430 execution->initialize(static_cast<int>(channels));
431 int descriptor[9];
432 auto local = execution->alloc_local(
433 static_cast<int>(order), static_cast<int>(order), descriptor);
434 if (local.empty()) local.resize(1, 0.0);
435 int valid = 1;
436 try {
437 ScopedPhaseTimer timer(PhaseTimers::projection_assembly_seconds,
438 "projection anchor assembly");
439 const auto prepared_tiles =
440 kernel.prepareJointCovarianceTiles(anchors);
441 const bool whole_tiles = execution->block_size() == channels;
442 for (Eigen::Index i = 0; i < anchor_rows_; ++i) {
443 for (Eigen::Index j = 0; j <= i; ++j) {
444 const int row_start = static_cast<int>(i * channels);
445 const int col_start = static_cast<int>(j * channels);
446 bool owned =
447 execution->owns_global_entry(row_start, col_start);
448 if (!whole_tiles && !owned)
449 for (Eigen::Index a = 0; a < channels && !owned; ++a)
450 for (Eigen::Index b = 0; b < channels; ++b)
451 if (execution->owns_global_entry(
452 row_start + a, col_start + b)) {
453 owned = true;
454 break;
455 }
456 if (!owned) continue;
457 const Eigen::MatrixXd tile =
458 prepared_tiles
459 ? prepared_tiles(i, j)
460 : kernel.jointEnergyGradientCrossCovariance(
461 anchors.middleRows(i, 1),
462 anchors.middleRows(j, 1));
463 if (tile.rows() != channels || tile.cols() != channels ||
464 !tile.allFinite()) {
465 valid = 0;
466 continue;
467 }
468 if (whole_tiles) {
469 // An observation tile occupies one process block.
470 // Its columns are contiguous in the local matrix;
471 // ownership and local offsets are constant per tile.
472 const int local_row = execution->local_index_for_global(
473 row_start, execution->block_size(),
474 execution->myrow(), execution->nprow());
475 const int local_col = execution->local_index_for_global(
476 col_start, execution->block_size(),
477 execution->mycol(), execution->npcol());
478 for (Eigen::Index b = 0; b < channels; ++b) {
479 double* column =
480 local.data() + local_row +
481 static_cast<std::size_t>(local_col + b) *
482 descriptor[8];
483 for (Eigen::Index a = i == j ? b : 0; a < channels;
484 ++a)
485 column[a] =
486 tile(a, b) +
487 (i == j && a == b
488 ? anchor_noise(a * anchor_rows_ + i)
489 : 0.0);
490 }
491 continue;
492 }
493 for (Eigen::Index a = 0; a < channels; ++a)
494 for (Eigen::Index b = 0; b < channels; ++b) {
495 const int row = row_start + a, col = col_start + b;
496 if (row < col ||
497 !execution->owns_global_entry(row, col))
498 continue;
499 const int local_row =
500 execution->local_index_for_global(
501 row, execution->block_size(),
502 execution->myrow(), execution->nprow());
503 const int local_col =
504 execution->local_index_for_global(
505 col, execution->block_size(),
506 execution->mycol(), execution->npcol());
507 local[local_row +
508 static_cast<std::size_t>(local_col) *
509 descriptor[8]] =
510 tile(a, b) +
511 (row == col ? anchor_noise(a * anchor_rows_ + i)
512 : 0.0);
513 }
514 }
515 }
516 } catch (const std::exception&) {
517 valid = 0;
518 }
519 if (execution->nprocs() > 1)
520 MPI_Allreduce(MPI_IN_PLACE, &valid, 1, MPI_INT, MPI_MIN,
521 execution->world_comm());
522 if (!valid) return false;
523 factor_ = std::make_unique<Factor>();
524 factor_->observation_major = true;
525 factor_->distributed =
526 std::make_unique<linalg::ScalapackPDPotrfInverse>(execution);
527 {
528 ScopedPhaseTimer timer(PhaseTimers::projection_factor_seconds,
529 "projection factor");
530 if (!factor_->distributed->factorise(std::move(local), descriptor,
531 static_cast<int>(order)))
532 return false;
533 }
534 Eigen::MatrixXd aq, qq;
535 try {
536 ScopedPhaseTimer timer(PhaseTimers::projection_cross_seconds,
537 "projection cross covariance");
538 aq = kernel.jointEnergyGradientCrossCovariance(anchors, query);
539 qq = kernel.jointEnergyGradientCovariance(query);
540 const Eigen::Index query_order = query_rows_ * channels;
541 valid = aq.rows() == order && aq.cols() == query_order &&
542 qq.rows() == query_order && qq.cols() == query_order &&
543 aq.allFinite() && qq.allFinite();
544 } catch (const std::exception&) {
545 valid = 0;
546 }
547 if (execution->nprocs() > 1)
548 MPI_Allreduce(MPI_IN_PLACE, &valid, 1, MPI_INT, MPI_MIN,
549 execution->world_comm());
550 if (!valid) return false;
551 Eigen::MatrixXd ordered(aq.rows(), aq.cols());
552 for (Eigen::Index row = 0; row < order; ++row)
553 ordered.row(row) =
554 aq.row((row % channels) * anchor_rows_ + row / channels);
555 {
556 ScopedPhaseTimer timer(PhaseTimers::projection_solve_seconds,
557 "projection solve");
558 ordered = factor_->distributed->solve_mat(ordered);
559 }
560 weights_.resize(aq.rows(), aq.cols());
561 for (Eigen::Index row = 0; row < order; ++row)
562 weights_.row((row % channels) * anchor_rows_ + row / channels) =
563 ordered.row(row);
564 {
565 ScopedPhaseTimer timer(PhaseTimers::projection_schur_seconds,
566 "projection Schur complement");
567 covariance = conditionalCovariance(qq, aq, weights_);
568 }
569 valid_ = covariance.allFinite() && weights_.allFinite();
570 return valid_;
571 }
572#endif
573 return prepareBlocks(
574 kernel.jointEnergyGradientCovariance(anchors),
575 kernel.jointEnergyGradientCrossCovariance(anchors, query),
576 kernel.jointEnergyGradientCovariance(query), anchor_noise, covariance,
577 execution);
578}
static double projection_cross_seconds
static double projection_schur_seconds
static long projection_max_anchor_rows
static int projection_count
static double projection_factor_seconds
static double projection_assembly_seconds
static double projection_solve_seconds

The documentation for this class was generated from the following files: