From 2df4be5d3533ce7cf549a76e59efab78480a6a6e Mon Sep 17 00:00:00 2001 From: zhouwei25 <52485244+zhouwei25@users.noreply.github.com> Date: Thu, 26 Dec 2019 11:42:52 +0800 Subject: [PATCH] Fix openblas bug to support compile on windows when WITH_MKL=OFF (#21902) * Fix openblas to support compile on Windows when WITH_MKL=OFF --- cmake/external/openblas.cmake | 2 + cmake/inference_lib.cmake | 6 +++ paddle/fluid/pybind/CMakeLists.txt | 59 +++++++++++++----------------- python/setup.py.in | 2 +- 4 files changed, 34 insertions(+), 35 deletions(-) diff --git a/cmake/external/openblas.cmake b/cmake/external/openblas.cmake index 250228de3fe..eff73c792a8 100644 --- a/cmake/external/openblas.cmake +++ b/cmake/external/openblas.cmake @@ -68,10 +68,12 @@ ELSE(NOT WIN32) -DCMAKE_INSTALL_PREFIX=${CBLAS_INSTALL_DIR} -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_BUILD_TYPE=${THIRD_PARTY_BUILD_TYPE} + -DBUILD_SHARED_LIBS=ON -DMSVC_STATIC_CRT=${MSVC_STATIC_CRT} ${EXTERNAL_OPTIONAL_ARGS} CMAKE_CACHE_ARGS -DCMAKE_INSTALL_PREFIX:PATH=${CBLAS_INSTALL_DIR} -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON -DCMAKE_BUILD_TYPE:STRING=${THIRD_PARTY_BUILD_TYPE} ) + SET(OPENBLAS_SHARED_LIB ${CBLAS_INSTALL_DIR}/bin/openblas${CMAKE_SHARED_LIBRARY_SUFFIX}) ENDIF(NOT WIN32) diff --git a/cmake/inference_lib.cmake b/cmake/inference_lib.cmake index 1e7d5114a45..0eabf89d364 100644 --- a/cmake/inference_lib.cmake +++ b/cmake/inference_lib.cmake @@ -71,9 +71,15 @@ function(copy_part_of_thrid_party TARGET DST) endif() elseif(${CBLAS_PROVIDER} STREQUAL EXTERN_OPENBLAS) set(dst_dir "${DST}/third_party/install/openblas") + if(WIN32) + copy(${TARGET} + SRCS ${CBLAS_INSTALL_DIR}/lib ${OPENBLAS_SHARED_LIB} ${CBLAS_INSTALL_DIR}/include + DSTS ${dst_dir} ${dst_dir}/lib ${dst_dir}) + else() copy(${TARGET} SRCS ${CBLAS_INSTALL_DIR}/lib ${CBLAS_INSTALL_DIR}/include DSTS ${dst_dir} ${dst_dir}) + endif() endif() if(WITH_MKLDNN) diff --git a/paddle/fluid/pybind/CMakeLists.txt b/paddle/fluid/pybind/CMakeLists.txt index c76f7c977dd..4482dca01e6 100644 --- a/paddle/fluid/pybind/CMakeLists.txt +++ b/paddle/fluid/pybind/CMakeLists.txt @@ -34,7 +34,6 @@ if (WITH_DISTRIBUTE) endif() if(WITH_PYTHON) - # generate op pybind functions automatically for dygraph. set(OP_FUNCTION_GENERETOR_DEPS pybind proto_desc executor layer tracer engine imperative_profiler imperative_flag) list(APPEND OP_FUNCTION_GENERETOR_DEPS ${GLOB_OP_LIB}) @@ -45,27 +44,31 @@ if(WITH_PYTHON) endif(NOT WIN32) add_executable(op_function_generator op_function_generator.cc) - target_link_libraries(op_function_generator ${OP_FUNCTION_GENERETOR_DEPS} ) + target_link_libraries(op_function_generator ${OP_FUNCTION_GENERETOR_DEPS}) get_property (os_dependency_modules GLOBAL PROPERTY OS_DEPENDENCY_MODULES) target_link_libraries(op_function_generator ${os_dependency_modules}) - if (WIN32) + if(WIN32) add_custom_target(op_function_cmd COMMAND "${CMAKE_BINARY_DIR}/paddle/fluid/pybind/${CMAKE_BUILD_TYPE}/op_function_generator" - "${CMAKE_SOURCE_DIR}/paddle/fluid/pybind/op_function_impl.h") - add_dependencies(op_function_cmd op_function_generator) - if(WITH_MKL) - add_custom_target(copy_mkl - COMMAND ${CMAKE_COMMAND} -E copy ${MKLML_SHARED_LIB} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE} - COMMAND ${CMAKE_COMMAND} -E copy ${MKLML_SHARED_LIB_DEPS} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE} - COMMAND ${CMAKE_COMMAND} -E copy ${MKLML_SHARED_IOMP_LIB} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE} + "${CMAKE_SOURCE_DIR}/paddle/fluid/pybind/op_function_impl.h") + add_dependencies(op_function_cmd op_function_generator) + if(${CBLAS_PROVIDER} STREQUAL MKLML) + add_custom_command(TARGET op_function_generator POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy ${MKLML_SHARED_LIB} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE} + COMMAND ${CMAKE_COMMAND} -E copy ${MKLML_SHARED_LIB_DEPS} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE} + COMMAND ${CMAKE_COMMAND} -E copy ${MKLML_SHARED_IOMP_LIB} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE} ) - endif(WITH_MKL) + else(${CBLAS_PROVIDER} STREQUAL EXTERN_OPENBLAS) + add_custom_command(TARGET op_function_generator POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy ${OPENBLAS_SHARED_LIB} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE} + ) + endif() if(WITH_MKLDNN) - add_custom_target(copy_mkldnn + add_custom_command(TARGET op_function_generator POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${MKLDNN_SHARED_LIB} ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE} ) - endif(WITH_MKLDNN) + endif() else(WIN32) # If there are no *.so in /usr/lib or LD_LIBRARY_PATH, # copy these *.so to current directory and append current directory to @@ -73,33 +76,22 @@ if(WITH_PYTHON) # *.dll in current directory automatically. add_custom_target(op_function_cmd COMMAND ${CMAKE_COMMAND} -E env "LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH}:." - "${CMAKE_CURRENT_BINARY_DIR}/op_function_generator" - "${CMAKE_SOURCE_DIR}/paddle/fluid/pybind/op_function_impl.h") - add_dependencies(op_function_cmd op_function_generator) + "${CMAKE_CURRENT_BINARY_DIR}/op_function_generator" + "${CMAKE_SOURCE_DIR}/paddle/fluid/pybind/op_function_impl.h") + add_dependencies(op_function_cmd op_function_generator) if(WITH_MKL) - add_custom_target(copy_mkl - COMMAND ${CMAKE_COMMAND} -E copy ${MKLML_SHARED_LIB} ${CMAKE_CURRENT_BINARY_DIR} - COMMAND ${CMAKE_COMMAND} -E copy ${MKLML_SHARED_IOMP_LIB} ${CMAKE_CURRENT_BINARY_DIR} - ) + add_custom_command(TARGET op_function_generator POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy ${MKLML_SHARED_LIB} ${CMAKE_CURRENT_BINARY_DIR} + COMMAND ${CMAKE_COMMAND} -E copy ${MKLML_SHARED_IOMP_LIB} ${CMAKE_CURRENT_BINARY_DIR} + ) endif(WITH_MKL) if(WITH_MKLDNN) - add_custom_target(copy_mkldnn + add_custom_command(TARGET op_function_generator POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${MKLDNN_SHARED_LIB} ${CMAKE_CURRENT_BINARY_DIR} ) endif(WITH_MKLDNN) endif(WIN32) - if(WITH_MKL) - add_dependencies(copy_mkl op_function_generator) - add_dependencies(op_function_cmd copy_mkl) - endif(WITH_MKL) - - if(WITH_MKLDNN) - add_dependencies(copy_mkldnn op_function_generator) - add_dependencies(op_function_cmd copy_mkldnn) - endif(WITH_MKLDNN) - - if(WITH_AMD_GPU) hip_library(paddle_pybind SHARED SRCS ${PYBIND_SRCS} @@ -117,6 +109,5 @@ if(WITH_PYTHON) get_property (os_dependency_modules GLOBAL PROPERTY OS_DEPENDENCY_MODULES) target_link_libraries(paddle_pybind ${os_dependency_modules}) - add_dependencies(paddle_pybind op_function_cmd) - + add_dependencies(paddle_pybind op_function_cmd) endif(WITH_PYTHON) diff --git a/python/setup.py.in b/python/setup.py.in index cebb460bd77..74e38d20ec2 100644 --- a/python/setup.py.in +++ b/python/setup.py.in @@ -190,7 +190,7 @@ if '${WITH_MKL}' == 'ON': else: if os.name == 'nt': # copy the openblas.dll - shutil.copy(os.path.dirname('${CBLAS_LIBRARIES}') + '/openblas' + ext_name, libs_path) + shutil.copy('${OPENBLAS_SHARED_LIB}', libs_path) package_data['paddle.libs'] += ['openblas' + ext_name] if '${WITH_PSLIB}' == 'ON': -- GitLab