Program Listing for File DeepPotPTExpt.h#
↰ Return to documentation for file (source/api_cc/include/DeepPotPTExpt.h)
// SPDX-License-Identifier: LGPL-3.0-or-later
#pragma once
#ifdef BUILD_PYTORCH
// AOTInductor package loader requires a header that may not exist on all
// platforms (e.g. macOS x86_64). Disable pt_expt support when missing.
#if __has_include(<torch/csrc/inductor/aoti_package/model_package_loader.h>)
#define BUILD_PT_EXPT 1
#else
#define BUILD_PT_EXPT 0
#endif
#if BUILD_PT_EXPT
#include <torch/torch.h>
#include <array>
#include <cstdint>
#include <mutex>
#include <optional>
#include "DeepPot.h"
#include "graph_assembly.h"
// Forward-declare to keep these out of the public header. Defined in
// commonPTExpt.h, and PinnedCell in longRangePTExpt.h.
namespace deepmd::ptexpt {
class TempFile;
class ChargeStateFold;
class GraphReplay;
class PinnedCell;
struct HaloTensors;
} // namespace deepmd::ptexpt
namespace torch::inductor {
class AOTIModelPackageLoader;
}
namespace deepmd {
class DeepPotPTExpt : public DeepPotBackend {
public:
DeepPotPTExpt();
virtual ~DeepPotPTExpt();
DeepPotPTExpt(const std::string& model,
const int& gpu_rank = 0,
const std::string& file_content = "");
void init(const std::string& model,
const int& gpu_rank = 0,
const std::string& file_content = "");
private:
template <typename VALUETYPE, typename ENERGYVTYPE>
void compute(ENERGYVTYPE& ener,
std::vector<VALUETYPE>& force,
std::vector<VALUETYPE>& virial,
std::vector<VALUETYPE>& atom_energy,
std::vector<VALUETYPE>& atom_virial,
const std::vector<VALUETYPE>& coord,
const std::vector<int>& atype,
const std::vector<VALUETYPE>& box,
const int nghost,
const InputNlist& lmp_list,
const int& ago,
const std::vector<VALUETYPE>& fparam,
const std::vector<VALUETYPE>& aparam,
const std::vector<double>& charge_spin,
const bool atomic);
template <typename VALUETYPE, typename ENERGYVTYPE>
void compute(ENERGYVTYPE& ener,
std::vector<VALUETYPE>& force,
std::vector<VALUETYPE>& virial,
std::vector<VALUETYPE>& atom_energy,
std::vector<VALUETYPE>& atom_virial,
const std::vector<VALUETYPE>& coord,
const std::vector<int>& atype,
const std::vector<VALUETYPE>& box,
const std::vector<VALUETYPE>& fparam,
const std::vector<VALUETYPE>& aparam,
const std::vector<double>& charge_spin,
const bool atomic);
public:
double cutoff() const {
assert(inited);
return rcut;
};
int numb_types() const {
assert(inited);
return ntypes;
};
int numb_types_spin() const {
assert(inited);
return 0;
};
int dim_fparam() const {
assert(inited);
return dfparam;
};
int dim_aparam() const {
assert(inited);
return daparam;
};
int dim_chg_spin() const override {
assert(inited);
return settable_chgspin;
};
void set_charge_spin(const std::vector<double>& charge_spin) override;
void get_type_map(std::string& type_map);
bool is_aparam_nall() const {
assert(inited);
return aparam_nall;
};
bool has_default_fparam() const {
assert(inited);
return has_default_fparam_;
};
// forward to template class (no charge_spin — uses default_chg_spin_
// fallback)
void computew(std::vector<double>& ener,
std::vector<double>& force,
std::vector<double>& virial,
std::vector<double>& atom_energy,
std::vector<double>& atom_virial,
const std::vector<double>& coord,
const std::vector<int>& atype,
const std::vector<double>& box,
const std::vector<double>& fparam,
const std::vector<double>& aparam,
const bool atomic);
void computew(std::vector<double>& ener,
std::vector<float>& force,
std::vector<float>& virial,
std::vector<float>& atom_energy,
std::vector<float>& atom_virial,
const std::vector<float>& coord,
const std::vector<int>& atype,
const std::vector<float>& box,
const std::vector<float>& fparam,
const std::vector<float>& aparam,
const bool atomic);
void computew(std::vector<double>& ener,
std::vector<double>& force,
std::vector<double>& virial,
std::vector<double>& atom_energy,
std::vector<double>& atom_virial,
const std::vector<double>& coord,
const std::vector<int>& atype,
const std::vector<double>& box,
const int nghost,
const InputNlist& inlist,
const int& ago,
const std::vector<double>& fparam,
const std::vector<double>& aparam,
const bool atomic);
void computew(std::vector<double>& ener,
std::vector<float>& force,
std::vector<float>& virial,
std::vector<float>& atom_energy,
std::vector<float>& atom_virial,
const std::vector<float>& coord,
const std::vector<int>& atype,
const std::vector<float>& box,
const int nghost,
const InputNlist& inlist,
const int& ago,
const std::vector<float>& fparam,
const std::vector<float>& aparam,
const bool atomic);
void computew_mixed_type(std::vector<double>& ener,
std::vector<double>& force,
std::vector<double>& virial,
std::vector<double>& atom_energy,
std::vector<double>& atom_virial,
const int& nframes,
const std::vector<double>& coord,
const std::vector<int>& atype,
const std::vector<double>& box,
const std::vector<double>& fparam,
const std::vector<double>& aparam,
const bool atomic);
void computew_mixed_type(std::vector<double>& ener,
std::vector<float>& force,
std::vector<float>& virial,
std::vector<float>& atom_energy,
std::vector<float>& atom_virial,
const int& nframes,
const std::vector<float>& coord,
const std::vector<int>& atype,
const std::vector<float>& box,
const std::vector<float>& fparam,
const std::vector<float>& aparam,
const bool atomic);
// charge_spin overloads — pass runtime charge/spin per call
void computew(std::vector<double>& ener,
std::vector<double>& force,
std::vector<double>& virial,
std::vector<double>& atom_energy,
std::vector<double>& atom_virial,
const std::vector<double>& coord,
const std::vector<int>& atype,
const std::vector<double>& box,
const std::vector<double>& fparam,
const std::vector<double>& aparam,
const std::vector<double>& charge_spin,
const bool atomic) override;
void computew(std::vector<double>& ener,
std::vector<float>& force,
std::vector<float>& virial,
std::vector<float>& atom_energy,
std::vector<float>& atom_virial,
const std::vector<float>& coord,
const std::vector<int>& atype,
const std::vector<float>& box,
const std::vector<float>& fparam,
const std::vector<float>& aparam,
const std::vector<double>& charge_spin,
const bool atomic) override;
void computew(std::vector<double>& ener,
std::vector<double>& force,
std::vector<double>& virial,
std::vector<double>& atom_energy,
std::vector<double>& atom_virial,
const std::vector<double>& coord,
const std::vector<int>& atype,
const std::vector<double>& box,
const int nghost,
const InputNlist& inlist,
const int& ago,
const std::vector<double>& fparam,
const std::vector<double>& aparam,
const std::vector<double>& charge_spin,
const bool atomic) override;
void computew(std::vector<double>& ener,
std::vector<float>& force,
std::vector<float>& virial,
std::vector<float>& atom_energy,
std::vector<float>& atom_virial,
const std::vector<float>& coord,
const std::vector<int>& atype,
const std::vector<float>& box,
const int nghost,
const InputNlist& inlist,
const int& ago,
const std::vector<float>& fparam,
const std::vector<float>& aparam,
const std::vector<double>& charge_spin,
const bool atomic) override;
void computew_mixed_type(std::vector<double>& ener,
std::vector<double>& force,
std::vector<double>& virial,
std::vector<double>& atom_energy,
std::vector<double>& atom_virial,
const int& nframes,
const std::vector<double>& coord,
const std::vector<int>& atype,
const std::vector<double>& box,
const std::vector<double>& fparam,
const std::vector<double>& aparam,
const std::vector<double>& charge_spin,
const bool atomic) override;
void computew_mixed_type(std::vector<double>& ener,
std::vector<float>& force,
std::vector<float>& virial,
std::vector<float>& atom_energy,
std::vector<float>& atom_virial,
const int& nframes,
const std::vector<float>& coord,
const std::vector<int>& atype,
const std::vector<float>& box,
const std::vector<float>& fparam,
const std::vector<float>& aparam,
const std::vector<double>& charge_spin,
const bool atomic) override;
void compute_edges_gpu(double* d_atom_energy,
double* d_force,
double* d_atom_virial,
const double* d_coord,
const int* d_atype,
const int* d_edge_index,
const double* d_edge_vec,
const int nloc,
const int nedge) override;
void compute_edges_gpu(double* d_atom_energy,
double* d_force,
double* d_atom_virial,
const double* d_coord,
const int* d_atype,
const int* d_edge_index,
const double* d_edge_vec,
const int nloc,
const int nedge,
const std::vector<double>& fparam,
const std::vector<double>& aparam,
const int nall_nodes,
const InputNlist* comm_nlist) override;
void compute_canonical_graph_gpu(double* d_atom_energy,
double* d_force,
double* d_virial,
double* d_atom_virial,
const std::int64_t* d_atype,
const void* d_source,
DP_CanonicalIndexWidth index_width,
const float* d_edge_vec,
const std::int64_t* d_destination_row_ptr,
const std::int64_t* d_source_row_ptr,
const std::int64_t* d_source_order,
const double* d_coord,
void* world,
const DP_HaloExchange* halo,
const double* box,
const std::int64_t nloc,
const std::int64_t nall_nodes,
const std::int64_t edge_storage,
void* stream) override;
void compute_edges_gpu(double* d_atom_energy,
double* d_force,
double* d_atom_virial,
const double* d_coord,
const int* d_atype,
const int* d_edge_index,
const float* d_edge_vec,
const int nloc,
const int nedge,
const std::vector<double>& fparam,
const std::vector<double>& aparam,
const int nall_nodes,
const InputNlist* comm_nlist) override;
bool supports_device_edge_inference() const override;
bool uses_fp32_edge_vectors() const override;
bool uses_canonical_graph_inference() const override;
bool canonical_graph_requires_source_csr() const override;
bool canonical_graph_requires_halo() const override;
bool has_long_range() const override;
private:
template <typename EDGE_TYPE>
void compute_edges_gpu_impl(double* d_atom_energy,
double* d_force,
double* d_atom_virial,
const double* d_coord,
const int* d_atype,
const int* d_edge_index,
const EDGE_TYPE* d_edge_vec,
const int nloc,
const int nedge,
const std::vector<double>& fparam,
const std::vector<double>& aparam,
const int nall_nodes,
const InputNlist* comm_nlist);
void compute_canonical_graph_gpu_impl(
double* d_atom_energy,
double* d_force,
double* d_virial,
double* d_atom_virial,
const std::int64_t* d_atype,
const void* d_source,
DP_CanonicalIndexWidth index_width,
const float* d_edge_vec,
const std::int64_t* d_destination_row_ptr,
const std::int64_t* d_source_row_ptr,
const std::int64_t* d_source_order,
const double* d_coord,
void* world,
const DP_HaloExchange* halo,
const double* box,
const std::int64_t nloc,
const std::int64_t nall_nodes,
const std::int64_t edge_storage,
void* stream);
bool inited;
// Every width below is a property of the loaded archive. They read as zero
// until ``init`` has run, so that a query on an uninitialised backend
// answers "none" rather than whatever the allocation happened to hold.
int ntypes = 0;
int dfparam = 0;
int daparam = 0;
// Conditioning width of the compiled forward's argument list. Zero for a
// model that carries no condition, and also for a compressed one, whose
// condition lives in frozen tables rather than in an input. Every gate that
// decides whether to hand the forward a condition tensor reads this.
int dchgspin = 0;
// Width of a charge state this model can be given; see ``dim_chg_spin()``.
int settable_chgspin = 0;
bool aparam_nall;
bool has_default_fparam_;
std::vector<double> default_fparam_;
std::vector<double> default_chg_spin_;
std::vector<std::pair<double, double> > chg_spin_table_ranges_;
double rcut;
int gpu_id;
bool gpu_enabled;
std::vector<std::string> type_map;
std::vector<std::string> output_keys; // sorted internal output key names
bool do_atomic_virial; // whether model was exported with atomic virial corr
int nnei; // expected nlist nnei dimension (= sum(sel))
bool lower_input_is_edge_ = false;
bool lower_input_is_graph_ = false;
bool lower_input_is_canonical_ = false;
// Whether the canonical force assembly groups edges by source, which makes
// the source CSR an input of every evaluation rather than of the per-atom
// virial alone.
bool canonical_requires_source_csr_ = false;
// Two-entry source CSR placeholders that decline the per-atom virial.
at::Tensor source_row_placeholder_;
at::Tensor source_order_placeholder_;
bool graph_edge_fp32_ = false;
// The model carries a long-range module, whose lower reads the topologies
// of the frame after its other inputs: the pair lists, and the mesh of the
// cell of a module that serves periodic systems, for the graph lower; the
// node positions, the communicator of the ranks and the cell for the compact
// canonical lower.
bool long_range_ = false;
// The module serves periodic systems, on a mesh of at most this spacing.
bool long_range_periodic_ = false;
double mesh_spacing_ = 0.0;
NeighborListData nlist_data;
at::Tensor mapping_tensor; // cached mapping tensor (LAMMPS path)
std::vector<std::int64_t> mapping_; // cached mapping vector (LAMMPS path)
at::Tensor firstneigh_tensor; // cached nlist tensor (LAMMPS path)
at::Tensor edge_index_tensor; // cached local edge graph (LAMMPS path)
at::Tensor edge_index_ext_tensor; // cached extended edge graph (LAMMPS path)
std::unique_ptr<torch::inductor::AOTIModelPackageLoader> loader;
// The cell input of the device-resident evaluations of a model with a
// long-range module, in pinned memory that a replay reads again.
std::unique_ptr<deepmd::ptexpt::PinnedCell> long_range_cell_;
// The mesh size of the cell of the last device-resident evaluation of a
// model with a long-range module, zero for a non-periodic frame, and the
// evaluations since it last changed, counted up to the number of calls for
// which a changed mesh is evaluated eagerly before it is captured again.
std::optional<std::array<std::int64_t, 3> > long_range_mesh_;
std::int64_t long_range_mesh_calls_ = 0;
// CUDA-graph replay of repeated device-resident canonical evaluations,
// declared after the loader and the cell so that the graph is released
// before the constants and the cell it reads.
std::unique_ptr<deepmd::ptexpt::GraphReplay> canonical_replay_;
// Serializes the runs of the model, which runs on a single-threaded runner,
// the device-resident entry points, whose replay of canonical evaluations
// keeps its graph across calls, and the replacement of the constants. The
// mutex is recursive because a replayed evaluation enters the runner again.
std::recursive_mutex evaluation_mutex_;
// Optional AOTInductor artifact for multi-rank message-passing inference,
// loaded only when the .pt2 metadata declares ``has_comm_artifact``. It is
// required when ghost features must be exchanged across ranks inside the
// forward pass. ``with_comm_tempfile_`` owns the extracted nested .pt2 for
// the lifetime of ``with_comm_loader``.
bool has_comm_artifact_ = false;
// Whether the regular .pt2 graph consumes the mapping tensor to gather ghost
// features, i.e. the descriptor performs message passing. Read from the
// ``has_message_passing`` metadata field; archives without it default to
// non-message-passing.
bool has_message_passing_ = false;
// Whether the collective empty-rank preflight (allreduce of the minimum
// owned+ghost node count over the LAMMPS communicator, graph with-comm
// route) has PASSED for the current neighbor topology. The preflight
// re-runs whenever the with-comm graph branch is entered with
// ``ago == 0`` (every topology rebuild) or before its first success:
// the node count shares the lifetime of the cached nlist/mapping/edge
// topology, so re-running the collective on cache-hit (``ago > 0``)
// force calls added a global synchronization per MD step without any
// added protection.
bool graph_comm_preflight_done_ = false;
// Device-resident (ntypes+1)^2 model-level pair-type keep table, uploaded
// ONCE in ``init`` from the ``pair_exclude_types`` metadata field (see
// ``deepmd::buildPairExcludeTable``). An UNDEFINED tensor => no model-level
// exclusion (identity). The device is fixed at ``init`` (``gpu_id`` /
// ``gpu_enabled``), so the seam helpers ``index_select`` it directly with no
// per-step CPU clone / H2D upload. Exclusion is a BUILD-time transform
// (decision #18/A4): the C++ ingestion seam is the single application site
// (``applyPairExclusion`` graph / ``applyPairExclusionNlist`` dense); the
// exported .pt2 lowers consume pre-excluded inputs and never re-apply it.
torch::Tensor pair_exclude_table_;
// Host copy of the same table. The graph route folds the exclusion into the
// per-edge assembly predicate, so an excluded edge is never allocated; that
// predicate runs on the host, before the payload reaches the model device.
std::vector<int> pair_exclude_host_;
// Destination-grouped skin topology, rebuilt only when the host rebuilds its
// neighbor list, and the row-pointer scratch the per-step assembly reuses.
deepmd::SkinTopology skin_topology_;
deepmd::GraphAssemblyScratch graph_scratch_;
// Whether the compiled graph lower reads the source-major permutation.
// Recorded by the artifact (``graph_source_csr``); absent metadata means the
// permutation is built, which is always correct.
bool graph_reads_source_csr_ = true;
std::unique_ptr<deepmd::ptexpt::TempFile> with_comm_tempfile_;
std::unique_ptr<torch::inductor::AOTIModelPackageLoader> with_comm_loader;
// The charge/spin condition a compressed descriptor folded into the
// constants of its lower, re-runnable so that a condition chosen at runtime
// can be served. Null for an uncompressed model, which reads its condition
// as an ordinary input and needs no rebuild.
std::unique_ptr<deepmd::ptexpt::ChargeStateFold> charge_state_fold_;
template <typename VALUETYPE, typename ENERGYVTYPE>
void compute_nframes(ENERGYVTYPE& ener,
std::vector<VALUETYPE>& force,
std::vector<VALUETYPE>& virial,
std::vector<VALUETYPE>& atom_energy,
std::vector<VALUETYPE>& atom_virial,
const int nframes,
const std::vector<VALUETYPE>& coord,
const std::vector<int>& atype,
const std::vector<VALUETYPE>& box,
const std::vector<VALUETYPE>& fparam,
const std::vector<VALUETYPE>& aparam,
const std::vector<double>& charge_spin,
const bool atomic);
template <typename VALUETYPE>
void compute_mixed_type_impl(std::vector<double>& ener,
std::vector<VALUETYPE>& force,
std::vector<VALUETYPE>& virial,
std::vector<VALUETYPE>& atom_energy,
std::vector<VALUETYPE>& atom_virial,
const int& nframes,
const std::vector<VALUETYPE>& coord,
const std::vector<int>& atype,
const std::vector<VALUETYPE>& box,
const std::vector<VALUETYPE>& fparam,
const std::vector<VALUETYPE>& aparam,
const std::vector<double>& charge_spin,
const bool atomic);
std::vector<torch::Tensor> run_model(const torch::Tensor& coord,
const torch::Tensor& atype,
const torch::Tensor& nlist,
const torch::Tensor& mapping,
const torch::Tensor& fparam,
const torch::Tensor& aparam,
const torch::Tensor& charge_spin);
std::vector<torch::Tensor> run_model_edges(
const torch::Tensor& coord,
const torch::Tensor& atype,
const torch::Tensor& edge_index,
const torch::Tensor& edge_vec,
const torch::Tensor& edge_scatter_index,
const torch::Tensor& edge_mask,
const torch::Tensor& fparam,
const torch::Tensor& aparam,
const torch::Tensor& charge_spin);
std::vector<torch::Tensor> run_model_graph(
const torch::Tensor& atype,
const torch::Tensor& n_node,
const torch::Tensor& n_local,
const torch::Tensor& edge_index,
const torch::Tensor& edge_vec,
const torch::Tensor& edge_mask,
const torch::Tensor& destination_order,
const torch::Tensor& destination_row_ptr,
const torch::Tensor& source_order,
const torch::Tensor& source_row_ptr,
const torch::Tensor& fparam,
const torch::Tensor& aparam,
const torch::Tensor& charge_spin,
const torch::Tensor& coord,
const std::vector<double>& box);
std::vector<torch::Tensor> run_model_canonical_graph(
const torch::Tensor& atype,
const torch::Tensor& n_node,
const torch::Tensor& n_local,
const torch::Tensor& source,
const torch::Tensor& edge_vec,
const torch::Tensor& destination_row_ptr,
const torch::Tensor& source_row_ptr,
const torch::Tensor& source_order,
const torch::Tensor& coord,
const void* world,
const torch::Tensor& cell,
const ptexpt::HaloTensors& halo);
void join_long_range_exchange(const void* world,
const std::vector<double>& box);
void check_long_range_box(const std::vector<double>& box) const;
std::vector<torch::Tensor> run_model_with_comm(
const torch::Tensor& coord,
const torch::Tensor& atype,
const torch::Tensor& nlist,
const torch::Tensor& mapping,
const torch::Tensor& fparam,
const torch::Tensor& aparam,
const torch::Tensor& charge_spin,
const std::vector<at::Tensor>& comm_tensors);
std::vector<torch::Tensor> run_model_edges_with_comm(
const torch::Tensor& coord,
const torch::Tensor& atype,
const torch::Tensor& extended_atype,
const torch::Tensor& edge_index,
const torch::Tensor& edge_vec,
const torch::Tensor& edge_scatter_index,
const torch::Tensor& edge_mask,
const torch::Tensor& fparam,
const torch::Tensor& aparam,
const torch::Tensor& charge_spin,
const std::vector<at::Tensor>& comm_tensors);
std::vector<torch::Tensor> run_model_graph_with_comm(
const torch::Tensor& atype,
const torch::Tensor& n_node,
const torch::Tensor& n_local,
const torch::Tensor& edge_index,
const torch::Tensor& edge_vec,
const torch::Tensor& edge_mask,
const torch::Tensor& destination_order,
const torch::Tensor& destination_row_ptr,
const torch::Tensor& source_order,
const torch::Tensor& source_row_ptr,
const torch::Tensor& fparam,
const torch::Tensor& aparam,
const torch::Tensor& charge_spin,
const std::vector<at::Tensor>& comm_tensors);
void extract_outputs(std::map<std::string, torch::Tensor>& output_map,
const std::vector<torch::Tensor>& flat_outputs);
void translate_error(std::function<void()> f);
};
} // namespace deepmd
#endif // BUILD_PT_EXPT
#endif // BUILD_PYTORCH