Loading...
Searching...
No Matches
RFFModel.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#ifndef GPR_ML_RFFMODEL_H_
6#define GPR_ML_RFFMODEL_H_
7
8#include <Eigen/Dense>
9#include <memory>
10
15
16namespace gpr {
17
18namespace linalg {
19class MpiCommunicator;
21} // namespace linalg
22
51class RFFModel {
52public:
53 RFFModel() = default;
54 explicit RFFModel(
55 std::shared_ptr<const linalg::MpiCommunicator> communication);
56
61
76 void build(SexpatCF& kernel, const AtomsConfiguration& conf_info,
77 const Observation& training_data, int D_rff,
78 double noise_var = 1e-6, double grad_noise_var = 1e-4);
79
87 void calculatePotential(Observation& image) const;
88
97 void calculateVariance(Observation& image) const;
98
107 [[nodiscard]] bool meanHessianVector(const Eigen::VectorXd& x,
108 const Eigen::VectorXd& v,
109 Eigen::VectorXd& action) const;
110
111 bool isBuilt() const
112 {
113 return built_;
114 }
115 [[nodiscard]] NormalEquationBackend normalEquationBackend() const noexcept
116 {
117 return normal_equation_backend_;
118 }
119 [[nodiscard]] const char* normalEquationBackendName() const noexcept
120 {
121 switch (normal_equation_backend_) {
123 return "eigen";
125 return "cutlass";
126 }
127 return "unknown";
128 }
129
130private:
131 std::shared_ptr<const linalg::MpiCommunicator> communication_;
132 std::shared_ptr<linalg::DistributedState> distributed_state_;
133 bool built_ = false;
134 int D_rff_ = 0;
135 NormalEquationBackend normal_equation_backend_ =
137
138 EigenMatrix W_; // (D_rff x d_feat) frequency matrix
139 Eigen::VectorXd b_; // (D_rff,) random phases
140 double c_ = 0.0; // sigma * sqrt(2/D_rff)
141
142 Eigen::VectorXd alpha_; // (D_rff + 1,) BLR weights (last entry = const)
143
144 // KernelInverse over the BLR system A = sum_r Z_r^T diag(prec_r) Z_r + I.
145 // Held by unique_ptr so the concrete impl is selected at build()-time:
146 // * scalapack build, np > 1 -> ScalapackPDPotrfInverse: A is scattered
147 // through the BLACS grid, factorised via pdpotrf, and consumed by
148 // pdtrsm-distributed solves. Per-instance L_dist_ keeps the RFF
149 // factorisation off the DistributedState singleton L cache that the
150 // GP-side cholesky_distributed owns (the cache pollution that
151 // SEGV'd calculateVarianceLaplaceDelta in PR-pre-A6).
152 // * eigen build OR np == 1 -> DenseLLTInverse: rank-local Eigen LLT;
153 // threaded BLAS via EIGEN_USE_BLAS gives single-rank parallelism.
154 // Both impls satisfy the same interface (solve / solve_mat / log_det /
155 // trace_inv_times); calculatePotential / calculateVariance consume them
156 // through the abstract base, no #ifdef leakage into the predict path.
157 std::unique_ptr<linalg::KernelInverse> inverse_;
158
159 double e_ref_ = 0.0; // training_data.E(0, 0); subtracted before
160 // BLR fit, added back to predict mean for
161 // numerical stability (matches ChemGP's
162 // RFF integration in chemgp-core/src/predict.rs).
163
164 AtomsConfiguration conf_info_;
165 double far_field_radius_ = 0.0;
166};
167
168} // namespace gpr
169
170#endif // GPR_ML_RFFMODEL_H_
@ x
Definition Enums.h:28
Abstract operator interface for K^{-1} actions used by Laplace Hessian probe loops and Williams-Rasmu...
const char * normalEquationBackendName() const noexcept
Definition RFFModel.h:119
void build(SexpatCF &kernel, const AtomsConfiguration &conf_info, const Observation &training_data, int D_rff, double noise_var=1e-6, double grad_noise_var=1e-4)
Build the RFF model from trained hyperparameters and data.
Definition RFFModel.cpp:42
NormalEquationBackend normalEquationBackend() const noexcept
Definition RFFModel.h:115
bool isBuilt() const
Definition RFFModel.h:111
void calculateVariance(Observation &image) const
Predict energy/gradient variance at test configurations.
Definition RFFModel.cpp:464
void calculatePotential(Observation &image) const
Predict energy and gradient at test configurations.
Definition RFFModel.cpp:382
RFFModel()=default
bool meanHessianVector(const Eigen::VectorXd &x, const Eigen::VectorXd &v, Eigen::VectorXd &action) const
Exact Hessian of the predicted energy at x applied to v.
Definition RFFModel.cpp:436
Pair-type inverse-distance squared-exponential primitive.
Definition SexpatCF.h:74
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20
Stores configurations of all atoms in the system.
Definition Structures.h:832
Observation data: energy, gradient and positions.