Skip to content

Commit

Permalink
There's no need for building libtensorflow_cc.so anymore (#1744)
Browse files Browse the repository at this point in the history
* There's no need for building libtensorflow_cc.so anymore

* update symbolic link

* Update source/cmake/Findtensorflow.cmake

Co-authored-by: Jinzhe Zeng <[email protected]>

* add support for unit-test

* Update install-from-source.md

* bump the cmake version at setup.py

* set _GLIBCXX_USE_CXX11_ABI flag globally

Co-authored-by: Jinzhe Zeng <[email protected]>
  • Loading branch information
denghuilu and njzjz authored Jul 1, 2022
1 parent be7935d commit 2ea4c78
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 18 deletions.
2 changes: 2 additions & 0 deletions doc/install/install-from-source.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ One may add the following arguments to `cmake`:
| -DROCM_ROOT=&lt;value&gt; | Path | Detected automatically | The path to the ROCM toolkit directory. |
| -DLAMMPS_VERSION_NUMBER=&lt;value&gt; | 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=&lt;value&gt; | 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=&lt;value&gt; | `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
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 11 additions & 2 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.7)
cmake_minimum_required(VERSION 3.12)
project(DeePMD)

# build cpp or python interfaces
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 ()
Expand Down
5 changes: 5 additions & 0 deletions source/api_cc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 41 additions & 8 deletions source/api_cc/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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 )

Expand Down
12 changes: 12 additions & 0 deletions source/cmake/Findtensorflow.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
39 changes: 39 additions & 0 deletions source/install/install_tf.sh
Original file line number Diff line number Diff line change
@@ -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

6 changes: 5 additions & 1 deletion source/lmp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
13 changes: 13 additions & 0 deletions source/lmp/env_py.sh.in
Original file line number Diff line number Diff line change
@@ -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 "
5 changes: 0 additions & 5 deletions source/op/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 ()
Expand Down

0 comments on commit 2ea4c78

Please sign in to comment.