Loading...
Searching...
No Matches
pot::ActiveGP Class Reference

#include <gpr/potentials/ActiveGP.h>

Public Types

enum class  AcquireReason { Variance , Certify , Batch , Reject }
 Fired when a point is delegated, before the oracle runs. More...
using OracleFn
using AcquireHook = std::function<void(AcquireReason)>
using ModelUpdateHook = std::function<void(std::uint64_t epoch)>
 Fired after an incorporation changed the surface.
using Config = ActiveGPConfig

Public Member Functions

 ActiveGP (std::shared_ptr< gpr::neb::ActiveLearningSession > session, OracleFn oracle, Config config=Config())
std::uint64_t modelState () const noexcept
long trainingSize () const noexcept
 Training rows currently held.
long oracleCalls () const noexcept
 Oracle calls made through this surrogate.
void setAcquireHook (AcquireHook hook)
void setModelUpdateHook (ModelUpdateHook hook)
int force (long n_atoms, const double *positions, const int *atomic_nrs, double *forces, double *energy, double *variance, const double *box)
int forceBatch (long n_systems, long n_atoms, const double *positions, const int *atomic_nrs, double *forces, double *energies, double *variances, const double *boxes)
void incorporate ()
 Host-requested incorporation boundary (the ABI hint channel).

Detailed Description

Definition at line 55 of file ActiveGP.h.

Member Typedef Documentation

◆ AcquireHook

using pot::ActiveGP::AcquireHook = std::function<void(AcquireReason)>

Definition at line 65 of file ActiveGP.h.

◆ Config

Definition at line 69 of file ActiveGP.h.

◆ ModelUpdateHook

using pot::ActiveGP::ModelUpdateHook = std::function<void(std::uint64_t epoch)>

Fired after an incorporation changed the surface.

Definition at line 67 of file ActiveGP.h.

◆ OracleFn

Initial value:
std::function<int(long n_atoms, const double* positions,
const int* atomic_nrs, const double* box,
double* energy, double* forces)>

Ground truth. Returns 0 on success, positive recoverable, negative fatal, matching the engine ABI convention.

Definition at line 59 of file ActiveGP.h.

Member Enumeration Documentation

◆ AcquireReason

enum class pot::ActiveGP::AcquireReason
strong

Fired when a point is delegated, before the oracle runs.

Enumerator
Variance 
Certify 
Batch 
Reject 

Definition at line 64 of file ActiveGP.h.

64{ Variance, Certify, Batch, Reject };

Constructor & Destructor Documentation

◆ ActiveGP()

pot::ActiveGP::ActiveGP ( std::shared_ptr< gpr::neb::ActiveLearningSession > session,
OracleFn oracle,
Config config = Config() )

Definition at line 13 of file ActiveGP.cpp.

15 : session_(std::move(session)), oracle_(std::move(oracle)), config_(config)
16{
17 if (!session_) throw std::invalid_argument("ActiveGP: null session");
18 if (!oracle_) throw std::invalid_argument("ActiveGP: null oracle");
19 session_->setRefitTrainingRevisions(config_.refit_training_revisions);
20}

Member Function Documentation

◆ force()

int pot::ActiveGP::force ( long n_atoms,
const double * positions,
const int * atomic_nrs,
double * forces,
double * energy,
double * variance,
const double * box )

LOTF force. Returns 0 on success; a nonzero oracle return is propagated unchanged.

Definition at line 107 of file ActiveGP.cpp.

110{
111 if (n_atoms <= 0 || positions == nullptr || forces == nullptr ||
112 energy == nullptr) {
113 return -1;
114 }
115 std::lock_guard<std::mutex> guard(mutex_);
116
117 // Acquired rows do not define a posterior before incorporation.
118 const bool cold = !session_->gp.activateMaterializedPosterior();
119 double var = 0.0;
120 if (!cold) {
121 predict(n_atoms, positions, forces, energy, &var);
122 // *energy is the prediction only after this write. On the cold
123 // path the caller's slot is unread and must not skip acquisition.
124 // A non-finite prediction returns a sized zero gradient and does
125 // not call abs on the force.
126 if (!std::isfinite(*energy)) {
127 for (long i = 0; i < 3 * n_atoms; ++i)
128 forces[i] = 0.0;
129 if (variance != nullptr) *variance = var;
130 return 0;
131 }
132 }
133
134 const double sigma = noiseSigma();
135 const double lo = config_.sigma_lo_factor * sigma;
136 const double hi = config_.sigma_hi_factor * sigma;
137 const double sd = std::sqrt(var);
138
139 // A cold model has no posterior to gate on.
140 bool delegate = cold || sd > lo;
141 AcquireReason reason =
144 bool learn = !(!cold && sd > hi);
145
146 // Certify below force: a surrogate convergence claim is verified
147 // against ground truth before it can stand.
148 if (!delegate && config_.certify_below_force > 0.0) {
149 double max_force = 0.0;
150 bool finite_forces = true;
151 for (long i = 0; i < 3 * n_atoms; ++i) {
152 if (!std::isfinite(forces[i])) {
153 finite_forces = false;
154 break;
155 }
156 max_force = std::max(max_force, std::abs(forces[i]));
157 }
158 if (!finite_forces) {
159 for (long i = 0; i < 3 * n_atoms; ++i)
160 forces[i] = 0.0;
161 } else if (max_force < config_.certify_below_force) {
162 delegate = true;
163 reason = AcquireReason::Certify;
164 learn = true;
165 }
166 }
167
168 if (!delegate) {
169 if (variance != nullptr) *variance = var;
170 return 0;
171 }
172
173 const int rc = acquire(n_atoms, positions, atomic_nrs, box, energy, forces,
174 reason, learn);
175 if (rc != 0) return rc;
176 // The oracle is exact at the point it was called.
177 if (variance != nullptr) *variance = 0.0;
178 return 0;
179}
AcquireReason
Fired when a point is delegated, before the oracle runs.
Definition ActiveGP.h:64
double variance(const Weights &w, const Matrix &covariance)

