Loading...
Searching...
No Matches
Hessian.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5// Exact NLML Hessian and directional-curvature evaluations.
6// evaluateDirectionalCurvature is the scalar p^T H p oracle.
7// evaluateNlmlHessianVector is the matrix-free product H v.
8// assembleExactHessian builds the full p x p Hessian.
9// assembleExactHessianLocalDense is the rank-local helper.
10
16#include "../PhaseTimers.h"
17#include "Helpers.h"
18#include "NlmlHessianVector.h"
19#ifdef GPR_LINALG_SCALAPACK
22#endif
23
24#include <Eigen/Core>
25#include <algorithm>
26#include <cmath>
27#include <limits>
28#include <utility>
29#include <vector>
30
31namespace gpr {
32
33using detail::localTraceProduct;
34using detail::priorParametersVector;
35using detail::symmetrizeObservedInformation;
36
37namespace {
38
46bool composeChartDirection(const std::vector<EigenMatrix>& terms,
47 Eigen::Index n_covariance, double noise,
48 bool learn_noise, const Eigen::VectorXd& direction,
49 EigenMatrix& first, EigenMatrix& second)
50{
51 if (terms.empty() ||
52 direction.size() > n_covariance + (learn_noise ? 1 : 0) ||
53 static_cast<Eigen::Index>(terms.size()) != n_covariance) {
54 return false;
55 }
56 const Eigen::Index n = terms.front().rows();
57 if (second.rows() != n || second.cols() != n) return false;
58 first = EigenMatrix::Zero(n, n);
59 const Eigen::Index n_terms = std::min(direction.size(), n_covariance);
60 for (Eigen::Index i = 0; i < n_terms; ++i) {
61 first.noalias() += direction(i) * terms[static_cast<std::size_t>(i)];
62 }
63 if (learn_noise && direction.size() > n_covariance && noise > 0.0) {
64 const double noise_direction = direction(n_covariance);
65 first.diagonal().array() += noise * noise_direction;
66 second.diagonal().array() += noise * noise_direction * noise_direction;
67 }
68 return first.allFinite() && second.allFinite();
69}
70
71} // namespace
72
74 const Eigen::VectorXd& direction) const
75{
76 const double noise = likelihoodNoiseVariance();
77 if (direction.size() <= covarianceParameterCount() || !(noise > 0.0))
78 return {0.0, 0.0};
79 const double noise_direction = direction(direction.size() - 1);
80 return {noise * noise_direction, noise * noise_direction * noise_direction};
81}
82
84 const Eigen::VectorXd& x_ind, const EigenMatrix& x,
85 const Eigen::VectorXd& y, const Eigen::VectorXd& direction,
86 double& curvature, const TrainingKernelGeometry* training_geom)
87{
88 const auto curvature_start = std::chrono::steady_clock::now();
89 const double solve0 = linalg::teamSolveWallSeconds();
90 const double factor0 = linalg::teamFactorWallSeconds();
91 const double eigen0 = PhaseTimers::eigensolve_seconds;
92 const double kernel0 = PhaseTimers::assembly_seconds;
93 struct ExclusiveCurvature {
94 std::chrono::steady_clock::time_point start;
95 double solve0, factor0, eigen0, kernel0;
96 ~ExclusiveCurvature()
97 {
98 const double wall = std::chrono::duration<double>(
99 std::chrono::steady_clock::now() - start)
100 .count();
101 const double nested = (linalg::teamSolveWallSeconds() - solve0) +
102 (linalg::teamFactorWallSeconds() - factor0) +
105 PhaseTimers::curvature_seconds += std::max(0.0, wall - nested);
106 }
107 } exclusive{curvature_start, solve0, factor0, eigen0, kernel0};
108 curvature = std::numeric_limits<double>::quiet_NaN();
109
110 // Rasmussen-Williams 5.9 is the Gaussian NLML Hessian. Student-t, LOO and
111 // LOCAL_LOO energy/gradient already switch; SCG uses the Moller FD probe
112 // when this returns false.
114 hyperparameter_objective_mode_ == HyperparameterObjectiveMode::LOO ||
115 hyperparameter_objective_mode_ ==
117 return false;
118 }
119
120 if (!is_decomposed_succesfully || direction.size() == 0) return false;
121 if (sexpat_cov_func == nullptr) return false;
122 // The analytic oracle is valid for single-pair-type systems:
123 // the closed-form derivation
124 // (Williams-Rasmussen 5.9: -(1/2) tr(ADAD) + (1/2) tr(AE)
125 // + alpha^T DAD alpha - (1/2) alpha^T E alpha) is well-defined for
126 // any n_ls >= 0; assembleDirectionalCovarianceDerivatives + the
127 // SexpatCF kernels handle n_ls=1 correctly.
128
129 // alpha = back_solve(L, b) = K_perm^{-1} (P y) lives in the same permuted
130 // frame as L. The directional derivatives below are assembled in the
131 // original (un-permuted) atom order, so we permute them to match L before
132 // doing the trace algebra. All traces here are basis-invariant under the
133 // symmetric similarity P (·) P^T, so the final scalar curvature is
134 // unaffected by which frame we operate in - what matters is that alpha,
135 // first, second, and L all agree.
136 Eigen::VectorXd uDdim;
137 extractUniqueIndices(x_ind, uDdim);
138
139 // A charted (composite) Sexpat has no pair-distance geometry; its
140 // curvature comes from the composite tensors instead:
141 // d^2 L[p,p] = 1/2 tr(C^-1 C_pp) - 1/2 tr(C^-1 C_p C^-1 C_p)
142 // - 1/2 a^T C_pp a + a^T C_p C^-1 C_p a, a = C^-1 y,
143 // with C_p = sum_i p_i dC/dtheta_i over every covariance direction, plus
144 // noise * p_noise on the diagonal for a learned noise direction, and C_pp
145 // from the second-order tensors
146 // (scripts/derive_chart_second_derivatives.py).
147 if (sexpat_cov_func && sexpat_cov_func->usesManifoldChart()) {
148 std::vector<EigenMatrix> dC;
149 EigenMatrix Cpp;
150 EigenMatrix Cp;
152 x, x_ind, *sexpat_cov_func, dC) ||
154 x, x_ind, *sexpat_cov_func, direction, Cpp) ||
155 !composeChartDirection(
157 lik_gaussian != nullptr && lik_gaussian->getOptimizeSigma2(),
158 direction, Cp, Cpp)) {
159 return false;
160 }
162 evaluateTrainingCovarianceMatrix(x, x_ind, C, /*serial_per_rank=*/true,
163 /*include_constant=*/false);
164 if (jitter_sigma2 > 0.0) C.diagonal().array() += jitter_sigma2;
165 const Eigen::Index n = C.rows();
166 if (n == 0 || y.size() != n || dC[0].rows() != n || Cpp.rows() != n)
167 return false;
168 Eigen::LLT<EigenMatrix> llt(C);
169 if (llt.info() != Eigen::Success) return false;
170 EigenMatrix factor = llt.matrixL();
172 factor,
173 detail::constantCovarianceUpdate(const_cov_fun.get(), x_ind)))
174 return false;
175 const auto solve = [&](const EigenMatrix& rhs) -> EigenMatrix {
176 const EigenMatrix forward =
177 factor.triangularView<Eigen::Lower>().solve(rhs);
178 return factor.transpose().triangularView<Eigen::Upper>().solve(
179 forward);
180 };
181 const Eigen::VectorXd alpha = solve(y);
182 const EigenMatrix CinvCp = solve(Cp);
183 const EigenMatrix CinvCpp = solve(Cpp);
184 const Eigen::VectorXd Cp_alpha = Cp * alpha;
185 const Eigen::VectorXd solved_Cp_alpha = solve(Cp_alpha);
186 curvature =
187 0.5 * CinvCpp.trace() -
188 0.5 * (CinvCp.cwiseProduct(CinvCp.transpose())).sum() -
189 0.5 * alpha.dot(Cpp * alpha) + Cp_alpha.dot(solved_Cp_alpha) -
190 sexpat_cov_func->calculateDirectionalLogPriorCurvature(direction);
191 return std::isfinite(curvature);
192 }
193 EigenMatrix first, second;
194 bool derivatives_ready = false;
195 // Prefer the precomputed-geometry derivative assembly even when the caller
196 // did not pass a geometry: build one locally. The geometry and live-rebuild
197 // paths round the shared kernel differently, and the Q-form curvature
198 // amplifies that by 1/sigma^2; routing both through the geometry assembly
199 // makes the curvature path-invariant (geometry-passed vs not) to rounding.
200 // Pair tiles reuse the fit's geometry cache. That cache is coordinates,
201 // channels and the kernel geometry; theta, the probe direction and the
202 // Cholesky factor are not stored in it.
203 TrainingKernelGeometry local_geom;
204 if (training_geom == nullptr) {
205 if (use_pair_local_directional_tiles_ && use_pair_local_tiles_)
206 training_geom = fitGeometryFor(x, x_ind);
207 else if (prepareTrainingKernelGeometry(x, x_ind, local_geom))
208 training_geom = &local_geom;
209 }
210 if (training_geom != nullptr) {
211 derivatives_ready =
213 *training_geom, x, x_ind, uDdim, direction, first, second,
214 /*serial_per_rank=*/false);
215 }
216 if (!derivatives_ready) {
217 assembleDirectionalCovarianceDerivatives(x, x_ind, uDdim, direction,
218 first, second,
219 /*serial_per_rank=*/false);
220 }
221
222 if (first.size() == 0 || second.size() == 0) return false;
223
224 // Energy/gradient on the HS-SVD contractions path cache a Cholesky
225 // factor of the same C (hssvd_objective_trace_via_cholesky_). The
226 // Q-form Woodbury contraction of that cache is a different K^{-1}
227 // and diverges by O(cond * eps) from the Cholesky reduction SCG
228 // just used for E/g. assembleExactHessian and the gradient walk
229 // already refuse that mix; the directional oracle must too.
230 const bool use_hssvd_curvature =
231 use_hssvd_backend_ && use_hssvd_objective_contractions_ &&
232 hssvdObjectiveStampMatches() && !hssvd_objective_trace_via_cholesky_ &&
233 hssvd_objective_alpha_.size() == y.size() &&
234 hssvd_objective_trace_basis_.rows() == y.size() &&
235 hssvd_objective_trace_weighted_basis_.rows() == y.size() &&
236 hssvd_objective_trace_basis_.cols() ==
237 hssvd_objective_trace_weighted_basis_.cols();
238
239 if (use_hssvd_curvature) {
240 const EigenMatrix& Q = hssvd_objective_trace_basis_;
241 const EigenMatrix& Qshrink = hssvd_objective_trace_weighted_basis_;
242 const Eigen::VectorXd& alpha = hssvd_objective_alpha_;
243 const double inv_noise = hssvd_objective_trace_inv_noise_;
244
245 const double prior_curvature =
246 -sexpat_cov_func->calculateDirectionalLogPriorCurvature(direction);
247 EigenMatrix solve_first;
248 Eigen::VectorXd z;
249 double trace_ae = 0.0;
250 if (hssvd_objective_trace_direct_inverse_) {
251 solve_first = Qshrink * (Q.transpose() * first);
252 const Eigen::VectorXd v_direct = first * alpha;
253 z = Qshrink * (Q.transpose() * v_direct);
254 trace_ae = (Q.array() * (second * Qshrink).array()).sum();
255 } else if (inv_noise > 0.0) {
256 solve_first =
257 inv_noise * (first - Qshrink * (Q.transpose() * first));
258 const double projected_trace =
259 (Q.array() * (second * Qshrink).array()).sum();
260 trace_ae = inv_noise * (second.trace() - projected_trace);
261 } else {
262 return false;
263 }
264 const double trace_adad =
265 backend().trace_product(solve_first, solve_first.transpose());
266 const Eigen::VectorXd v = first * alpha;
267 if (!hssvd_objective_trace_direct_inverse_) {
268 z = inv_noise * (v - Qshrink * (Q.transpose() * v));
269 }
270 const double quad = v.dot(z);
271 const double alpha_e_alpha = alpha.dot(second * alpha);
272
273 curvature =
274 0.5 * (-trace_adad + trace_ae + 2.0 * quad - alpha_e_alpha) +
275 prior_curvature;
276 return std::isfinite(curvature);
277 }
278
282 }
283
284 // One trace formula at every rank count. A distributed build takes the
285 // trace of C^{-1} second from the inverse of the cached factor, formed
286 // by the same tile-ordered routine and summed exactly at one rank as at
287 // several, so the rounding does not change with the rank count; the
288 // inverse is not cached. A serial build has one path.
289#ifdef GPR_LINALG_SCALAPACK
290 const bool use_solve_based_trace = true;
291#else
292 const bool use_solve_based_trace = false;
293#endif
294
295 const double prior_curvature =
296 -sexpat_cov_func->calculateDirectionalLogPriorCurvature(direction);
297
298 const bool factor_ready = hasTrainingCholeskyFactor(y.rows());
299 if (b.rows() != activeFactorOrder(y.rows()) || !factor_ready) {
300 // Distributed Cholesky leaves the dense member empty. Ownership
301 // and order distinguish its live factor from a consumed or foreign
302 // factor before the triangular solves.
303 if (!factor_ready && x.rows() > 0) {
304 (void)decomposeCovarianceMatrix(x, x_ind);
305 if (!is_decomposed_succesfully) return false;
306 }
308 }
309
310 // A device-resident posterior leaves b empty and keeps the weights on the
311 // device.
312 Eigen::VectorXd alpha;
313#ifdef GPR_LINALG_KOKKOS
314 if (hasDevicePosterior()) {
316 alpha = a;
317 } else
318#endif
319 alpha = trainingSolver().unwhiten(b);
320 // The deflated objective is block diagonal in [T, U]: the factor carries
321 // T^T C T and the rigid block is the noise alone. Contract the projected
322 // derivatives against the factor and add the rigid block in closed form.
323 const bool deflated = rigid_deflation_.isActiveForFullOrder(y.rows());
324 double rigid_curvature = 0.0;
325 if (deflated) {
326 if (alpha.size() != rigid_deflation_.deflated_rows) return false;
327 if (rigid_deflation_.a_rigid.size() != y.rows()) {
328 rigid_deflation_.updateTargetStats(
330 }
331 first = rigid_deflation_.projectSymmetric(first);
332 second = rigid_deflation_.projectSymmetric(second);
333 const auto [noise_first, noise_second] =
334 rigidNoiseDerivatives(direction);
335 rigid_curvature = rigid_deflation_.rigidCurvature(
336 noise_first, noise_first, noise_second);
337 }
338 const EigenMatrix solve_first = trainingSolver().solveColumns(first);
339 const double trace_adad =
340 backend().trace_product(solve_first, solve_first.transpose());
341 const Eigen::VectorXd v = first * alpha;
342 const Eigen::VectorXd z = trainingSolver().solve(v);
343
344 double trace_ae = 0.0;
345 if (use_solve_based_trace) {
346#ifdef GPR_LINALG_SCALAPACK
347 trace_ae = backend().trace_inverse_times_symmetric(L, second);
348#endif
349 } else {
350 trace_ae = backend().trace_product(trainingSolver().inverse(), second);
351 }
352
353 const double quad = v.dot(z);
354 const double alpha_e_alpha = alpha.dot(second * alpha);
355
356 curvature = 0.5 * (-trace_adad + trace_ae + 2.0 * quad - alpha_e_alpha) +
357 rigid_curvature + prior_curvature;
358 return std::isfinite(curvature);
359}
360
362 const char* why)
363{
364 ++nlml_hessian_vector_fallbacks_;
365 if (report_level >= 1) {
366 log_man << "curvature_mode nlml_hessian_vector: " << why << "; "
367 << where << " takes the finite_difference path.\n";
368 }
369}
370
372 const Eigen::VectorXd& x_ind, const EigenMatrix& x,
373 const Eigen::VectorXd& y, const Eigen::VectorXd& direction,
374 Eigen::VectorXd& product, const TrainingKernelGeometry* training_geom)
375{
376 const auto curvature_start = std::chrono::steady_clock::now();
377 const double solve0 = linalg::teamSolveWallSeconds();
378 const double factor0 = linalg::teamFactorWallSeconds();
379 const double eigen0 = PhaseTimers::eigensolve_seconds;
380 const double kernel0 = PhaseTimers::assembly_seconds;
381 struct ExclusiveCurvature {
382 std::chrono::steady_clock::time_point start;
383 double solve0, factor0, eigen0, kernel0;
384 ~ExclusiveCurvature()
385 {
386 const double wall = std::chrono::duration<double>(
387 std::chrono::steady_clock::now() - start)
388 .count();
389 const double nested = (linalg::teamSolveWallSeconds() - solve0) +
390 (linalg::teamFactorWallSeconds() - factor0) +
393 PhaseTimers::curvature_seconds += std::max(0.0, wall - nested);
394 }
395 } exclusive{curvature_start, solve0, factor0, eigen0, kernel0};
396
398 fail_nlml_hessian_vector_for_testing_) {
399 return false;
400 }
401 if (!is_decomposed_succesfully || sexpat_cov_func == nullptr ||
402 direction.size() == 0 || !direction.allFinite() || y.size() == 0) {
403 return false;
404 }
405 // The Q-form Woodbury inverse is not the Cholesky factor. Applying the
406 // factor to that objective would be a different operator.
407 if (use_hssvd_backend_ && use_hssvd_objective_contractions_ &&
408 hssvdObjectiveStampMatches() && !hssvd_objective_trace_via_cholesky_) {
409 return false;
410 }
411 if (!hasTrainingCholeskyFactor(y.rows())) return false;
412#ifdef GPR_LINALG_KOKKOS
413 if (!hasDevicePosterior() && b.rows() != activeFactorOrder(y.rows()))
414 return false;
415#else
416 if (b.rows() != activeFactorOrder(y.rows())) return false;
417#endif
418
419 Eigen::VectorXd alpha;
420#ifdef GPR_LINALG_KOKKOS
421 if (hasDevicePosterior()) {
423 alpha = a;
424 } else
425#endif
426 alpha = trainingSolver().unwhiten(b);
427 const EigenMatrix inverse = trainingSolver().inverse();
428 // As in evaluateDirectionalCurvature: contract the projected derivatives
429 // against the deflated factor and add the noise-only rigid block in
430 // closed form.
431 const bool deflated = rigid_deflation_.isActiveForFullOrder(y.rows());
432 const Eigen::Index order = activeFactorOrder(y.rows());
433 if (deflated && rigid_deflation_.a_rigid.size() != y.rows()) {
434 rigid_deflation_.updateTargetStats(
436 }
437 if (alpha.size() != order || !alpha.allFinite() ||
438 inverse.rows() != order || inverse.cols() != order ||
439 !inverse.allFinite()) {
440 return false;
441 }
442
443 const Eigen::Index p = direction.size();
444 const bool permute = hasTrainingPermutation();
445 const auto factor_frame = [&](EigenMatrix& matrix) {
446 if (permute) {
448 }
449 if (deflated) matrix = rigid_deflation_.projectSymmetric(matrix);
450 };
451
452 // beta = K^{-1} (D alpha) is the one triangular pair. The inverse above
453 // is the factor's cached inverse (the analytic gradient already formed
454 // it); forming it on a cold cache is that same inverse, not a solve
455 // per column.
456 Eigen::VectorXd data(p);
457 auto contract = [&](auto&& assemble) -> bool {
458 EigenMatrix directional;
459 EigenMatrix second_direction;
460 if (!assemble(direction, directional, second_direction)) return false;
461 factor_frame(directional);
462 const Eigen::VectorXd beta =
463 trainingSolver().solve(directional * alpha);
464 if (beta.size() != alpha.size() || !beta.allFinite()) return false;
465 const EigenMatrix metric = (inverse * directional) * inverse;
466 const auto [noise_first, noise_second] =
467 rigidNoiseDerivatives(direction);
468 for (Eigen::Index i = 0; i < p; ++i) {
469 const Eigen::VectorXd axis = Eigen::VectorXd::Unit(p, i);
470 EigenMatrix kernel_i;
471 EigenMatrix second_i;
472 EigenMatrix unused;
473 EigenMatrix second_sum;
474 if (!assemble(axis, kernel_i, second_i) ||
475 !assemble(direction + axis, unused, second_sum)) {
476 return false;
477 }
478 EigenMatrix mixed =
479 0.5 * (second_sum - second_direction - second_i);
480 factor_frame(kernel_i);
481 factor_frame(mixed);
482 data(i) = detail::nlmlHessianVectorComponent(
483 inverse, alpha, beta, metric, kernel_i, mixed);
484 if (deflated) {
485 const auto [axis_first, axis_second] =
487 const double sum_second =
488 rigidNoiseDerivatives(direction + axis).second;
489 data(i) += rigid_deflation_.rigidCurvature(
490 noise_first, axis_first,
491 0.5 * (sum_second - noise_second - axis_second));
492 }
493 }
494 return data.allFinite();
495 };
496
497 bool contracted = false;
498 if (sexpat_cov_func->usesManifoldChart()) {
499 std::vector<EigenMatrix> kernel_derivatives;
501 x, x_ind, *sexpat_cov_func, kernel_derivatives) ||
502 kernel_derivatives.empty() ||
503 static_cast<Eigen::Index>(kernel_derivatives.size()) !=
505 kernel_derivatives.front().rows() != y.rows()) {
506 return false;
507 }
508 const auto assemble_chart = [&](const Eigen::VectorXd& dir,
509 EigenMatrix& first,
510 EigenMatrix& second) -> bool {
512 x, x_ind, *sexpat_cov_func, dir, second)) {
513 return false;
514 }
515 return second.rows() == y.rows() &&
516 composeChartDirection(kernel_derivatives,
519 lik_gaussian != nullptr &&
520 lik_gaussian->getOptimizeSigma2(),
521 dir, first, second) &&
522 first.rows() == y.rows();
523 };
524 contracted = contract(assemble_chart);
525 } else {
526 Eigen::VectorXd uDdim;
527 extractUniqueIndices(x_ind, uDdim);
528 TrainingKernelGeometry local_geom;
529 if (training_geom == nullptr) {
530 if (use_pair_local_directional_tiles_ && use_pair_local_tiles_)
531 training_geom = fitGeometryFor(x, x_ind);
532 else if (prepareTrainingKernelGeometry(x, x_ind, local_geom))
533 training_geom = &local_geom;
534 }
535 const auto assemble_pair = [&](const Eigen::VectorXd& dir,
536 EigenMatrix& first,
537 EigenMatrix& second) -> bool {
538 bool ready = false;
539 if (training_geom != nullptr) {
541 *training_geom, x, x_ind, uDdim, dir, first, second,
542 /*serial_per_rank=*/false);
543 }
544 if (!ready) {
546 x, x_ind, uDdim, dir, first, second,
547 /*serial_per_rank=*/false);
548 }
549 return first.rows() == y.rows() && first.cols() == y.rows() &&
550 second.rows() == y.rows() && second.cols() == y.rows() &&
551 first.allFinite() && second.allFinite();
552 };
553 contracted = contract(assemble_pair);
554 }
555 if (!contracted) return false;
556
557 for (Eigen::Index i = 0; i < p; ++i) {
558 const Eigen::VectorXd axis = Eigen::VectorXd::Unit(p, i);
559 data(i) -=
560 sexpat_cov_func->calculateDirectionalLogPriorCurvature(axis) *
561 direction(i);
562 }
563 if (!data.allFinite()) return false;
564 product = std::move(data);
565 return true;
566}
567
569 const Eigen::VectorXd& x_ind, const EigenMatrix& x,
570 const Eigen::VectorXd& y, int n_hyperparameters, Eigen::MatrixXd& H)
571{
572 if (n_hyperparameters <= 0) {
573 H.resize(0, 0);
574 return false;
575 }
577 x_ind, x, y,
578 Eigen::MatrixXd::Identity(n_hyperparameters, n_hyperparameters), H);
579}
580
582 const Eigen::VectorXd& x_ind, const EigenMatrix& x,
583 const Eigen::VectorXd& y, const Eigen::MatrixXd& directions,
584 Eigen::MatrixXd& H)
585{
586 // Polarisation identity, symbolically validated in
587 // python/tests/test_sympy_laplace.py::test_polarisation_identity_recovers_off_diagonal.
588 // Uses p(p+1)/2 directional probes at theta*: p for the diagonal,
589 // p(p-1)/2 for the off-diagonal recovered via
590 // H_ij = ( (e_i+e_j)^T H (e_i+e_j) - H_ii - H_jj ) / 2.
591 //
592 // alpha = K^{-1} y and invC = K^{-1} are
593 // loop-invariants of the Cholesky polarisation path: they depend on
594 // the training-set factor, not on the probe direction. Structured
595 // HS-SVD objectives instead reuse evaluateDirectionalCurvature so the
596 // same Hessian probes are contracted through the orthonormal Psi basis.
597 const int p = static_cast<int>(directions.cols());
598 H = Eigen::MatrixXd::Constant(p, p,
599 std::numeric_limits<double>::quiet_NaN());
600 if (p <= 0 || directions.rows() < p || !directions.allFinite())
601 return false;
603 hyperparameter_objective_mode_ == HyperparameterObjectiveMode::LOO ||
604 hyperparameter_objective_mode_ ==
606 return false;
607 }
608 if (sexpat_cov_func == nullptr) return false;
609 // Pair-distance second derivatives do not describe a charted kernel.
610 if (sexpat_cov_func->usesManifoldChart()) return false;
611 if (!is_decomposed_succesfully) return false;
612 auto hssvd_curvature_ready = [&]() {
613 return use_hssvd_backend_ && use_hssvd_objective_contractions_ &&
614 hssvdObjectiveStampMatches() &&
615 // The Cholesky-trace SCG objective state caches a factor and an
616 // alpha tied to the last line-search theta; rebuild a clean
617 // self-contained Q-form objective for the exact-Hessian probes
618 // at theta* rather than reuse it.
619 !hssvd_objective_trace_via_cholesky_ &&
620 hssvd_objective_alpha_.size() == y.size() &&
621 hssvd_objective_trace_basis_.rows() == y.size() &&
622 hssvd_objective_trace_weighted_basis_.rows() == y.size() &&
623 hssvd_objective_trace_basis_.cols() ==
624 hssvd_objective_trace_weighted_basis_.cols();
625 };
626 bool use_hssvd_curvature = hssvd_curvature_ready();
627 if (!use_hssvd_curvature && use_hssvd_backend_ &&
628 use_hssvd_objective_contractions_) {
629 const double total_sigma2 =
630 (lik_gaussian ? lik_gaussian->getSigma2() : 0.0) + jitter_sigma2;
631 auto install_noisy_hssvd_objective =
632 [&](const StableBasisND& basis, const Eigen::VectorXd& y_factor,
633 const bool unpermute_from_factor_layout) {
634 if (basis.rank() <= 0 || basis.numObs() != y.size() ||
635 y_factor.size() != y.size() || !(total_sigma2 > 0.0)) {
636 return false;
637 }
638 const StableBasisPosteriorWeights weights =
639 basis.noisyPosteriorWeights(y_factor, total_sigma2);
640 const EigenMatrix& Q = basis.leftSingularVectors();
641 const Eigen::ArrayXd s2 =
642 basis.singularValues().array().square();
643 const Eigen::VectorXd shrink =
644 (s2 / (s2 + total_sigma2)).matrix();
645 const EigenMatrix Qshrink = Q * shrink.asDiagonal();
646 clearHssvdPendingCovariance();
647 hssvd_objective_trace_basis_ =
648 unpermute_from_factor_layout
650 : Q;
651 hssvd_objective_trace_weighted_basis_ =
652 unpermute_from_factor_layout
653 ? gpr::unpermuteMatrixRows(Qshrink,
655 : Qshrink;
656 hssvd_objective_alpha_ =
657 unpermute_from_factor_layout
658 ? gpr::unpermuteVector(weights.alpha,
660 : weights.alpha;
661 hssvd_objective_trace_inv_noise_ = 1.0 / total_sigma2;
662 hssvd_objective_trace_direct_inverse_ = false;
663 // This install builds the orthonormal Q-form trace state with
664 // no cached Cholesky factor, so the curvature must use the
665 // Q-form, not the Cholesky path. Clear any stale via-cholesky
666 // flag left by a prior geometry objective evaluation.
667 hssvd_objective_trace_via_cholesky_ = false;
668 hssvd_objective_basis_valid_ =
669 hssvd_objective_alpha_.size() == y.size() &&
670 hssvd_objective_alpha_.allFinite();
671 stampHssvdObjective();
672 return hssvd_curvature_ready();
673 };
674
675 // The HS-SVD basis the posterior is fitted with is the curvature
676 // model consistent with that posterior, and it is rank-sized (n x
677 // r). It is tried first. The full-spectrum rebuild below is the
678 // fallback when no valid basis exists: it materialises the dense
679 // noisy kernel, its symmetrised copy and a complete eigenvector
680 // matrix (n x n each), which on a 16-image, 14-atom band with 200
681 // rows (8600 joint rows) took the process to 17 GB and the OOM
682 // killer (17_claisen), for a curvature the truncated model does not
683 // even use.
684 if (!use_hssvd_curvature && hssvd_basis_valid_ &&
685 hssvd_basis_.rank() > 0 && hssvd_basis_.numObs() == y.size() &&
686 total_sigma2 > 0.0) {
687 use_hssvd_curvature =
688 install_noisy_hssvd_objective(hssvd_basis_, y,
689 /*unpermute_from_factor_layout=*/
690 false);
691 }
692 // Rebuild a self-contained Q-form objective at theta* from the noisy
693 // kernel C (full spectrum mu_i = eig(C)). This leaves no Cholesky-trace
694 // flag or cached factor, so the Laplace mean path that follows is not
695 // perturbed, and the curvature is consistent at theta*.
696 if (!use_hssvd_curvature && total_sigma2 > 0.0) {
698 if (prepareTrainingKernelGeometry(x, x_ind, geom)) {
700 EigenMatrix noisy_cov;
702 geom, x, x_ind, noisy_cov,
703 /*serial_per_rank=*/false)) {
704 const bool permuted_factor_layout =
706 static_cast<Eigen::Index>(
707 training_permutation_.perm.size()) == y.size();
708 if (permuted_factor_layout) {
709 noisy_cov = gpr::permuteSymmetricMatrix(
710 noisy_cov, training_permutation_);
711 }
712 if (jitter_sigma2 > 0.0) {
713 noisy_cov.diagonal().array() += jitter_sigma2;
714 }
715 if (use_correction_) {
716 const double K_sum = noisy_cov.diagonal().sum();
717 const Eigen::Index n_ind = noisy_cov.rows();
718 const double denom = 1.0 / correction_eps_ -
719 static_cast<double>(n_ind * n_ind);
720 if (std::abs(denom) > 1e-30) {
721 const double corr = K_sum * K_sum / denom;
722 noisy_cov.diagonal().array() += corr;
723 }
724 }
725
726 noisy_cov =
727 (0.5 * (noisy_cov + noisy_cov.transpose())).eval();
728
729 // Factorize the noisy kernel directly (full spectrum
730 // mu_i = eig(C)); the signal-kernel subtract-then-
731 // eigendecompose path loses precision on an
732 // ill-conditioned C and can flip the Hessian sign.
733 StableBasisND objective_basis(backend_context_);
734 objective_basis.factorizeNoisyKernel(noisy_cov,
735 total_sigma2);
736 const Eigen::VectorXd y_factor =
737 permuted_factor_layout ? permuteTrainingVector(y) : y;
738 use_hssvd_curvature = install_noisy_hssvd_objective(
739 objective_basis, y_factor, permuted_factor_layout);
740 }
741 }
742 }
743 if (!use_hssvd_curvature) return false;
744 }
745
746#ifdef GPR_LINALG_SCALAPACK
747 const int mpi_nprocs = backend().distributed().nprocs();
748 const bool distributed_probes = mpi_nprocs > 1;
749#else
750 constexpr bool distributed_probes = false;
751#endif
752
753 Eigen::VectorXd alpha;
754 EigenMatrix invC;
755 // The deflated objective is contracted as in evaluateDirectionalCurvature.
756 bool deflated = false;
757 if (!use_hssvd_curvature) {
758 const bool factor_ready = hasTrainingCholeskyFactor(y.rows());
759 if (b.rows() != activeFactorOrder(y.rows()) || !factor_ready) {
760 // Every polarization probe uses the same owned training factor.
761 if (!factor_ready && x.rows() > 0) {
762 (void)decomposeCovarianceMatrix(x, x_ind);
763 if (!is_decomposed_succesfully) return false;
764 }
766 }
767 // A device-resident posterior leaves b empty and keeps the weights on
768 // the device.
769#ifdef GPR_LINALG_KOKKOS
770 if (hasDevicePosterior()) {
772 alpha = a;
773 } else
774#endif
775 alpha = trainingSolver().unwhiten(b);
776 deflated = rigid_deflation_.isActiveForFullOrder(y.rows());
777 if (deflated && rigid_deflation_.a_rigid.size() != y.rows()) {
778 rigid_deflation_.updateTargetStats(
780 }
781 // The trace tr(C^-1 C_dd) is linear in the second derivative.
782 // One inverse of the common training factor serves every direction;
783 // solving against each C_dd repeats cubic work for the same operator.
784 invC = trainingSolver().inverse();
785 const Eigen::Index order = activeFactorOrder(y.rows());
786 if (alpha.size() != order || invC.rows() != order ||
787 invC.cols() != order || !invC.allFinite())
788 return false;
789 }
790 Eigen::VectorXd uDdim;
791 if (!use_hssvd_curvature) extractUniqueIndices(x_ind, uDdim);
792 TrainingKernelGeometry training_geometry;
793 const bool geometry_ready =
794 prepareTrainingKernelGeometry(x, x_ind, training_geometry);
795
796 auto probe = [&](const Eigen::VectorXd& direction) -> double {
797 PhaseTimers::notePeak("curvature_probe");
798 if (use_hssvd_curvature) {
799 double curvature = std::numeric_limits<double>::quiet_NaN();
801 x_ind, x, y, direction, curvature,
802 geometry_ready ? &training_geometry : nullptr)
803 ? curvature
804 : std::numeric_limits<double>::quiet_NaN();
805 }
806
807 EigenMatrix first, second;
808 if (!geometry_ready ||
810 training_geometry, x, x_ind, uDdim, direction, first, second,
811 /*serial_per_rank=*/false)) {
812 assembleDirectionalCovarianceDerivatives(x, x_ind, uDdim, direction,
813 first, second,
814 /*serial_per_rank=*/false);
815 }
816 if (first.size() == 0 || second.size() == 0) {
817 return std::numeric_limits<double>::quiet_NaN();
818 }
822 }
823 double rigid_curvature = 0.0;
824 if (deflated) {
825 first = rigid_deflation_.projectSymmetric(first);
826 second = rigid_deflation_.projectSymmetric(second);
827 const auto [noise_first, noise_second] =
828 rigidNoiseDerivatives(direction);
829 rigid_curvature = rigid_deflation_.rigidCurvature(
830 noise_first, noise_first, noise_second);
831 }
832
833 const double prior_curvature =
834 -sexpat_cov_func->calculateDirectionalLogPriorCurvature(direction);
835
836 const EigenMatrix solve_first = trainingSolver().solveColumns(first);
837 const double trace_adad =
838 backend().trace_product(solve_first, solve_first.transpose());
839 const Eigen::VectorXd v = first * alpha;
840 const Eigen::VectorXd z = trainingSolver().solve(v);
841
842 const double trace_ae = backend().trace_product(invC, second);
843
844 const double quad = v.dot(z);
845 const double alpha_e_alpha = alpha.dot(second * alpha);
846
847 const double curvature =
848 0.5 * (-trace_adad + trace_ae + 2.0 * quad - alpha_e_alpha) +
849 rigid_curvature + prior_curvature;
850 return std::isfinite(curvature)
851 ? curvature
852 : std::numeric_limits<double>::quiet_NaN();
853 };
854
855 // Layout of the flat probe vector (length n_probes = p(p+1)/2):
856 // [0 .. p) -> H(i, i) diagonal probes (one per i)
857 // [p .. p + p(p-1)/2) -> H(i, j) off-diagonal sum probes
858 // ordered (0,1), (0,2), .., (0,p-1),
859 // (1,2), .., (p-2, p-1).
860 const int n_diag = p;
861 const int n_off = p * (p - 1) / 2;
862 const int n_probes = n_diag + n_off;
863
864 auto direction_for_probe = [p, n_diag, &directions](int probe_idx) {
865 Eigen::VectorXd direction = Eigen::VectorXd::Zero(directions.rows());
866 if (probe_idx < n_diag) {
867 direction = directions.col(probe_idx);
868 return direction;
869 }
870
871 int off_idx = probe_idx - n_diag;
872 for (int i = 0; i < p; ++i) {
873 const int row_count = p - i - 1;
874 if (off_idx < row_count) {
875 const int j = i + 1 + off_idx;
876 direction = directions.col(i) + directions.col(j);
877 return direction;
878 }
879 off_idx -= row_count;
880 }
881
882 return direction;
883 };
884
885 const std::function<double(int)> probe_by_index = [&](int probe_idx) {
886 return probe(direction_for_probe(probe_idx));
887 };
888
890 linalg::KernelInverse::Kind probe_kind =
892#ifdef GPR_LINALG_SCALAPACK
893 int mpi_up = 0;
894 MPI_Initialized(&mpi_up);
895 if (mpi_up) {
896 probe_ctx = mpiContext();
897 if (distributed_probes) {
899 }
900 }
901#endif
902
903 const linalg::ProbeExecutor probe_exec;
904 const Eigen::VectorXd probe_values =
905 probe_exec.run(n_probes, probe_by_index, probe_kind, probe_ctx);
906 if (report_level >= 1) {
907 log_man << "[laplace-curvature-work] streamed="
908 << training_geometry.streamed << " geometry_blocks="
909 << training_geometry.directional_geometry_blocks_for_testing
910 << " available_blocks=" << training_geometry.blocks.size()
911 << " probes=" << n_probes << "\n";
912 }
913 if (probe_values.size() != n_probes || !probe_values.allFinite()) {
914 return false;
915 }
916
917 // Assemble the symmetric H from the flat probe vector.
918 for (int i = 0; i < p; ++i) {
919 H(i, i) = probe_values(i);
920 }
921 int off_idx = 0;
922 for (int i = 0; i < p; ++i) {
923 for (int j = i + 1; j < p; ++j, ++off_idx) {
924 const double h_sum = probe_values(n_diag + off_idx);
925 const double h_ij = 0.5 * (h_sum - H(i, i) - H(j, j));
926 H(i, j) = h_ij;
927 H(j, i) = h_ij;
928 }
929 }
930
931 // The exact-Hessian assembly builds a self-contained objective basis for
932 // its directional probes. Drop it on exit so it is not a cross-call cache:
933 // each Laplace/SVGD fit (which re-runs assembleExactHessian) pays the same
934 // objective build, keeping the per-call SVGD wall flat instead of cheap
935 // after a one-time install. The Laplace predict path has its own posterior
936 // cache (ensureLaplacePosterior) and is unaffected; the MAP path assembles
937 // the Hessian once per fit, so no reuse is lost.
938 hssvd_objective_basis_valid_ = false;
939 clearHssvdPendingCovariance();
940 hssvd_objective_trace_via_cholesky_ = false;
941
942 return true;
943}
944
946 const Eigen::VectorXd& x_ind, const EigenMatrix& x,
947 const Eigen::VectorXd& y, int n_hyperparameters, Eigen::MatrixXd& H)
948{
949 const int p = n_hyperparameters;
950 H = Eigen::MatrixXd::Constant(p, p,
951 std::numeric_limits<double>::quiet_NaN());
952 if (p <= 0 || sexpat_cov_func == nullptr || y.rows() != x.rows()) {
953 return false;
954 }
956 hyperparameter_objective_mode_ == HyperparameterObjectiveMode::LOO ||
957 hyperparameter_objective_mode_ ==
959 return false;
960 }
961 // The local oracle uses the same pair-distance derivative assembly.
962 if (sexpat_cov_func->usesManifoldChart()) return false;
963
964 EigenMatrix C_local;
965 evaluateTrainingCovarianceMatrix(x, x_ind, C_local,
966 /*serial_per_rank=*/true,
967 /*include_constant=*/false);
968 if (jitter_sigma2 > 0.0) C_local.diagonal().array() += jitter_sigma2;
969
970 Eigen::LLT<EigenMatrix> llt(C_local);
971 if (llt.info() != Eigen::Success) return false;
972 EigenMatrix factor = llt.matrixL();
973 if (!linalg::rank_one_update_lower(factor, detail::constantCovarianceUpdate(
974 const_cov_fun.get(), x_ind)))
975 return false;
976 const auto solve = [&](const EigenMatrix& rhs) -> EigenMatrix {
977 const EigenMatrix forward =
978 factor.triangularView<Eigen::Lower>().solve(rhs);
979 return factor.transpose().triangularView<Eigen::Upper>().solve(forward);
980 };
981
982 const Eigen::VectorXd alpha = solve(y);
983 const EigenMatrix invC =
984 solve(EigenMatrix::Identity(C_local.rows(), C_local.cols()));
985
986 Eigen::VectorXd uDdim;
987 extractUniqueIndices(x_ind, uDdim);
988 TrainingKernelGeometry training_geometry;
989 const bool geometry_ready =
990 prepareTrainingKernelGeometry(x, x_ind, training_geometry);
991
992 auto probe = [&](const Eigen::VectorXd& direction) -> double {
993 EigenMatrix first, second;
994 if (!geometry_ready ||
996 training_geometry, x, x_ind, uDdim, direction, first, second,
997 /*serial_per_rank=*/true)) {
998 assembleDirectionalCovarianceDerivatives(x, x_ind, uDdim, direction,
999 first, second,
1000 /*serial_per_rank=*/true);
1001 }
1002 if (first.size() == 0 || second.size() == 0) {
1003 return std::numeric_limits<double>::quiet_NaN();
1004 }
1005
1006 const double prior_curvature =
1007 -sexpat_cov_func->calculateDirectionalLogPriorCurvature(direction);
1008 const EigenMatrix solve_first = solve(first);
1009 const double trace_adad =
1010 localTraceProduct(solve_first, solve_first.transpose());
1011 const double trace_ae = localTraceProduct(invC, second);
1012 const Eigen::VectorXd v = first * alpha;
1013 const Eigen::VectorXd z = solve(v);
1014 const double quad = v.dot(z);
1015 const double alpha_e_alpha = alpha.dot(second * alpha);
1016 const double curvature =
1017 0.5 * (-trace_adad + trace_ae + 2.0 * quad - alpha_e_alpha) +
1018 prior_curvature;
1019 return std::isfinite(curvature)
1020 ? curvature
1021 : std::numeric_limits<double>::quiet_NaN();
1022 };
1023
1024 const int n_diag = p;
1025 const int n_off = p * (p - 1) / 2;
1026 const int n_probes = n_diag + n_off;
1027
1028 auto direction_for_probe = [p, n_diag](int probe_idx) {
1029 Eigen::VectorXd direction = Eigen::VectorXd::Zero(p);
1030 if (probe_idx < n_diag) {
1031 direction(probe_idx) = 1.0;
1032 return direction;
1033 }
1034
1035 int off_idx = probe_idx - n_diag;
1036 for (int i = 0; i < p; ++i) {
1037 const int row_count = p - i - 1;
1038 if (off_idx < row_count) {
1039 const int j = i + 1 + off_idx;
1040 direction(i) = 1.0;
1041 direction(j) = 1.0;
1042 return direction;
1043 }
1044 off_idx -= row_count;
1045 }
1046 return direction;
1047 };
1048
1049 const std::function<double(int)> probe_by_index = [&](int probe_idx) {
1050 return probe(direction_for_probe(probe_idx));
1051 };
1052
1053 const linalg::ProbeExecutor probe_exec;
1054 const Eigen::VectorXd probe_values = probe_exec.run(
1055 n_probes, probe_by_index, linalg::KernelInverse::Kind::RankLocal,
1057 if (probe_values.size() != n_probes || !probe_values.allFinite()) {
1058 return false;
1059 }
1060
1061 for (int i = 0; i < p; ++i)
1062 H(i, i) = probe_values(i);
1063 int off_idx = 0;
1064 for (int i = 0; i < p; ++i) {
1065 for (int j = i + 1; j < p; ++j, ++off_idx) {
1066 const double h_sum = probe_values(n_diag + off_idx);
1067 const double h_ij = 0.5 * (h_sum - H(i, i) - H(j, j));
1068 H(i, j) = h_ij;
1069 H(j, i) = h_ij;
1070 }
1071 }
1072
1073 return true;
1074}
1075
1076} // namespace gpr
Linear algebra abstraction layer for GPR hot-path operations.
@ z
Definition Enums.h:28
@ x
Definition Enums.h:28
@ y
Definition Enums.h:28
Tiny value type describing the MPI dispatch context for a probe loop.
MPI-aware dispatcher for the Hutchinson-style probe loop used by the Laplace Hessian assembler and th...
linalg::BackendContext & backend() const
Eigen::VectorXd permuteTrainingVector(const Eigen::VectorXd &input) const
bool evaluateTrainingCovarianceMatrixFromGeometry(const TrainingKernelGeometry &geom, const EigenMatrix &x, const Eigen::VectorXd &x_ind, EigenMatrix &cov_matrix, bool serial_per_rank=false, bool include_constant=true, bool include_noise=true)
bool assembleExactHessianLocalDense(const Eigen::VectorXd &x_ind, const EigenMatrix &x, const Eigen::VectorXd &y, int n_hyperparameters, Eigen::MatrixXd &H)
Definition Hessian.cpp:945
bool assembleCompositeJointCovarianceDerivatives(const EigenMatrix &x, const Eigen::VectorXd &indices, SexpatCF &covariance, std::vector< EigenMatrix > &derivatives) const
detail::RigidDeflationState rigid_deflation_
bool decomposeCovarianceMatrix(const EigenMatrix &x, const Eigen::VectorXd &x_ind)
Evaluate and decompose the covariance matrix using Cholesky decomposition.
bool prepareTrainingKernelGeometry(const EigenMatrix &x, const Eigen::VectorXd &x_ind, TrainingKernelGeometry &out, bool reused=false) const
bool assembleDirectionalCovarianceDerivativesFromGeometry(const TrainingKernelGeometry &geom, const EigenMatrix &x, const Eigen::VectorXd &ind_Ddim, const Eigen::VectorXd &uDdim, const Eigen::VectorXd &direction, EigenMatrix &first, EigenMatrix &second, bool serial_per_rank=false) const
bool evaluateDirectionalCurvature(const Eigen::VectorXd &x_ind, const EigenMatrix &x, const Eigen::VectorXd &y, const Eigen::VectorXd &direction, double &curvature, const TrainingKernelGeometry *training_geom=nullptr)
Evaluate the exact directional curvature p^T H p for the current GP objective state.
Definition Hessian.cpp:83
bool assembleExactHessian(const Eigen::VectorXd &x_ind, const EigenMatrix &x, const Eigen::VectorXd &y, int n_hyperparameters, Eigen::MatrixXd &H)
Assemble the full p x p observed-information Hessian of the negative log marginal likelihood at the c...
Definition Hessian.cpp:568
void calculateMeanPrediction(const Eigen::VectorXd &y)
Posterior mean prediction w.r.t energy. This function mimics the following step from the MATLAB code:...
Definition Predict.cpp:688
bool assembleCompositeJointCovarianceSecondDerivative(const EigenMatrix &x, const Eigen::VectorXd &indices, SexpatCF &covariance, const Eigen::VectorXd &direction, EigenMatrix &second) const
bool evaluateNlmlHessianVector(const Eigen::VectorXd &x_ind, const EigenMatrix &x, const Eigen::VectorXd &y, const Eigen::VectorXd &direction, Eigen::VectorXd &product, const TrainingKernelGeometry *training_geom=nullptr)
Exact Hessian-vector product of the negative log marginal likelihood, including the covariance-functi...
Definition Hessian.cpp:371
bool hasDevicePosterior() const noexcept
std::unique_ptr< SexpatCF > sexpat_cov_func
void prepareTrainingPermutation(const Eigen::VectorXd &x_ind)
void extractUniqueIndices(const Eigen::VectorXd &input_ind, Eigen::VectorXd &output_ind) const
Extract unique indices from the input vector.
linalg::linop::DenseCholeskySolver trainingSolver() const
std::pair< double, double > rigidNoiseDerivatives(const Eigen::VectorXd &direction) const
Definition Hessian.cpp:73
bool assembleHessianInDirections(const Eigen::VectorXd &x_ind, const EigenMatrix &x, const Eigen::VectorXd &y, const Eigen::MatrixXd &directions, Eigen::MatrixXd &H)
Definition Hessian.cpp:581
void noteNlmlHessianVectorFallback(const char *where, const char *why)
Definition Hessian.cpp:361
void assembleDirectionalCovarianceDerivatives(const EigenMatrix &x, const Eigen::VectorXd &ind_Ddim, const Eigen::VectorXd &uDdim, const Eigen::VectorXd &direction, EigenMatrix &first, EigenMatrix &second, bool serial_per_rank=false) const
Assemble the directional first and second derivatives of the training covariance matrix for the optim...
Eigen::Index activeFactorOrder(Eigen::Index full_rows) const noexcept
virtual bool exactNlmlHessianVectorAvailable() const noexcept
bool hasTrainingCholeskyFactor(Eigen::Index expected_rows) const noexcept
void evaluateTrainingCovarianceMatrix(const EigenMatrix &x, const Eigen::VectorXd &x_ind, EigenMatrix &cov_matrix, bool serial_per_rank=false, bool include_constant=true, bool include_noise=true)
Evaluate training covariance matrix (gp_cov + noise covariance).
const TrainingKernelGeometry * fitGeometryFor(const EigenMatrix &x, const Eigen::VectorXd &x_ind) const
void factorizeNoisyKernel(const Eigen::MatrixXd &noisy_kernel, double sigma2)
const Eigen::MatrixXd & leftSingularVectors() const
const Eigen::VectorXd & singularValues() const
Eigen::Index numObs() const
StableBasisPosteriorWeights noisyPosteriorWeights(const Eigen::VectorXd &y, double sigma2) const
double trace_product(const EigenMatrix &A, const EigenMatrix &B)
Eigen::VectorXd run(int n_probes, const std::function< double(int)> &probe_fn, KernelInverse::Kind kind, const MpiContext &ctx) const
EigenMatrix solveColumns(const EigenMatrix &B) const
K^{-1} B through the two triangular solves.
Eigen::VectorXd unwhiten(const Eigen::VectorXd &b) const
Eigen::VectorXd solve(const Eigen::VectorXd &y) const override
double teamFactorWallSeconds()
double teamSolveWallSeconds()
bool rank_one_update_lower(EigenMatrix &lower, const Eigen::VectorXd &vector)
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
Eigen::VectorXd unpermuteVector(const Eigen::VectorXd &input, const StructuredPermutation &p)
EigenMatrix permuteSymmetricMatrix(const EigenMatrix &input, const StructuredPermutation &p)
EigenMatrix unpermuteMatrixRows(const EigenMatrix &input, const StructuredPermutation &p)
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20
static void notePeak(const char *phase) noexcept
static double eigensolve_seconds
static double assembly_seconds
static double curvature_seconds
std::vector< TrainingKernelGeometryBlock > blocks
static MpiContext local() noexcept
Definition MpiContext.h:75