From f5fb9596401e9022965d8bff978ea45b0be77416 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Sun, 26 Jun 2022 02:07:57 -0400 Subject: [PATCH] remove dependency of TF headers from C++ public headers TensorFlow headers were required to build packages using C++ APIs, such as LAMMPS, GROMACS, etc. However, TensorFlow headers are quite heavy and have lots of dependencies like Protobuf. In this commit, we remove TF headers from C++ public APIs. Now we split the header to `tf_private.h` and `tf_public.h`. When building DP, `tf_private.h` is used as the same behavior before. TF classes are incomplete defined in `tf_public.h` for public includes, as TF classes are indeed not public members in APIs. Details of this commit: (1) split C++ API TF headers to `tf_private.h` and `tf_public.h`. It's controlled by compilation options `TF_PRIVATE`. Now `TensorFlow_INCLUDE_DIRS` is private include_directories. (2) now `graph_def` in DP classes is a pointer (GraphDef*), as class member cannot be imcomplete (the size must be determined). (3) add a function `deepmd::convert_pbtxt_to_pb` and remove the direct dependency of TF from unit tests; (4) add a function `deepmd::read_file_to_string` and remove the direct dependency of TF from LAMMPS package; (5) there is no `#include ` for `std::map` in `fix_dplr.h`. I guess it is included by TF before. (6) Update docs; remove TF include files from all scripts. C++ Unit test is passed on my local machine. --- doc/inference/cxx.md | 2 +- source/api_cc/CMakeLists.txt | 8 +++- source/api_cc/include/DataModifier.h | 2 +- source/api_cc/include/DeepPot.h | 5 +-- source/api_cc/include/DeepTensor.h | 2 +- source/api_cc/include/common.h | 33 ++++++++++----- source/api_cc/include/tf_private.h | 19 +++++++++ source/api_cc/include/tf_public.h | 15 +++++++ source/api_cc/src/DataModifier.cc | 10 +++-- source/api_cc/src/DeepPot.cc | 41 +++++++------------ source/api_cc/src/DeepTensor.cc | 10 +++-- source/api_cc/src/common.cc | 25 +++++++++++ source/api_cc/tests/CMakeLists.txt | 12 +++--- source/api_cc/tests/test_deepdipole.cc | 37 ++--------------- source/api_cc/tests/test_deeppolar.cc | 24 +---------- source/api_cc/tests/test_deeppot_a.cc | 21 +--------- .../api_cc/tests/test_deeppot_model_devi.cc | 34 ++------------- source/api_cc/tests/test_deeppot_r.cc | 21 +--------- source/api_cc/tests/test_dipolecharge.cc | 16 +------- source/gmx/CMakeLists.txt | 1 - source/gmx/src/gmx_plugin.cpp | 1 + source/install/test_cc.sh | 2 +- source/lmp/env.sh.in | 2 +- source/lmp/fix_dplr.h | 1 + source/lmp/pair_deepmd.cpp | 2 +- 25 files changed, 143 insertions(+), 203 deletions(-) create mode 100644 source/api_cc/include/tf_private.h create mode 100644 source/api_cc/include/tf_public.h diff --git a/doc/inference/cxx.md b/doc/inference/cxx.md index 3871a3d92d..746164df23 100644 --- a/doc/inference/cxx.md +++ b/doc/inference/cxx.md @@ -17,7 +17,7 @@ where `e`, `f` and `v` are predicted energy, force and virial of the system, res You can compile `infer_water.cpp` using `gcc`: ```sh -gcc infer_water.cpp -D HIGH_PREC -L $deepmd_root/lib -L $tensorflow_root/lib -I $deepmd_root/include -I $tensorflow_root/include -Wl,--no-as-needed -ldeepmd_cc -lstdc++ -ltensorflow_cc -Wl,-rpath=$deepmd_root/lib -Wl,-rpath=$tensorflow_root/lib -o infer_water +gcc infer_water.cpp -D HIGH_PREC -L $deepmd_root/lib -L $tensorflow_root/lib -I $deepmd_root/include -Wl,--no-as-needed -ldeepmd_cc -lstdc++ -ltensorflow_cc -Wl,-rpath=$deepmd_root/lib -Wl,-rpath=$tensorflow_root/lib -o infer_water ``` and then run the program: ```sh diff --git a/source/api_cc/CMakeLists.txt b/source/api_cc/CMakeLists.txt index 830ecd6b78..e244c294ec 100644 --- a/source/api_cc/CMakeLists.txt +++ b/source/api_cc/CMakeLists.txt @@ -17,14 +17,18 @@ add_library(${libname} SHARED ${LIB_SRC}) # link: libdeepmd libdeepmd_op libtensorflow_cc libtensorflow_framework target_link_libraries (${libname} PUBLIC ${LIB_DEEPMD} ${TensorFlow_LIBRARY} ${TensorFlowFramework_LIBRARY}) -target_include_directories(${libname} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_BINARY_DIR} ${TensorFlow_INCLUDE_DIRS}) +target_include_directories(${libname} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_BINARY_DIR}) +target_include_directories(${libname} PRIVATE ${TensorFlow_INCLUDE_DIRS}) set_target_properties( ${libname} PROPERTIES - COMPILE_DEFINITIONS ${prec_def} INSTALL_RPATH "$ORIGIN;${TensorFlow_LIBRARY_PATH}" ) +target_compile_definitions(${libname} + PUBLIC ${prec_def} + PRIVATE TF_PRIVATE +) install(TARGETS ${libname} DESTINATION lib/) diff --git a/source/api_cc/include/DataModifier.h b/source/api_cc/include/DataModifier.h index 114abfd889..98634281cd 100644 --- a/source/api_cc/include/DataModifier.h +++ b/source/api_cc/include/DataModifier.h @@ -32,7 +32,7 @@ class DipoleChargeModifier tensorflow::Session* session; std::string name_scope, name_prefix; int num_intra_nthreads, num_inter_nthreads; - tensorflow::GraphDef graph_def; + tensorflow::GraphDef* graph_def; bool inited; VALUETYPE rcut; VALUETYPE cell_size; diff --git a/source/api_cc/include/DeepPot.h b/source/api_cc/include/DeepPot.h index 2b76cc4e0b..48d2a3cd7f 100644 --- a/source/api_cc/include/DeepPot.h +++ b/source/api_cc/include/DeepPot.h @@ -179,7 +179,7 @@ class DeepPot private: tensorflow::Session* session; int num_intra_nthreads, num_inter_nthreads; - tensorflow::GraphDef graph_def; + tensorflow::GraphDef* graph_def; bool inited; template VT get_scalar(const std::string & name) const; // VALUETYPE get_rcut () const; @@ -401,7 +401,7 @@ class DeepPotModelDevi unsigned numb_models; 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; @@ -426,7 +426,6 @@ class DeepPotModelDevi // function used for nborlist copy std::vector > get_sel() const; - 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 2f696d5289..0bdec4ce56 100644 --- a/source/api_cc/include/DeepTensor.h +++ b/source/api_cc/include/DeepTensor.h @@ -160,7 +160,7 @@ class DeepTensor tensorflow::Session* session; std::string name_scope; int num_intra_nthreads, num_inter_nthreads; - tensorflow::GraphDef graph_def; + tensorflow::GraphDef* graph_def; bool inited; VALUETYPE rcut; VALUETYPE cell_size; diff --git a/source/api_cc/include/common.h b/source/api_cc/include/common.h index 9d8faa83c4..d82ce50e84 100644 --- a/source/api_cc/include/common.h +++ b/source/api_cc/include/common.h @@ -8,21 +8,15 @@ #include "AtomMap.h" #include "errors.h" -#include "tensorflow/core/platform/env.h" -#include "tensorflow/core/public/session.h" -#include "tensorflow/core/public/version.h" -#include -#include +#ifdef TF_PRIVATE +#include "tf_private.h" +#else +#include "tf_public.h" +#endif namespace deepmd{ -#if TF_MAJOR_VERSION >= 2 && TF_MINOR_VERSION >= 2 -typedef tensorflow::tstring STRINGTYPE; -#else -typedef std::string STRINGTYPE; -#endif - #ifdef HIGH_PREC typedef double VALUETYPE; typedef double ENERGYTYPE; @@ -188,5 +182,22 @@ session_input_tensors (std::vector> & const int nghost, const int ago, const std::string scope = ""); + +/** +* @brief Read model file to a string. +* @param[in] model Path to the model. +* @param[out] file_content Content of the model file. +**/ +void +read_file_to_string(std::string model, std::string & file_content); + + +/** +* @brief Convert pbtxt to pb. +* @param[in] fn_pb_txt Filename of the pb txt file. +* @param[out] fn_pb Filename of the pb file. +**/ +void +convert_pbtxt_to_pb(std::string fn_pb_txt, std::string fn_pb); } diff --git a/source/api_cc/include/tf_private.h b/source/api_cc/include/tf_private.h new file mode 100644 index 0000000000..12b7077139 --- /dev/null +++ b/source/api_cc/include/tf_private.h @@ -0,0 +1,19 @@ +/** + * @file tf_private.h + * @brief This file includes TensorFlow headers used for compilation. + * + */ + +#include "tensorflow/core/platform/env.h" +#include "tensorflow/core/public/session.h" +#include "tensorflow/core/public/version.h" +#include +#include + +namespace deepmd { +#if TF_MAJOR_VERSION >= 2 && TF_MINOR_VERSION >= 2 +typedef tensorflow::tstring STRINGTYPE; +#else +typedef std::string STRINGTYPE; +#endif +} diff --git a/source/api_cc/include/tf_public.h b/source/api_cc/include/tf_public.h new file mode 100644 index 0000000000..e766168c98 --- /dev/null +++ b/source/api_cc/include/tf_public.h @@ -0,0 +1,15 @@ +/** + * @file tf_public.h + * @brief This file declares incompleted TensorFlow class used for public headers. + * + */ + +// skip if TF headers have been included +#ifndef TF_MAJOR_VERSION +namespace tensorflow{ + class Session; + class Tensor; + class GraphDef; + class Status; +} +#endif diff --git a/source/api_cc/src/DataModifier.cc b/source/api_cc/src/DataModifier.cc index 2fbd58584b..fef74596b5 100644 --- a/source/api_cc/src/DataModifier.cc +++ b/source/api_cc/src/DataModifier.cc @@ -5,7 +5,8 @@ using namespace tensorflow; DipoleChargeModifier:: DipoleChargeModifier() - : inited (false) + : inited (false), + graph_def(new GraphDef()) { } @@ -13,7 +14,8 @@ DipoleChargeModifier:: DipoleChargeModifier(const std::string & model, const int & gpu_rank, const std::string &name_scope_) - : inited (false), name_scope(name_scope_) + : inited (false), name_scope(name_scope_), + graph_def(new GraphDef()) { init(model, gpu_rank); } @@ -35,8 +37,8 @@ init (const std::string & model, options.config.set_intra_op_parallelism_threads(num_intra_nthreads); deepmd::load_op_library(); deepmd::check_status(NewSession(options, &session)); - deepmd::check_status(ReadBinaryProto(Env::Default(), model, &graph_def)); - deepmd::check_status(session->Create(graph_def)); + 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; diff --git a/source/api_cc/src/DeepPot.cc b/source/api_cc/src/DeepPot.cc index 1b99e78920..f6b16f1064 100644 --- a/source/api_cc/src/DeepPot.cc +++ b/source/api_cc/src/DeepPot.cc @@ -162,13 +162,15 @@ static void run_model (ENERGYTYPE & dener, DeepPot:: DeepPot () - : inited (false), init_nbor (false) + : inited (false), init_nbor (false), + graph_def(new GraphDef()) { } DeepPot:: DeepPot (const std::string & model, const int & gpu_rank, const std::string & file_content) - : inited (false), init_nbor (false) + : inited (false), init_nbor (false), + graph_def(new GraphDef()) { init(model, gpu_rank, file_content); } @@ -190,9 +192,9 @@ init (const std::string & model, const int & gpu_rank, const std::string & file_ deepmd::load_op_library(); if(file_content.size() == 0) - check_status (ReadBinaryProto(Env::Default(), model, &graph_def)); + check_status (ReadBinaryProto(Env::Default(), model, graph_def)); else - graph_def.ParseFromString(file_content); + (*graph_def).ParseFromString(file_content); int gpu_num = -1; #if GOOGLE_CUDA || TENSORFLOW_USE_ROCM DPGetDeviceCount(gpu_num); // check current device environment @@ -203,11 +205,11 @@ init (const std::string & model, const int & gpu_rank, const std::string & file_ DPErrcheck(DPSetDevice(gpu_rank % gpu_num)); std::string str = "/gpu:"; str += std::to_string(gpu_rank % gpu_num); - graph::SetDefaultDevice(str, &graph_def); + graph::SetDefaultDevice(str, graph_def); } #endif // GOOGLE_CUDA || TENSORFLOW_USE_ROCM check_status (NewSession(options, &session)); - check_status (session->Create(graph_def)); + check_status (session->Create(*graph_def)); rcut = get_scalar("descrpt_attr/rcut"); cell_size = rcut; ntypes = get_scalar("descrpt_attr/ntypes"); @@ -282,7 +284,7 @@ std::string graph_info(const GraphDef & graph_def) { // init the tmp array data std::vector DeepPot::get_sel_a () const { std::vector sel_a; - std::istringstream is(graph_info(graph_def)); + std::istringstream is(graph_info(*graph_def)); std::string line = ""; while(is >> line) { if (line.find("sel_a") != line.npos) { @@ -548,10 +550,11 @@ init (const std::vector & models, const int & gpu_rank, const std:: options.config.set_inter_op_parallelism_threads(num_inter_nthreads); options.config.set_intra_op_parallelism_threads(num_intra_nthreads); for (unsigned ii = 0; ii < numb_models; ++ii){ + graph_defs[ii] = new GraphDef(); if (file_contents.size() == 0) - check_status (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]); + (*graph_defs[ii]).ParseFromString(file_contents[ii]); } #if GOOGLE_CUDA || TENSORFLOW_USE_ROCM if (gpu_num > 0) { @@ -566,10 +569,10 @@ init (const std::vector & models, const int & gpu_rank, const std:: if (gpu_num > 0) { std::string str = "/gpu:"; str += std::to_string(gpu_rank % gpu_num); - graph::SetDefaultDevice(str, &graph_defs[ii]); + graph::SetDefaultDevice(str, &(*graph_defs[ii])); } check_status (NewSession(options, &(sessions[ii]))); - check_status (sessions[ii]->Create(graph_defs[ii])); + check_status (sessions[ii]->Create(*graph_defs[ii])); } rcut = get_scalar("descrpt_attr/rcut"); cell_size = rcut; @@ -620,7 +623,7 @@ get_sel () const std::vector > sec; for (int ii = 0; ii < numb_models; ii++) { std::vector sel; - std::istringstream is(graph_info(graph_defs[ii])); + std::istringstream is(graph_info(*graph_defs[ii])); std::string line = ""; while(is >> line) { if (line.find("sel") != line.npos) { @@ -643,20 +646,6 @@ get_sel () const return sec; } -void -DeepPotModelDevi:: -cum_sum (const std::vector > n_sel) -{ - for (int ii = 0; ii < numb_models; ++ii) { - std::vector _sec; - _sec.resize (n_sel[ii].size() + 1); - _sec[0] = 0; - for (int jj = 1; jj < _sec.size(); ++jj) { - _sec[jj] = _sec[jj-1] + n_sel[ii][jj-1]; - } - sec.push_back(_sec); - } -} void DeepPotModelDevi:: diff --git a/source/api_cc/src/DeepTensor.cc b/source/api_cc/src/DeepTensor.cc index 316b6ec3a9..6a67b70ea0 100644 --- a/source/api_cc/src/DeepTensor.cc +++ b/source/api_cc/src/DeepTensor.cc @@ -5,7 +5,8 @@ using namespace tensorflow; DeepTensor:: DeepTensor() - : inited (false) + : inited (false), + graph_def(new GraphDef()) { } @@ -13,7 +14,8 @@ DeepTensor:: DeepTensor(const std::string & model, const int & gpu_rank, const std::string &name_scope_) - : inited (false), name_scope(name_scope_) + : inited (false), name_scope(name_scope_), + graph_def(new GraphDef()) { init(model, gpu_rank); } @@ -35,8 +37,8 @@ init (const std::string & model, options.config.set_intra_op_parallelism_threads(num_intra_nthreads); deepmd::load_op_library(); deepmd::check_status (NewSession(options, &session)); - deepmd::check_status (ReadBinaryProto(Env::Default(), model, &graph_def)); - deepmd::check_status (session->Create(graph_def)); + 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"); diff --git a/source/api_cc/src/common.cc b/source/api_cc/src/common.cc index a83f364e11..01c2dd5f8d 100644 --- a/source/api_cc/src/common.cc +++ b/source/api_cc/src/common.cc @@ -2,6 +2,9 @@ #include "AtomMap.h" #include "device.h" #include +#include +#include "google/protobuf/text_format.h" +#include "google/protobuf/io/zero_copy_stream_impl.h" using namespace tensorflow; @@ -855,3 +858,25 @@ select_map_inv( const typename std::vector::const_iterator in, const std::vector & idx_map, const int & stride); + + +void +deepmd:: +read_file_to_string(std::string model, std::string & file_content) +{ + deepmd::check_status(tensorflow::ReadFileToString(tensorflow::Env::Default(), model, &file_content)); +} + + +void +deepmd:: +convert_pbtxt_to_pb(std::string fn_pb_txt, std::string fn_pb) +{ + int fd = open(fn_pb_txt.c_str(), O_RDONLY); + 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(fn_pb, std::ios::out | std::ios::trunc | std::ios::binary); + graph_def.SerializeToOstream(&output); +} diff --git a/source/api_cc/tests/CMakeLists.txt b/source/api_cc/tests/CMakeLists.txt index 5609f0a8f2..b4ac448f9c 100644 --- a/source/api_cc/tests/CMakeLists.txt +++ b/source/api_cc/tests/CMakeLists.txt @@ -26,6 +26,10 @@ include_directories(${API_BASE_DIR}/include) include_directories(${CMAKE_SOURCE_DIR}) file(GLOB API_SRC ${API_BASE_DIR}/src/*.cc ${API_BASE_DIR}/src/*.cpp) add_library(${apiname} SHARED ${API_SRC}) +target_compile_definitions(${apiname} + PRIVATE TF_PRIVATE +) +target_include_directories(${apiname} PRIVATE ${TensorFlow_INCLUDE_DIRS}) configure_file( ${API_BASE_DIR}/include/version.h.in ${CMAKE_SOURCE_DIR}/version.h @@ -44,8 +48,8 @@ if (TENSORFLOW_VERSION GREATER_EQUAL 2.7) else() set (CMAKE_CXX_STANDARD 11) endif() -include_directories(${TensorFlow_INCLUDE_DIRS}) add_library(${opname} SHARED ${OP_SRC}) +target_include_directories(${opname} PRIVATE ${TensorFlow_INCLUDE_DIRS}) find_package(Threads) # find openmp @@ -105,12 +109,6 @@ else() target_link_libraries(runUnitTests gtest gtest_main ${libname} ${apiname} pthread ${TensorFlow_LIBRARY} rt coverage_config) endif() -find_package(Protobuf) -if(Protobuf_FOUND) - include_directories(${Protobuf_INCLUDE_DIRS}) - target_link_libraries(runUnitTests ${Protobuf_LIBRARIES}) -endif() - add_test( runUnitTests runUnitTests ) find_package(GTest) diff --git a/source/api_cc/tests/test_deepdipole.cc b/source/api_cc/tests/test_deepdipole.cc index 94f21ec675..23382f49df 100644 --- a/source/api_cc/tests/test_deepdipole.cc +++ b/source/api_cc/tests/test_deepdipole.cc @@ -7,8 +7,6 @@ #include "neighbor_list.h" #include "test_utils.h" -#include "google/protobuf/text_format.h" -#include "google/protobuf/io/zero_copy_stream_impl.h" #include #include #include @@ -38,17 +36,7 @@ class TestInferDeepDipole : public ::testing::Test deepmd::DeepTensor dp; void SetUp() override { - std::string file_name = "../../tests/infer/deepdipole.pbtxt"; - int fd = open(file_name.c_str(), O_RDONLY); - 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; - // tensorflow::protobuf::TextFormat::PrintToString(graph_def, &txt); + deepmd::convert_pbtxt_to_pb("../../tests/infer/deepdipole.pbtxt", "deepdipole.pb"); dp.init("deepdipole.pb"); }; @@ -139,16 +127,7 @@ class TestInferDeepDipoleNew : public ::testing::Test void SetUp() override { std::string file_name = "../../tests/infer/deepdipole_new.pbtxt"; - int fd = open(file_name.c_str(), O_RDONLY); - 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_new.pb", std::ios::out | std::ios::trunc | std::ios::binary); - graph_def.SerializeToOstream(&output); - // check the string by the following commands - // string txt; - // tensorflow::protobuf::TextFormat::PrintToString(graph_def, &txt); + deepmd::convert_pbtxt_to_pb("../../tests/infer/deepdipole_new.pbtxt", "deepdipole_new.pb"); dp.init("deepdipole_new.pb"); odim = dp.output_dim (); @@ -339,17 +318,7 @@ class TestInferDeepDipoleFake : public ::testing::Test deepmd::DeepTensor dp; void SetUp() override { - std::string file_name = "../../tests/infer/deepdipole_fake.pbtxt"; - int fd = open(file_name.c_str(), O_RDONLY); - 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_fake.pb", std::ios::out | std::ios::trunc | std::ios::binary); - graph_def.SerializeToOstream(&output); - // check the string by the following commands - // string txt; - // tensorflow::protobuf::TextFormat::PrintToString(graph_def, &txt); + deepmd::convert_pbtxt_to_pb("../../tests/infer/deepdipole_fake.pbtxt", "deepdipole_fake.pb"); dp.init("deepdipole_fake.pb"); }; diff --git a/source/api_cc/tests/test_deeppolar.cc b/source/api_cc/tests/test_deeppolar.cc index 3dfc34c1d0..ad4ccdca5b 100644 --- a/source/api_cc/tests/test_deeppolar.cc +++ b/source/api_cc/tests/test_deeppolar.cc @@ -7,8 +7,6 @@ #include "neighbor_list.h" #include "test_utils.h" -#include "google/protobuf/text_format.h" -#include "google/protobuf/io/zero_copy_stream_impl.h" #include #include #include @@ -39,16 +37,7 @@ class TestInferDeepPolar : public ::testing::Test void SetUp() override { std::string file_name = "../../tests/infer/deeppolar.pbtxt"; - int fd = open(file_name.c_str(), O_RDONLY); - 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; - // tensorflow::protobuf::TextFormat::PrintToString(graph_def, &txt); + deepmd::convert_pbtxt_to_pb("../../tests/infer/deeppolar.pbtxt", "deeppolar.pb"); dp.init("deeppolar.pb"); @@ -141,16 +130,7 @@ class TestInferDeepPolarNew : public ::testing::Test void SetUp() override { std::string file_name = "../../tests/infer/deeppolar_new.pbtxt"; - int fd = open(file_name.c_str(), O_RDONLY); - 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_new.pb", std::ios::out | std::ios::trunc | std::ios::binary); - graph_def.SerializeToOstream(&output); - // check the string by the following commands - // string txt; - // tensorflow::protobuf::TextFormat::PrintToString(graph_def, &txt); + deepmd::convert_pbtxt_to_pb("../../tests/infer/deeppolar_new.pbtxt", "deeppolar_new.pb"); dp.init("deeppolar_new.pb"); odim = dp.output_dim (); diff --git a/source/api_cc/tests/test_deeppot_a.cc b/source/api_cc/tests/test_deeppot_a.cc index 61336c4e33..d8082972f8 100644 --- a/source/api_cc/tests/test_deeppot_a.cc +++ b/source/api_cc/tests/test_deeppot_a.cc @@ -7,8 +7,6 @@ #include "neighbor_list.h" #include "test_utils.h" -#include "google/protobuf/text_format.h" -#include "google/protobuf/io/zero_copy_stream_impl.h" #include #include #include @@ -47,16 +45,7 @@ class TestInferDeepPotA : public ::testing::Test void SetUp() override { std::string file_name = "../../tests/infer/deeppot.pbtxt"; - int fd = open(file_name.c_str(), O_RDONLY); - 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; - // tensorflow::protobuf::TextFormat::PrintToString(graph_def, &txt); + deepmd::convert_pbtxt_to_pb("../../tests/infer/deeppot.pbtxt", "deeppot.pb"); dp.init("deeppot.pb"); @@ -430,13 +419,7 @@ class TestInferDeepPotANoPbc : public ::testing::Test void SetUp() override { std::string file_name = "../../tests/infer/deeppot.pbtxt"; - int fd = open(file_name.c_str(), O_RDONLY); - 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); + deepmd::convert_pbtxt_to_pb(file_name, "deeppot.pb"); dp.init("deeppot.pb"); diff --git a/source/api_cc/tests/test_deeppot_model_devi.cc b/source/api_cc/tests/test_deeppot_model_devi.cc index 3b10fbf5c2..2b532f098e 100644 --- a/source/api_cc/tests/test_deeppot_model_devi.cc +++ b/source/api_cc/tests/test_deeppot_model_devi.cc @@ -7,8 +7,6 @@ #include "neighbor_list.h" #include "test_utils.h" -#include "google/protobuf/text_format.h" -#include "google/protobuf/io/zero_copy_stream_impl.h" #include #include #include @@ -39,24 +37,12 @@ class TestInferDeepPotModeDevi : public ::testing::Test void SetUp() override { { std::string file_name = "../../tests/infer/deeppot.pbtxt"; - int fd = open(file_name.c_str(), O_RDONLY); - 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); + deepmd::convert_pbtxt_to_pb("../../tests/infer/deeppot.pbtxt", "deeppot.pb"); dp0.init("deeppot.pb"); } { std::string file_name = "../../tests/infer/deeppot-1.pbtxt"; - int fd = open(file_name.c_str(), O_RDONLY); - 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); + deepmd::convert_pbtxt_to_pb("../../tests/infer/deeppot-1.pbtxt", "deeppot-1.pb"); dp1.init("deeppot-1.pb"); } dp_md.init(std::vector({"deeppot.pb", "deeppot-1.pb"})); @@ -101,24 +87,12 @@ class TestInferDeepPotModeDeviPython : public ::testing::Test void SetUp() override { { std::string file_name = "../../tests/infer/deeppot.pbtxt"; - int fd = open(file_name.c_str(), O_RDONLY); - 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); + deepmd::convert_pbtxt_to_pb("../../tests/infer/deeppot.pbtxt", "deeppot.pb"); dp0.init("deeppot.pb"); } { std::string file_name = "../../tests/infer/deeppot-1.pbtxt"; - int fd = open(file_name.c_str(), O_RDONLY); - 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); + deepmd::convert_pbtxt_to_pb("../../tests/infer/deeppot-1.pbtxt", "deeppot-1.pb"); dp1.init("deeppot-1.pb"); } dp_md.init(std::vector({"deeppot.pb", "deeppot-1.pb"})); diff --git a/source/api_cc/tests/test_deeppot_r.cc b/source/api_cc/tests/test_deeppot_r.cc index 2d6af9f6ae..3bd34f398c 100644 --- a/source/api_cc/tests/test_deeppot_r.cc +++ b/source/api_cc/tests/test_deeppot_r.cc @@ -7,8 +7,6 @@ #include "neighbor_list.h" #include "test_utils.h" -#include "google/protobuf/text_format.h" -#include "google/protobuf/io/zero_copy_stream_impl.h" #include #include #include @@ -47,16 +45,7 @@ class TestInferDeepPotR : public ::testing::Test void SetUp() override { std::string file_name = "../../tests/infer/deeppot-r.pbtxt"; - int fd = open(file_name.c_str(), O_RDONLY); - 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; - // tensorflow::protobuf::TextFormat::PrintToString(graph_def, &txt); + deepmd::convert_pbtxt_to_pb("../../tests/infer/deeppot-r.pbtxt", "deeppot.pb"); dp.init("deeppot.pb"); @@ -430,13 +419,7 @@ class TestInferDeepPotRNoPbc : public ::testing::Test void SetUp() override { std::string file_name = "../../tests/infer/deeppot-r.pbtxt"; - int fd = open(file_name.c_str(), O_RDONLY); - 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); + deepmd::convert_pbtxt_to_pb("../../tests/infer/deeppot-r.pbtxt", "deeppot.pb"); dp.init("deeppot.pb"); diff --git a/source/api_cc/tests/test_dipolecharge.cc b/source/api_cc/tests/test_dipolecharge.cc index 6db7b8af0e..b75677f8f6 100644 --- a/source/api_cc/tests/test_dipolecharge.cc +++ b/source/api_cc/tests/test_dipolecharge.cc @@ -10,8 +10,6 @@ #include "neighbor_list.h" #include "test_utils.h" -#include "google/protobuf/text_format.h" -#include "google/protobuf/io/zero_copy_stream_impl.h" #include #include #include @@ -58,20 +56,8 @@ class TestDipoleCharge : public ::testing::Test void SetUp() override { std::string file_name = "../../tests/infer/dipolecharge_e.pbtxt"; - int fd = open(file_name.c_str(), O_RDONLY); - 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::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; - // tensorflow::protobuf::TextFormat::PrintToString(graph_def, &txt); - - // dp.init("dipolecharge_d.pb"); - // dm.init("dipolecharge_d.pb"); + deepmd::convert_pbtxt_to_pb(file_name, model); dp.init(model, 0, "dipole_charge"); dm.init(model, 0, "dipole_charge"); diff --git a/source/gmx/CMakeLists.txt b/source/gmx/CMakeLists.txt index c0683b01ba..baa2e13140 100644 --- a/source/gmx/CMakeLists.txt +++ b/source/gmx/CMakeLists.txt @@ -15,7 +15,6 @@ file(GLOB INC_SRC include/*.h) add_library(${libgmxname} SHARED ${LIB_SRC}) target_link_libraries(${libgmxname} ${LIB_DEEPMD_CC} ${LIB_DEEPMD} ${TensorFlow_LIBRARY} ${TensorFlowFramework_LIBRARY}) -target_include_directories(${libgmxname} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../api_cc/include) target_include_directories(${libgmxname} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) target_include_directories(${libgmxname} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../3rdparty/) diff --git a/source/gmx/src/gmx_plugin.cpp b/source/gmx/src/gmx_plugin.cpp index db68d6e55a..65ddc14f02 100644 --- a/source/gmx/src/gmx_plugin.cpp +++ b/source/gmx/src/gmx_plugin.cpp @@ -2,6 +2,7 @@ #include "json.hpp" #include #include +#include using namespace deepmd; diff --git a/source/install/test_cc.sh b/source/install/test_cc.sh index 58fa86eb10..65582e2986 100755 --- a/source/install/test_cc.sh +++ b/source/install/test_cc.sh @@ -26,7 +26,7 @@ INSTALL_PREFIX=${SCRIPT_PATH}/../../dp_test_cc mkdir -p ${BUILD_TMP_DIR} mkdir -p ${INSTALL_PREFIX} cd ${BUILD_TMP_DIR} -cmake -DINSTALL_TENSORFLOW=TRUE -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} ../api_cc/tests +cmake -DINSTALL_TENSORFLOW=TRUE -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} -DTENSORFLOW_ROOT=${INSTALL_PREFIX} ../api_cc/tests make -j${NPROC} make install diff --git a/source/lmp/env.sh.in b/source/lmp/env.sh.in index b419154a32..b0b2f4d7bf 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++@CMAKE_CXX_STANDARD@ -D@prec_def@ @TTM_DEF@ -DLAMMPS_VERSION_NUMBER=@LAMMPS_VERSION_NUMBER@ -I$TF_INCLUDE_DIRS -I$DEEPMD_ROOT/include/ " +NNP_INC=" -std=c++@CMAKE_CXX_STANDARD@ -D@prec_def@ @TTM_DEF@ -DLAMMPS_VERSION_NUMBER=@LAMMPS_VERSION_NUMBER@ -I$DEEPMD_ROOT/include/ " NNP_PATH=" -L$TF_LIBRARY_PATH -L$DEEPMD_ROOT/lib" NNP_LIB=" -Wl,--no-as-needed -l@LIB_DEEPMD_CC@@variant_name@ -ltensorflow_cc -ltensorflow_framework -Wl,-rpath=$TF_RPATH -Wl,-rpath=$DEEPMD_ROOT/lib" diff --git a/source/lmp/fix_dplr.h b/source/lmp/fix_dplr.h index cdb5b36b1f..c5cf5d22da 100644 --- a/source/lmp/fix_dplr.h +++ b/source/lmp/fix_dplr.h @@ -8,6 +8,7 @@ FixStyle(dplr,FixDPLR) #define LMP_FIX_DPLR_H #include +#include #include "fix.h" #include "pair_deepmd.h" #ifdef LMPPLUGIN diff --git a/source/lmp/pair_deepmd.cpp b/source/lmp/pair_deepmd.cpp index f457d2a183..fbc3a696ce 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) { - deepmd::check_status(tensorflow::ReadFileToString(tensorflow::Env::Default(), model, &file_content)); + deepmd::read_file_to_string(model, file_content); nchar = file_content.size(); } MPI_Bcast(&nchar, 1, MPI_INT, root, MPI_COMM_WORLD);