65 model_(torch::jit::Module()),
78 torch::jit::getProfilingMode() = false;
79 const bool strict = m_metatomic_opts.deterministic_strict ||
80 (std::getenv(
"CUBLAS_WORKSPACE_CONFIG") != nullptr);
81 at::globalContext().setDeterministicAlgorithms(true,
83 at::globalContext().setBenchmarkCuDNN(false);
85 "[MetatomicPotential] Deterministic algorithms enabled "
90 "[MetatomicPotential] Deterministic algorithms disabled "
91 "(faster, may diverge across runs / NEB images)");
97 QUILL_LOG_INFO(
m_log,
"[MetatomicPotential] Initializing...");
100 torch::optional<std::string> extensions_directory = torch::nullopt;
102 extensions_directory = m_metatomic_opts.extensions_directory;
106 QUILL_LOG_INFO(m_log,
"[MetatomicPotential] Loading model from '{}'",
107 m_metatomic_opts.model_path);
108 this->model_ = metatomic_torch::load_atomistic_model(
109 m_metatomic_opts.model_path, extensions_directory);
110 } catch (
const std::exception &e) {
111 QUILL_LOG_ERROR(
m_log,
"[MetatomicPotential] Failed to load model: {}",
118 this->
model_.run_method(
"capabilities")
119 .toCustomClass<metatomic_torch::ModelCapabilitiesHolder>();
120 auto requests_ivalue = this->
model_.run_method(
"requested_neighbor_lists");
121 for (
const auto &request_ivalue : requests_ivalue.toList()) {
124 .toCustomClass<metatomic_torch::NeighborListOptionsHolder>();
125 this->nl_requests_.push_back(request);
129 torch::optional<std::string> desired = torch::nullopt;
131 desired = m_metatomic_opts.device;
134 this->capabilities_->supported_devices, desired);
137 QUILL_LOG_INFO(
m_log,
"[MetatomicPotential] Using device: {}",
device_.str());
143 this->dtype_ = torch::kFloat64;
145 this->dtype_ = torch::kFloat32;
147 throw std::runtime_error(
"Unsupported dtype: " +
148 this->capabilities_->dtype());
150 QUILL_LOG_INFO(
m_log,
"[MetatomicPotential] Using dtype: {}",
155 auto outputs = this->capabilities_->outputs();
170 this->energy_key_ = m_metatomic_opts.energy_output;
173 metatomic_torch::pick_output(
"energy", outputs, v_energy);
182 if (!outputs.contains(this->nc_forces_key_)) {
183 throw std::runtime_error(
184 "Missing explicit force_output in metatomic model: " +
189 "non_conservative_force", outputs, v_force);
191 QUILL_LOG_INFO(
m_log,
192 "[MetatomicPotential] Non-conservative forces from '{}'",
196 QUILL_LOG_INFO(
m_log,
197 "[MetatomicPotential] Symmetry averaging over {} rotations",
201 m_log,
"[MetatomicPotential] Per-call random SO(3) rotation enabled");
205 torch::manual_seed(
static_cast<uint64_t
>(random_seed));
206 QUILL_LOG_INFO(
m_log,
207 "[MetatomicPotential] torch RNG seeded from "
208 "main.randomSeed={}",
212 if (!outputs.contains(this->energy_key_)) {
215 "[MetatomicPotential] The model does not provide an '{}' output.",
217 throw std::runtime_error(
"Missing energy output in metatomic model");
221 this->evaluations_options_ =
222 torch::make_intrusive<metatomic_torch::ModelEvaluationOptionsHolder>();
225 auto model_output = outputs.at(this->energy_key_);
226 auto requested_output =
227 torch::make_intrusive<metatomic_torch::ModelOutputHolder>();
230 requested_output->set_sample_kind(model_output->sample_kind());
231 requested_output->explicit_gradients = {};
232 requested_output->set_unit(
"eV");
237 auto nc_info = outputs.at(this->nc_forces_key_);
239 torch::make_intrusive<metatomic_torch::ModelOutputHolder>();
240 requested_nc->set_sample_kind(nc_info->sample_kind());
241 requested_nc->explicit_gradients = {};
242 requested_nc->set_unit(
"eV/Angstrom");
247 if (m_metatomic_opts.uncertainty_threshold > 0) {
248 this->uncertainty_threshold_ = m_metatomic_opts.uncertainty_threshold;
249 const bool explicit_uq_key =
250 !m_metatomic_opts.energy_uncertainty_output.empty();
251 if (explicit_uq_key) {
253 this->energy_uncertainty_key_ =
254 m_metatomic_opts.energy_uncertainty_output;
255 if (!outputs.contains(this->energy_uncertainty_key_)) {
256 QUILL_LOG_ERROR(m_log,
257 "[MetatomicPotential] energy_uncertainty_output '{}' "
258 "is not provided by the model.",
259 this->energy_uncertainty_key_);
260 throw std::runtime_error(
261 "Missing explicit energy_uncertainty_output in metatomic model: " +
262 this->energy_uncertainty_key_);
266 this->energy_uncertainty_key_ = metatomic_torch::pick_output(
267 "energy_uncertainty", outputs, v_energy_uq);
268 } catch (
const std::exception &e) {
270 m_log,
"[MetatomicPotential] No uncertainty output available: {}",
272 this->uncertainty_threshold_ = -1.0;
276 if (this->uncertainty_threshold_ > 0) {
277 auto uncertainty_info = outputs.at(this->energy_uncertainty_key_);
278 if (uncertainty_info->sample_kind() ==
"atom") {
279 auto requested_uncertainty =
280 torch::make_intrusive<metatomic_torch::ModelOutputHolder>();
281 requested_uncertainty->set_sample_kind(
"atom");
282 requested_uncertainty->explicit_gradients = {};
283 requested_uncertainty->set_unit(
"eV");
284 evaluations_options_->outputs.insert(this->energy_uncertainty_key_,
285 requested_uncertainty);
286 QUILL_LOG_INFO(m_log,
287 "[MetatomicPotential] Requested per-atom "
288 "'{}' from model (threshold = {})",
289 this->energy_uncertainty_key_,
290 this->uncertainty_threshold_);
292 QUILL_LOG_DEBUG(m_log,
293 "[MetatomicPotential] Model provides '{}' "
294 "but sample_kind is not \"atom\"; skipping uncertainty "
296 this->energy_uncertainty_key_);
297 this->uncertainty_threshold_ = -1.0;
302 this->check_consistency_ = m_metatomic_opts.check_consistency;
303 QUILL_LOG_INFO(m_log,
"[MetatomicPotential] Initialization complete.");
359 const int *atomicNrs,
double *forces,
360 double *energy,
double *variance,
370 throw std::runtime_error(
371 "[MetatomicPotential] `atomicNrs` must be provided.");
374 const bool use_rotation =
377 const long n_passes =
381 torch::TensorOptions().dtype(torch::kFloat64).device(torch::kCPU);
382 std::vector<int32_t> types_vec(atomicNrs, atomicNrs + nAtoms);
383 auto atomic_types_cpu =
384 torch::tensor(types_vec, torch::TensorOptions().dtype(torch::kInt32));
386 double energy_acc = 0.0;
387 auto forces_acc = torch::zeros({nAtoms, 3}, f64_options);
388 bool variance_set =
false;
390 for (
long i_pass = 0; i_pass < n_passes; ++i_pass) {
391 torch::Tensor R = torch::eye(
392 3, torch::TensorOptions().dtype(this->
dtype_).device(this->
device_));
397 auto R_cpu = R.to(torch::kCPU).to(torch::kFloat64);
398 auto R_T = R.transpose(0, 1);
400 auto pos_cpu = torch::from_blob(
const_cast<double *
>(positions),
401 {nAtoms, 3}, f64_options)
404 torch::from_blob(
const_cast<double *
>(box), {3, 3}, f64_options)
407 pos_cpu = pos_cpu.matmul(R_cpu.transpose(0, 1));
409 cell_cpu = cell_cpu.matmul(R_cpu.transpose(0, 1));
412 std::vector<double> pos_buf(
static_cast<size_t>(nAtoms) * 3);
413 std::vector<double> cell_buf(9);
414 std::memcpy(pos_buf.data(), pos_cpu.contiguous().data_ptr<
double>(),
415 pos_buf.size() *
sizeof(
double));
416 std::memcpy(cell_buf.data(), cell_cpu.contiguous().data_ptr<
double>(),
419 auto torch_positions =
420 torch::from_blob(pos_buf.data(), {nAtoms, 3}, f64_options)
425 auto torch_cell = torch::from_blob(cell_buf.data(), {3, 3}, f64_options)
429 auto cell_norms = torch::norm(torch_cell, 2, 1);
430 auto torch_pbc = cell_norms.abs() > 1e-9;
431 bool periodic[3] = {torch_pbc[0].item<
bool>(), torch_pbc[1].item<bool>(),
432 torch_pbc[2].item<
bool>()};
434 auto atomic_types = atomic_types_cpu.to(this->
device_);
436 auto system = torch::make_intrusive<metatomic_torch::SystemHolder>(
437 atomic_types, torch_positions, torch_cell, torch_pbc);
441 cell_buf.data(), periodic);
442 metatomic_torch::register_autograd_neighbors(system, neighbors,
444 system->add_neighbor_list(request, neighbors);
447 torch::Tensor forces_tensor;
449 auto ivalue_output = this->
model_.forward({
450 std::vector<metatomic_torch::System>{system},
454 auto dict_output = ivalue_output.toGenericDict();
455 auto output_map = dict_output.at(this->
energy_key_)
456 .toCustomClass<metatensor_torch::TensorMapHolder>();
460 if (dict_output.contains(this->energy_uncertainty_key_)) {
461 auto uncertainty_map =
463 .toCustomClass<metatensor_torch::TensorMapHolder>();
464 auto uncertainty_block =
465 metatensor_torch::TensorMapHolder::block_by_id(uncertainty_map,
467 auto flat_uncertainty =
468 uncertainty_block->values().reshape({-1}).to(torch::kCPU);
469 if (variance !=
nullptr && flat_uncertainty.numel() > 0) {
472 flat_uncertainty.to(torch::kFloat64).mean().item<
double>();
475 QUILL_LOG_DEBUG(
m_log,
476 "[MetatomicPotential] Failed to compute mean "
477 "uncertainty for variance.");
480 auto atoms_above_threshold =
482 if (torch::any(atoms_above_threshold).item<bool>()) {
483 auto samples = uncertainty_block->samples();
484 auto atom_indices_all = samples->column(
"atom").to(torch::kCPU);
485 auto atom_indices_above =
486 atom_indices_all.index({atoms_above_threshold});
487 std::ostringstream ss;
488 ss <<
"atoms at index [";
489 auto n_report = std::min<int64_t>(10, atom_indices_above.size(0));
490 for (int64_t i = 0; i < n_report; ++i) {
493 ss << atom_indices_above[i].item<int32_t>();
496 if (atom_indices_above.size(0) > n_report) {
497 ss <<
" and " << (atom_indices_above.size(0) - n_report)
502 "[MetatomicPotential] The uncertainty on atomic energies for "
503 "{} are larger than the threshold of {}. (Key: {}) Be "
505 "when analyzing the results, and consider retraining the "
506 "model to better describe these configurations.",
507 ss.str(), this->uncertainty_threshold_,
508 this->energy_uncertainty_key_);
511 }
catch (
const std::exception &e) {
512 QUILL_LOG_WARNING(
m_log,
513 "[MetatomicPotential] Failed to check {}: {}",
519 metatensor_torch::TensorMapHolder::block_by_id(output_map, 0);
520 auto energy_tensor = energy_block->values();
521 energy_acc += energy_tensor.sum().item<
double>();
525 .toCustomClass<metatensor_torch::TensorMapHolder>();
527 metatensor_torch::TensorMapHolder::block_by_id(nc_map, 0);
528 auto nc_vals = nc_block->values();
529 if (nc_vals.numel() != nAtoms * 3) {
530 throw std::runtime_error(
"[MetatomicPotential] NC force block has " +
531 std::to_string(nc_vals.numel()) +
532 " values, expected " +
533 std::to_string(nAtoms * 3));
535 auto nc_samples = nc_block->samples();
536 if (nc_samples->size() > 0 && nc_samples->names().size() > 1) {
537 auto atom_col = nc_samples->column(
"atom").to(torch::kCPU);
538 if (atom_col.size(0) != nAtoms) {
539 throw std::runtime_error(
540 "[MetatomicPotential] NC force samples atom count mismatch");
544 nc_vals.reshape({nAtoms, 3}).to(torch::kCPU).to(torch::kFloat64);
546 energy_tensor.backward(torch::ones_like(energy_tensor));
547 auto positions_grad = system->positions().grad();
548 forces_tensor = (-positions_grad).to(torch::kCPU).to(torch::kFloat64);
550 }
catch (
const std::exception &e) {
551 QUILL_LOG_ERROR(
m_log,
"[MetatomicPotential] Model evaluation failed: {}",
559 forces_tensor = forces_tensor.matmul(R_cpu);
561 forces_acc += forces_tensor;
564 const double inv_n = 1.0 /
static_cast<double>(n_passes);
565 *energy = energy_acc * inv_n;
566 forces_acc = forces_acc * inv_n;
567 static_cast<void>(variance_set);
569 std::memcpy(forces, forces_acc.contiguous().data_ptr<
double>(),
570 nAtoms * 3 *
sizeof(
double));
578 metatomic_torch::NeighborListOptions request,
long nAtoms,
579 const double *positions,
const double *box,
const bool periodic[3]) {
585 VesinOptions options{};
586 options.cutoff = cutoff;
587 options.full = request->full_list();
588 options.sorted =
false;
589 options.return_shifts =
true;
590 options.return_distances =
false;
591 options.return_vectors =
true;
593 auto owned_list = std::make_unique<VesinNeighborList>();
594 VesinNeighborList *vesin_neighbor_list = owned_list.get();
596 VesinDevice cpu{VesinCPU, 0};
597 const char *error_message =
nullptr;
598 int status = vesin_neighbors(
reinterpret_cast<const double (*)[3]
>(positions),
599 static_cast<size_t>(nAtoms),
600 reinterpret_cast<const double (*)[3]
>(box),
601 const_cast<bool *
>(periodic), cpu, options,
602 vesin_neighbor_list, &error_message);
604 if (status != EXIT_SUCCESS) {
605 std::string err_str =
"vesin_neighbors failed";
606 if (error_message !=
nullptr) {
607 err_str +=
": " + std::string(error_message);
609 err_str +=
" (no message; vesin header/lib ABI mismatch? need vesin>=0.6 "
610 "with matching engine)";
612 throw std::runtime_error(err_str);
616 auto n_pairs =
static_cast<int64_t
>(vesin_neighbor_list->length);
617 auto labels_options_cpu =
618 torch::TensorOptions().dtype(torch::kInt32).device(torch::kCPU);
620 auto pair_samples_values = torch::empty({n_pairs, 5}, labels_options_cpu);
621 auto pair_samples_values_ptr = pair_samples_values.accessor<int32_t, 2>();
622 for (int64_t i = 0; i < n_pairs; i++) {
623 pair_samples_values_ptr[i][0] =
624 static_cast<int32_t
>(vesin_neighbor_list->pairs[i][0]);
625 pair_samples_values_ptr[i][1] =
626 static_cast<int32_t
>(vesin_neighbor_list->pairs[i][1]);
627 pair_samples_values_ptr[i][2] = vesin_neighbor_list->shifts[i][0];
628 pair_samples_values_ptr[i][3] = vesin_neighbor_list->shifts[i][1];
629 pair_samples_values_ptr[i][4] = vesin_neighbor_list->shifts[i][2];
633 auto *raw_list = owned_list.release();
634 auto deleter = [raw_list](
void *) {
635 vesin_free(raw_list);
636 std::unique_ptr<VesinNeighborList> reclaim(raw_list);
638 vesin_neighbor_list = raw_list;
640 auto pair_vectors = torch::from_blob(
641 vesin_neighbor_list->vectors, {n_pairs, 3, 1}, deleter,
642 torch::TensorOptions().dtype(torch::kFloat64).device(torch::kCPU));
644 auto neighbor_samples = torch::make_intrusive<metatensor_torch::LabelsHolder>(
645 std::vector<std::string>{
"first_atom",
"second_atom",
"cell_shift_a",
646 "cell_shift_b",
"cell_shift_c"},
647 pair_samples_values.to(this->
device_));
649 auto labels_options_dev =
650 torch::TensorOptions().dtype(torch::kInt32).device(this->
device_);
651 auto neighbor_component =
652 torch::make_intrusive<metatensor_torch::LabelsHolder>(
653 "xyz", torch::tensor({0, 1, 2}, labels_options_dev).reshape({3, 1}));
654 auto neighbor_properties =
655 torch::make_intrusive<metatensor_torch::LabelsHolder>(
656 "distance", torch::zeros({1, 1}, labels_options_dev));
658 return torch::make_intrusive<metatensor_torch::TensorBlockHolder>(
659 pair_vectors.to(this->dtype_).to(this->device_), neighbor_samples,
660 std::vector<metatensor_torch::Labels>{neighbor_component},
661 neighbor_properties);
701 const double *
const *positions,
702 const int *
const *atomicNrs,
703 double *
const *forces,
704 double *energies,
double *variances,
705 const double *
const *boxes) {
712 torch::TensorOptions().dtype(torch::kFloat64).device(torch::kCPU);
714 std::vector<metatomic_torch::System> systems;
715 std::vector<torch::Tensor> pos_tensors;
716 systems.reserve(
static_cast<size_t>(nSystems));
717 pos_tensors.reserve(
static_cast<size_t>(nSystems));
719 for (
long s = 0; s < nSystems; s++) {
720 auto torch_positions = torch::from_blob(
const_cast<double *
>(positions[s]),
721 {nAtoms, 3}, f64_options)
724 .set_requires_grad(
true);
725 pos_tensors.push_back(torch_positions);
728 torch::from_blob(
const_cast<double *
>(boxes[s]), {3, 3}, f64_options)
732 auto cell_norms = torch::norm(torch_cell, 2, 1);
733 auto torch_pbc = cell_norms.abs() > 1e-9;
734 bool periodic[3] = {torch_pbc[0].item<
bool>(), torch_pbc[1].item<bool>(),
735 torch_pbc[2].item<
bool>()};
738 throw std::runtime_error(
739 "[MetatomicPotential] `atomicNrs` must be provided.");
741 std::vector<int32_t> types_vec(atomicNrs[s], atomicNrs[s] + nAtoms);
743 torch::tensor(types_vec, torch::TensorOptions().dtype(torch::kInt32))
746 auto system = torch::make_intrusive<metatomic_torch::SystemHolder>(
747 atomic_types, torch_positions, torch_cell, torch_pbc);
753 metatomic_torch::register_autograd_neighbors(system, neighbors,
755 system->add_neighbor_list(request, neighbors);
758 systems.push_back(system);
762 metatensor_torch::TensorMap output_map;
764 auto ivalue_output = this->
model_.forward({
769 auto dict_output = ivalue_output.toGenericDict();
771 .toCustomClass<metatensor_torch::TensorMapHolder>();
772 }
catch (
const std::exception &e) {
773 QUILL_LOG_ERROR(
m_log,
774 "[MetatomicPotential] Batched model evaluation failed: {}",
784 metatensor_torch::TensorMapHolder::block_by_id(output_map, 0);
785 auto energy_values = energy_block->values();
786 auto samples = energy_block->samples();
789 bool per_atom = samples->size() > 0 && samples->names().size() > 1;
793 auto system_col = samples->column(
"system").to(torch::kCPU);
794 auto flat_energies = energy_values.reshape({-1}).to(torch::kCPU);
797 for (
long s = 0; s < nSystems; s++) {
798 auto mask = (system_col == s);
800 flat_energies.index({mask}).sum().to(torch::kFloat64).item<
double>();
806 energy_values.sum().backward();
809 auto cpu_energies = energy_values.to(torch::kCPU).to(torch::kFloat64);
810 for (
long s = 0; s < nSystems; s++) {
811 energies[s] = cpu_energies[s].sum().item<
double>();
813 energy_values.backward(torch::ones_like(energy_values));
817 for (
long s = 0; s < nSystems; s++) {
818 auto positions_grad = pos_tensors[s].grad();
819 auto forces_tensor = -positions_grad.to(torch::kCPU).to(torch::kFloat64);
820 std::memcpy(forces[s], forces_tensor.contiguous().data_ptr<
double>(),
821 nAtoms * 3 *
sizeof(
double));
826 for (
long s = 0; s < nSystems; s++) {