Build the RFF model from trained hyperparameters and data.
45{
46 built_ = false;
47 inverse_.reset();
48 detail::requireJointTrainingObservation(training_data, "RFFModel::build");
49 D_rff_ = D_rff;
51 conf_info_.copy(kernel.getConfInfo());
52 far_field_radius_ = kernel.farFieldRadius();
53
55 far_field_radius_);
56 Eigen::Index N = training_data.R.rows();
57 Eigen::Index D = training_data.R.cols();
58
59
60
61
62
65 conf_info_, far_field_radius_);
66
67
68 Eigen::Index n_pt = ls.size();
69 Eigen::VectorXd s2(n_pt);
70 for (Eigen::Index p = 0; p < n_pt; ++p) {
71 double l = *(ls.data() + p);
72 s2(p) = 1.0 / (l * l);
73 }
74
75 if (n_pt == 1 && conf_info_.n_pt > 1) {
76 double ref = s2(0);
77 s2.resize(conf_info_.n_pt);
78 s2.setConstant(ref);
79 }
80
81
82 std::mt19937 rng(42);
83 std::normal_distribution<double> normal(0.0, 1.0);
84 std::uniform_real_distribution<double> uniform(0.0, 2.0 * M_PI);
85
86 W_.resize(D_rff, d_feat);
87 b_.resize(D_rff);
88 for (int f = 0; f < d_feat; ++f) {
89 const int ptf = pt[static_cast<std::size_t>(f)];
90 double s2_f = 0.0;
91 if (ptf >= 0 && ptf < s2.size()) {
92 s2_f = s2(ptf);
93 } else if (s2.size() == 1) {
94 s2_f = s2(0);
95 } else {
96 continue;
97 }
98 const double std_dev = std::sqrt(2.0 * s2_f);
99 for (int r = 0; r < D_rff; ++r) {
100 W_(r, f) = normal(rng) * std_dev;
101 }
102 }
103 for (int r = 0; r < D_rff; ++r) {
104 b_(r) = uniform(rng);
105 }
106
107 double sigma = std::sqrt(kernel.getMagnSigma2());
108 c_ = sigma * std::sqrt(2.0 / D_rff);
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126 const Eigen::Index D_eff = D_rff + 1;
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145 int mpi_nprocs = 1;
146 int mpi_rank = 0;
147#ifdef GPR_HAVE_CUTLASS
148 int mpi_local_nprocs = 1;
149 int mpi_local_rank = 0;
150#endif
151#ifdef GPR_HAVE_MPI
152 {
153 int mpi_up = 0;
154 MPI_Initialized(&mpi_up);
155 if (mpi_up) {
156 if (!communication_)
157 communication_ =
158 std::make_shared<linalg::MpiCommunicator>(MPI_COMM_WORLD);
159 const auto process_group = communication_->view();
160 mpi_nprocs = process_group.nprocs;
161 mpi_rank = process_group.rank;
162
163#ifdef GPR_HAVE_CUTLASS
164
165
166 MPI_Comm local_comm = MPI_COMM_NULL;
167 MPI_Comm_split_type(process_group.comm, MPI_COMM_TYPE_SHARED,
168 mpi_rank, MPI_INFO_NULL, &local_comm);
169 if (local_comm != MPI_COMM_NULL) {
170 MPI_Comm_size(local_comm, &mpi_local_nprocs);
171 MPI_Comm_rank(local_comm, &mpi_local_rank);
172 MPI_Comm_free(&local_comm);
173 }
174#endif
175 }
176 }
177#endif
178
179
180
181
182
183 const auto energy_at = [&](Eigen::Index row) -> double {
184 return training_data.E.rows() == 1 ? training_data.E(0, row)
185 : training_data.E(row, 0);
186 };
187 e_ref_ = training_data.E.size() > 0 ? energy_at(0) : 0.0;
188
189
190
191 constexpr Eigen::Index kObsTile = 8;
192 const Eigen::Index n_obs_tiles = (N + kObsTile - 1) / kObsTile;
193 std::vector<EigenMatrix> owned_A_tiles;
194 std::vector<linalg::ExactSum> rhs_exact(static_cast<std::size_t>(D_eff));
195 Eigen::VectorXd phi;
197
198 for (Eigen::Index tile = mpi_rank; tile < n_obs_tiles; tile += mpi_nprocs) {
199 const Eigen::Index tile_lo = tile * kObsTile;
200 const Eigen::Index n_loc = std::min(kObsTile, N - tile_lo);
201 const Eigen::Index n_loc_rows = n_loc * (1 + D);
202 EigenMatrix Z_loc = EigenMatrix::Zero(n_loc_rows, D_eff);
203 Eigen::VectorXd y_loc(n_loc_rows);
204 Eigen::VectorXd prec_loc(n_loc_rows);
205
206 for (Eigen::Index i_loc = 0; i_loc < n_loc; ++i_loc) {
207 const Eigen::Index i = tile_lo + i_loc;
209 xi.row(0) = training_data.R.row(i);
211 xi, conf_info_, phi, J_phi, far_field_radius_);
212
213 Eigen::VectorXd u = W_ * phi + b_;
214 Eigen::VectorXd
z = c_ * u.array().cos().matrix();
215
216 Z_loc.row(i_loc).head(D_rff) =
z.transpose();
217 Z_loc(i_loc, D_rff) = 1.0;
218 y_loc(i_loc) = energy_at(i) - e_ref_;
219 prec_loc(i_loc) = 1.0 / noise_var;
220
221 Eigen::VectorXd sin_u = u.array().sin().matrix();
223 for (Eigen::Index d = 0; d < D; ++d) {
224 const Eigen::Index row = n_loc + i_loc * D + d;
225 Z_loc.row(row).head(D_rff) =
226 (-c_ * sin_u.array() * WJ.col(d).array())
227 .matrix()
228 .transpose();
229 y_loc(row) = training_data.G(i, d);
230 prec_loc(row) = 1.0 / grad_noise_var;
231 }
232 }
233
235 for (Eigen::Index j = 0; j < n_loc_rows; ++j) {
236 ZtP_loc.col(j) *= prec_loc(j);
237 }
238
240 Eigen::VectorXd rhs_tile(D_eff);
241 bool used_cutlass = false;
242#ifdef GPR_HAVE_CUTLASS
244 mpi_local_nprocs)) {
247 y_mat.col(0) = y_loc;
250 rhs_tile = rhs_mat.col(0);
251 used_cutlass = true;
253 }
254#endif
255 if (!used_cutlass) {
256 A_tile.noalias() = ZtP_loc * Z_loc;
257 rhs_tile.noalias() = ZtP_loc * y_loc;
258 }
259 owned_A_tiles.push_back(std::move(A_tile));
260 for (Eigen::Index r = 0; r < D_eff; ++r) {
261 rhs_exact[static_cast<std::size_t>(r)].add(rhs_tile(r));
262 }
263 }
264
265#ifdef GPR_HAVE_MPI
266 if (mpi_nprocs > 1) {
267#ifndef GPR_LINALG_SCALAPACK
268 throw std::runtime_error(
269 "RFFModel::build: multi-rank MPI RFF requires "
270 "linalg_backend=scalapack");
271#endif
272 }
273#endif
274
275 if (mpi_nprocs > 1) {
277 communication_->view());
278 }
279 Eigen::VectorXd rhs(D_eff);
280 for (Eigen::Index r = 0; r < D_eff; ++r)
281 rhs(r) = rhs_exact[static_cast<std::size_t>(r)].value();
282
284 constexpr Eigen::Index kRowBatch = 32;
285 std::vector<linalg::ExactSum> row_exact;
286 for (Eigen::Index r0 = 0; r0 < D_eff; r0 += kRowBatch) {
287 const Eigen::Index rlen = std::min(kRowBatch, D_eff - r0);
288 row_exact.assign(static_cast<std::size_t>(rlen * D_eff),
289 linalg::ExactSum{});
290 for (const auto& tile_mat: owned_A_tiles) {
291 for (Eigen::Index col = 0; col < D_eff; ++col) {
292 for (Eigen::Index dr = 0; dr < rlen; ++dr) {
293 row_exact[static_cast<std::size_t>(dr + col * rlen)].add(
294 tile_mat(r0 + dr, col));
295 }
296 }
297 }
298 if (mpi_nprocs > 1) {
300 static_cast<int>(rlen * D_eff),
301 communication_->view());
302 }
303 for (Eigen::Index col = 0; col < D_eff; ++col) {
304 for (Eigen::Index dr = 0; dr < rlen; ++dr) {
305 A(r0 + dr, col) =
306 row_exact[static_cast<std::size_t>(dr + col * rlen)]
307 .value();
308 }
309 }
310 }
311 if (mpi_nprocs > 1 && mpi_rank != 0) {
312 A.resize(0, 0);
313 rhs.resize(0);
314 }
315
316
317
318
319
320
321
322
323 A.diagonal().array() += 1.0;
324
325
326
327
328
329
330
331
332
333
334
335
336
337 auto try_factor = [&](double ridge_bump) -> bool {
338 if (ridge_bump > 0.0) A.diagonal().array() += ridge_bump;
339
340#ifdef GPR_LINALG_SCALAPACK
341 if (mpi_nprocs > 1) {
342 if (!distributed_state_)
343 distributed_state_ =
344 std::make_shared<linalg::DistributedState>(communication_);
345 auto& st = *distributed_state_;
346 st.initialize();
347 int desc[9];
348 std::vector<double> A_dist = st.scatter(
349 A, static_cast<int>(D_eff), static_cast<int>(D_eff), desc);
350 auto inv = std::make_unique<linalg::ScalapackPDPotrfInverse>(
351 distributed_state_);
352 if (!inv->factorise(std::move(A_dist), desc,
353 static_cast<int>(D_eff))) {
354 return false;
355 }
356 inverse_ = std::move(inv);
357 return true;
358 }
359#endif
360 auto inv = std::make_unique<linalg::DenseLLTInverse>();
361 if (!inv->factorise(A)) return false;
362 inverse_ = std::move(inv);
363 return true;
364 };
365
366 bool ok = try_factor(0.0);
367 if (!ok) ok = try_factor(1e-6);
368 if (!ok) {
369 throw std::runtime_error(
370 "RFFModel::build: Cholesky failed even after 1e-6 ridge bump");
371 }
372
373
374
375
376
377 alpha_ = inverse_->solve(rhs);
378
379 built_ = true;
380}
static std::vector< int > featurePairtypes(const AtomsConfiguration &conf_info)
Pairtype index for each feature.
static void computeWithJacobian(const Coord &x, const AtomsConfiguration &conf_info, Eigen::VectorXd &phi, EigenMatrix &J_phi)
Compute phi(x) and its Jacobian d(phi)/d(x).
static int featureDimension(const AtomsConfiguration &conf_info)
Number of features after omitting EMPTY pairtypes.
bool availableForLocalRank(int local_rank, int local_nprocs) noexcept
EigenMatrix gemm_nn(const EigenMatrix &A, const EigenMatrix &B)
void exact_sum_bcast(ExactSum *sums, int count, const MpiContext &context)
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Eigen::Matrix< double, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixd