Program Listing for File DeepPotJAX.h

Program Listing for File DeepPotJAX.h#

↰ Return to documentation for file (source/api_cc/include/DeepPotJAX.h)

// SPDX-License-Identifier: LGPL-3.0-or-later
#pragma once

#include <tensorflow/c/c_api.h>
#include <tensorflow/c/eager/c_api.h>

#include "DeepPot.h"
#include "common.h"
#include "neighbor_list.h"

namespace deepmd {
class DeepPotJAX : public DeepPotBackend {
 public:
  DeepPotJAX();
  virtual ~DeepPotJAX();
  DeepPotJAX(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 = "");
  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;
  };
  void get_type_map(std::string& type_map);

  bool is_aparam_nall() const {
    assert(inited);
    return false;
  };
  bool has_default_fparam() const {
    assert(inited);
    return has_default_fparam_;
  };
  int dim_chg_spin() const override {
    assert(inited);
    return dchgspin;
  };
  void set_charge_spin(const std::vector<double>& charge_spin) override {
    assert(inited);
    if (dchgspin == 0) {
      throw deepmd::deepmd_exception(
          "this model does not support a charge/spin condition");
    }
    if (static_cast<int>(charge_spin.size()) != dchgspin) {
      throw deepmd::deepmd_exception("the charge/spin condition carries " +
                                     std::to_string(charge_spin.size()) +
                                     " values but the model expects " +
                                     std::to_string(dchgspin));
    }
    require_addressable_charge_spin(charge_spin);
    default_chg_spin_ = charge_spin;
    has_default_chg_spin_ = true;
  };

  // forward to template class
  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<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 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 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 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 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 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 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 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);

 private:
  void clear_tf_resources() noexcept;

  bool inited;
  // device
  std::string device;
  // the cutoff radius
  double rcut;
  // the number of types
  int ntypes;
  // the dimension of the frame parameter
  int dfparam;
  // the dimension of the atomic parameter
  int daparam;
  // the dimension of charge/spin condition inputs
  int dchgspin;
  // has default charge/spin values
  bool has_default_chg_spin_;
  // default charge/spin values
  std::vector<double> default_chg_spin_;
  // type map
  std::string type_map;
  // sel
  std::vector<int64_t> sel;
  // number of neighbors
  int nnei;
  // do message passing
  bool do_message_passing;
  // has default fparam
  bool has_default_fparam_;
  // Frame parameters embedded in the exported model. The JAX SavedModel
  // signatures require a concrete tensor, so the C++ API materializes these
  // values when callers omit fparam.
  std::vector<double> default_fparam_;
  // Model-level pair-exclusion keep table (flat (ntypes+1)^2), built once in
  // init from the exported ``get_pair_exclude_types``. Empty => no exclusion.
  // The exported ``call_lower_*`` consumes a pre-excluded nlist (decision
  // #18/A4), so this ingestion seam folds exclusion into the LAMMPS nlist.
  std::vector<int> pair_exclude_table_;
  // whether SavedModel execution goes through XLA and benefits from shape
  // padding; true for JAX/jax2tf XlaCallModule and TF2 jit_compile exports
  bool uses_xla_compilation_ = false;
  // padding to nall
  int padding_to_nall = 0;
  // padding for nloc
  int padding_for_nloc = 0;
  TF_Graph* graph = nullptr;
  TF_Status* status = nullptr;
  TF_Session* session = nullptr;
  TF_SessionOptions* sessionopts = nullptr;
  TFE_ContextOptions* ctx_opts = nullptr;
  TFE_Context* ctx = nullptr;
  std::vector<TF_Function*> func_vector;
  // neighbor list data
  NeighborListData nlist_data;
  template <typename VALUETYPE>
  void compute(std::vector<ENERGYTYPE>& 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);

  template <typename VALUETYPE>
  void compute(std::vector<ENERGYTYPE>& 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);
};
}  // namespace deepmd