diff --git a/source/api_cc/include/AtomMap.h b/source/api_cc/include/AtomMap.h index 32c4ed7c99..bf9a8c650c 100644 --- a/source/api_cc/include/AtomMap.h +++ b/source/api_cc/include/AtomMap.h @@ -4,6 +4,7 @@ // using namespace std; +namespace deepmd{ template class AtomMap { @@ -25,3 +26,4 @@ class AtomMap std::vector fwd_idx_map; std::vector atype; }; +} diff --git a/source/api_cc/include/DataModifier.h b/source/api_cc/include/DataModifier.h index 822812621b..114abfd889 100644 --- a/source/api_cc/include/DataModifier.h +++ b/source/api_cc/include/DataModifier.h @@ -2,6 +2,7 @@ #include "DeepPot.h" +namespace deepmd{ class DipoleChargeModifier { public: @@ -28,10 +29,10 @@ class DipoleChargeModifier int numb_types () const {assert(inited); return ntypes;}; std::vector sel_types () const {assert(inited); return sel_type;}; private: - Session* session; + tensorflow::Session* session; std::string name_scope, name_prefix; int num_intra_nthreads, num_inter_nthreads; - GraphDef graph_def; + tensorflow::GraphDef graph_def; bool inited; VALUETYPE rcut; VALUETYPE cell_size; @@ -42,9 +43,10 @@ class DipoleChargeModifier template void get_vector(std::vector & vec, const std::string & name) const; void run_model (std::vector & dforce, std::vector & dvirial, - Session * session, - const std::vector> & input_tensors, + tensorflow::Session * session, + const std::vector> & input_tensors, const AtomMap & atommap, const int nghost); }; +} diff --git a/source/api_cc/include/DeepPot.h b/source/api_cc/include/DeepPot.h index 53996903c1..63cf568376 100644 --- a/source/api_cc/include/DeepPot.h +++ b/source/api_cc/include/DeepPot.h @@ -2,8 +2,8 @@ #include "common.h" #include "neighbor_list.h" -typedef double compute_t; +namespace deepmd{ class DeepPot { public: @@ -61,9 +61,9 @@ class DeepPot int dim_aparam () const {assert(inited); return daparam;}; void get_type_map (std::string & type_map); private: - Session* session; + tensorflow::Session* session; int num_intra_nthreads, num_inter_nthreads; - GraphDef graph_def; + tensorflow::GraphDef graph_def; bool inited; template VT get_scalar(const std::string & name) const; // VALUETYPE get_rcut () const; @@ -92,7 +92,6 @@ class DeepPot // copy neighbor list info from host bool init_nbor; std::vector sec_a; - compute_t *array_double; NeighborListData nlist_data; InputNlist nlist; AtomMap atommap; @@ -156,9 +155,9 @@ class DeepPotModelDevi const VALUETYPE eps); private: unsigned numb_models; - std::vector sessions; + std::vector sessions; int num_intra_nthreads, num_inter_nthreads; - std::vector graph_defs; + std::vector graph_defs; bool inited; template VT get_scalar(const std::string name) const; // VALUETYPE get_rcut () const; @@ -176,15 +175,15 @@ class DeepPotModelDevi // copy neighbor list info from host bool init_nbor; - compute_t *array_double; std::vector > sec; - AtomMap atommap; + deepmd::AtomMap atommap; NeighborListData nlist_data; InputNlist nlist; // function used for nborlist copy std::vector > get_sel() const; - void cum_sum(const std::vector > n_sel); + void cum_sum(const std::vector > n_sel); }; +} diff --git a/source/api_cc/include/DeepTensor.h b/source/api_cc/include/DeepTensor.h index a60c6e1ed9..bfa5cd1ba8 100644 --- a/source/api_cc/include/DeepTensor.h +++ b/source/api_cc/include/DeepTensor.h @@ -1,7 +1,9 @@ #pragma once -#include "DeepPot.h" +#include "common.h" +#include "neighbor_list.h" +namespace deepmd{ class DeepTensor { public: @@ -29,10 +31,10 @@ class DeepTensor int output_dim () const {assert(inited); return odim;}; const std::vector & sel_types () const {assert(inited); return sel_type;}; private: - Session* session; + tensorflow::Session* session; std::string name_scope; int num_intra_nthreads, num_inter_nthreads; - GraphDef graph_def; + tensorflow::GraphDef graph_def; bool inited; VALUETYPE rcut; VALUETYPE cell_size; @@ -44,8 +46,8 @@ class DeepTensor template VT get_scalar(const std::string & name) const; template void get_vector (std::vector & vec, const std::string & name) const; void run_model (std::vector & d_tensor_, - Session * session, - const std::vector> & input_tensors, + tensorflow::Session * session, + const std::vector> & input_tensors, const AtomMap & atommap, const int nghost = 0); void compute_inner (std::vector & value, @@ -59,4 +61,5 @@ class DeepTensor const int nghost, const InputNlist& inlist); }; +} diff --git a/source/api_cc/include/common.h b/source/api_cc/include/common.h index 46eb076d4f..c489053b6c 100644 --- a/source/api_cc/include/common.h +++ b/source/api_cc/include/common.h @@ -13,7 +13,9 @@ #include #include -using namespace tensorflow; + +namespace deepmd{ + #if TF_MAJOR_VERSION >= 2 && TF_MINOR_VERSION >= 2 typedef tensorflow::tstring STRINGTYPE; #else @@ -37,7 +39,7 @@ struct NeighborListData public: void copy_from_nlist(const InputNlist & inlist); void shuffle(const std::vector & fwd_map); - void shuffle(const AtomMap & map); + void shuffle(const deepmd::AtomMap & map); void shuffle_exclude_empty(const std::vector & fwd_map); void make_inlist(InputNlist & inlist); }; @@ -76,20 +78,30 @@ get_env_nthreads(int & num_intra_nthreads, int & num_inter_nthreads); void -checkStatus(const tensorflow::Status& status); +check_status( + const tensorflow::Status& status); -std::string name_prefix(const std::string & name_scope); +std::string +name_prefix( + const std::string & name_scope); template VT -session_get_scalar(Session* session, const std::string name, const std::string scope = ""); +session_get_scalar( + tensorflow::Session* session, + const std::string name, + const std::string scope = ""); template void -session_get_vector(std::vector & o_vec, Session* session, const std::string name_, const std::string scope = ""); +session_get_vector( + std::vector & o_vec, + tensorflow::Session* session, + const std::string name_, + const std::string scope = ""); int -session_input_tensors (std::vector> & input_tensors, +session_input_tensors (std::vector> & input_tensors, const std::vector & dcoord_, const int & ntypes, const std::vector & datype_, @@ -97,11 +109,11 @@ session_input_tensors (std::vector> & input_tenso const VALUETYPE & cell_size, const std::vector & fparam_, const std::vector & aparam_, - const AtomMap& atommap, + const deepmd::AtomMap&atommap, const std::string scope = ""); int -session_input_tensors (std::vector> & input_tensors, +session_input_tensors (std::vector> & input_tensors, const std::vector & dcoord_, const int & ntypes, const std::vector & datype_, @@ -109,103 +121,9 @@ session_input_tensors (std::vector> & input_tenso InputNlist & dlist, const std::vector & fparam_, const std::vector & aparam_, - const AtomMap& atommap, + const deepmd::AtomMap&atommap, const int nghost, const int ago, const std::string scope = ""); - -// int -// session_input_tensors (std::vector> & input_tensors, -// const std::vector & dcoord_, -// const int & ntypes, -// const std::vector & datype_, -// const std::vector & dbox, -// InputNlist & dlist, -// const std::vector & fparam_, -// const std::vector & aparam_, -// const AtomMap& atommap, -// const int nghost, -// const std::string scope = ""); - -// int -// session_input_tensors (std::vector>& input_tensors, -// const std::vector & dcoord_, -// const int & ntypes, -// const std::vector & atype_, -// const std::vector & dbox, -// const int * ilist, -// const int * jrange, -// const int * jlist, -// const std::vector & fparam_, -// const std::vector & aparam_, -// const AtomMap & atommap, -// const int & nghost); - - -template -VT -session_get_scalar(Session* session, const std::string name_, const std::string scope) -{ - std::string name = name_; - if (scope != "") { - name = scope + "/" + name; - } - std::vector output_tensors; - checkStatus (session->Run(std::vector> ({}), - {name.c_str()}, - {}, - &output_tensors)); - Tensor output_rc = output_tensors[0]; - auto orc = output_rc.flat (); - return orc(0); -} - -template -void -session_get_vector(std::vector & o_vec, Session* session, const std::string name_, const std::string scope) -{ - std::string name = name_; - if (scope != "") { - name = scope + "/" + name; - } - std::vector output_tensors; - checkStatus (session->Run(std::vector> ({}), - {name.c_str()}, - {}, - &output_tensors)); - Tensor output_rc = output_tensors[0]; - assert(1 == output_rc.shape().dims()); - int dof = output_rc.shape().dim_size(0); - o_vec.resize(dof); - auto orc = output_rc.flat (); - for (int ii = 0; ii < dof; ++ii){ - o_vec[ii] = orc(ii); - } -} - - -template -void -select_map(std::vector & out, - const std::vector & in, - const std::vector & idx_map, - const int & stride) -{ -#ifdef DEBUG - assert(in.size() / stride * stride == in.size()), "in size should be multiples of stride" -#endif - for (int ii = 0; ii < in.size() / stride; ++ii){ -#ifdef DEBUG - assert(ii < idx_map.size()), "idx goes over the idx map size"; - assert(idx_map[ii] < out.size()), "mappped idx goes over the out size"; -#endif - if (idx_map[ii] >= 0) { - int to_ii = idx_map[ii]; - for (int dd = 0; dd < stride; ++dd){ - out[to_ii * stride + dd] = in[ii * stride + dd]; - } - } - } } - diff --git a/source/api_cc/src/AtomMap.cc b/source/api_cc/src/AtomMap.cc index dc37669fa6..8e7e82a1ce 100644 --- a/source/api_cc/src/AtomMap.cc +++ b/source/api_cc/src/AtomMap.cc @@ -3,6 +3,8 @@ #include #include +using namespace deepmd; + template AtomMap:: AtomMap() {} diff --git a/source/api_cc/src/DataModifier.cc b/source/api_cc/src/DataModifier.cc index 94ebadf724..f704ba599e 100644 --- a/source/api_cc/src/DataModifier.cc +++ b/source/api_cc/src/DataModifier.cc @@ -1,5 +1,8 @@ #include "DataModifier.h" +using namespace deepmd; +using namespace tensorflow; + DipoleChargeModifier:: DipoleChargeModifier() : inited (false) @@ -30,9 +33,9 @@ init (const std::string & model, get_env_nthreads(num_intra_nthreads, num_inter_nthreads); options.config.set_inter_op_parallelism_threads(num_inter_nthreads); options.config.set_intra_op_parallelism_threads(num_intra_nthreads); - checkStatus(NewSession(options, &session)); - checkStatus(ReadBinaryProto(Env::Default(), model, &graph_def)); - checkStatus(session->Create(graph_def)); + deepmd::check_status(NewSession(options, &session)); + deepmd::check_status(ReadBinaryProto(Env::Default(), model, &graph_def)); + deepmd::check_status(session->Create(graph_def)); // int nnodes = graph_def.node_size(); // for (int ii = 0; ii < nnodes; ++ii){ // cout << ii << " \t " << graph_def.node(ii).name() << endl; @@ -80,7 +83,7 @@ run_model (std::vector & dforce, } std::vector output_tensors; - checkStatus (session->Run(input_tensors, + deepmd::check_status (session->Run(input_tensors, {"o_dm_force", "o_dm_virial", "o_dm_av"}, {}, &output_tensors)); diff --git a/source/api_cc/src/DeepPot.cc b/source/api_cc/src/DeepPot.cc index a5b9e6db8e..9a3e8d7a10 100644 --- a/source/api_cc/src/DeepPot.cc +++ b/source/api_cc/src/DeepPot.cc @@ -2,6 +2,8 @@ #include "AtomMap.h" #include +using namespace tensorflow; +using namespace deepmd; #if GOOGLE_CUDA #include "cuda_runtime.h" @@ -54,7 +56,7 @@ run_model (ENERGYTYPE & dener, } std::vector output_tensors; - checkStatus (session->Run(input_tensors, + check_status (session->Run(input_tensors, {"o_energy", "o_force", "o_atom_virial"}, {}, &output_tensors)); @@ -95,7 +97,7 @@ static void run_model (ENERGYTYPE & dener, std::vector& datom_virial_, Session* session, const std::vector> & input_tensors, - const AtomMap & atommap, + const deepmd::AtomMap & atommap, const int& nghost = 0) { unsigned nloc = atommap.get_type().size(); @@ -119,7 +121,7 @@ static void run_model (ENERGYTYPE & dener, } std::vector output_tensors; - checkStatus (session->Run(input_tensors, + check_status (session->Run(input_tensors, {"o_energy", "o_force", "o_atom_energy", "o_atom_virial"}, {}, &output_tensors)); @@ -199,7 +201,7 @@ init (const std::string & model, const int & gpu_rank, const std::string & file_ options.config.set_intra_op_parallelism_threads(num_intra_nthreads); if(file_content.size() == 0) - checkStatus (ReadBinaryProto(Env::Default(), model, &graph_def)); + check_status (ReadBinaryProto(Env::Default(), model, &graph_def)); else graph_def.ParseFromString(file_content); int gpu_num = -1; @@ -215,8 +217,8 @@ init (const std::string & model, const int & gpu_rank, const std::string & file_ graph::SetDefaultDevice(str, &graph_def); } #endif // GOOGLE_CUDA - checkStatus (NewSession(options, &session)); - checkStatus (session->Create(graph_def)); + check_status (NewSession(options, &session)); + check_status (session->Create(graph_def)); rcut = get_scalar("descrpt_attr/rcut"); cell_size = rcut; ntypes = get_scalar("descrpt_attr/ntypes"); @@ -334,7 +336,7 @@ compute (ENERGYTYPE & dener, { int nall = dcoord_.size() / 3; int nloc = nall; - atommap = AtomMap (datype_.begin(), datype_.begin() + nloc); + atommap = deepmd::AtomMap (datype_.begin(), datype_.begin() + nloc); assert (nloc == atommap.get_type().size()); validate_fparam_aparam(nloc, fparam, aparam); @@ -406,7 +408,7 @@ compute_inner (ENERGYTYPE & dener, // agp == 0 means that the LAMMPS nbor list has been updated if (ago == 0) { - atommap = AtomMap (datype_.begin(), datype_.begin() + nloc); + atommap = deepmd::AtomMap (datype_.begin(), datype_.begin() + nloc); assert (nloc == atommap.get_type().size()); nlist_data.shuffle(atommap); nlist_data.make_inlist(nlist); @@ -430,7 +432,7 @@ compute (ENERGYTYPE & dener, const std::vector & fparam, const std::vector & aparam) { - atommap = AtomMap (datype_.begin(), datype_.end()); + atommap = deepmd::AtomMap (datype_.begin(), datype_.end()); validate_fparam_aparam(atommap.get_type().size(), fparam, aparam); std::vector> input_tensors; @@ -527,7 +529,7 @@ init (const std::vector & models, const int & gpu_rank, const std:: options.config.set_intra_op_parallelism_threads(num_intra_nthreads); for (unsigned ii = 0; ii < numb_models; ++ii){ if (file_contents.size() == 0) - checkStatus (ReadBinaryProto(Env::Default(), models[ii], &graph_defs[ii])); + check_status (ReadBinaryProto(Env::Default(), models[ii], &graph_defs[ii])); else graph_defs[ii].ParseFromString(file_contents[ii]); } @@ -546,8 +548,8 @@ init (const std::vector & models, const int & gpu_rank, const std:: str += std::to_string(gpu_rank % gpu_num); graph::SetDefaultDevice(str, &graph_defs[ii]); } - checkStatus (NewSession(options, &(sessions[ii]))); - checkStatus (sessions[ii]->Create(graph_defs[ii])); + check_status (NewSession(options, &(sessions[ii]))); + check_status (sessions[ii]->Create(graph_defs[ii])); } rcut = get_scalar("descrpt_attr/rcut"); cell_size = rcut; diff --git a/source/api_cc/src/DeepTensor.cc b/source/api_cc/src/DeepTensor.cc index e701c044f9..a907ae0e19 100644 --- a/source/api_cc/src/DeepTensor.cc +++ b/source/api_cc/src/DeepTensor.cc @@ -1,5 +1,8 @@ #include "DeepTensor.h" +using namespace deepmd; +using namespace tensorflow; + DeepTensor:: DeepTensor() : inited (false) @@ -30,9 +33,9 @@ init (const std::string & model, get_env_nthreads(num_intra_nthreads, num_inter_nthreads); options.config.set_inter_op_parallelism_threads(num_inter_nthreads); options.config.set_intra_op_parallelism_threads(num_intra_nthreads); - checkStatus (NewSession(options, &session)); - checkStatus (ReadBinaryProto(Env::Default(), model, &graph_def)); - checkStatus (session->Create(graph_def)); + deepmd::check_status (NewSession(options, &session)); + deepmd::check_status (ReadBinaryProto(Env::Default(), model, &graph_def)); + deepmd::check_status (session->Create(graph_def)); rcut = get_scalar("descrpt_attr/rcut"); cell_size = rcut; ntypes = get_scalar("descrpt_attr/ntypes"); @@ -82,7 +85,7 @@ run_model (std::vector & d_tensor_, } std::vector output_tensors; - checkStatus (session->Run(input_tensors, + deepmd::check_status (session->Run(input_tensors, {name_prefix(name_scope) + "o_" + model_type}, {}, &output_tensors)); diff --git a/source/api_cc/src/common.cc b/source/api_cc/src/common.cc index 560790c1a1..74c317529e 100644 --- a/source/api_cc/src/common.cc +++ b/source/api_cc/src/common.cc @@ -2,6 +2,8 @@ #include "AtomMap.h" #include "device.h" +using namespace tensorflow; + static std::vector split(const std::string &input_, const std::string &delimiter) @@ -18,6 +20,7 @@ split(const std::string &input_, } bool +deepmd:: model_compatable( std::string & model_version) { @@ -43,10 +46,11 @@ model_compatable( } void +deepmd:: select_by_type(std::vector & fwd_map, std::vector & bkw_map, int & nghost_real, - const std::vector & dcoord_, + const std::vector & dcoord_, const std::vector & datype_, const int & nghost, const std::vector & sel_type_) @@ -84,10 +88,11 @@ select_by_type(std::vector & fwd_map, void +deepmd:: select_real_atoms(std::vector & fwd_map, std::vector & bkw_map, int & nghost_real, - const std::vector & dcoord_, + const std::vector & dcoord_, const std::vector & datype_, const int & nghost, const int & ntypes) @@ -96,12 +101,12 @@ select_real_atoms(std::vector & fwd_map, for (int ii = 0; ii < ntypes; ++ii){ sel_type.push_back(ii); } - select_by_type(fwd_map, bkw_map, nghost_real, dcoord_, datype_, nghost, sel_type); + deepmd::select_by_type(fwd_map, bkw_map, nghost_real, dcoord_, datype_, nghost, sel_type); } void -NeighborListData:: +deepmd::NeighborListData:: copy_from_nlist(const InputNlist & inlist) { int inum = inlist.inum; @@ -117,15 +122,15 @@ copy_from_nlist(const InputNlist & inlist) void -NeighborListData:: -shuffle(const AtomMap & map) +deepmd::NeighborListData:: +shuffle(const AtomMap & map) { const std::vector & fwd_map = map.get_fwd_map(); shuffle(fwd_map); } void -NeighborListData:: +deepmd::NeighborListData:: shuffle(const std::vector & fwd_map) { int nloc = fwd_map.size(); @@ -144,7 +149,7 @@ shuffle(const std::vector & fwd_map) } void -NeighborListData:: +deepmd::NeighborListData:: shuffle_exclude_empty (const std::vector & fwd_map) { shuffle(fwd_map); @@ -175,7 +180,7 @@ shuffle_exclude_empty (const std::vector & fwd_map) } void -NeighborListData:: +deepmd::NeighborListData:: make_inlist(InputNlist & inlist) { int nloc = ilist.size(); @@ -192,7 +197,8 @@ make_inlist(InputNlist & inlist) } void -checkStatus(const tensorflow::Status& status) { +deepmd:: +check_status(const tensorflow::Status& status) { if (!status.ok()) { std::cout << status.ToString() << std::endl; exit(1); @@ -200,6 +206,7 @@ checkStatus(const tensorflow::Status& status) { } void +deepmd:: get_env_nthreads(int & num_intra_nthreads, int & num_inter_nthreads) { @@ -222,6 +229,7 @@ get_env_nthreads(int & num_intra_nthreads, } std::string +deepmd:: name_prefix(const std::string & scope) { std::string prefix = ""; @@ -232,16 +240,18 @@ name_prefix(const std::string & scope) } int -session_input_tensors (std::vector> & input_tensors, - const std::vector & dcoord_, - const int & ntypes, - const std::vector & datype_, - const std::vector & dbox, - const VALUETYPE & cell_size, - const std::vector & fparam_, - const std::vector & aparam_, - const AtomMap& atommap, - const std::string scope) +deepmd:: +session_input_tensors ( + std::vector> & input_tensors, + const std::vector & dcoord_, + const int & ntypes, + const std::vector & datype_, + const std::vector & dbox, + const deepmd::VALUETYPE & cell_size, + const std::vector & fparam_, + const std::vector & aparam_, + const deepmd::AtomMap& atommap, + const std::string scope) { bool b_pbc = (dbox.size() == 9); @@ -297,15 +307,15 @@ session_input_tensors (std::vector> & input_tenso Tensor mesh_tensor (DT_INT32, mesh_shape); Tensor natoms_tensor (DT_INT32, natoms_shape); - auto coord = coord_tensor.matrix (); + auto coord = coord_tensor.matrix (); auto type = type_tensor.matrix (); - auto box = box_tensor.matrix (); + auto box = box_tensor.matrix (); auto mesh = mesh_tensor.flat (); auto natoms = natoms_tensor.flat (); - auto fparam = fparam_tensor.matrix (); - auto aparam = aparam_tensor.matrix (); + auto fparam = fparam_tensor.matrix (); + auto aparam = aparam_tensor.matrix (); - std::vector dcoord (dcoord_); + std::vector dcoord (dcoord_); atommap.forward (dcoord.begin(), dcoord_.begin(), 3); for (int ii = 0; ii < nframes; ++ii){ @@ -365,18 +375,20 @@ session_input_tensors (std::vector> & input_tenso } int -session_input_tensors (std::vector> & input_tensors, - const std::vector & dcoord_, - const int & ntypes, - const std::vector & datype_, - const std::vector & dbox, - InputNlist & dlist, - const std::vector & fparam_, - const std::vector & aparam_, - const AtomMap& atommap, - const int nghost, - const int ago, - const std::string scope) +deepmd:: +session_input_tensors ( + std::vector> & input_tensors, + const std::vector & dcoord_, + const int & ntypes, + const std::vector & datype_, + const std::vector & dbox, + InputNlist & dlist, + const std::vector & fparam_, + const std::vector & aparam_, + const deepmd::AtomMap& atommap, + const int nghost, + const int ago, + const std::string scope) { assert (dbox.size() == 9); @@ -427,15 +439,15 @@ session_input_tensors (std::vector> & input_tenso Tensor mesh_tensor (DT_INT32, mesh_shape); Tensor natoms_tensor (DT_INT32, natoms_shape); - auto coord = coord_tensor.matrix (); + auto coord = coord_tensor.matrix (); auto type = type_tensor.matrix (); - auto box = box_tensor.matrix (); + auto box = box_tensor.matrix (); auto mesh = mesh_tensor.flat (); auto natoms = natoms_tensor.flat (); - auto fparam = fparam_tensor.matrix (); - auto aparam = aparam_tensor.matrix (); + auto fparam = fparam_tensor.matrix (); + auto aparam = aparam_tensor.matrix (); - std::vector dcoord (dcoord_); + std::vector dcoord (dcoord_); atommap.forward (dcoord.begin(), dcoord_.begin(), 3); for (int ii = 0; ii < nframes; ++ii){ @@ -493,3 +505,151 @@ session_input_tensors (std::vector> & input_tenso return nloc; } +template +VT +deepmd:: +session_get_scalar(Session* session, const std::string name_, const std::string scope) +{ + std::string name = name_; + if (scope != "") { + name = scope + "/" + name; + } + std::vector output_tensors; + deepmd::check_status (session->Run(std::vector> ({}), + {name.c_str()}, + {}, + &output_tensors)); + Tensor output_rc = output_tensors[0]; + auto orc = output_rc.flat (); + return orc(0); +} + +template +void +deepmd:: +session_get_vector(std::vector & o_vec, Session* session, const std::string name_, const std::string scope) +{ + std::string name = name_; + if (scope != "") { + name = scope + "/" + name; + } + std::vector output_tensors; + deepmd::check_status (session->Run(std::vector> ({}), + {name.c_str()}, + {}, + &output_tensors)); + Tensor output_rc = output_tensors[0]; + assert(1 == output_rc.shape().dims()); + int dof = output_rc.shape().dim_size(0); + o_vec.resize(dof); + auto orc = output_rc.flat (); + for (int ii = 0; ii < dof; ++ii){ + o_vec[ii] = orc(ii); + } +} + + +template +void +deepmd:: +select_map(std::vector & out, + const std::vector & in, + const std::vector & idx_map, + const int & stride) +{ +#ifdef DEBUG + assert(in.size() / stride * stride == in.size()), "in size should be multiples of stride" +#endif + for (int ii = 0; ii < in.size() / stride; ++ii){ +#ifdef DEBUG + assert(ii < idx_map.size()), "idx goes over the idx map size"; + assert(idx_map[ii] < out.size()), "mappped idx goes over the out size"; +#endif + if (idx_map[ii] >= 0) { + int to_ii = idx_map[ii]; + for (int dd = 0; dd < stride; ++dd){ + out[to_ii * stride + dd] = in[ii * stride + dd]; + } + } + } +} + + +template +int +deepmd:: +session_get_scalar(Session*, const std::string, const std::string); + +template +void +deepmd:: +session_get_vector(std::vector &, Session*, const std::string, const std::string); + +template +void +deepmd:: +select_map( + std::vector & out, + const std::vector & in, + const std::vector & idx_map, + const int & stride); + + +template +float +deepmd:: +session_get_scalar(Session*, const std::string, const std::string); + +template +void +deepmd:: +session_get_vector(std::vector &, Session*, const std::string, const std::string); + +template +void +deepmd:: +select_map( + std::vector & out, + const std::vector & in, + const std::vector & idx_map, + const int & stride); + + +template +double +deepmd:: +session_get_scalar(Session*, const std::string, const std::string); + +template +void +deepmd:: +session_get_vector(std::vector &, Session*, const std::string, const std::string); + +template +void +deepmd:: +select_map( + std::vector & out, + const std::vector & in, + const std::vector & idx_map, + const int & stride); + + +template +deepmd::STRINGTYPE +deepmd:: +session_get_scalar(Session*, const std::string, const std::string); + +template +void +deepmd:: +session_get_vector(std::vector &, Session*, const std::string, const std::string); + +template +void +deepmd:: +select_map( + std::vector & out, + const std::vector & in, + const std::vector & idx_map, + const int & stride); diff --git a/source/api_cc/tests/test_deepdipole.cc b/source/api_cc/tests/test_deepdipole.cc index 04d068ddfb..bfdcc1de17 100644 --- a/source/api_cc/tests/test_deepdipole.cc +++ b/source/api_cc/tests/test_deepdipole.cc @@ -35,20 +35,20 @@ class TestInferDeepDipole : public ::testing::Test }; int natoms; - DeepTensor dp; + deepmd::DeepTensor dp; void SetUp() override { std::string file_name = "../../tests/infer/deepdipole.pbtxt"; int fd = open(file_name.c_str(), O_RDONLY); - protobuf::io::ZeroCopyInputStream* input = new protobuf::io::FileInputStream(fd); - GraphDef graph_def; - protobuf::TextFormat::Parse(input, &graph_def); + tensorflow::protobuf::io::ZeroCopyInputStream* input = new tensorflow::protobuf::io::FileInputStream(fd); + tensorflow::GraphDef graph_def; + tensorflow::protobuf::TextFormat::Parse(input, &graph_def); delete input; std::fstream output("deepdipole.pb", std::ios::out | std::ios::trunc | std::ios::binary); graph_def.SerializeToOstream(&output); // check the string by the following commands // string txt; - // protobuf::TextFormat::PrintToString(graph_def, &txt); + // tensorflow::protobuf::TextFormat::PrintToString(graph_def, &txt); dp.init("deepdipole.pb"); diff --git a/source/api_cc/tests/test_deeppolar.cc b/source/api_cc/tests/test_deeppolar.cc index acae6e13e0..19e9b8af7c 100644 --- a/source/api_cc/tests/test_deeppolar.cc +++ b/source/api_cc/tests/test_deeppolar.cc @@ -35,20 +35,20 @@ class TestInferDeepPolar : public ::testing::Test }; int natoms; - DeepTensor dp; + deepmd::DeepTensor dp; void SetUp() override { std::string file_name = "../../tests/infer/deeppolar.pbtxt"; int fd = open(file_name.c_str(), O_RDONLY); - protobuf::io::ZeroCopyInputStream* input = new protobuf::io::FileInputStream(fd); - GraphDef graph_def; - protobuf::TextFormat::Parse(input, &graph_def); + tensorflow::protobuf::io::ZeroCopyInputStream* input = new tensorflow::protobuf::io::FileInputStream(fd); + tensorflow::GraphDef graph_def; + tensorflow::protobuf::TextFormat::Parse(input, &graph_def); delete input; std::fstream output("deeppolar.pb", std::ios::out | std::ios::trunc | std::ios::binary); graph_def.SerializeToOstream(&output); // check the string by the following commands // string txt; - // protobuf::TextFormat::PrintToString(graph_def, &txt); + // tensorflow::protobuf::TextFormat::PrintToString(graph_def, &txt); dp.init("deeppolar.pb"); diff --git a/source/api_cc/tests/test_deeppot_a.cc b/source/api_cc/tests/test_deeppot_a.cc index 01f5d6f421..77b343f91e 100644 --- a/source/api_cc/tests/test_deeppot_a.cc +++ b/source/api_cc/tests/test_deeppot_a.cc @@ -43,20 +43,20 @@ class TestInferDeepPotA : public ::testing::Test double expected_tot_e; std::vectorexpected_tot_v; - DeepPot dp; + deepmd::DeepPot dp; void SetUp() override { std::string file_name = "../../tests/infer/deeppot.pbtxt"; int fd = open(file_name.c_str(), O_RDONLY); - protobuf::io::ZeroCopyInputStream* input = new protobuf::io::FileInputStream(fd); - GraphDef graph_def; - protobuf::TextFormat::Parse(input, &graph_def); + tensorflow::protobuf::io::ZeroCopyInputStream* input = new tensorflow::protobuf::io::FileInputStream(fd); + tensorflow::GraphDef graph_def; + tensorflow::protobuf::TextFormat::Parse(input, &graph_def); delete input; std::fstream output("deeppot.pb", std::ios::out | std::ios::trunc | std::ios::binary); graph_def.SerializeToOstream(&output); // check the string by the following commands // string txt; - // protobuf::TextFormat::PrintToString(graph_def, &txt); + // tensorflow::protobuf::TextFormat::PrintToString(graph_def, &txt); dp.init("deeppot.pb"); @@ -103,11 +103,11 @@ TEST_F(TestInferDeepPotA, cpu_build_nlist_numfv) { class MyModel : public EnergyModelTest { - DeepPot & mydp; + deepmd::DeepPot & mydp; const std::vector atype; public: MyModel( - DeepPot & dp_, + deepmd::DeepPot & dp_, const std::vector & atype_ ) : mydp(dp_), atype(atype_) {}; virtual void compute ( @@ -426,14 +426,14 @@ class TestInferDeepPotANoPbc : public ::testing::Test double expected_tot_e; std::vectorexpected_tot_v; - DeepPot dp; + deepmd::DeepPot dp; void SetUp() override { std::string file_name = "../../tests/infer/deeppot.pbtxt"; int fd = open(file_name.c_str(), O_RDONLY); - protobuf::io::ZeroCopyInputStream* input = new protobuf::io::FileInputStream(fd); - GraphDef graph_def; - protobuf::TextFormat::Parse(input, &graph_def); + tensorflow::protobuf::io::ZeroCopyInputStream* input = new tensorflow::protobuf::io::FileInputStream(fd); + tensorflow::GraphDef graph_def; + tensorflow::protobuf::TextFormat::Parse(input, &graph_def); delete input; std::fstream output("deeppot.pb", std::ios::out | std::ios::trunc | std::ios::binary); graph_def.SerializeToOstream(&output); diff --git a/source/api_cc/tests/test_deeppot_model_devi.cc b/source/api_cc/tests/test_deeppot_model_devi.cc index fc5c116924..f70b66a9eb 100644 --- a/source/api_cc/tests/test_deeppot_model_devi.cc +++ b/source/api_cc/tests/test_deeppot_model_devi.cc @@ -32,17 +32,17 @@ class TestInferDeepPotModeDevi : public ::testing::Test }; int natoms; - DeepPot dp0; - DeepPot dp1; - DeepPotModelDevi dp_md; + deepmd::DeepPot dp0; + deepmd::DeepPot dp1; + deepmd::DeepPotModelDevi dp_md; void SetUp() override { { std::string file_name = "../../tests/infer/deeppot.pbtxt"; int fd = open(file_name.c_str(), O_RDONLY); - protobuf::io::ZeroCopyInputStream* input = new protobuf::io::FileInputStream(fd); - GraphDef graph_def; - protobuf::TextFormat::Parse(input, &graph_def); + tensorflow::protobuf::io::ZeroCopyInputStream* input = new tensorflow::protobuf::io::FileInputStream(fd); + tensorflow::GraphDef graph_def; + tensorflow::protobuf::TextFormat::Parse(input, &graph_def); delete input; std::fstream output("deeppot.pb", std::ios::out | std::ios::trunc | std::ios::binary); graph_def.SerializeToOstream(&output); @@ -51,9 +51,9 @@ class TestInferDeepPotModeDevi : public ::testing::Test { std::string file_name = "../../tests/infer/deeppot-1.pbtxt"; int fd = open(file_name.c_str(), O_RDONLY); - protobuf::io::ZeroCopyInputStream* input = new protobuf::io::FileInputStream(fd); - GraphDef graph_def; - protobuf::TextFormat::Parse(input, &graph_def); + tensorflow::protobuf::io::ZeroCopyInputStream* input = new tensorflow::protobuf::io::FileInputStream(fd); + tensorflow::GraphDef graph_def; + tensorflow::protobuf::TextFormat::Parse(input, &graph_def); delete input; std::fstream output("deeppot-1.pb", std::ios::out | std::ios::trunc | std::ios::binary); graph_def.SerializeToOstream(&output); diff --git a/source/api_cc/tests/test_deeppot_r.cc b/source/api_cc/tests/test_deeppot_r.cc index 7002a07f5a..e9b1c45c85 100644 --- a/source/api_cc/tests/test_deeppot_r.cc +++ b/source/api_cc/tests/test_deeppot_r.cc @@ -43,20 +43,20 @@ class TestInferDeepPotR : public ::testing::Test double expected_tot_e; std::vectorexpected_tot_v; - DeepPot dp; + deepmd::DeepPot dp; void SetUp() override { std::string file_name = "../../tests/infer/deeppot-r.pbtxt"; int fd = open(file_name.c_str(), O_RDONLY); - protobuf::io::ZeroCopyInputStream* input = new protobuf::io::FileInputStream(fd); - GraphDef graph_def; - protobuf::TextFormat::Parse(input, &graph_def); + tensorflow::protobuf::io::ZeroCopyInputStream* input = new tensorflow::protobuf::io::FileInputStream(fd); + tensorflow::GraphDef graph_def; + tensorflow::protobuf::TextFormat::Parse(input, &graph_def); delete input; std::fstream output("deeppot.pb", std::ios::out | std::ios::trunc | std::ios::binary); graph_def.SerializeToOstream(&output); // check the string by the following commands // string txt; - // protobuf::TextFormat::PrintToString(graph_def, &txt); + // tensorflow::protobuf::TextFormat::PrintToString(graph_def, &txt); dp.init("deeppot.pb"); @@ -104,11 +104,11 @@ TEST_F(TestInferDeepPotR, cpu_build_nlist_numfv) { class MyModel : public EnergyModelTest { - DeepPot & mydp; + deepmd::DeepPot & mydp; const std::vector & atype; public: MyModel( - DeepPot & dp_, + deepmd::DeepPot & dp_, const std::vector & atype_ ) : mydp(dp_), atype(atype_) {}; virtual void compute ( @@ -426,14 +426,14 @@ class TestInferDeepPotRNoPbc : public ::testing::Test double expected_tot_e; std::vectorexpected_tot_v; - DeepPot dp; + deepmd::DeepPot dp; void SetUp() override { std::string file_name = "../../tests/infer/deeppot-r.pbtxt"; int fd = open(file_name.c_str(), O_RDONLY); - protobuf::io::ZeroCopyInputStream* input = new protobuf::io::FileInputStream(fd); - GraphDef graph_def; - protobuf::TextFormat::Parse(input, &graph_def); + tensorflow::protobuf::io::ZeroCopyInputStream* input = new tensorflow::protobuf::io::FileInputStream(fd); + tensorflow::GraphDef graph_def; + tensorflow::protobuf::TextFormat::Parse(input, &graph_def); delete input; std::fstream output("deeppot.pb", std::ios::out | std::ios::trunc | std::ios::binary); graph_def.SerializeToOstream(&output); diff --git a/source/api_cc/tests/test_dipolecharge.cc b/source/api_cc/tests/test_dipolecharge.cc index 87fd2f4a78..9ce16b9442 100644 --- a/source/api_cc/tests/test_dipolecharge.cc +++ b/source/api_cc/tests/test_dipolecharge.cc @@ -53,22 +53,22 @@ class TestDipoleCharge : public ::testing::Test double expected_tot_e; std::vectorexpected_tot_v; - DeepTensor dp; - DipoleChargeModifier dm; + deepmd::DeepTensor dp; + deepmd::DipoleChargeModifier dm; void SetUp() override { std::string file_name = "../../tests/infer/dipolecharge_e.pbtxt"; int fd = open(file_name.c_str(), O_RDONLY); - protobuf::io::ZeroCopyInputStream* input = new protobuf::io::FileInputStream(fd); - GraphDef graph_def; - protobuf::TextFormat::Parse(input, &graph_def); + tensorflow::protobuf::io::ZeroCopyInputStream* input = new tensorflow::protobuf::io::FileInputStream(fd); + tensorflow::GraphDef graph_def; + tensorflow::protobuf::TextFormat::Parse(input, &graph_def); delete input; - string model = "dipolecharge_e.pb"; + std::string model = "dipolecharge_e.pb"; std::fstream output(model.c_str(), std::ios::out | std::ios::trunc | std::ios::binary); graph_def.SerializeToOstream(&output); // check the string by the following commands // string txt; - // protobuf::TextFormat::PrintToString(graph_def, &txt); + // tensorflow::protobuf::TextFormat::PrintToString(graph_def, &txt); // dp.init("dipolecharge_d.pb"); // dm.init("dipolecharge_d.pb"); @@ -128,12 +128,12 @@ TEST_F(TestDipoleCharge, cpu_lmp_nlist) std::vector sel_types = dp.sel_types(); std::vector sel_fwd, sel_bwd; int sel_nghost; - select_by_type(sel_fwd, sel_bwd, sel_nghost, coord_cpy, atype_cpy, nghost, sel_types); + deepmd::select_by_type(sel_fwd, sel_bwd, sel_nghost, coord_cpy, atype_cpy, nghost, sel_types); int sel_nall = sel_bwd.size(); int sel_nloc = sel_nall - sel_nghost; std::vector sel_atype(sel_bwd.size()); - select_map(sel_atype, atype, sel_fwd, 1); - AtomMap nnp_map(sel_atype.begin(), sel_atype.begin() + sel_nloc); + deepmd::select_map(sel_atype, atype, sel_fwd, 1); + deepmd::AtomMap nnp_map(sel_atype.begin(), sel_atype.begin() + sel_nloc); const std::vector & sort_fwd_map(nnp_map.get_fwd_map()); // // add coords diff --git a/source/ipi/driver.cc b/source/ipi/driver.cc index 605a26a1e0..2732b9b990 100644 --- a/source/ipi/driver.cc +++ b/source/ipi/driver.cc @@ -75,14 +75,14 @@ int main(int argc, char * argv[]) inet = 0; } int port = jdata["port"]; - string host_str = jdata["host"]; + std::string host_str = jdata["host"]; const char * host = host_str.c_str(); - string graph_file = jdata["graph_file"]; - string coord_file = jdata["coord_file"]; - std::map name_type_map = jdata["atom_type"]; + std::string graph_file = jdata["graph_file"]; + std::string coord_file = jdata["coord_file"]; + std::map name_type_map = jdata["atom_type"]; bool b_verb = jdata["verbose"]; - std::vector atom_name; + std::vector atom_name; { std::vector > posi; std::vector > velo; @@ -91,7 +91,7 @@ int main(int argc, char * argv[]) } Convert cvt (atom_name, name_type_map); - DeepPot nnp_inter (graph_file); + deepmd::DeepPot nnp_inter (graph_file); enum { _MSGLEN = 12 }; int MSGLEN = _MSGLEN; @@ -126,7 +126,7 @@ int main(int argc, char * argv[]) while (true) { readbuffer_ (&socket, header, MSGLEN); - string header_str (trimwhitespace(header)); + std::string header_str (trimwhitespace(header)); if (b_verb) std::cout << "# get header " << header_str << std::endl; if (header_str == "STATUS"){ diff --git a/source/lmp/env.sh.in b/source/lmp/env.sh.in index 4baf835aff..a59ad84960 100644 --- a/source/lmp/env.sh.in +++ b/source/lmp/env.sh.in @@ -6,6 +6,6 @@ TF_INCLUDE_DIRS=`echo $TENSORFLOW_INCLUDE_DIRS | sed "s/;/ -I/g"` TF_LIBRARY_PATH=`echo $TENSORFLOW_LIBRARY_PATH | sed "s/;/ -L/g"` TF_RPATH=`echo $TENSORFLOW_LIBRARY_PATH | sed "s/;/ -Wl,-rpath=/g"` -NNP_INC=" -std=c++11 @PREC_DEF@ @TTM_DEF@ @OLD_LMP_PPPM_DEF@ -I$TF_INCLUDE_DIRS -I$DEEPMD_ROOT/include/deepmd " +NNP_INC=" -std=c++11 @PREC_DEF@ @TTM_DEF@ @OLD_LMP_PPPM_DEF@ -I$TF_INCLUDE_DIRS -I$DEEPMD_ROOT/include/ " NNP_PATH=" -L$TF_LIBRARY_PATH -L$DEEPMD_ROOT/lib" NNP_LIB=" -Wl,--no-as-needed -l@LIB_DEEPMD_OP_CUDA@ -l@LIB_DEEPMD_OP@ -l@LIB_DEEPMD_CC@ -l@LIB_DEEPMD@ -ltensorflow_cc -ltensorflow_framework -Wl,-rpath=$TF_RPATH -Wl,-rpath=$DEEPMD_ROOT/lib" diff --git a/source/lmp/fix_dplr.cpp b/source/lmp/fix_dplr.cpp index 5d89ff8dad..2a5826aa84 100644 --- a/source/lmp/fix_dplr.cpp +++ b/source/lmp/fix_dplr.cpp @@ -299,13 +299,13 @@ void FixDPLR::pre_force(int vflag) } vector sel_fwd, sel_bwd; int sel_nghost; - select_by_type(sel_fwd, sel_bwd, sel_nghost, dcoord, dtype, nghost, sel_type); + deepmd::select_by_type(sel_fwd, sel_bwd, sel_nghost, dcoord, dtype, nghost, sel_type); int sel_nall = sel_bwd.size(); int sel_nloc = sel_nall - sel_nghost; vector sel_type(sel_bwd.size()); - select_map(sel_type, dtype, sel_fwd, 1); + deepmd::select_map(sel_type, dtype, sel_fwd, 1); - AtomMap atom_map(sel_type.begin(), sel_type.begin() + sel_nloc); + deepmd::AtomMap atom_map(sel_type.begin(), sel_type.begin() + sel_nloc); const vector & sort_fwd_map(atom_map.get_fwd_map()); vector > valid_pairs; diff --git a/source/lmp/fix_dplr.h b/source/lmp/fix_dplr.h index d81d8bd3db..6de055529b 100644 --- a/source/lmp/fix_dplr.h +++ b/source/lmp/fix_dplr.h @@ -10,8 +10,8 @@ FixStyle(dplr,FixDPLR) #include #include "fix.h" #include "pair_deepmd.h" -#include "DeepTensor.h" -#include "DataModifier.h" +#include "deepmd/DeepTensor.h" +#include "deepmd/DataModifier.h" #ifdef HIGH_PREC #define FLOAT_PREC double @@ -36,8 +36,8 @@ namespace LAMMPS_NS { double compute_vector(int); private: PairDeepMD * pair_deepmd; - DeepTensor dpt; - DipoleChargeModifier dtm; + deepmd::DeepTensor dpt; + deepmd::DipoleChargeModifier dtm; std::string model; int ntypes; std::vector sel_type; diff --git a/source/lmp/pair_deepmd.cpp b/source/lmp/pair_deepmd.cpp index 859c7be34e..593e6a4246 100644 --- a/source/lmp/pair_deepmd.cpp +++ b/source/lmp/pair_deepmd.cpp @@ -111,7 +111,7 @@ std::string PairDeepMD::get_file_content(const std::string & model) { int nchar = 0; std::string file_content; if (myrank == root) { - checkStatus (ReadFileToString(Env::Default(), model, &file_content)); + deepmd::check_status(tensorflow::ReadFileToString(tensorflow::Env::Default(), model, &file_content)); nchar = file_content.size(); } MPI_Bcast(&nchar, 1, MPI_INT, root, MPI_COMM_WORLD); diff --git a/source/lmp/pair_deepmd.h.in b/source/lmp/pair_deepmd.h.in index 6a5c24444a..a54f45c2f3 100644 --- a/source/lmp/pair_deepmd.h.in +++ b/source/lmp/pair_deepmd.h.in @@ -21,7 +21,7 @@ PairStyle(deepmd,PairDeepMD) #define LMP_PAIR_NNP_H #include "pair.h" -#include "DeepPot.h" +#include "deepmd/DeepPot.h" #include #include @@ -71,8 +71,8 @@ class PairDeepMD : public Pair { double **scale; private: - DeepPot deep_pot; - DeepPotModelDevi deep_pot_model_devi; + deepmd::DeepPot deep_pot; + deepmd::DeepPotModelDevi deep_pot_model_devi; unsigned numb_models; double cutoff; int numb_types;