From 607a1d65de8f9c01a7e17e95160928030c45553b Mon Sep 17 00:00:00 2001 From: heliqi <1101791222@qq.com> Date: Mon, 6 Jun 2022 06:59:27 -0500 Subject: [PATCH] [inference]Resolve protobuf of ORT Backend conflict (#43159) * modify paddle2onnx cmake * modify paddle2onnx cmake * modify export interface * modify paddle2onnx export * paddle2onnx add mac windows * modify paddle2onnx mac windows cmake * modify paddle2onnx mac windows cmake * modify paddle2onnx cmake support windows * modify paddle2onnx cmake support windows * modify paddle2onnx cmake support windows Co-authored-by: xiegegege --- cmake/external/paddle2onnx.cmake | 102 +++++++++--------- cmake/external/protobuf.cmake | 9 +- cmake/inference_lib.cmake | 16 +-- .../eager/auto_code_generator/CMakeLists.txt | 2 +- .../inference/api/onnxruntime_predictor.cc | 27 +++-- paddle/fluid/pybind/CMakeLists.txt | 2 +- python/setup.py.in | 10 +- 7 files changed, 84 insertions(+), 84 deletions(-) diff --git a/cmake/external/paddle2onnx.cmake b/cmake/external/paddle2onnx.cmake index 8252b2a73e..75e2c42cb5 100644 --- a/cmake/external/paddle2onnx.cmake +++ b/cmake/external/paddle2onnx.cmake @@ -25,82 +25,82 @@ include(ExternalProject) set(PADDLE2ONNX_PROJECT "extern_paddle2onnx") set(PADDLE2ONNX_PREFIX_DIR ${THIRD_PARTY_PATH}/paddle2onnx) +set(PADDLE2ONNX_SOURCE_DIR + ${THIRD_PARTY_PATH}/paddle2onnx/src/${PADDLE2ONNX_PROJECT}) set(PADDLE2ONNX_INSTALL_DIR ${THIRD_PARTY_PATH}/install/paddle2onnx) set(PADDLE2ONNX_INC_DIR "${PADDLE2ONNX_INSTALL_DIR}/include" CACHE PATH "paddle2onnx include directory." FORCE) -set(PADDLE2ONNX_REPOSITORY ${GIT_URL}/PaddlePaddle/Paddle2ONNX.git) -set(PADDLE2ONNX_TAG cpp) -set(LIBDIR "lib") +set(PADDLE2ONNX_LIB_DIR + "${PADDLE2ONNX_INSTALL_DIR}/lib" + CACHE PATH "onnxruntime lib directory." FORCE) set(CMAKE_BUILD_RPATH "${CMAKE_BUILD_RPATH}" "${PADDLE2ONNX_INSTALL_DIR}/${LIBDIR}") include_directories(${PADDLE2ONNX_INC_DIR} )# For PADDLE2ONNX code to include internal headers. if(WIN32) + set(PADDLE2ONNX_SOURCE_LIB + "${PADDLE2ONNX_SOURCE_DIR}/lib/libpaddle2onnx.dylib" + CACHE FILEPATH "Paddle2ONNX source library." FORCE) set(PADDLE2ONNX_LIB - "${PADDLE2ONNX_INSTALL_DIR}/${LIBDIR}/paddle2onnx.lib" - CACHE FILEPATH "paddle2onnx static library." FORCE) - set(PADDLE2ONNX_SHARED_LIB - "${PADDLE2ONNX_INSTALL_DIR}/${LIBDIR}/paddle2onnx.dll" - CACHE FILEPATH "paddle2onnx shared library." FORCE) + "${PADDLE2ONNX_INSTALL_DIR}/lib/paddle2onnx.dll" + CACHE FILEPATH "paddle2onnx library." FORCE) + set(PADDLE2ONNX_COMPILE_LIB + "${PADDLE2ONNX_INSTALL_DIR}/lib/paddle2onnx.lib" + CACHE FILEPATH "paddle2onnx compile library." FORCE) elseif(APPLE) + set(PADDLE2ONNX_SOURCE_LIB + "${PADDLE2ONNX_SOURCE_DIR}/lib/libpaddle2onnx.dylib" + CACHE FILEPATH "Paddle2ONNX source library." FORCE) set(PADDLE2ONNX_LIB - "${PADDLE2ONNX_INSTALL_DIR}/${LIBDIR}/libpaddle2onnx.dylib" + "${PADDLE2ONNX_INSTALL_DIR}/lib/libpaddle2onnx.dylib" CACHE FILEPATH "PADDLE2ONNX library." FORCE) + set(PADDLE2ONNX_COMPILE_LIB + "${PADDLE2ONNX_INSTALL_DIR}/lib/libpaddle2onnx.dylib" + CACHE FILEPATH "paddle2onnx compile library." FORCE) else() + set(PADDLE2ONNX_SOURCE_LIB + "${PADDLE2ONNX_SOURCE_DIR}/lib/libpaddle2onnx.so" + CACHE FILEPATH "Paddle2ONNX source library." FORCE) set(PADDLE2ONNX_LIB - "${PADDLE2ONNX_INSTALL_DIR}/${LIBDIR}/libpaddle2onnx.so" + "${PADDLE2ONNX_INSTALL_DIR}/lib/libpaddle2onnx.so" CACHE FILEPATH "PADDLE2ONNX library." FORCE) + set(PADDLE2ONNX_COMPILE_LIB + "${PADDLE2ONNX_INSTALL_DIR}/lib/libpaddle2onnx.so" + CACHE FILEPATH "paddle2onnx compile library." FORCE) endif(WIN32) -# The protoc path is required to compile onnx. -string(REPLACE "/" ";" PROTOC_BIN_PATH ${PROTOBUF_PROTOC_EXECUTABLE}) -list(POP_BACK PROTOC_BIN_PATH) -list(JOIN PROTOC_BIN_PATH "/" PROTOC_BIN_PATH) - -set(PADDLE2ONNX_OPTIONAL_ARGS - -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} - -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} - -DCMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS} - -DCMAKE_CXX_STANDARD=14 - -DCMAKE_CXX_FLAGS_RELEASE=${CMAKE_CXX_FLAGS_RELEASE} - -DCMAKE_CXX_FLAGS_DEBUG=${CMAKE_CXX_FLAGS_DEBUG} - -DCMAKE_C_FLAGS=${CMAKE_C_FLAGS} - -DCMAKE_C_FLAGS_DEBUG=${CMAKE_C_FLAGS_DEBUG} - -DCMAKE_C_FLAGS_RELEASE=${CMAKE_C_FLAGS_RELEASE} - -DONNX_CUSTOM_PROTOC_PATH=${PROTOC_BIN_PATH} - -DWITH_STATIC=OFF - -DMSVC_STATIC_CRT=${MSVC_STATIC_CRT} - -DCMAKE_INSTALL_PREFIX=${PADDLE2ONNX_INSTALL_DIR} - -DCMAKE_INSTALL_LIBDIR=${PADDLE2ONNX_INSTALL_DIR}/${LIBDIR} - -DCMAKE_POSITION_INDEPENDENT_CODE=ON - -DCMAKE_BUILD_TYPE=${THIRD_PARTY_BUILD_TYPE} - ${EXTERNAL_OPTIONAL_ARGS}) - -if(WITH_PYTHON) - set(PADDLE2ONNX_OPTIONAL_ARGS - ${PADDLE2ONNX_OPTIONAL_ARGS} - -DPYTHON_EXECUTABLE:FILEPATH=${PYTHON_EXECUTABLE} - -DPYTHON_INCLUDE_DIR:PATH=${PYTHON_INCLUDE_DIR} - -DPYTHON_LIBRARY:FILEPATH=${PYTHON_LIBRARY}) +if(WIN32) + set(PADDLE2ONNX_URL + "https://github.com/PaddlePaddle/Paddle2ONNX/releases/download/v0.9.7/paddle2onnx-win-x64-0.9.7.zip" + ) +elseif(APPLE) + set(PADDLE2ONNX_URL + "https://github.com/PaddlePaddle/Paddle2ONNX/releases/download/v0.9.7/paddle2onnx-osx-x86_64-0.9.7.tgz" + ) +else() + set(PADDLE2ONNX_URL + "https://github.com/PaddlePaddle/Paddle2ONNX/releases/download/v0.9.7/paddle2onnx-linux-x64-0.9.7.tgz" + ) endif() ExternalProject_Add( ${PADDLE2ONNX_PROJECT} - ${EXTERNAL_PROJECT_LOG_ARGS} ${SHALLOW_CLONE} - GIT_REPOSITORY ${PADDLE2ONNX_REPOSITORY} - GIT_TAG ${PADDLE2ONNX_TAG} - DEPENDS protobuf + ${EXTERNAL_PROJECT_LOG_ARGS} + URL ${PADDLE2ONNX_URL} PREFIX ${PADDLE2ONNX_PREFIX_DIR} + DOWNLOAD_NO_PROGRESS 1 + CONFIGURE_COMMAND "" + BUILD_COMMAND "" UPDATE_COMMAND "" - CMAKE_ARGS ${PADDLE2ONNX_OPTIONAL_ARGS} - CMAKE_CACHE_ARGS - -DCMAKE_INSTALL_PREFIX:PATH=${PADDLE2ONNX_INSTALL_DIR} - -DCMAKE_POSITION_INDEPENDENT_CODE:BOOL=ON - -DCMAKE_BUILD_TYPE:STRING=${THIRD_PARTY_BUILD_TYPE} - BUILD_BYPRODUCTS ${PADDLE2ONNX_LIB}) + INSTALL_COMMAND + ${CMAKE_COMMAND} -E copy_directory ${PADDLE2ONNX_SOURCE_DIR}/lib + ${PADDLE2ONNX_LIB_DIR} && ${CMAKE_COMMAND} -E copy_directory + ${PADDLE2ONNX_SOURCE_DIR}/include ${PADDLE2ONNX_INC_DIR} + BUILD_BYPRODUCTS ${PADDLE2ONNX_COMPILE_LIB}) add_library(paddle2onnx STATIC IMPORTED GLOBAL) -set_property(TARGET paddle2onnx PROPERTY IMPORTED_LOCATION ${PADDLE2ONNX_LIB}) +set_property(TARGET paddle2onnx PROPERTY IMPORTED_LOCATION + ${PADDLE2ONNX_COMPILE_LIB}) add_dependencies(paddle2onnx ${PADDLE2ONNX_PROJECT}) diff --git a/cmake/external/protobuf.cmake b/cmake/external/protobuf.cmake index 1368081b58..7c5de92362 100755 --- a/cmake/external/protobuf.cmake +++ b/cmake/external/protobuf.cmake @@ -234,10 +234,7 @@ function(build_protobuf TARGET_NAME BUILD_FOR_HOST) "-Dprotobuf_MSVC_STATIC_RUNTIME=${MSVC_STATIC_CRT}") endif() - if(WITH_ONNXRUNTIME) - set(PROTOBUF_REPOSITORY ${GIT_URL}/protocolbuffers/protobuf.git) - set(PROTOBUF_TAG v3.18.0) - elseif(WITH_ASCEND AND NOT WITH_ASCEND_CXX11) + if(WITH_ASCEND AND NOT WITH_ASCEND_CXX11) set(PROTOBUF_REPOSITORY https://gitee.com/tianjianhe/protobuf.git) set(PROTOBUF_TAG v3.8.0) elseif(WITH_ASCEND_CL AND NOT WITH_ASCEND_CXX11) @@ -319,9 +316,7 @@ function(build_protobuf TARGET_NAME BUILD_FOR_HOST) endif() endfunction() -if(WITH_ONNXRUNTIME) - set(PROTOBUF_VERSION 3.18.0) -elseif(WITH_ASCEND OR WITH_ASCEND_CL) +if(WITH_ASCEND OR WITH_ASCEND_CL) set(PROTOBUF_VERSION 3.8.0) elseif(WITH_IPU) set(PROTOBUF_VERSION 3.6.1) diff --git a/cmake/inference_lib.cmake b/cmake/inference_lib.cmake index bf69ddc8fb..14ae8efb5b 100644 --- a/cmake/inference_lib.cmake +++ b/cmake/inference_lib.cmake @@ -148,18 +148,10 @@ function(copy_part_of_thrid_party TARGET DST) DSTS ${dst_dir} ${dst_dir}) set(dst_dir "${DST}/third_party/install/paddle2onnx") - if(WIN32) - copy( - ${TARGET} - SRCS ${PADDLE2ONNX_INC_DIR}/paddle2onnx ${PADDLE2ONNX_SHARED_LIB} - ${PADDLE2ONNX_LIB} - DSTS ${dst_dir}/include ${dst_dir}/lib ${dst_dir}/lib) - else() - copy( - ${TARGET} - SRCS ${PADDLE2ONNX_INC_DIR}/paddle2onnx ${PADDLE2ONNX_LIB} - DSTS ${dst_dir}/include ${dst_dir}/lib) - endif() + copy( + ${TARGET} + SRCS ${PADDLE2ONNX_INC_DIR}/paddle2onnx ${PADDLE2ONNX_LIB_DIR} + DSTS ${dst_dir}/include ${dst_dir}) endif() set(dst_dir "${DST}/third_party/install/gflags") diff --git a/paddle/fluid/eager/auto_code_generator/CMakeLists.txt b/paddle/fluid/eager/auto_code_generator/CMakeLists.txt index 8c067074d6..aff7f057f4 100644 --- a/paddle/fluid/eager/auto_code_generator/CMakeLists.txt +++ b/paddle/fluid/eager/auto_code_generator/CMakeLists.txt @@ -103,7 +103,7 @@ if(WIN32) list(APPEND EAGER_CODEGEN_DEPS ${eager_generator_path}/onnxruntime.dll) add_custom_command( OUTPUT ${eager_generator_path}/paddle2onnx.dll - COMMAND ${CMAKE_COMMAND} -E copy ${PADDLE2ONNX_SHARED_LIB} + COMMAND ${CMAKE_COMMAND} -E copy ${PADDLE2ONNX_LIB} ${eager_generator_path} DEPENDS paddle2onnx) list(APPEND EAGER_CODEGEN_DEPS ${eager_generator_path}/paddle2onnx.dll) diff --git a/paddle/fluid/inference/api/onnxruntime_predictor.cc b/paddle/fluid/inference/api/onnxruntime_predictor.cc index e42e395ce9..93a9686305 100644 --- a/paddle/fluid/inference/api/onnxruntime_predictor.cc +++ b/paddle/fluid/inference/api/onnxruntime_predictor.cc @@ -74,8 +74,14 @@ bool CheckConvertToONNX(const AnalysisConfig &config) { config.model_dir(), config.prog_file(), config.params_file()); return false; } - return paddle2onnx::IsExportable(config.prog_file(), config.params_file(), - config.model_from_memory()); + if (config.model_from_memory()) { + return paddle2onnx::IsExportable( + config.prog_file().data(), config.prog_file().size(), + config.params_file().data(), config.params_file().size()); + } else { + return paddle2onnx::IsExportable(config.prog_file().c_str(), + config.params_file().c_str()); + } } bool ONNXRuntimePredictor::Init() { @@ -89,9 +95,16 @@ bool ONNXRuntimePredictor::Init() { place_ = paddle::platform::CPUPlace(); } - std::string onnx_proto; - paddle2onnx::Export(config_.prog_file(), config_.params_file(), &onnx_proto, - config_.model_from_memory()); + char *onnx_proto = nullptr; + int out_size; + if (config_.model_from_memory()) { + paddle2onnx::Export(config_.prog_file().data(), config_.prog_file().size(), + config_.params_file().data(), + config_.params_file().size(), &onnx_proto, &out_size); + } else { + paddle2onnx::Export(config_.prog_file().c_str(), + config_.params_file().c_str(), &onnx_proto, &out_size); + } Ort::SessionOptions session_options; if (config_.ort_optimization_enabled()) { @@ -118,7 +131,7 @@ bool ONNXRuntimePredictor::Init() { "will be " "generated."; } - session_ = {env_, onnx_proto.data(), onnx_proto.size(), session_options}; + session_ = {env_, onnx_proto, static_cast(out_size), session_options}; binding_ = std::make_shared(session_); Ort::MemoryInfo memory_info(device_name, OrtDeviceAllocator, @@ -153,6 +166,8 @@ bool ONNXRuntimePredictor::Init() { allocator.Free(output_name); } + delete onnx_proto; + onnx_proto = nullptr; return true; } diff --git a/paddle/fluid/pybind/CMakeLists.txt b/paddle/fluid/pybind/CMakeLists.txt index bf74d11843..a99dded4d5 100755 --- a/paddle/fluid/pybind/CMakeLists.txt +++ b/paddle/fluid/pybind/CMakeLists.txt @@ -335,7 +335,7 @@ if(WITH_PYTHON) if(WITH_ONNXRUNTIME) add_custom_command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/paddle2onnx.dll - COMMAND ${CMAKE_COMMAND} -E copy ${PADDLE2ONNX_SHARED_LIB} + COMMAND ${CMAKE_COMMAND} -E copy ${PADDLE2ONNX_LIB} ${CMAKE_CURRENT_BINARY_DIR} DEPENDS paddle2onnx) list(APPEND OP_IMPL_DEPS ${CMAKE_CURRENT_BINARY_DIR}/paddle2onnx.dll) diff --git a/python/setup.py.in b/python/setup.py.in index ca1768c946..bb6416038f 100755 --- a/python/setup.py.in +++ b/python/setup.py.in @@ -531,15 +531,13 @@ if '${WITH_MKLDNN}' == 'ON': if '${WITH_ONNXRUNTIME}' == 'ON': shutil.copy('${ONNXRUNTIME_SHARED_LIB}', libs_path) + shutil.copy('${PADDLE2ONNX_LIB}', libs_path) if os.name == 'nt': - shutil.copy('${PADDLE2ONNX_SHARED_LIB}', libs_path) package_data['paddle.libs']+=['paddle2onnx.dll', 'onnxruntime.dll'] + elif sys.platform == 'darwin': + package_data['paddle.libs']+=['libpaddle2onnx.dylib', 'libonnxruntime.1.10.0.dylib'] else: - shutil.copy('${PADDLE2ONNX_LIB}', libs_path) - if sys.platform == 'darwin': - package_data['paddle.libs']+=['libpaddle2onnx.dylib', 'libonnxruntime.1.10.0.dylib'] - else: - package_data['paddle.libs']+=['libpaddle2onnx.so', 'libonnxruntime.so.1.10.0'] + package_data['paddle.libs']+=['libpaddle2onnx.so', 'libonnxruntime.so.1.10.0'] if '${WITH_XPU}' == 'ON': # only change rpath in Release mode, -- GitLab