diff --git a/doc/install/install-from-source.md b/doc/install/install-from-source.md index cecce0f3cd..81a7520653 100644 --- a/doc/install/install-from-source.md +++ b/doc/install/install-from-source.md @@ -180,6 +180,8 @@ One may add the following arguments to `cmake`: | -DROCM_ROOT=<value> | Path | Detected automatically | The path to the ROCM toolkit directory. | | -DLAMMPS_VERSION_NUMBER=<value> | Number | `20220723` | Only neccessary for LAMMPS built-in mode. The version number of LAMMPS (yyyymmdd). LAMMPS 29Oct2020 (20201029) or later is supported. | | -DLAMMPS_SOURCE_ROOT=<value> | Path | - | Only neccessary for LAMMPS plugin mode. The path to the [LAMMPS source code](install-lammps.md). LAMMPS 8Apr2021 or later is supported. If not assigned, the plugin mode will not be enabled. | +| -DUSE_TF_PYTHON_LIBS=<value> | `TRUE` or `FALSE` | `FALSE` | If `TRUE`, Build C++ interface with TensorFlow's Python libraries(TensorFlow's Python Interface is required). And there's no need for building TensorFlow's C++ interface.| + If the cmake has been executed successfully, then run the following make commands to build the package: ```bash diff --git a/setup.py b/setup.py index 01af95ee22..ef617857c3 100644 --- a/setup.py +++ b/setup.py @@ -83,13 +83,13 @@ # setuptools will re-find tensorflow after installing setup_requires tf_install_dir = None -# add cmake as a build requirement if cmake>3.7 is not installed +# add cmake as a build requirement if cmake>3.12 is not installed try: cmake_version = get_cmake_version() except SKBuildError: setup_requires.append("cmake") else: - if cmake_version in SpecifierSet("<3.7"): + if cmake_version in SpecifierSet("<3.12"): setup_requires.append("cmake") Path("deepmd").mkdir(exist_ok=True) diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 8680931299..f068649fd4 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.7) +cmake_minimum_required(VERSION 3.12) project(DeePMD) # build cpp or python interfaces @@ -74,6 +74,12 @@ else() message(STATUS "Will not build AMD GPU support") endif (USE_ROCM_TOOLKIT) +set(DEEPMD_SOURCE_DIR ${PROJECT_SOURCE_DIR}/..) + +# setup tensorflow libraries by python +if (USE_TF_PYTHON_LIBS) + find_package (Python COMPONENTS Interpreter Development REQUIRED) +endif(USE_TF_PYTHON_LIBS) # find tensorflow, I need tf abi info find_package(tensorflow REQUIRED) @@ -142,6 +148,9 @@ else () message (STATUS "Set GLIBCXX_USE_CXX_ABI=1 when compiling ops") endif () +# set _GLIBCXX_USE_CXX11_ABI flag globally +add_definitions(-D_GLIBCXX_USE_CXX11_ABI=${OP_CXX_ABI}) + # define USE_TTM if (NOT DEFINED USE_TTM) set(USE_TTM FALSE) @@ -201,7 +210,7 @@ if (BUILD_CPP_IF) add_subdirectory (api_cc/) add_subdirectory (lmp/) if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.8) -# add_subdirectory (md/) + # add_subdirectory (md/) add_subdirectory (ipi/) add_subdirectory (gmx/) endif () diff --git a/source/api_cc/CMakeLists.txt b/source/api_cc/CMakeLists.txt index e244c294ec..6eb70c04b6 100644 --- a/source/api_cc/CMakeLists.txt +++ b/source/api_cc/CMakeLists.txt @@ -20,6 +20,11 @@ target_link_libraries (${libname} PUBLIC ${LIB_DEEPMD} ${TensorFlow_LIBRARY} ${T target_include_directories(${libname} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_BINARY_DIR}) target_include_directories(${libname} PRIVATE ${TensorFlow_INCLUDE_DIRS}) +# link: libpython3.x.so +if (USE_TF_PYTHON_LIBS) + target_link_libraries (${libname} PUBLIC ${Python_LIBRARIES}) +endif(USE_TF_PYTHON_LIBS) + set_target_properties( ${libname} PROPERTIES diff --git a/source/api_cc/tests/CMakeLists.txt b/source/api_cc/tests/CMakeLists.txt index b4ac448f9c..5f2d8b3df3 100644 --- a/source/api_cc/tests/CMakeLists.txt +++ b/source/api_cc/tests/CMakeLists.txt @@ -8,6 +8,40 @@ add_definitions ("-DHIGH_PREC") enable_testing() +set(DEEPMD_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../../..) +list (APPEND CMAKE_MODULE_PATH ${DEEPMD_SOURCE_DIR}/source/cmake/) + +# setup tensorflow libraries by python +if (USE_TF_PYTHON_LIBS) + set(DEEPMD_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../../..) + find_package (Python COMPONENTS Interpreter Development REQUIRED) + # find tensorflow, I need tf abi info + find_package(tensorflow REQUIRED) + # auto op_cxx_abi + try_run( + CPP_CXX_ABI_RUN_RESULT_VAR CPP_CXX_ABI_COMPILE_RESULT_VAR + ${CMAKE_CURRENT_BINARY_DIR}/tf_cxx_abi + "${DEEPMD_SOURCE_DIR}/source/cmake/tf_cxx_abi.cpp" + LINK_LIBRARIES ${TensorFlowFramework_LIBRARY} + CMAKE_FLAGS "-DINCLUDE_DIRECTORIES:STRING=${TensorFlow_INCLUDE_DIRS}" + RUN_OUTPUT_VARIABLE CPP_CXX_ABI + COMPILE_OUTPUT_VARIABLE CPP_CXX_ABI_COMPILE_OUTPUT_VAR + ) + if (NOT ${CPP_CXX_ABI_COMPILE_RESULT_VAR}) + message(FATAL_ERROR "Failed to compile: \n ${CPP_CXX_ABI_COMPILE_OUTPUT_VAR}" ) + endif() + if (NOT ${CPP_CXX_ABI_RUN_RESULT_VAR} EQUAL "0") + message(FATAL_ERROR "Failed to run, return code: ${CPP_CXX_ABI}" ) + endif() + if (DEFINED PY_CXX_ABI) + if (NOT (${CPP_CXX_ABI} EQUAL ${PY_CXX_ABI})) + message (WARNNING "NOT consistent CXX_ABIs: python interface of tf uses ${PY_CXX_ABI}, while c++ interface of tf uses ${CPP_CXX_ABI}, we follow c++ interface ") + endif() + endif() + set(OP_CXX_ABI ${CPP_CXX_ABI}) + message (STATUS "Automatically determined OP_CXX_ABI=${OP_CXX_ABI} ") + add_definitions(-D_GLIBCXX_USE_CXX11_ABI=${OP_CXX_ABI}) +endif(USE_TF_PYTHON_LIBS) # model version file(READ ${PROJECT_SOURCE_DIR}/../../config/MODEL_VER MODEL_VERSION) @@ -36,12 +70,15 @@ configure_file( @ONLY ) +if (USE_TF_PYTHON_LIBS) + target_link_libraries (${apiname} PUBLIC ${Python_LIBRARIES}) +endif() + set(opname "deepmd_op") set(OP_BASE_DIR ${CMAKE_SOURCE_DIR}/../../op) # file(GLOB OP_SRC ${OP_BASE_DIR}/*.cc) file(GLOB OP_SRC ${OP_BASE_DIR}/custom_op.cc ${OP_BASE_DIR}/prod_force.cc ${OP_BASE_DIR}/prod_virial.cc ${OP_BASE_DIR}/descrpt.cc ${OP_BASE_DIR}/descrpt_se_a_ef.cc ${OP_BASE_DIR}/descrpt_se_a_ef.cc ${OP_BASE_DIR}/descrpt_se_a_ef_para.cc ${OP_BASE_DIR}/descrpt_se_a_ef_vert.cc ${OP_BASE_DIR}/pair_tab.cc ${OP_BASE_DIR}/prod_force_multi_device.cc ${OP_BASE_DIR}/prod_virial_multi_device.cc ${OP_BASE_DIR}/soft_min.cc ${OP_BASE_DIR}/soft_min_force.cc ${OP_BASE_DIR}/soft_min_virial.cc ${OP_BASE_DIR}/ewald_recp.cc ${OP_BASE_DIR}/gelu_multi_device.cc ${OP_BASE_DIR}/map_aparam.cc ${OP_BASE_DIR}/neighbor_stat.cc ${OP_BASE_DIR}/unaggregated_grad.cc ${OP_BASE_DIR}/tabulate_multi_device.cc ${OP_BASE_DIR}/prod_env_mat_multi_device.cc) -list (APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/../../cmake/) find_package(tensorflow REQUIRED) if (TENSORFLOW_VERSION GREATER_EQUAL 2.7) set (CMAKE_CXX_STANDARD 14) @@ -71,6 +108,7 @@ if (USE_CUDA_TOOLKIT) message(STATUS "Found CUDA in ${CUDA_TOOLKIT_ROOT_DIR}, build nv GPU support") include_directories(${CUDA_INCLUDE_DIRS}) add_subdirectory(${LIB_BASE_DIR}/src/cuda cuda_binary_dir) + target_link_libraries(${opname} PUBLIC deepmd_op_cuda) else() message(STATUS "Will not build nv GPU support") endif(USE_CUDA_TOOLKIT) @@ -82,6 +120,7 @@ if (USE_ROCM_TOOLKIT) add_compile_definitions(__HIP_PLATFORM_HCC__) include_directories(${ROCM_INCLUDE_DIRS}) add_subdirectory(${LIB_BASE_DIR}/src/rocm rocm_binary_dir) + target_link_libraries(${opname} PUBLIC deepmd_op_rocm) else() message(STATUS "Will not build AMD GPU support") endif (USE_ROCM_TOOLKIT) @@ -101,13 +140,7 @@ else() target_link_libraries(coverage_config INTERFACE --coverage) endif() -if (USE_CUDA_TOOLKIT) - target_link_libraries(runUnitTests gtest gtest_main ${libname} ${apiname} pthread ${TensorFlow_LIBRARY} rt deepmd_op_cuda coverage_config) -elseif(USE_ROCM_TOOLKIT) - target_link_libraries(runUnitTests gtest gtest_main ${libname} ${apiname} pthread ${TensorFlow_LIBRARY} rt deepmd_op_rocm coverage_config) -else() - target_link_libraries(runUnitTests gtest gtest_main ${libname} ${apiname} pthread ${TensorFlow_LIBRARY} rt coverage_config) -endif() +target_link_libraries(runUnitTests gtest gtest_main ${libname} ${apiname} pthread ${TensorFlow_LIBRARY} rt coverage_config) add_test( runUnitTests runUnitTests ) diff --git a/source/cmake/Findtensorflow.cmake b/source/cmake/Findtensorflow.cmake index 87e9918463..7c8de58e45 100644 --- a/source/cmake/Findtensorflow.cmake +++ b/source/cmake/Findtensorflow.cmake @@ -29,6 +29,18 @@ if (BUILD_CPP_IF AND INSTALL_TENSORFLOW) ) endif () +if (BUILD_CPP_IF AND USE_TF_PYTHON_LIBS) + # Here we try to install libtensorflow_cc.so as well as libtensorflow_framework.so using libs within the python site-package tensorflow folder. + + if (NOT DEFINED TENSORFLOW_ROOT) + set (TENSORFLOW_ROOT ${CMAKE_INSTALL_PREFIX}) + endif () + # execute install script + execute_process( + COMMAND sh ${DEEPMD_SOURCE_DIR}/source/install/install_tf.sh ${Python_SITELIB} ${TENSORFLOW_ROOT} + ) +endif () + if(DEFINED TENSORFLOW_ROOT) string(REPLACE "lib64" "lib" TENSORFLOW_ROOT_NO64 ${TENSORFLOW_ROOT}) endif(DEFINED TENSORFLOW_ROOT) diff --git a/source/install/install_tf.sh b/source/install/install_tf.sh new file mode 100755 index 0000000000..9dd9db6727 --- /dev/null +++ b/source/install/install_tf.sh @@ -0,0 +1,39 @@ +set -e + +SCRIPT_PATH=$(dirname $(realpath -s $0)) +if [ ! $# -eq 2 ];then + echo "${SCRIPT_PATH}: Params error, installation of tensorflow libraries failed!" + exit 1 +fi + +PYTHON_SITE_PACKAGE_PATH=$(realpath -s $1) +TENSORFLOW_ROOT=$(realpath -s $2) +TF_INSTALL_PATH=${PYTHON_SITE_PACKAGE_PATH}/tensorflow + +if [ ! -d ${TF_INSTALL_PATH} ];then + echo "${SCRIPT_PATH}: ${TF_INSTALL_PATH}, TensorFlow not found!" + exit 1 +fi + +#---------------------------------------- +# check if the installation folders exist +#---------------------------------------- +if [ ! -d ${TENSORFLOW_ROOT} ];then + mkdir ${TENSORFLOW_ROOT} +fi +if [ ! -d ${TENSORFLOW_ROOT}/include ];then + mkdir ${TENSORFLOW_ROOT}/include +fi +if [ ! -d ${TENSORFLOW_ROOT}/lib ];then + mkdir ${TENSORFLOW_ROOT}/lib +fi + +#---------------------------------------- +# install the TF libraries +#---------------------------------------- +cp -r ${TF_INSTALL_PATH}/include ${TENSORFLOW_ROOT} +cp ${TF_INSTALL_PATH}/libtensorflow_framework.so* ${TENSORFLOW_ROOT}/lib +cp ${TF_INSTALL_PATH}/python/_pywrap_tensorflow_internal.so ${TENSORFLOW_ROOT}/lib +ln -s ${TENSORFLOW_ROOT}/lib/libtensorflow_framework.so* ${TENSORFLOW_ROOT}/lib/libtensorflow_framework.so +ln -s ${TENSORFLOW_ROOT}/lib/_pywrap_tensorflow_internal.so ${TENSORFLOW_ROOT}/lib/libtensorflow_cc.so + diff --git a/source/lmp/CMakeLists.txt b/source/lmp/CMakeLists.txt index dbf259800a..3f31aef793 100644 --- a/source/lmp/CMakeLists.txt +++ b/source/lmp/CMakeLists.txt @@ -15,7 +15,11 @@ list (APPEND LMP_INSTALL_FILES ${LMP_SRC}) list (APPEND LMP_INSTALL_FILES ${LMP_SHSCRIPT}) function(_add_lmp_variant variant_name prec_def) -configure_file("env.sh.in" "env${variant_name}.sh" @ONLY) +if (USE_TF_PYTHON_LIBS) + configure_file("env_py.sh.in" "env${variant_name}.sh" @ONLY) +else() + configure_file("env.sh.in" "env${variant_name}.sh" @ONLY) +endif() endfunction() _add_lmp_variant("${HIGH_PREC_VARIANT}" "${HIGH_PREC_DEF}") _add_lmp_variant("${LOW_PREC_VARIANT}" "${LOW_PREC_DEF}") diff --git a/source/lmp/env_py.sh.in b/source/lmp/env_py.sh.in new file mode 100644 index 0000000000..59ccc1129b --- /dev/null +++ b/source/lmp/env_py.sh.in @@ -0,0 +1,13 @@ +DEEPMD_ROOT=@CMAKE_INSTALL_PREFIX@ +TENSORFLOW_INCLUDE_DIRS="@TensorFlow_INCLUDE_DIRS@" +TENSORFLOW_LIBRARY_PATH="@TensorFlow_LIBRARY_PATH@" +PYTHON_LIBRARY_PATH="@Python_LIBRARY_DIRS@" + +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"` +PYTHON_RPATH=`echo $PYTHON_LIBRARY_PATH | sed "s/;/ -Wl,-rpath=/g"` + +NNP_INC=" -D_GLIBCXX_USE_CXX11_ABI=@OP_CXX_ABI@ -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_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 -lpython@Python_VERSION_MAJOR@.@Python_VERSION_MINOR@ -Wl,-rpath=$TF_RPATH -Wl,-rpath=$DEEPMD_ROOT/lib -Wl,-rpath=$PYTHON_RPATH " diff --git a/source/op/CMakeLists.txt b/source/op/CMakeLists.txt index ffae97a506..ddcd833b55 100644 --- a/source/op/CMakeLists.txt +++ b/source/op/CMakeLists.txt @@ -2,7 +2,6 @@ set(OP_LIB ${PROJECT_SOURCE_DIR}/lib/src/SimulationRegion.cpp ${PROJECT_SOURCE_DIR}/lib/src/neighbor_list.cc) -set (OP_CXX_FLAG -D_GLIBCXX_USE_CXX11_ABI=${OP_CXX_ABI} ) file(GLOB OP_SRC prod_env_mat_multi_device_nvnmd.cc map_nvnmd.cc matmul_nvnmd.cc quantize_nvnmd.cc tanh2_nvnmd.cc tanh4_nvnmd.cc custom_op.cc prod_force.cc prod_virial.cc descrpt.cc descrpt_se_a_ef.cc descrpt_se_a_ef.cc descrpt_se_a_ef_para.cc descrpt_se_a_ef_vert.cc pair_tab.cc prod_force_multi_device.cc prod_virial_multi_device.cc soft_min.cc soft_min_force.cc soft_min_virial.cc ewald_recp.cc gelu_multi_device.cc map_aparam.cc neighbor_stat.cc unaggregated_grad.cc tabulate_multi_device.cc prod_env_mat_multi_device.cc) file(GLOB OP_GRADS_SRC custom_op.cc prod_force_grad.cc prod_force_grad_multi_device.cc prod_virial_grad.cc prod_virial_grad_multi_device.cc soft_min_force_grad.cc soft_min_virial_grad.cc ) file(GLOB OP_PY *.py) @@ -37,26 +36,22 @@ if (BUILD_PY_IF) set_target_properties( op_abi PROPERTIES - COMPILE_FLAGS ${OP_CXX_FLAG} INSTALL_RPATH @loader_path ) set_target_properties( op_grads PROPERTIES - COMPILE_FLAGS ${OP_CXX_FLAG} INSTALL_RPATH @loader_path ) else() set_target_properties( op_abi PROPERTIES - COMPILE_FLAGS ${OP_CXX_FLAG} INSTALL_RPATH $ORIGIN ) set_target_properties( op_grads PROPERTIES - COMPILE_FLAGS ${OP_CXX_FLAG} INSTALL_RPATH $ORIGIN ) endif ()