44 double noise_var,
double grad_noise_var)
48 detail::requireJointTrainingObservation(training_data,
"RFFModel::build");
56 Eigen::Index N = training_data.
R.rows();
57 Eigen::Index D = training_data.
R.cols();
65 conf_info_, far_field_radius_);
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);
75 if (n_pt == 1 && conf_info_.n_pt > 1) {
77 s2.resize(conf_info_.n_pt);
83 std::normal_distribution<double> normal(0.0, 1.0);
84 std::uniform_real_distribution<double> uniform(0.0, 2.0 * M_PI);
86 W_.resize(D_rff, d_feat);
88 for (
int f = 0; f < d_feat; ++f) {
89 const int ptf = pt[
static_cast<std::size_t
>(f)];
91 if (ptf >= 0 && ptf < s2.size()) {
93 }
else if (s2.size() == 1) {
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;
103 for (
int r = 0; r < D_rff; ++r) {
104 b_(r) = uniform(rng);
108 c_ = sigma * std::sqrt(2.0 / D_rff);
126 const Eigen::Index D_eff = D_rff + 1;
147#ifdef GPR_HAVE_CUTLASS
148 int mpi_local_nprocs = 1;
149 int mpi_local_rank = 0;
154 MPI_Initialized(&mpi_up);
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;
163#ifdef GPR_HAVE_CUTLASS
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);
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);
187 e_ref_ = training_data.
E.size() > 0 ? energy_at(0) : 0.0;
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));
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);
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_);
213 Eigen::VectorXd u = W_ * phi + b_;
214 Eigen::VectorXd
z = c_ * u.array().cos().matrix();
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;
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())
229 y_loc(row) = training_data.
G(i, d);
230 prec_loc(row) = 1.0 / grad_noise_var;
235 for (Eigen::Index j = 0; j < n_loc_rows; ++j) {
236 ZtP_loc.col(j) *= prec_loc(j);
240 Eigen::VectorXd rhs_tile(D_eff);
241 bool used_cutlass =
false;
242#ifdef GPR_HAVE_CUTLASS
247 y_mat.col(0) = y_loc;
250 rhs_tile = rhs_mat.col(0);
256 A_tile.noalias() = ZtP_loc * Z_loc;
257 rhs_tile.noalias() = ZtP_loc * y_loc;
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));
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");
275 if (mpi_nprocs > 1) {
277 communication_->view());
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();
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),
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));
298 if (mpi_nprocs > 1) {
300 static_cast<int>(rlen * D_eff),
301 communication_->view());
303 for (Eigen::Index col = 0; col < D_eff; ++col) {
304 for (Eigen::Index dr = 0; dr < rlen; ++dr) {
306 row_exact[
static_cast<std::size_t
>(dr + col * rlen)]
311 if (mpi_nprocs > 1 && mpi_rank != 0) {
323 A.diagonal().array() += 1.0;
337 auto try_factor = [&](
double ridge_bump) ->
bool {
338 if (ridge_bump > 0.0) A.diagonal().array() += ridge_bump;
340#ifdef GPR_LINALG_SCALAPACK
341 if (mpi_nprocs > 1) {
342 if (!distributed_state_)
344 std::make_shared<linalg::DistributedState>(communication_);
345 auto& st = *distributed_state_;
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>(
352 if (!inv->factorise(std::move(A_dist), desc,
353 static_cast<int>(D_eff))) {
356 inverse_ = std::move(inv);
360 auto inv = std::make_unique<linalg::DenseLLTInverse>();
361 if (!inv->factorise(A))
return false;
362 inverse_ = std::move(inv);
366 bool ok = try_factor(0.0);
367 if (!ok) ok = try_factor(1e-6);
369 throw std::runtime_error(
370 "RFFModel::build: Cholesky failed even after 1e-6 ridge bump");
377 alpha_ = inverse_->solve(rhs);