Skip to content

Commit

Permalink
fix: set rpath for libtorch and protobuf (deepmodeling#3636)
Browse files Browse the repository at this point in the history
Fix deepmodeling#3568.

Set run path for libtorch and protobuf. Refactor the original
`TensorFlow_LIBRARY_PATH` to `BACKEND_LIBRARY_PATH` (list).

---------

Signed-off-by: Jinzhe Zeng <jinzhe.zeng@rutgers.edu>
  • Loading branch information
njzjz authored Apr 2, 2024
1 parent 9808898 commit 4c546d0
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 20 deletions.
13 changes: 12 additions & 1 deletion source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ endif()
# find tensorflow, I need tf abi info
if(ENABLE_TENSORFLOW AND NOT DEEPMD_C_ROOT)
find_package(tensorflow REQUIRED)
list(APPEND BACKEND_LIBRARY_PATH ${TensorFlow_LIBRARY_PATH})
list(APPEND BACKEND_INCLUDE_DIRS ${TENSORFLOW_INCLUDE_DIRS})
endif()
if(BUILD_CPP_IF
AND USE_PT_PYTHON_LIBS
Expand All @@ -174,7 +176,10 @@ if(BUILD_CPP_IF
"Cannot determine PyTorch CMake prefix path, error code: $PYTORCH_CMAKE_PREFIX_PATH_RESULT_VAR}, error message: ${PYTORCH_CMAKE_PREFIX_PATH_ERROR_VAR}"
)
endif()
list(APPEND CMAKE_PREFIX_PATH ${PYTORCH_CMAKE_PREFIX_PATH})
list(APPEND BACKEND_LIBRARY_PATH ${TensorFlow_LIBRARY_PATH})
if(Protobuf_LIBRARY_DIRECTORY)
list(APPEND BACKEND_LIBRARY_PATH ${Protobuf_LIBRARY_DIRECTORY})
endif()
endif()
if(ENABLE_PYTORCH AND NOT DEEPMD_C_ROOT)
find_package(Torch REQUIRED)
Expand All @@ -194,6 +199,10 @@ if(ENABLE_PYTORCH AND NOT DEEPMD_C_ROOT)
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=${OP_CXX_ABI})
endif()
endif()
# get torch directory
set(PyTorch_LIBRARY_PATH ${TORCH_INCLUDE_DIRS}/../lib)
list(APPEND BACKEND_LIBRARY_PATH ${PyTorch_LIBRARY_PATH})
list(APPEND BACKEND_INCLUDE_DIRS ${TORCH_INCLUDE_DIRS})
endif()
# log enabled backends
if(NOT DEEPMD_C_ROOT)
Expand Down Expand Up @@ -297,7 +306,9 @@ if(DEEPMD_C_ROOT)
# use variable for TF path to set deepmd_c path
set(TENSORFLOW_ROOT "${DEEPMD_C_ROOT}")
set(TensorFlow_LIBRARY_PATH "${DEEPMD_C_ROOT}/lib")
set(BACKEND_LIBRARY_PATH "${DEEPMD_C_ROOT}/lib")
set(TENSORFLOW_INCLUDE_DIRS "${DEEPMD_C_ROOT}/include")
set(BACKEND_INCLUDE_DIRS "${DEEPMD_C_ROOT}/include")
set(TORCH_LIBRARIES "${DEEPMD_C_ROOT}/lib/libtorch.so")
endif()

Expand Down
4 changes: 2 additions & 2 deletions source/api_c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ target_include_directories(
${libname} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)

set_target_properties(
${libname} PROPERTIES INSTALL_RPATH "$ORIGIN;${TensorFlow_LIBRARY_PATH}")
set_target_properties(${libname} PROPERTIES INSTALL_RPATH
"$ORIGIN;${BACKEND_LIBRARY_PATH}")
if(CMAKE_TESTING_ENABLED)
target_link_libraries(${libname} PRIVATE coverage_config)
endif()
Expand Down
2 changes: 1 addition & 1 deletion source/api_c/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project(deepmd_api_c_test)
file(GLOB TEST_SRC test_*.cc)

set_target_properties(
${LIB_DEEPMD_C} PROPERTIES INSTALL_RPATH "$ORIGIN;${TensorFlow_LIBRARY_PATH}")
${LIB_DEEPMD_C} PROPERTIES INSTALL_RPATH "$ORIGIN;${BACKEND_LIBRARY_PATH}")

add_executable(runUnitTests_c ${TEST_SRC})
target_link_libraries(runUnitTests_c PRIVATE GTest::gtest_main ${LIB_DEEPMD_C}
Expand Down
2 changes: 1 addition & 1 deletion source/api_cc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ endif()

set_target_properties(
${libname}
PROPERTIES INSTALL_RPATH "$ORIGIN;${TensorFlow_LIBRARY_PATH}"
PROPERTIES INSTALL_RPATH "$ORIGIN;${BACKEND_LIBRARY_PATH}"
INSTALL_RPATH_USE_LINK_PATH TRUE
BUILD_RPATH "$ORIGIN/../op/tf;$ORIGIN/../op/pt")
target_compile_definitions(${libname} PRIVATE TF_PRIVATE)
Expand Down
2 changes: 1 addition & 1 deletion source/gmx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ target_include_directories(${libgmxname}
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../3rdparty/)

set_target_properties(
${libgmxname} PROPERTIES INSTALL_RPATH "$ORIGIN;${TensorFlow_LIBRARY_PATH}")
${libgmxname} PROPERTIES INSTALL_RPATH "$ORIGIN;${BACKEND_LIBRARY_PATH}")

