|
| 1 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 2 | +#pragma once |
| 3 | + |
| 4 | +#include <torch/script.h> |
| 5 | +#include <torch/torch.h> |
| 6 | + |
| 7 | +#include "DeepTensor.h" |
| 8 | + |
| 9 | +namespace deepmd { |
| 10 | +/** |
| 11 | + * @brief PyTorch implementation for Deep Tensor. |
| 12 | + **/ |
| 13 | +class DeepTensorPT : public DeepTensorBase { |
| 14 | + public: |
| 15 | + /** |
| 16 | + * @brief Deep Tensor constructor without initialization. |
| 17 | + **/ |
| 18 | + DeepTensorPT(); |
| 19 | + virtual ~DeepTensorPT(); |
| 20 | + /** |
| 21 | + * @brief Deep Tensor constructor with initialization. |
| 22 | + * @param[in] model The name of the frozen model file. |
| 23 | + * @param[in] gpu_rank The GPU rank. Default is 0. |
| 24 | + * @param[in] name_scope Name scopes of operations. |
| 25 | + **/ |
| 26 | + DeepTensorPT(const std::string& model, |
| 27 | + const int& gpu_rank = 0, |
| 28 | + const std::string& name_scope = ""); |
| 29 | + /** |
| 30 | + * @brief Initialize the Deep Tensor. |
| 31 | + * @param[in] model The name of the frozen model file. |
| 32 | + * @param[in] gpu_rank The GPU rank. Default is 0. |
| 33 | + * @param[in] name_scope Name scopes of operations. |
| 34 | + **/ |
| 35 | + void init(const std::string& model, |
| 36 | + const int& gpu_rank = 0, |
| 37 | + const std::string& name_scope = ""); |
| 38 | + |
| 39 | + private: |
| 40 | + /** |
| 41 | + * @brief Evaluate the global tensor and component-wise force and virial. |
| 42 | + * @param[out] global_tensor The global tensor to evaluate. |
| 43 | + * @param[out] force The component-wise force of the global tensor, size odim |
| 44 | + *x natoms x 3. |
| 45 | + * @param[out] virial The component-wise virial of the global tensor, size |
| 46 | + *odim x 9. |
| 47 | + * @param[out] atom_tensor The atomic tensor value of the model, size natoms x |
| 48 | + *odim. |
| 49 | + * @param[out] atom_virial The component-wise atomic virial of the global |
| 50 | + *tensor, size odim x natoms x 9. |
| 51 | + * @param[in] coord The coordinates of atoms. The array should be of size |
| 52 | + *natoms x 3. |
| 53 | + * @param[in] atype The atom types. The list should contain natoms ints. |
| 54 | + * @param[in] box The cell of the region. The array should be of size 9. |
| 55 | + * @param[in] request_deriv Whether to request the derivative of the global |
| 56 | + * tensor, including force and virial. |
| 57 | + **/ |
| 58 | + template <typename VALUETYPE> |
| 59 | + void compute(std::vector<VALUETYPE>& global_tensor, |
| 60 | + std::vector<VALUETYPE>& force, |
| 61 | + std::vector<VALUETYPE>& virial, |
| 62 | + std::vector<VALUETYPE>& atom_tensor, |
| 63 | + std::vector<VALUETYPE>& atom_virial, |
| 64 | + const std::vector<VALUETYPE>& coord, |
| 65 | + const std::vector<int>& atype, |
| 66 | + const std::vector<VALUETYPE>& box, |
| 67 | + const bool request_deriv); |
| 68 | + /** |
| 69 | + * @brief Evaluate the global tensor and component-wise force and virial. |
| 70 | + * @param[out] global_tensor The global tensor to evaluate. |
| 71 | + * @param[out] force The component-wise force of the global tensor, size odim |
| 72 | + *x natoms x 3. |
| 73 | + * @param[out] virial The component-wise virial of the global tensor, size |
| 74 | + *odim x 9. |
| 75 | + * @param[out] atom_tensor The atomic tensor value of the model, size natoms x |
| 76 | + *odim. |
| 77 | + * @param[out] atom_virial The component-wise atomic virial of the global |
| 78 | + *tensor, size odim x natoms x 9. |
| 79 | + * @param[in] coord The coordinates of atoms. The array should be of size |
| 80 | + *natoms x 3. |
| 81 | + * @param[in] atype The atom types. The list should contain natoms ints. |
| 82 | + * @param[in] box The cell of the region. The array should be of size 9. |
| 83 | + * @param[in] nghost The number of ghost atoms. |
| 84 | + * @param[in] inlist The input neighbour list. |
| 85 | + * @param[in] request_deriv Whether to request the derivative of the global |
| 86 | + * tensor, including force and virial. |
| 87 | + **/ |
| 88 | + template <typename VALUETYPE> |
| 89 | + void compute(std::vector<VALUETYPE>& global_tensor, |
| 90 | + std::vector<VALUETYPE>& force, |
| 91 | + std::vector<VALUETYPE>& virial, |
| 92 | + std::vector<VALUETYPE>& atom_tensor, |
| 93 | + std::vector<VALUETYPE>& atom_virial, |
| 94 | + const std::vector<VALUETYPE>& coord, |
| 95 | + const std::vector<int>& atype, |
| 96 | + const std::vector<VALUETYPE>& box, |
| 97 | + const int nghost, |
| 98 | + const InputNlist& inlist, |
| 99 | + const bool request_deriv); |
| 100 | + |
| 101 | + public: |
| 102 | + /** |
| 103 | + * @brief Get the cutoff radius. |
| 104 | + * @return The cutoff radius. |
| 105 | + **/ |
| 106 | + double cutoff() const { |
| 107 | + assert(inited); |
| 108 | + return rcut; |
| 109 | + }; |
| 110 | + /** |
| 111 | + * @brief Get the number of types. |
| 112 | + * @return The number of types. |
| 113 | + **/ |
| 114 | + int numb_types() const { |
| 115 | + assert(inited); |
| 116 | + return ntypes; |
| 117 | + }; |
| 118 | + /** |
| 119 | + * @brief Get the output dimension. |
| 120 | + * @return The output dimension. |
| 121 | + **/ |
| 122 | + int output_dim() const { |
| 123 | + assert(inited); |
| 124 | + return odim; |
| 125 | + }; |
| 126 | + /** |
| 127 | + * @brief Get the list of sel types. |
| 128 | + * @return The list of sel types. |
| 129 | + */ |
| 130 | + const std::vector<int>& sel_types() const { |
| 131 | + assert(inited); |
| 132 | + return sel_type; |
| 133 | + }; |
| 134 | + /** |
| 135 | + * @brief Get the type map (element name of the atom types) of this model. |
| 136 | + * @param[out] type_map The type map of this model. |
| 137 | + **/ |
| 138 | + void get_type_map(std::string& type_map); |
| 139 | + |
| 140 | + /** |
| 141 | + * @brief Evaluate the global tensor and component-wise force and virial. |
| 142 | + * @param[out] global_tensor The global tensor to evaluate. |
| 143 | + * @param[out] force The component-wise force of the global tensor, size odim |
| 144 | + *x natoms x 3. |
| 145 | + * @param[out] virial The component-wise virial of the global tensor, size |
| 146 | + *odim x 9. |
| 147 | + * @param[out] atom_tensor The atomic tensor value of the model, size natoms x |
| 148 | + *odim. |
| 149 | + * @param[out] atom_virial The component-wise atomic virial of the global |
| 150 | + *tensor, size odim x natoms x 9. |
| 151 | + * @param[in] coord The coordinates of atoms. The array should be of size |
| 152 | + *natoms x 3. |
| 153 | + * @param[in] atype The atom types. The list should contain natoms ints. |
| 154 | + * @param[in] box The cell of the region. The array should be of size 9. |
| 155 | + * @param[in] request_deriv Whether to request the derivative of the global |
| 156 | + * tensor, including force and virial. |
| 157 | + * @{ |
| 158 | + **/ |
| 159 | + void computew(std::vector<double>& global_tensor, |
| 160 | + std::vector<double>& force, |
| 161 | + std::vector<double>& virial, |
| 162 | + std::vector<double>& atom_tensor, |
| 163 | + std::vector<double>& atom_virial, |
| 164 | + const std::vector<double>& coord, |
| 165 | + const std::vector<int>& atype, |
| 166 | + const std::vector<double>& box, |
| 167 | + const bool request_deriv); |
| 168 | + void computew(std::vector<float>& global_tensor, |
| 169 | + std::vector<float>& force, |
| 170 | + std::vector<float>& virial, |
| 171 | + std::vector<float>& atom_tensor, |
| 172 | + std::vector<float>& atom_virial, |
| 173 | + const std::vector<float>& coord, |
| 174 | + const std::vector<int>& atype, |
| 175 | + const std::vector<float>& box, |
| 176 | + const bool request_deriv); |
| 177 | + /** @} */ |
| 178 | + /** |
| 179 | + * @brief Evaluate the global tensor and component-wise force and virial. |
| 180 | + * @param[out] global_tensor The global tensor to evaluate. |
| 181 | + * @param[out] force The component-wise force of the global tensor, size odim |
| 182 | + *x natoms x 3. |
| 183 | + * @param[out] virial The component-wise virial of the global tensor, size |
| 184 | + *odim x 9. |
| 185 | + * @param[out] atom_tensor The atomic tensor value of the model, size natoms x |
| 186 | + *odim. |
| 187 | + * @param[out] atom_virial The component-wise atomic virial of the global |
| 188 | + *tensor, size odim x natoms x 9. |
| 189 | + * @param[in] coord The coordinates of atoms. The array should be of size |
| 190 | + *natoms x 3. |
| 191 | + * @param[in] atype The atom types. The list should contain natoms ints. |
| 192 | + * @param[in] box The cell of the region. The array should be of size 9. |
| 193 | + * @param[in] nghost The number of ghost atoms. |
| 194 | + * @param[in] inlist The input neighbour list. |
| 195 | + * @param[in] request_deriv Whether to request the derivative of the global |
| 196 | + * tensor, including force and virial. |
| 197 | + * @{ |
| 198 | + **/ |
| 199 | + void computew(std::vector<double>& global_tensor, |
| 200 | + std::vector<double>& force, |
| 201 | + std::vector<double>& virial, |
| 202 | + std::vector<double>& atom_tensor, |
| 203 | + std::vector<double>& atom_virial, |
| 204 | + const std::vector<double>& coord, |
| 205 | + const std::vector<int>& atype, |
| 206 | + const std::vector<double>& box, |
| 207 | + const int nghost, |
| 208 | + const InputNlist& inlist, |
| 209 | + const bool request_deriv); |
| 210 | + void computew(std::vector<float>& global_tensor, |
| 211 | + std::vector<float>& force, |
| 212 | + std::vector<float>& virial, |
| 213 | + std::vector<float>& atom_tensor, |
| 214 | + std::vector<float>& atom_virial, |
| 215 | + const std::vector<float>& coord, |
| 216 | + const std::vector<int>& atype, |
| 217 | + const std::vector<float>& box, |
| 218 | + const int nghost, |
| 219 | + const InputNlist& inlist, |
| 220 | + const bool request_deriv); |
| 221 | + /** @} */ |
| 222 | + |
| 223 | + private: |
| 224 | + int num_intra_nthreads, num_inter_nthreads; |
| 225 | + bool inited; |
| 226 | + double rcut; |
| 227 | + int ntypes; |
| 228 | + mutable int odim; |
| 229 | + std::vector<int> sel_type; |
| 230 | + std::string name_scope; |
| 231 | + // PyTorch module and device management |
| 232 | + mutable torch::jit::script::Module module; |
| 233 | + int gpu_id; |
| 234 | + bool gpu_enabled; |
| 235 | + NeighborListData nlist_data; |
| 236 | + // Neighbor list tensors for efficient computation |
| 237 | + at::Tensor firstneigh_tensor; |
| 238 | + |
| 239 | + /** |
| 240 | + * @brief Translate PyTorch exceptions to the DeePMD-kit exception. |
| 241 | + * @param[in] f The function to run. |
| 242 | + * @example translate_error([&](){...}); |
| 243 | + */ |
| 244 | + void translate_error(std::function<void()> f); |
| 245 | +}; |
| 246 | + |
| 247 | +} // namespace deepmd |
0 commit comments