From cdcc0013194e6824e0bba84ee48dafd66e6381ef Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Mon, 26 Sep 2022 22:06:18 +0800 Subject: [PATCH] Fix libpaddle soname mismatch error (#46344) * fix libpaddle soname mismatch error * fix windows failed * polish linux and windows make impl * unify winddows lib name * fix windows error * revert copy dst change * revert naming change * revert windows change * fix gpups compile failed --- paddle/fluid/pybind/CMakeLists.txt | 24 ++++++++++++++++-------- python/CMakeLists.txt | 5 ++--- python/paddle/fluid/core.py | 20 +++++++++++--------- 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/paddle/fluid/pybind/CMakeLists.txt b/paddle/fluid/pybind/CMakeLists.txt index 9d51fe61341..c5540fa94eb 100755 --- a/paddle/fluid/pybind/CMakeLists.txt +++ b/paddle/fluid/pybind/CMakeLists.txt @@ -577,29 +577,37 @@ if(WITH_PYTHON) list(APPEND PYBIND_DEPS saved_tensors_hooks) endif() + # On Linux, cc_library(paddle SHARED ..) will generate the libpaddle.so, + # add a prefix `lib` by default, but on Windows, cc_library(paddle SHARED ..) + # will not add prefix, so it generate paddle.lib and paddle.pyd, + # we need to pay attention to the difference + set(SHARD_LIB_NAME paddle) + if(WIN32) + set(SHARD_LIB_NAME libpaddle) + endif() cc_library( - libpaddle SHARED + ${SHARD_LIB_NAME} SHARED SRCS ${PYBIND_SRCS} DEPS ${PYBIND_DEPS} ${GLOB_OP_LIB} ${GLOB_OPERATOR_DEPS}) if(NOT ((NOT WITH_PYTHON) AND ON_INFER)) - add_dependencies(libpaddle legacy_eager_codegen) - add_dependencies(libpaddle eager_legacy_op_function_generator_cmd) + add_dependencies(${SHARD_LIB_NAME} legacy_eager_codegen) + add_dependencies(${SHARD_LIB_NAME} eager_legacy_op_function_generator_cmd) endif() if(NOT APPLE AND NOT WIN32) - target_link_libraries(libpaddle rt) + target_link_libraries(${SHARD_LIB_NAME} rt) endif() if(WITH_ROCM) - target_link_libraries(libpaddle ${ROCM_HIPRTC_LIB}) + target_link_libraries(${SHARD_LIB_NAME} ${ROCM_HIPRTC_LIB}) endif() if(WITH_MPI) - target_link_libraries(libpaddle ${MPI_CXX_LIBRARIES}) + target_link_libraries(${SHARD_LIB_NAME} ${MPI_CXX_LIBRARIES}) endif() get_property(os_dependency_modules GLOBAL PROPERTY OS_DEPENDENCY_MODULES) - target_link_libraries(libpaddle ${os_dependency_modules}) - add_dependencies(libpaddle op_function_generator_cmd) + target_link_libraries(${SHARD_LIB_NAME} ${os_dependency_modules}) + add_dependencies(${SHARD_LIB_NAME} op_function_generator_cmd) endif() diff --git a/python/CMakeLists.txt b/python/CMakeLists.txt index b935fb78f4e..b7d8eb1dcbc 100644 --- a/python/CMakeLists.txt +++ b/python/CMakeLists.txt @@ -31,7 +31,6 @@ if(WIN32) # Python would use the .pyd by default under Windows series platform set(FLUID_CORE ${FLUID_DST_DIR}/${FLUID_CORE_NAME}.pyd) set(FLUID_CORE_LIB ${FLUID_DST_DIR}/${FLUID_CORE_NAME}.lib) - add_custom_command( OUTPUT ${FLUID_CORE} COMMAND cmake -E copy $ ${FLUID_CORE} @@ -41,8 +40,8 @@ else() set(FLUID_CORE ${FLUID_DST_DIR}/${FLUID_CORE_NAME}.so) add_custom_command( OUTPUT ${FLUID_CORE} - COMMAND cmake -E copy $ ${FLUID_CORE} - DEPENDS libpaddle) + COMMAND cmake -E copy $ ${FLUID_CORE} + DEPENDS paddle) endif() set(FLUID_CORE_DEPS ${FLUID_CORE}) diff --git a/python/paddle/fluid/core.py b/python/paddle/fluid/core.py index 09a659ff573..b58bead476a 100644 --- a/python/paddle/fluid/core.py +++ b/python/paddle/fluid/core.py @@ -20,14 +20,16 @@ import os import warnings import platform -core_suffix = 'so' +has_paddle_dy_lib = False + +dy_lib_name = 'libpaddle' +dy_lib_suffix = 'so' if os.name == 'nt': - core_suffix = 'pyd' + dy_lib_suffix = 'pyd' -has_libpaddle_so = False current_path = os.path.abspath(os.path.dirname(__file__)) -if os.path.exists(current_path + os.sep + 'libpaddle.' + core_suffix): - has_libpaddle_so = True +if os.path.exists(current_path + os.sep + dy_lib_name + '.' + dy_lib_suffix): + has_paddle_dy_lib = True try: if os.name == 'nt': @@ -193,8 +195,8 @@ def load_dso(dso_absolute_path): def pre_load(dso_name): - if has_libpaddle_so: - core_so = current_path + os.sep + 'libpaddle.' + core_suffix + if has_paddle_dy_lib: + core_so = current_path + os.sep + dy_lib_name + '.' + dy_lib_suffix else: core_so = None dso_path = get_dso_path(core_so, dso_name) @@ -290,10 +292,10 @@ try: from .libpaddle import _cleanup_mmap_fds from .libpaddle import _remove_tensor_list_mmap_fds except Exception as e: - if has_libpaddle_so: + if has_paddle_dy_lib: sys.stderr.write( 'Error: Can not import paddle core while this file exists: ' + - current_path + os.sep + 'libpaddle.' + core_suffix + '\n') + current_path + os.sep + 'libpaddle.' + dy_lib_suffix + '\n') if not avx_supported() and libpaddle.is_compiled_with_avx(): sys.stderr.write( "Error: Your machine doesn't support AVX, but the installed PaddlePaddle is avx core, " -- GitLab