install(
FILES dp_gmx_patch
Expand Down
4 changes: 2 additions & 2 deletions source/ipi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ if(APPLE)
${ipiname}
PROPERTIES LINK_FLAGS "${extra_link_flags}"
INSTALL_RPATH
"@loader_path/../${LIB_DIR}:${TensorFlow_LIBRARY_PATH}")
"@loader_path/../${LIB_DIR};${BACKEND_LIBRARY_PATH}")
else()
set_target_properties(
${ipiname}
PROPERTIES LINK_FLAGS
"-Wl,-rpath,'$ORIGIN'/../${LIB_DIR} ${extra_link_flags}"
INSTALL_RPATH "$ORIGIN/../${LIB_DIR}:${TensorFlow_LIBRARY_PATH}")
INSTALL_RPATH "$ORIGIN/../${LIB_DIR};${BACKEND_LIBRARY_PATH}")
endif()

if(CMAKE_TESTING_ENABLED)
Expand Down
4 changes: 2 additions & 2 deletions source/lmp/env.sh.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DEEPMD_ROOT=@CMAKE_INSTALL_PREFIX@
TENSORFLOW_INCLUDE_DIRS="@TensorFlow_INCLUDE_DIRS@"
TENSORFLOW_LIBRARY_PATH="@TensorFlow_LIBRARY_PATH@"
TENSORFLOW_INCLUDE_DIRS="@BACKEND_INCLUDE_DIRS@"
TENSORFLOW_LIBRARY_PATH="@BACKEND_LIBRARY_PATH@"

TF_INCLUDE_DIRS=`echo $TENSORFLOW_INCLUDE_DIRS | sed "s/;/ -I/g"`
TF_LIBRARY_PATH=`echo $TENSORFLOW_LIBRARY_PATH | sed "s/;/ -L/g"`
Expand Down
4 changes: 2 additions & 2 deletions source/lmp/env_c.sh.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DEEPMD_ROOT=@CMAKE_INSTALL_PREFIX@
TENSORFLOW_INCLUDE_DIRS="@TensorFlow_INCLUDE_DIRS@"
TENSORFLOW_LIBRARY_PATH="@TensorFlow_LIBRARY_PATH@"
TENSORFLOW_INCLUDE_DIRS="@BACKEND_INCLUDE_DIRS@"
TENSORFLOW_LIBRARY_PATH="@BACKEND_LIBRARY_PATH@"

TF_INCLUDE_DIRS=`echo $TENSORFLOW_INCLUDE_DIRS | sed "s/;/ -I/g"`
TF_LIBRARY_PATH=`echo $TENSORFLOW_LIBRARY_PATH | sed "s/;/ -L/g"`
Expand Down
4 changes: 2 additions & 2 deletions source/lmp/env_py.sh.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DEEPMD_ROOT=@CMAKE_INSTALL_PREFIX@
TENSORFLOW_INCLUDE_DIRS="@TensorFlow_INCLUDE_DIRS@"
TENSORFLOW_LIBRARY_PATH="@TensorFlow_LIBRARY_PATH@"
TENSORFLOW_INCLUDE_DIRS="@BACKEND_INCLUDE_DIRS@"
TENSORFLOW_LIBRARY_PATH="@BACKEND_LIBRARY_PATH@"
PYTHON_LIBRARY_PATH="@Python_LIBRARY_DIRS@"

TF_INCLUDE_DIRS=`echo $TENSORFLOW_INCLUDE_DIRS | sed "s/;/ -I/g"`
Expand Down
4 changes: 2 additions & 2 deletions source/lmp/env_py_c.sh.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DEEPMD_ROOT=@CMAKE_INSTALL_PREFIX@
TENSORFLOW_INCLUDE_DIRS="@TensorFlow_INCLUDE_DIRS@"
TENSORFLOW_LIBRARY_PATH="@TensorFlow_LIBRARY_PATH@"
TENSORFLOW_INCLUDE_DIRS="@BACKEND_INCLUDE_DIRS@"
TENSORFLOW_LIBRARY_PATH="@BACKEND_LIBRARY_PATH@"
PYTHON_LIBRARY_PATH="@Python_LIBRARY_DIRS@"

TF_INCLUDE_DIRS=`echo $TENSORFLOW_INCLUDE_DIRS | sed "s/;/ -I/g"`
Expand Down
2 changes: 1 addition & 1 deletion source/lmp/plugin/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ if(DEFINED LAMMPS_SOURCE_ROOT OR DEFINED LAMMPS_VERSION)
"-Wl,-undefined,dynamic_lookup")
else()
set_target_properties(
${libname} PROPERTIES INSTALL_RPATH "$ORIGIN;${TensorFlow_LIBRARY_PATH}"
${libname} PROPERTIES INSTALL_RPATH "$ORIGIN;${BACKEND_LIBRARY_PATH}"
LINK_FLAGS "-rdynamic")
endif()
target_compile_definitions(
Expand Down
5 changes: 2 additions & 3 deletions source/op/tf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,10 @@ endif()
if(APPLE)
set_target_properties(
${LIB_DEEPMD_OP} PROPERTIES INSTALL_RPATH
"@loader_path;${TensorFlow_LIBRARY_PATH}")
"@loader_path;${BACKEND_LIBRARY_PATH}")
else()
set_target_properties(
${LIB_DEEPMD_OP} PROPERTIES INSTALL_RPATH
"$ORIGIN;${TensorFlow_LIBRARY_PATH}")
${LIB_DEEPMD_OP} PROPERTIES INSTALL_RPATH "$ORIGIN;${BACKEND_LIBRARY_PATH}")
endif()
if(CMAKE_TESTING_ENABLED)
target_link_libraries(${LIB_DEEPMD_OP} PRIVATE coverage_config)
Expand Down

0 comments on commit 4c546d0

Please sign in to comment.