◆ forceBatch()

int pot::ActiveGP::forceBatch ( long n_systems,
long n_atoms,
const double * positions,
const int * atomic_nrs,
double * forces,
double * energies,
double * variances,
const double * boxes )

Batched channel and incorporation boundary. Every system is predicted; the least resolved one above the gate is delegated to the oracle and learned, which is OIE-class argmax-variance acquisition from inside a passive potential. Systems share one composition.

Definition at line 181 of file ActiveGP.cpp.

185{
186 if (n_systems <= 0 || n_atoms <= 0 || positions == nullptr ||
187 forces == nullptr || energies == nullptr) {
188 return -1;
189 }
190 std::lock_guard<std::mutex> guard(mutex_);
191 const long dof = 3 * n_atoms;
192 // Acquired rows do not define a posterior before incorporation.
193 const bool cold = !session_->gp.activateMaterializedPosterior();
194
195 std::vector<double> var(static_cast<std::size_t>(n_systems), 0.0);
196 long worst = -1;
197 double worst_sd = -1.0;
198 for (long s = 0; s < n_systems; ++s) {
199 if (cold) {
200 for (long i = 0; i < dof; ++i)
201 forces[s * dof + i] = 0.0;
202 energies[s] = 0.0;
203 } else {
204 predict(n_atoms, positions + s * dof, forces + s * dof,
205 &energies[s], &var[static_cast<std::size_t>(s)]);
206 }
207 const double sd = std::sqrt(var[static_cast<std::size_t>(s)]);
208 if (sd > worst_sd) {
209 worst_sd = sd;
210 worst = s;
211 }
212 }
213
214 const double sigma = noiseSigma();
215 const double lo = config_.sigma_lo_factor * sigma;
216 const double hi = config_.sigma_hi_factor * sigma;
217
218 // Acquisition: the least resolved system above the gate is the
219 // one worth a true evaluation this round.
220 if (cold || (worst >= 0 && worst_sd > lo)) {
221 const long pick = cold ? 0 : worst;
222 const bool learn = cold || worst_sd <= hi;
223 const int rc = acquire(n_atoms, positions + pick * dof, atomic_nrs,
224 boxes != nullptr ? boxes + pick * 9 : nullptr,
225 &energies[pick], forces + pick * dof,
226 AcquireReason::Batch, learn);
227 if (rc != 0) return rc;
228 var[static_cast<std::size_t>(pick)] = 0.0;
229 }
230
231 if (variances != nullptr) {
232 for (long s = 0; s < n_systems; ++s) {
233 variances[s] = var[static_cast<std::size_t>(s)];
234 }
235 }
236
237 // The batch call is an incorporation boundary.
238 if (pending_incorporation_) {
239 const std::uint64_t before = session_->surfaceEpoch();
240 session_->refreshCovarianceCache();
241 pending_incorporation_ = false;
242 const std::uint64_t after = session_->surfaceEpoch();
243 if (after != before && on_model_update_) on_model_update_(after);
244 }
245 return 0;
246}

◆ incorporate()

void pot::ActiveGP::incorporate ( )

Host-requested incorporation boundary (the ABI hint channel).

Definition at line 94 of file ActiveGP.cpp.

95{
96 std::lock_guard<std::mutex> guard(mutex_);
97 if (!pending_incorporation_) return;
98 const std::uint64_t before = session_->surfaceEpoch();
99 // The session's own training-revision cadence decides whether this
100 // is a full hyperparameter refit or a fixed-theta rematerialize.
101 session_->refreshCovarianceCache();
102 pending_incorporation_ = false;
103 const std::uint64_t after = session_->surfaceEpoch();
104 if (after != before && on_model_update_) on_model_update_(after);
105}

◆ modelState()

std::uint64_t pot::ActiveGP::modelState ( ) const
nodiscardnoexcept

Monotone surface epoch: changes only when an incorporation changes the posterior. Host caches key on this.

Definition at line 22 of file ActiveGP.cpp.

23{
24 return session_->surfaceEpoch();
25}

◆ oracleCalls()

long pot::ActiveGP::oracleCalls ( ) const
inlinenodiscardnoexcept

Oracle calls made through this surrogate.

Definition at line 82 of file ActiveGP.h.

83 {
84 return oracle_calls_;
85 }

◆ setAcquireHook()

void pot::ActiveGP::setAcquireHook ( AcquireHook hook)
inline

Definition at line 87 of file ActiveGP.h.

88 {
89 on_acquire_ = std::move(hook);
90 }

◆ setModelUpdateHook()

void pot::ActiveGP::setModelUpdateHook ( ModelUpdateHook hook)
inline

Definition at line 91 of file ActiveGP.h.

92 {
93 on_model_update_ = std::move(hook);
94 }

◆ trainingSize()

long pot::ActiveGP::trainingSize ( ) const
nodiscardnoexcept

Training rows currently held.

Definition at line 27 of file ActiveGP.cpp.

28{
29 return static_cast<long>(session_->training_obs.R.rows());
30}

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