Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion source/api_cc/include/DataModifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ class DipoleChargeModifier
tensorflow::GraphDef* graph_def;
bool inited;
VALUETYPE rcut;
int dtype;
VALUETYPE cell_size;
int ntypes;
std::string model_type;
std::vector<int> sel_type;
template<class VT> VT get_scalar(const std::string & name) const;
template<class VT> void get_vector(std::vector<VT> & vec, const std::string & name) const;
void run_model (std::vector<VALUETYPE> & dforce,
template<typename MODELTYPE> void run_model (std::vector<VALUETYPE> & dforce,
std::vector<VALUETYPE> & dvirial,
tensorflow::Session * session,
const std::vector<std::pair<std::string, tensorflow::Tensor>> & input_tensors,
Expand Down
2 changes: 2 additions & 0 deletions source/api_cc/include/DeepPot.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ class DeepPot
// VALUETYPE get_rcut () const;
// int get_ntypes () const;
VALUETYPE rcut;
int dtype;
VALUETYPE cell_size;
std::string model_type;
std::string model_version;
Expand Down Expand Up @@ -408,6 +409,7 @@ class DeepPotModelDevi
// int get_ntypes () const;
VALUETYPE rcut;
VALUETYPE cell_size;
int dtype;
std::string model_type;
std::string model_version;
int ntypes;
Expand Down
5 changes: 3 additions & 2 deletions source/api_cc/include/DeepTensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ class DeepTensor
tensorflow::GraphDef* graph_def;
bool inited;
VALUETYPE rcut;
int dtype;
VALUETYPE cell_size;
int ntypes;
std::string model_type;
Expand All @@ -172,13 +173,13 @@ class DeepTensor
std::vector<int> sel_type;
template<class VT> VT get_scalar(const std::string & name) const;
template<class VT> void get_vector (std::vector<VT> & vec, const std::string & name) const;
void run_model (std::vector<VALUETYPE> & d_tensor_,
template<typename MODELTYPE> void run_model (std::vector<VALUETYPE> & d_tensor_,
tensorflow::Session * session,
const std::vector<std::pair<std::string, tensorflow::Tensor>> & input_tensors,
const AtomMap<VALUETYPE> & atommap,
const std::vector<int> & sel_fwd,
const int nghost = 0);
void run_model (std::vector<VALUETYPE> & dglobal_tensor_,
template<typename MODELTYPE> void run_model (std::vector<VALUETYPE> & dglobal_tensor_,
std::vector<VALUETYPE> & dforce_,
std::vector<VALUETYPE> & dvirial_,
std::vector<VALUETYPE> & datom_tensor_,
Expand Down
56 changes: 56 additions & 0 deletions source/api_cc/include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,27 @@ std::string
name_prefix(
const std::string & name_scope);

/**
* @brief Get the value of a tensor.
* @param[in] session TensorFlow session.
* @param[in] name The name of the tensor.
* @param[in] scope The scope of the tensor.
* @return The value of the tensor.
**/
template<typename VT>
VT
session_get_scalar(
tensorflow::Session* session,
const std::string name,
const std::string scope = "");

/**
* @brief Get the vector of a tensor.
* @param[out] o_vec The output vector.
* @param[in] session TensorFlow session.
* @param[in] name The name of the tensor.
* @param[in] scope The scope of the tensor.
**/
template<typename VT>
void
session_get_vector(
Expand All @@ -155,6 +169,33 @@ session_get_vector(
const std::string name_,
const std::string scope = "");

/**
* @brief Get the type of a tensor.
* @param[in] session TensorFlow session.
* @param[in] name The name of the tensor.
* @param[in] scope The scope of the tensor.
* @return The type of the tensor as int.
**/
int
session_get_dtype(
tensorflow::Session* session,
const std::string name,
const std::string scope = "");

/**
* @brief Get input tensors.
* @param[out] input_tensors Input tensors.
* @param[in] dcoord_ Coordinates of atoms.
* @param[in] ntypes Number of atom types.
* @param[in] datype_ Atom types.
* @param[in] dbox Box matrix.
* @param[in] cell_size Cell size.
* @param[in] fparam_ Frame parameters.
* @param[in] aparam_ Atom parameters.
* @param[in] atommap Atom map.
* @param[in] scope The scope of the tensors.
*/
template <typename MODELTYPE>
int
session_input_tensors (std::vector<std::pair<std::string, tensorflow::Tensor>> & input_tensors,
const std::vector<VALUETYPE> & dcoord_,
Expand All @@ -167,6 +208,21 @@ session_input_tensors (std::vector<std::pair<std::string, tensorflow::Tensor>> &
const deepmd::AtomMap<VALUETYPE>&atommap,
const std::string scope = "");

/**
* @brief Get input tensors.
* @param[out] input_tensors Input tensors.
* @param[in] dcoord_ Coordinates of atoms.
* @param[in] ntypes Number of atom types.
* @param[in] datype_ Atom types.
* @param[in] dlist Neighbor list.
* @param[in] fparam_ Frame parameters.
* @param[in] aparam_ Atom parameters.
* @param[in] atommap Atom map.
* @param[in] nghost Number of ghost atoms.
* @param[in] ago Update the internal neighbour list if ago is 0.
* @param[in] scope The scope of the tensors.
*/
template <typename MODELTYPE>
int
session_input_tensors (std::vector<std::pair<std::string, tensorflow::Tensor>> & input_tensors,
const std::vector<VALUETYPE> & dcoord_,
Expand Down
66 changes: 52 additions & 14 deletions source/api_cc/src/DataModifier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ init (const std::string & model,
// for (int ii = 0; ii < nnodes; ++ii){
// cout << ii << " \t " << graph_def.node(ii).name() << endl;
// }
rcut = get_scalar<VALUETYPE>("descrpt_attr/rcut");
dtype = session_get_dtype(session, "descrpt_attr/rcut");
if (dtype == tensorflow::DT_DOUBLE) {
rcut = get_scalar<double>("descrpt_attr/rcut");
} else {
rcut = get_scalar<float>("descrpt_attr/rcut");
}
cell_size = rcut;
ntypes = get_scalar<int>("descrpt_attr/ntypes");
model_type = get_scalar<STRINGTYPE>("model_attr/model_type");
Expand All @@ -73,6 +78,7 @@ get_vector (std::vector<VT> & vec, const std::string & name) const
session_get_vector<VT>(vec, session, name, name_scope);
}

template <typename MODELTYPE>
void
DipoleChargeModifier::
run_model (std::vector<VALUETYPE> & dforce,
Expand Down Expand Up @@ -111,8 +117,8 @@ run_model (std::vector<VALUETYPE> & dforce,
assert (output_av.dim_size(0) == nframes), "nframes should match";
assert (output_av.dim_size(1) == natoms * 9), "dof of atom virial should be 9 * natoms";

auto of = output_f.flat<VALUETYPE> ();
auto ov = output_v.flat<VALUETYPE> ();
auto of = output_f.flat<MODELTYPE> ();
auto ov = output_v.flat<MODELTYPE> ();

dforce.resize(nall*3);
dvirial.resize(9);
Expand All @@ -124,7 +130,25 @@ run_model (std::vector<VALUETYPE> & dforce,
}
}

template
void
DipoleChargeModifier::
run_model <double> (std::vector<VALUETYPE> & dforce,
std::vector<VALUETYPE> & dvirial,
Session * session,
const std::vector<std::pair<std::string, Tensor>> & input_tensors,
const AtomMap<VALUETYPE> & atommap,
const int nghost);

template
void
DipoleChargeModifier::
run_model <float> (std::vector<VALUETYPE> & dforce,
std::vector<VALUETYPE> & dvirial,
Session * session,
const std::vector<std::pair<std::string, Tensor>> & input_tensors,
const AtomMap<VALUETYPE> & atommap,
const int nghost);

void
DipoleChargeModifier::
Expand Down Expand Up @@ -179,7 +203,12 @@ compute (std::vector<VALUETYPE> & dfcorr_,
nlist_data.make_inlist(nlist);
// make input tensors
std::vector<std::pair<std::string, Tensor>> input_tensors;
int ret = session_input_tensors (input_tensors, dcoord_real, ntypes, datype_real, dbox, nlist, std::vector<VALUETYPE>(), std::vector<VALUETYPE>(), atommap, nghost_real, 0, name_scope);
int ret;
if (dtype == tensorflow::DT_DOUBLE) {
ret = session_input_tensors<double> (input_tensors, dcoord_real, ntypes, datype_real, dbox, nlist, std::vector<VALUETYPE>(), std::vector<VALUETYPE>(), atommap, nghost_real, 0, name_scope);
} else {
ret = session_input_tensors<float> (input_tensors, dcoord_real, ntypes, datype_real, dbox, nlist, std::vector<VALUETYPE>(), std::vector<VALUETYPE>(), atommap, nghost_real, 0, name_scope);
}
assert (nloc_real == ret);
// make bond idx map
std::vector<int > bd_idx(nall, -1);
Expand Down Expand Up @@ -207,22 +236,31 @@ compute (std::vector<VALUETYPE> & dfcorr_,
TensorShape extf_shape ;
extf_shape.AddDim (nframes);
extf_shape.AddDim (dextf.size());
#ifdef HIGH_PREC
Tensor extf_tensor (DT_DOUBLE, extf_shape);
#else
Tensor extf_tensor (DT_FLOAT, extf_shape);
#endif
auto extf = extf_tensor.matrix<VALUETYPE> ();
for (int ii = 0; ii < nframes; ++ii){
for (int jj = 0; jj < extf.size(); ++jj){
extf(ii,jj) = dextf[jj];
Tensor extf_tensor ((tensorflow::DataType) dtype, extf_shape);
if (dtype == tensorflow::DT_DOUBLE) {
auto extf = extf_tensor.matrix<double> ();
for (int ii = 0; ii < nframes; ++ii){
for (int jj = 0; jj < extf.size(); ++jj){
extf(ii,jj) = dextf[jj];
}
}
} else {
auto extf = extf_tensor.matrix<float> ();
for (int ii = 0; ii < nframes; ++ii){
for (int jj = 0; jj < extf.size(); ++jj){
extf(ii,jj) = dextf[jj];
}
}
}
// append extf to input tensor
input_tensors.push_back({"t_ef", extf_tensor});
// run model
std::vector<VALUETYPE> dfcorr, dvcorr;
run_model (dfcorr, dvcorr, session, input_tensors, atommap, nghost_real);
if (dtype == tensorflow::DT_DOUBLE) {
run_model <double> (dfcorr, dvcorr, session, input_tensors, atommap, nghost_real);
} else {
run_model <float> (dfcorr, dvcorr, session, input_tensors, atommap, nghost_real);
}
assert(dfcorr.size() == nall_real * 3);
// back map force
std::vector<VALUETYPE> dfcorr_1 = dfcorr;
Expand Down
Loading