From 1fb1c1b5183a5ae5917152618e67efc80719cae1 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Tue, 23 Jan 2024 04:06:15 -0500 Subject: [PATCH 1/8] setup pytorch build options and environment Signed-off-by: Jinzhe Zeng --- .github/workflows/build_cc.yml | 5 + .github/workflows/test_cc.yml | 5 + source/CMakeLists.txt | 28 ++++- source/api_cc/CMakeLists.txt | 12 +- source/api_cc/include/DataModifierTF.h | 1 + source/api_cc/include/DeepPotTF.h | 1 + source/api_cc/include/DeepTensorTF.h | 1 + source/api_cc/include/common.h | 141 ------------------------ source/api_cc/include/commonTF.h | 147 +++++++++++++++++++++++++ source/api_cc/include/version.h.in | 1 + source/api_cc/src/DataModifier.cc | 7 +- source/api_cc/src/DataModifierTF.cc | 2 + source/api_cc/src/DeepPot.cc | 7 +- source/api_cc/src/DeepPotPT.cc | 8 ++ source/api_cc/src/DeepPotTF.cc | 2 + source/api_cc/src/DeepTensor.cc | 7 +- source/api_cc/src/DeepTensorTF.cc | 2 + source/api_cc/src/common.cc | 33 ++++++ source/install/build_cc.sh | 8 +- source/install/docker_package_c.sh | 5 + source/install/package_c.sh | 5 +- 21 files changed, 278 insertions(+), 150 deletions(-) create mode 100644 source/api_cc/include/commonTF.h create mode 100644 source/api_cc/src/DeepPotPT.cc diff --git a/.github/workflows/build_cc.yml b/.github/workflows/build_cc.yml index f029517d80..3b363911b9 100644 --- a/.github/workflows/build_cc.yml +++ b/.github/workflows/build_cc.yml @@ -27,6 +27,10 @@ jobs: cache: 'pip' - uses: lukka/get-cmake@latest - run: python -m pip install tensorflow + - name: Download libtorch + run: | + wget https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-2.1.2%2Bcpu.zip -O libtorch.zip + unzip libtorch.zip - run: | wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.0-1_all.deb \ && sudo dpkg -i cuda-keyring_1.0-1_all.deb \ @@ -61,6 +65,7 @@ jobs: CC: clang CXX: clang++ CMAKE_GENERATOR: Ninja + CMAKE_PREFIX_PATH: $GITHUB_WORKSPACE/libtorch if: matrix.variant == 'clang' - name: Test files exist run: | diff --git a/.github/workflows/test_cc.yml b/.github/workflows/test_cc.yml index ef6fade8e5..ca35f1e862 100644 --- a/.github/workflows/test_cc.yml +++ b/.github/workflows/test_cc.yml @@ -18,6 +18,10 @@ jobs: mpi: mpich - uses: lukka/get-cmake@latest - run: python -m pip install tensorflow + - name: Download libtorch + run: | + wget https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-2.1.2%2Bcpu.zip -O libtorch.zip + unzip libtorch.zip - run: source/install/test_cc_local.sh env: OMP_NUM_THREADS: 1 @@ -25,6 +29,7 @@ jobs: TF_INTER_OP_PARALLELISM_THREADS: 1 LMP_CXX11_ABI_0: 1 CMAKE_GENERATOR: Ninja + CMAKE_PREFIX_PATH: $GITHUB_WORKSPACE/libtorch # test lammps # ASE issue: https://gitlab.com/ase/ase/-/merge_requests/2843 # TODO: remove ase version when ase has new release diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index c1c9b8e7fe..5c3b5bf768 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.16) project(DeePMD) +option(ENABLE_TENSORFLOW "Enable TensorFlow interface" OFF) +option(ENABLE_PYTORCH "Enable PyTorch interface" OFF) option(BUILD_TESTING "Build test and enable converage" OFF) set(DEEPMD_C_ROOT "" @@ -131,6 +133,7 @@ if(INSTALL_TENSORFLOW) set(USE_TF_PYTHON_LIBS TRUE) endif(INSTALL_TENSORFLOW) if(USE_TF_PYTHON_LIBS) + set(ENABLE_TENSORFLOW TRUE) if(NOT "$ENV{CIBUILDWHEEL}" STREQUAL "1") find_package( Python @@ -141,11 +144,29 @@ if(USE_TF_PYTHON_LIBS) set(PYTHON_INCLUDE_DIRS ${PYTHON_INCLUDE_DIR}) endif() endif(USE_TF_PYTHON_LIBS) +if(TENSORFLOW_ROOT) + set(ENABLE_TENSORFLOW TRUE) +endif() # find tensorflow, I need tf abi info -if(NOT DEEPMD_C_ROOT) +if(ENABLE_TENSORFLOW AND NOT DEEPMD_C_ROOT) find_package(tensorflow REQUIRED) endif() +if(ENABLE_PYTORCH AND NOT DEEPMD_C_ROOT) + find_package(Torch REQUIRED) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}") +endif() +# log enabled backends +message(STATUS "Enabled backends:") +if(ENABLE_TENSORFLOW) + message(STATUS "- TensorFlow") +endif() +if(ENABLE_PYTORCH) + message(STATUS "- PyTorch") +endif() +if(NOT ENABLE_TENSORFLOW AND NOT ENABLE_PYTORCH) + message(FATAL_ERROR "No backend is enabled.") +endif() # find threads find_package(Threads) @@ -233,10 +254,13 @@ if(DEEPMD_C_ROOT) # use variable for TF path to set deepmd_c path set(TensorFlow_LIBRARY_PATH "${DEEPMD_C_ROOT}/lib") set(TENSORFLOW_INCLUDE_DIRS "${DEEPMD_C_ROOT}/include") + set(TORCH_LIBRARIES "${DEEPMD_C_ROOT}/lib/libtorch.so") endif() if(NOT DEEPMD_C_ROOT) - add_subdirectory(op/) + if(ENABLE_TENSORFLOW) + add_subdirectory(op/) + endif() add_subdirectory(lib/) endif() if(BUILD_PY_IF) diff --git a/source/api_cc/CMakeLists.txt b/source/api_cc/CMakeLists.txt index 2f296e3dfd..35a1f545dc 100644 --- a/source/api_cc/CMakeLists.txt +++ b/source/api_cc/CMakeLists.txt @@ -11,8 +11,16 @@ add_library(${libname} SHARED ${LIB_SRC}) # link: libdeepmd libdeepmd_op libtensorflow_cc libtensorflow_framework target_link_libraries(${libname} PUBLIC ${LIB_DEEPMD}) -target_link_libraries(${libname} PRIVATE TensorFlow::tensorflow_cc - TensorFlow::tensorflow_framework) +if(ENABLE_TENSORFLOW) + target_link_libraries(${libname} PRIVATE TensorFlow::tensorflow_cc + TensorFlow::tensorflow_framework) + target_compile_definitions(${libname} PRIVATE BUILD_TENSORFLOW) +endif() +if(ENABLE_PYTORCH) + target_link_libraries(${libname} PRIVATE "${TORCH_LIBRARIES}") + target_compile_definitions(${libname} PRIVATE BUILD_PYTORCH) +endif() + target_include_directories( ${libname} PUBLIC $ diff --git a/source/api_cc/include/DataModifierTF.h b/source/api_cc/include/DataModifierTF.h index 2ca3729525..c0021c6947 100644 --- a/source/api_cc/include/DataModifierTF.h +++ b/source/api_cc/include/DataModifierTF.h @@ -3,6 +3,7 @@ #include "DataModifier.h" #include "common.h" +#include "commonTF.h" namespace deepmd { /** diff --git a/source/api_cc/include/DeepPotTF.h b/source/api_cc/include/DeepPotTF.h index 0580c61da5..699b0ff7fe 100644 --- a/source/api_cc/include/DeepPotTF.h +++ b/source/api_cc/include/DeepPotTF.h @@ -3,6 +3,7 @@ #include "DeepPot.h" #include "common.h" +#include "commonTF.h" #include "neighbor_list.h" namespace deepmd { diff --git a/source/api_cc/include/DeepTensorTF.h b/source/api_cc/include/DeepTensorTF.h index 3c724dce88..3ca316a29f 100644 --- a/source/api_cc/include/DeepTensorTF.h +++ b/source/api_cc/include/DeepTensorTF.h @@ -3,6 +3,7 @@ #include "DeepTensor.h" #include "common.h" +#include "commonTF.h" #include "neighbor_list.h" namespace deepmd { diff --git a/source/api_cc/include/common.h b/source/api_cc/include/common.h index 7982c4f89d..0392747979 100644 --- a/source/api_cc/include/common.h +++ b/source/api_cc/include/common.h @@ -10,12 +10,6 @@ #include "neighbor_list.h" #include "version.h" -#ifdef TF_PRIVATE -#include "tf_private.h" -#else -#include "tf_public.h" -#endif - namespace deepmd { typedef double ENERGYTYPE; @@ -175,143 +169,8 @@ struct tf_exception : public deepmd::deepmd_exception { : deepmd::deepmd_exception(std::string("TensorFlow Error: ") + msg){}; }; -/** - * @brief Check TensorFlow status. Exit if not OK. - * @param[in] status TensorFlow status. - **/ -void check_status(const tensorflow::Status& status); - 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 -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 -void session_get_vector(std::vector& o_vec, - tensorflow::Session* session, - 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. - * @param[in] aparam_nall Whether the atomic dimesion of atomic parameters is - * nall. - */ -template -int session_input_tensors( - std::vector>& input_tensors, - const std::vector& dcoord_, - const int& ntypes, - const std::vector& datype_, - const std::vector& dbox, - const double& cell_size, - const std::vector& fparam_, - const std::vector& aparam_, - const deepmd::AtomMap& atommap, - const std::string scope = "", - const bool aparam_nall = false); - -/** - * @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. - * @param[in] aparam_nall Whether the atomic dimesion of atomic parameters is - * nall. - */ -template -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 deepmd::AtomMap& atommap, - const int nghost, - const int ago, - const std::string scope = "", - const bool aparam_nall = false); - -/** - * @brief Get input tensors for mixed type. - * @param[out] input_tensors Input tensors. - * @param[in] nframes Number of frames. - * @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. - * @param[in] aparam_nall Whether the atomic dimesion of atomic parameters is - * nall. - */ -template -int session_input_tensors_mixed_type( - std::vector>& input_tensors, - const int& nframes, - const std::vector& dcoord_, - const int& ntypes, - const std::vector& datype_, - const std::vector& dbox, - const double& cell_size, - const std::vector& fparam_, - const std::vector& aparam_, - const deepmd::AtomMap& atommap, - const std::string scope = "", - const bool aparam_nall = false); - /** * @brief Read model file to a string. * @param[in] model Path to the model. diff --git a/source/api_cc/include/commonTF.h b/source/api_cc/include/commonTF.h new file mode 100644 index 0000000000..0c14597e30 --- /dev/null +++ b/source/api_cc/include/commonTF.h @@ -0,0 +1,147 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later +#include +#include + +#ifdef TF_PRIVATE +#include "tf_private.h" +#else +#include "tf_public.h" +#endif + +namespace deepmd { +/** + * @brief Check TensorFlow status. Exit if not OK. + * @param[in] status TensorFlow status. + **/ +void check_status(const tensorflow::Status& status); + +/** + * @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 +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 +void session_get_vector(std::vector& o_vec, + tensorflow::Session* session, + 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. + * @param[in] aparam_nall Whether the atomic dimesion of atomic parameters is + * nall. + */ +template +int session_input_tensors( + std::vector>& input_tensors, + const std::vector& dcoord_, + const int& ntypes, + const std::vector& datype_, + const std::vector& dbox, + const double& cell_size, + const std::vector& fparam_, + const std::vector& aparam_, + const deepmd::AtomMap& atommap, + const std::string scope = "", + const bool aparam_nall = false); + +/** + * @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. + * @param[in] aparam_nall Whether the atomic dimesion of atomic parameters is + * nall. + */ +template +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 deepmd::AtomMap& atommap, + const int nghost, + const int ago, + const std::string scope = "", + const bool aparam_nall = false); + +/** + * @brief Get input tensors for mixed type. + * @param[out] input_tensors Input tensors. + * @param[in] nframes Number of frames. + * @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. + * @param[in] aparam_nall Whether the atomic dimesion of atomic parameters is + * nall. + */ +template +int session_input_tensors_mixed_type( + std::vector>& input_tensors, + const int& nframes, + const std::vector& dcoord_, + const int& ntypes, + const std::vector& datype_, + const std::vector& dbox, + const double& cell_size, + const std::vector& fparam_, + const std::vector& aparam_, + const deepmd::AtomMap& atommap, + const std::string scope = "", + const bool aparam_nall = false); + +} // namespace deepmd diff --git a/source/api_cc/include/version.h.in b/source/api_cc/include/version.h.in index c6bf6cf491..26b0c1be48 100644 --- a/source/api_cc/include/version.h.in +++ b/source/api_cc/include/version.h.in @@ -9,4 +9,5 @@ const std::string global_git_date="@GIT_DATE@"; const std::string global_git_branch="@GIT_BRANCH@"; const std::string global_tf_include_dir="@TensorFlow_INCLUDE_DIRS@"; const std::string global_tf_lib="@TensorFlow_LIBRARY@"; +const std::string global_pt_lib="@TORCH_LIBRARIES@"; const std::string global_model_version="@MODEL_VERSION@"; diff --git a/source/api_cc/src/DataModifier.cc b/source/api_cc/src/DataModifier.cc index 954c969c13..38d1fc879a 100644 --- a/source/api_cc/src/DataModifier.cc +++ b/source/api_cc/src/DataModifier.cc @@ -1,7 +1,9 @@ // SPDX-License-Identifier: LGPL-3.0-or-later #include "DataModifier.h" +#ifdef BUILD_TENSORFLOW #include "DataModifierTF.h" +#endif #include "common.h" using namespace deepmd; @@ -29,9 +31,12 @@ void DipoleChargeModifier::init(const std::string& model, // TODO: To implement detect_backend DPBackend backend = deepmd::DPBackend::TensorFlow; if (deepmd::DPBackend::TensorFlow == backend) { - // TODO: throw errors if TF backend is not built, without mentioning TF +#ifdef BUILD_TENSORFLOW dcm = std::make_shared(model, gpu_rank, name_scope_); +#else + throw deepmd::deepmd_exception("TensorFlow backend is not built"); +#endif } else if (deepmd::DPBackend::PyTorch == backend) { throw deepmd::deepmd_exception("PyTorch backend is not supported yet"); } else if (deepmd::DPBackend::Paddle == backend) { diff --git a/source/api_cc/src/DataModifierTF.cc b/source/api_cc/src/DataModifierTF.cc index 219139cf89..324cb14098 100644 --- a/source/api_cc/src/DataModifierTF.cc +++ b/source/api_cc/src/DataModifierTF.cc @@ -1,4 +1,5 @@ // SPDX-License-Identifier: LGPL-3.0-or-later +#ifdef BUILD_TENSORFLOW #include "DataModifierTF.h" #include "common.h" @@ -361,3 +362,4 @@ void DipoleChargeModifierTF::computew( compute(dfcorr_, dvcorr_, dcoord_, datype_, dbox, pairs, delef_, nghost, lmp_list); } +#endif // BUILD_TENSORFLOW diff --git a/source/api_cc/src/DeepPot.cc b/source/api_cc/src/DeepPot.cc index 083e9b091f..c598549844 100644 --- a/source/api_cc/src/DeepPot.cc +++ b/source/api_cc/src/DeepPot.cc @@ -7,7 +7,9 @@ #include #include "AtomMap.h" +#ifdef BUILD_TENSORFLOW #include "DeepPotTF.h" +#endif #include "device.h" using namespace deepmd; @@ -35,8 +37,11 @@ void DeepPot::init(const std::string& model, // TODO: To implement detect_backend DPBackend backend = deepmd::DPBackend::TensorFlow; if (deepmd::DPBackend::TensorFlow == backend) { - // TODO: throw errors if TF backend is not built, without mentioning TF +#ifdef BUILD_TENSORFLOW dp = std::make_shared(model, gpu_rank, file_content); +#else + throw deepmd::deepmd_exception("TensorFlow backend is not built"); +#endif } else if (deepmd::DPBackend::PyTorch == backend) { throw deepmd::deepmd_exception("PyTorch backend is not supported yet"); } else if (deepmd::DPBackend::Paddle == backend) { diff --git a/source/api_cc/src/DeepPotPT.cc b/source/api_cc/src/DeepPotPT.cc new file mode 100644 index 0000000000..c94fb4247b --- /dev/null +++ b/source/api_cc/src/DeepPotPT.cc @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: LGPL-3.0-or-later +#ifdef BUILD_PYTORCH +#include + +void test_function_please_remove_after_torch_is_actually_used() { + torch::Tensor tensor = torch::rand({2, 3}); +} +#endif diff --git a/source/api_cc/src/DeepPotTF.cc b/source/api_cc/src/DeepPotTF.cc index ef348fe14c..7bf2bebce4 100644 --- a/source/api_cc/src/DeepPotTF.cc +++ b/source/api_cc/src/DeepPotTF.cc @@ -1,4 +1,5 @@ // SPDX-License-Identifier: LGPL-3.0-or-later +#ifdef BUILD_TENSORFLOW #include "DeepPotTF.h" #include @@ -1051,3 +1052,4 @@ void DeepPotTF::computew_mixed_type(std::vector& ener, compute_mixed_type(ener, force, virial, atom_energy, atom_virial, nframes, coord, atype, box, fparam, aparam); } +#endif diff --git a/source/api_cc/src/DeepTensor.cc b/source/api_cc/src/DeepTensor.cc index 2c88ab2f4b..a0596e046f 100644 --- a/source/api_cc/src/DeepTensor.cc +++ b/source/api_cc/src/DeepTensor.cc @@ -3,7 +3,9 @@ #include +#ifdef BUILD_TENSORFLOW #include "DeepTensorTF.h" +#endif #include "common.h" using namespace deepmd; @@ -31,8 +33,11 @@ void DeepTensor::init(const std::string &model, // TODO: To implement detect_backend DPBackend backend = deepmd::DPBackend::TensorFlow; if (deepmd::DPBackend::TensorFlow == backend) { - // TODO: throw errors if TF backend is not built, without mentioning TF +#ifdef BUILD_TENSORFLOW dt = std::make_shared(model, gpu_rank, name_scope_); +#else + throw deepmd::deepmd_exception("TensorFlow backend is not built."); +#endif } else if (deepmd::DPBackend::PyTorch == backend) { throw deepmd::deepmd_exception("PyTorch backend is not supported yet"); } else if (deepmd::DPBackend::Paddle == backend) { diff --git a/source/api_cc/src/DeepTensorTF.cc b/source/api_cc/src/DeepTensorTF.cc index 436e389ad2..34a47bc6f3 100644 --- a/source/api_cc/src/DeepTensorTF.cc +++ b/source/api_cc/src/DeepTensorTF.cc @@ -1,4 +1,5 @@ // SPDX-License-Identifier: LGPL-3.0-or-later +#ifdef BUILD_TENSORFLOW #include "DeepTensorTF.h" using namespace deepmd; @@ -844,3 +845,4 @@ void DeepTensorTF::computew(std::vector &global_tensor, atom_virial.clear(); } } +#endif diff --git a/source/api_cc/src/common.cc b/source/api_cc/src/common.cc index 2f75aaa291..a552f646f1 100644 --- a/source/api_cc/src/common.cc +++ b/source/api_cc/src/common.cc @@ -3,6 +3,8 @@ #include +#include + #include "AtomMap.h" #include "device.h" #if defined(_WIN32) @@ -20,10 +22,13 @@ // not windows #include #endif +#ifdef BUILD_TENSORFLOW +#include "commonTF.h" #include "google/protobuf/io/zero_copy_stream_impl.h" #include "google/protobuf/text_format.h" using namespace tensorflow; +#endif static std::vector split(const std::string& input_, const std::string& delimiter) { @@ -300,12 +305,14 @@ void deepmd::NeighborListData::make_inlist(InputNlist& inlist) { inlist.firstneigh = &firstneigh[0]; } +#ifdef BUILD_TENSORFLOW void deepmd::check_status(const tensorflow::Status& status) { if (!status.ok()) { std::cout << status.ToString() << std::endl; throw deepmd::tf_exception(status.ToString()); } } +#endif void throw_env_not_set_warning(std::string env_name) { std::cerr << "DeePMD-kit WARNING: Environmental variable " << env_name @@ -345,6 +352,7 @@ void deepmd::get_env_nthreads(int& num_intra_nthreads, } void deepmd::load_op_library() { +#ifdef BUILD_TENSORFLOW tensorflow::Env* env = tensorflow::Env::Default(); #if defined(_WIN32) std::string dso_path = "deepmd_op.dll"; @@ -358,6 +366,7 @@ void deepmd::load_op_library() { dso_path + " is not found! You can add the library directory to LD_LIBRARY_PATH"); } +#endif } std::string deepmd::name_prefix(const std::string& scope) { @@ -368,6 +377,7 @@ std::string deepmd::name_prefix(const std::string& scope) { return prefix; } +#ifdef BUILD_TENSORFLOW template int deepmd::session_input_tensors( std::vector>& input_tensors, @@ -850,6 +860,7 @@ int deepmd::session_get_dtype(tensorflow::Session* session, // cast enum to int return (int)output_rc.dtype(); } +#endif template void deepmd::select_map(std::vector& out, @@ -940,6 +951,7 @@ void deepmd::select_map_inv(typename std::vector::iterator out, } } +#ifdef BUILD_TENSORFLOW template int deepmd::session_get_scalar(Session*, const std::string, const std::string); @@ -989,6 +1001,7 @@ template void deepmd::session_get_vector(std::vector&, Session*, const std::string, const std::string); +#endif template void deepmd::select_map(std::vector& out, const std::vector& in, @@ -1018,6 +1031,7 @@ template void deepmd::select_map_inv( const std::vector& idx_map, const int& stride); +#ifdef BUILD_TENSORFLOW template double deepmd::session_get_scalar(Session*, const std::string, const std::string); @@ -1026,6 +1040,7 @@ template void deepmd::session_get_vector(std::vector&, Session*, const std::string, const std::string); +#endif template void deepmd::select_map(std::vector& out, const std::vector& in, @@ -1055,6 +1070,7 @@ template void deepmd::select_map_inv( const std::vector& idx_map, const int& stride); +#ifdef BUILD_TENSORFLOW template deepmd::STRINGTYPE deepmd::session_get_scalar( Session*, const std::string, const std::string); @@ -1093,13 +1109,19 @@ template void deepmd::select_map_inv( const typename std::vector::const_iterator in, const std::vector& idx_map, const int& stride); +#endif void deepmd::read_file_to_string(std::string model, std::string& file_content) { +#ifdef BUILD_TENSORFLOW deepmd::check_status(tensorflow::ReadFileToString(tensorflow::Env::Default(), model, &file_content)); +#else + throw deepmd::deepmd_exception("TODO: read_file_to_string only support TF"); +#endif } void deepmd::convert_pbtxt_to_pb(std::string fn_pb_txt, std::string fn_pb) { +#ifdef BUILD_TENSORFLOW int fd = open(fn_pb_txt.c_str(), O_RDONLY); tensorflow::protobuf::io::ZeroCopyInputStream* input = new tensorflow::protobuf::io::FileInputStream(fd); @@ -1109,8 +1131,13 @@ void deepmd::convert_pbtxt_to_pb(std::string fn_pb_txt, std::string fn_pb) { std::fstream output(fn_pb, std::ios::out | std::ios::trunc | std::ios::binary); graph_def.SerializeToOstream(&output); +#else + throw deepmd::deepmd_exception( + "convert_pbtxt_to_pb: TensorFlow backend is not enabled."); +#endif } +#ifdef BUILD_TENSORFLOW template int deepmd::session_input_tensors( std::vector>& input_tensors, const std::vector& dcoord_, @@ -1272,6 +1299,7 @@ template int deepmd::session_input_tensors_mixed_type( const deepmd::AtomMap& atommap, const std::string scope, const bool aparam_nall); +#endif void deepmd::print_summary(const std::string& pre) { int num_intra_nthreads, num_inter_nthreads; @@ -1292,8 +1320,13 @@ void deepmd::print_summary(const std::string& pre) { std::cout << pre << "build variant: cpu" << "\n"; #endif +#ifdef BUILD_TENSORFLOW std::cout << pre << "build with tf inc: " + global_tf_include_dir << "\n"; std::cout << pre << "build with tf lib: " + global_tf_lib << "\n"; +#endif +#ifdef BUILD_PYTORCH + std::cout << pre << "build with pt lib: " + global_pt_lib << "\n"; +#endif std::cout << pre << "set tf intra_op_parallelism_threads: " << num_intra_nthreads << "\n"; diff --git a/source/install/build_cc.sh b/source/install/build_cc.sh index fef9e82ebc..83a586049d 100755 --- a/source/install/build_cc.sh +++ b/source/install/build_cc.sh @@ -20,7 +20,13 @@ NPROC=$(nproc --all) BUILD_TMP_DIR=${SCRIPT_PATH}/../build mkdir -p ${BUILD_TMP_DIR} cd ${BUILD_TMP_DIR} -cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} -DUSE_TF_PYTHON_LIBS=TRUE ${CUDA_ARGS} -DLAMMPS_VERSION=stable_2Aug2023_update2 .. +cmake -D ENABLE_TENSORFLOW=ON \ + -D ENABLE_PYTORCH=ON \ + -D CMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} \ + -D USE_TF_PYTHON_LIBS=TRUE \ + ${CUDA_ARGS} \ + -D LAMMPS_VERSION=stable_2Aug2023_update2 \ + .. cmake --build . -j${NPROC} cmake --install . diff --git a/source/install/docker_package_c.sh b/source/install/docker_package_c.sh index 544c175a0a..2a33ae78a4 100755 --- a/source/install/docker_package_c.sh +++ b/source/install/docker_package_c.sh @@ -2,10 +2,15 @@ set -e SCRIPT_PATH=$(dirname $(realpath -s $0)) +wget https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-2.1.2%2Bcpu.zip -O libtorch.zip +unzip libtorch.zip +mv libtorch ${SCRIPT_PATH}/../libtorch + docker run --rm -v ${SCRIPT_PATH}/../..:/root/deepmd-kit -w /root/deepmd-kit \ tensorflow/build:${TENSORFLOW_BUILD_VERSION:-2.15}-python3.11 \ /bin/sh -c "pip install \"tensorflow${TENSORFLOW_VERSION}\" cmake \ && cd /root/deepmd-kit/source/install \ && CC=/dt9/usr/bin/gcc \ CXX=/dt9/usr/bin/g++ \ + CMAKE_PREFIX_PATH=/root/deepmd-kit/source/libtorch \ /bin/sh package_c.sh" diff --git a/source/install/package_c.sh b/source/install/package_c.sh index 0c145b22af..272f6e6931 100755 --- a/source/install/package_c.sh +++ b/source/install/package_c.sh @@ -15,7 +15,10 @@ NPROC=$(nproc --all) BUILD_TMP_DIR=${SCRIPT_PATH}/../build_c mkdir -p ${BUILD_TMP_DIR} cd ${BUILD_TMP_DIR} -cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} \ +cmake \ + -D ENABLE_TENSORFLOW=ON \ + -D ENABLE_PYTORCH=ON \ + -D CMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} \ -DUSE_CUDA_TOOLKIT=TRUE \ -DPACKAGE_C=TRUE \ -DUSE_TF_PYTHON_LIBS=TRUE \ From 3912fd603e77f5c5463d4b8300f0609bd7590f43 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Tue, 23 Jan 2024 04:09:52 -0500 Subject: [PATCH 2/8] fix build_cc action Signed-off-by: Jinzhe Zeng --- .github/workflows/build_cc.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/build_cc.yml b/.github/workflows/build_cc.yml index 3b363911b9..2b39b40316 100644 --- a/.github/workflows/build_cc.yml +++ b/.github/workflows/build_cc.yml @@ -57,6 +57,7 @@ jobs: DP_VARIANT: ${{ matrix.dp_variant }} DOWNLOAD_TENSORFLOW: "FALSE" CMAKE_GENERATOR: Ninja + CMAKE_PREFIX_PATH: $GITHUB_WORKSPACE/libtorch if: matrix.variant != 'clang' - run: source/install/build_cc.sh env: From 7b90eee3aebf4bce1a2962b2c2a724e9b06aa887 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Tue, 23 Jan 2024 04:11:18 -0500 Subject: [PATCH 3/8] fix codeql Signed-off-by: Jinzhe Zeng --- .github/workflows/codeql.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index a9a162432c..3934199ad8 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -37,6 +37,8 @@ jobs: && sudo apt-get update \ && sudo apt-get -y install cuda-cudart-dev-12-2 cuda-nvcc-12-2 python -m pip install tensorflow + wget https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-2.1.2%2Bcpu.zip -O libtorch.zip + unzip libtorch.zip env: DEBIAN_FRONTEND: noninteractive # Initializes the CodeQL tools for scanning. @@ -50,6 +52,7 @@ jobs: env: DP_VARIANT: cuda DOWNLOAD_TENSORFLOW: "FALSE" + CMAKE_PREFIX_PATH: $GITHUB_WORKSPACE/libtorch if: matrix.language == 'c-cpp' - name: Perform CodeQL Analysis From 423e5c4a1e131127155f4b85206bc1cc51a10efd Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Tue, 23 Jan 2024 04:15:31 -0500 Subject: [PATCH 4/8] fix actions Signed-off-by: Jinzhe Zeng --- .github/workflows/build_cc.yml | 10 ++++++---- .github/workflows/codeql.yml | 5 +++-- .github/workflows/test_cc.yml | 5 +++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/.github/workflows/build_cc.yml b/.github/workflows/build_cc.yml index 2b39b40316..991be798aa 100644 --- a/.github/workflows/build_cc.yml +++ b/.github/workflows/build_cc.yml @@ -52,21 +52,23 @@ jobs: && sudo apt-get update \ && sudo apt-get install -y rocm-dev hipcub-dev if: matrix.variant == 'rocm' - - run: source/install/build_cc.sh + - run: | + export CMAKE_PREFIX_PATH=$GITHUB_WORKSPACE/libtorch + source/install/build_cc.sh env: DP_VARIANT: ${{ matrix.dp_variant }} DOWNLOAD_TENSORFLOW: "FALSE" CMAKE_GENERATOR: Ninja - CMAKE_PREFIX_PATH: $GITHUB_WORKSPACE/libtorch if: matrix.variant != 'clang' - - run: source/install/build_cc.sh + - run: | + export CMAKE_PREFIX_PATH=$GITHUB_WORKSPACE/libtorch + source/install/build_cc.sh env: DP_VARIANT: cpu DOWNLOAD_TENSORFLOW: "FALSE" CC: clang CXX: clang++ CMAKE_GENERATOR: Ninja - CMAKE_PREFIX_PATH: $GITHUB_WORKSPACE/libtorch if: matrix.variant == 'clang' - name: Test files exist run: | diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 3934199ad8..c5460109f4 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -48,11 +48,12 @@ jobs: languages: ${{ matrix.language }} queries: security-extended,security-and-quality - name: "Run, Build Application using script" - run: source/install/build_cc.sh + run: | + export CMAKE_PREFIX_PATH=$GITHUB_WORKSPACE/libtorch + source/install/build_cc.sh env: DP_VARIANT: cuda DOWNLOAD_TENSORFLOW: "FALSE" - CMAKE_PREFIX_PATH: $GITHUB_WORKSPACE/libtorch if: matrix.language == 'c-cpp' - name: Perform CodeQL Analysis diff --git a/.github/workflows/test_cc.yml b/.github/workflows/test_cc.yml index ca35f1e862..1ded666070 100644 --- a/.github/workflows/test_cc.yml +++ b/.github/workflows/test_cc.yml @@ -22,14 +22,15 @@ jobs: run: | wget https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-2.1.2%2Bcpu.zip -O libtorch.zip unzip libtorch.zip - - run: source/install/test_cc_local.sh + - run: | + export CMAKE_PREFIX_PATH=$GITHUB_WORKSPACE/libtorch + source/install/test_cc_local.sh env: OMP_NUM_THREADS: 1 TF_INTRA_OP_PARALLELISM_THREADS: 1 TF_INTER_OP_PARALLELISM_THREADS: 1 LMP_CXX11_ABI_0: 1 CMAKE_GENERATOR: Ninja - CMAKE_PREFIX_PATH: $GITHUB_WORKSPACE/libtorch # test lammps # ASE issue: https://gitlab.com/ase/ase/-/merge_requests/2843 # TODO: remove ase version when ase has new release From 486cca440e4302d9ad427aa2efa30591d712d11e Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Tue, 23 Jan 2024 04:26:14 -0500 Subject: [PATCH 5/8] revert packaging C library - not compatible Signed-off-by: Jinzhe Zeng --- source/install/docker_package_c.sh | 5 ----- source/install/package_c.sh | 5 +---- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/source/install/docker_package_c.sh b/source/install/docker_package_c.sh index 2a33ae78a4..544c175a0a 100755 --- a/source/install/docker_package_c.sh +++ b/source/install/docker_package_c.sh @@ -2,15 +2,10 @@ set -e SCRIPT_PATH=$(dirname $(realpath -s $0)) -wget https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-2.1.2%2Bcpu.zip -O libtorch.zip -unzip libtorch.zip -mv libtorch ${SCRIPT_PATH}/../libtorch - docker run --rm -v ${SCRIPT_PATH}/../..:/root/deepmd-kit -w /root/deepmd-kit \ tensorflow/build:${TENSORFLOW_BUILD_VERSION:-2.15}-python3.11 \ /bin/sh -c "pip install \"tensorflow${TENSORFLOW_VERSION}\" cmake \ && cd /root/deepmd-kit/source/install \ && CC=/dt9/usr/bin/gcc \ CXX=/dt9/usr/bin/g++ \ - CMAKE_PREFIX_PATH=/root/deepmd-kit/source/libtorch \ /bin/sh package_c.sh" diff --git a/source/install/package_c.sh b/source/install/package_c.sh index 272f6e6931..0c145b22af 100755 --- a/source/install/package_c.sh +++ b/source/install/package_c.sh @@ -15,10 +15,7 @@ NPROC=$(nproc --all) BUILD_TMP_DIR=${SCRIPT_PATH}/../build_c mkdir -p ${BUILD_TMP_DIR} cd ${BUILD_TMP_DIR} -cmake \ - -D ENABLE_TENSORFLOW=ON \ - -D ENABLE_PYTORCH=ON \ - -D CMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} \ +cmake -DCMAKE_INSTALL_PREFIX=${INSTALL_PREFIX} \ -DUSE_CUDA_TOOLKIT=TRUE \ -DPACKAGE_C=TRUE \ -DUSE_TF_PYTHON_LIBS=TRUE \ From 4e47e283d6d1f7a0ccfb3920e0dedfe1ecadbca4 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Tue, 23 Jan 2024 04:36:41 -0500 Subject: [PATCH 6/8] fix when building from DEEPMD_C_ROOT Signed-off-by: Jinzhe Zeng --- source/CMakeLists.txt | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 5c3b5bf768..c273bc9263 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -157,15 +157,17 @@ if(ENABLE_PYTORCH AND NOT DEEPMD_C_ROOT) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}") endif() # log enabled backends -message(STATUS "Enabled backends:") -if(ENABLE_TENSORFLOW) - message(STATUS "- TensorFlow") -endif() -if(ENABLE_PYTORCH) - message(STATUS "- PyTorch") -endif() -if(NOT ENABLE_TENSORFLOW AND NOT ENABLE_PYTORCH) - message(FATAL_ERROR "No backend is enabled.") +if(NOT DEEPMD_C_ROOT) + message(STATUS "Enabled backends:") + if(ENABLE_TENSORFLOW) + message(STATUS "- TensorFlow") + endif() + if(ENABLE_PYTORCH) + message(STATUS "- PyTorch") + endif() + if(NOT ENABLE_TENSORFLOW AND NOT ENABLE_PYTORCH) + message(FATAL_ERROR "No backend is enabled.") + endif() endif() # find threads From 0a6a412d94e9cdae9b2f8e685fc3e00a967731c5 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Tue, 23 Jan 2024 16:49:25 -0500 Subject: [PATCH 7/8] add a test to make sure api_cc can compile without any flag Signed-off-by: Jinzhe Zeng --- source/api_cc/CMakeLists.txt | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/source/api_cc/CMakeLists.txt b/source/api_cc/CMakeLists.txt index 35a1f545dc..cd42594f1e 100644 --- a/source/api_cc/CMakeLists.txt +++ b/source/api_cc/CMakeLists.txt @@ -63,3 +63,14 @@ ${CMAKE_INSTALL_PREFIX}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}${libname}${LOW_PREC_V add_subdirectory(tests) endif() endif(BUILD_PY_IF) + +if(BUILD_TESTING) + # A compilation test to make sure api_cc can compile without any backend + add_library(deepmd_cc_test_no_backend SHARED ${LIB_SRC}) + target_link_libraries(deepmd_cc_test_no_backend PUBLIC ${LIB_DEEPMD}) + target_include_directories( + deepmd_cc_test_no_backend + PUBLIC $ + $ + $) +endif() From 82e0676ddacc061fc98b8bf9be1bf68ed3ff3709 Mon Sep 17 00:00:00 2001 From: Jinzhe Zeng Date: Tue, 23 Jan 2024 17:12:16 -0500 Subject: [PATCH 8/8] set flags to docs Signed-off-by: Jinzhe Zeng --- doc/conf.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/doc/conf.py b/doc/conf.py index 63af974a86..261c105d9b 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -213,7 +213,10 @@ def setup(app): exhale_projects_args = { "cc": { "containmentFolder": "./API_CC", - "exhaleDoxygenStdin": "INPUT = ../source/api_cc/include/", + "exhaleDoxygenStdin": """INPUT = ../source/api_cc/include/ + PREDEFINED += BUILD_TENSORFLOW + BUILD_PYTORCH + """, "rootFileTitle": "C++ API", "rootFileName": "api_cc.rst", },