Skip to content

Commit

Permalink
[Build] Support build with source code of Paddle2ONNX (PaddlePaddle#1559
Browse files Browse the repository at this point in the history
)

* Add notes for tensors

* Optimize some apis

* move some warnings

* Support build with Paddle2ONNX

* Add protobuf support

* Fix compile on mac

* add clearn package script

* Add paddle2onnx code

* remove submodule

* Add onnx ocde

* remove softlink

* add onnx code

* fix error

* Add cmake file

* fix patchelf

* update paddle2onnx

* Delete .gitmodules

---------

Co-authored-by: PaddleCI <[email protected]>
Co-authored-by: pangyoki <[email protected]>
Co-authored-by: jiangjiajun <[email protected]>
  • Loading branch information
4 people authored Mar 17, 2023
1 parent f568c59 commit 6343b0d
Show file tree
Hide file tree
Showing 5,052 changed files with 222,092 additions and 32 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
16 changes: 13 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ if(NOT MSVC)
endif()
endif(NOT MSVC)

include(${PROJECT_SOURCE_DIR}/cmake/build_tools.cmake)
if(UNIX AND (NOT APPLE) AND (NOT ANDROID) AND (NOT WITH_TIMVX))
include(${PROJECT_SOURCE_DIR}/cmake/patchelf.cmake)
download_patchelf()
set(PATCHELF_EXE ${THIRD_PARTY_PATH}/patchelf/bin/patchelf)
endif()


Expand Down Expand Up @@ -423,9 +425,17 @@ if(ENABLE_ENCRYPTION)
endif()

if(ENABLE_PADDLE2ONNX)
set(BUILD_PADDLE2ONNX ON)
add_definitions(-DENABLE_PADDLE2ONNX)
include(${PROJECT_SOURCE_DIR}/cmake/paddle2onnx.cmake)
list(APPEND DEPEND_LIBS external_paddle2onnx)
if(BUILD_PADDLE2ONNX)
download_protobuf()
include(${PROJECT_SOURCE_DIR}/cmake/build_paddle2onnx.cmake)
list(APPEND ALL_DEPLOY_SRCS ${PADDLE2ONNX_ALL_SRCS})
list(APPEND DEPEND_LIBS p2o_paddle_proto onnx)
else()
include(${PROJECT_SOURCE_DIR}/cmake/paddle2onnx.cmake)
list(APPEND DEPEND_LIBS external_paddle2onnx)
endif()
endif(ENABLE_PADDLE2ONNX)

if(WITH_CAPI)
Expand Down
7 changes: 5 additions & 2 deletions FastDeploy.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ set(ENABLE_OPENVINO_BACKEND @ENABLE_OPENVINO_BACKEND@)
set(ENABLE_POROS_BACKEND @ENABLE_POROS_BACKEND@)
set(ENABLE_TRT_BACKEND @ENABLE_TRT_BACKEND@)
set(ENABLE_PADDLE2ONNX @ENABLE_PADDLE2ONNX@)
set(BUILD_PADDLE2ONNX @BUILD_PADDLE2ONNX@)

set(ENABLE_VISION @ENABLE_VISION@)
set(ENABLE_FLYCV @ENABLE_FLYCV@)
Expand Down Expand Up @@ -346,8 +347,10 @@ if(ENABLE_PADDLE2ONNX)
if(ANDROID)
message(FATAL_ERROR "Not support fastdeploy-paddle2onnx APIs with Android now!")
endif()
find_library(PADDLE2ONNX_LIB paddle2onnx ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/paddle2onnx/lib NO_DEFAULT_PATH)
list(APPEND FASTDEPLOY_LIBS ${PADDLE2ONNX_LIB})
if(NOT BUILD_PADDLE2ONNX)
find_library(PADDLE2ONNX_LIB paddle2onnx ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/paddle2onnx/lib NO_DEFAULT_PATH)
list(APPEND FASTDEPLOY_LIBS ${PADDLE2ONNX_LIB})
endif()
endif()

if(WITH_KUNLUNXIN)
Expand Down
41 changes: 41 additions & 0 deletions cmake/build_paddle2onnx.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
add_definitions(-DMAX_ONNX_OPSET_VERSION=16)
add_definitions(-DPADDLE2ONNX_LIB)

# Third dependency: onnx
if(NOT TARGET onnx_proto)
if(NOT ONNX_NAMESPACE)
set(ONNX_NAMESPACE "paddle2onnx")
endif()
add_definitions("-DONNX_NAMESPACE=${ONNX_NAMESPACE}")

set(MSVC_STATIC_CRT ON)
if(ONNX_CUSTOM_PROTOC_PATH)
if(WIN32)
if(MSVC_STATIC_CRT)
# MT
set(ONNX_USE_MSVC_STATIC_RUNTIME ON)
else()
# MD
set(ONNX_USE_MSVC_STATIC_RUNTIME OFF)
endif()
set(ONNX_CUSTOM_PROTOC_PATH "${ONNX_CUSTOM_PROTOC_PATH};$ENV{PATH}")
else()
set(ONNX_CUSTOM_PROTOC_PATH "${ONNX_CUSTOM_PROTOC_PATH}:$ENV{PATH}")
endif()
set(ENV{PATH} ${ONNX_CUSTOM_PROTOC_PATH})
endif()

set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/onnx)
endif()

include_directories(${PROJECT_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${CMAKE_CURRENT_BINARY_DIR}/third_party/onnx)

include_directories(${PROJECT_SOURCE_DIR}/third_party/optimizer)
add_subdirectory(${PROJECT_SOURCE_DIR}/paddle2onnx/proto)

file(GLOB_RECURSE PADDLE2ONNX_ALL_SRCS ${PROJECT_SOURCE_DIR}/paddle2onnx/*.cc ${PROJECT_SOURCE_DIR}/third_party/optimizer/onnxoptimizer/*.cc)
list(REMOVE_ITEM PADDLE2ONNX_ALL_SRCS ${PROJECT_SOURCE_DIR}/paddle2onnx/cpp2py_export.cc ${PROJECT_SOURCE_DIR}/third_party/optimizer/onnxoptimizer/cpp2py_export.cc)

57 changes: 57 additions & 0 deletions cmake/build_tools.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

function(download_patchelf)
if(UNIX AND (NOT APPLE) AND (NOT ANDROID))
set(PATCHELF_EXE "patchelf")
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
set(PATCHELF_URL https://bj.bcebos.com/fastdeploy/third_libs/patchelf-0.15.0-aarch64.tar.gz)
download_and_decompress(${PATCHELF_URL} ${CMAKE_CURRENT_BINARY_DIR}/patchelf-0.15.0-aarch64.tar.gz ${THIRD_PARTY_PATH}/patchelf)
else()
set(PATCHELF_URL https://bj.bcebos.com/fastdeploy/third_libs/patchelf-0.15.0-x86_64.tar.gz)
download_and_decompress(${PATCHELF_URL} ${CMAKE_CURRENT_BINARY_DIR}/patchelf-0.15.0-x86_64.tar.gz ${THIRD_PARTY_PATH}/patchelf)
endif()
endif()
endfunction()

function(download_protobuf)
if(WIN32)
if(NOT CMAKE_CL_64)
set(PATCHELF_URL https://bj.bcebos.com/fastdeploy/third_libs/protobuf-win-x86-3.16.0.zip)
else()
set(PATCHELF_URL https://bj.bcebos.com/fastdeploy/third_libs/protobuf-win-x64-3.16.0.zip)
endif()
set(ORIGIN_ENV_PATH "$ENV{PATH}")
download_and_decompress(${PATCHELF_URL} ${CMAKE_CURRENT_BINARY_DIR}/protobuf-win-3.16.0.tgz ${THIRD_PARTY_PATH}/protobuf)
set(ENV{PATH} "${THIRD_PARTY_PATH}\\protobuf\\bin;${ORIGIN_ENV_PATH}")
elseif(APPLE)
if(CURRENT_OSX_ARCH MATCHES "arm64")
set(PATCHELF_URL https://bj.bcebos.com/fastdeploy/third_libs/protobuf-osx-arm64-3.16.0.tgz)
else()
set(PATCHELF_URL https://bj.bcebos.com/fastdeploy/third_libs/protobuf-osx-x86_64-3.16.0.tgz)
endif()
set(ORIGIN_ENV_PATH "$ENV{PATH}")
download_and_decompress(${PATCHELF_URL} ${CMAKE_CURRENT_BINARY_DIR}/protobuf-osx-3.16.0.tgz ${THIRD_PARTY_PATH}/protobuf)
set(ENV{PATH} "${THIRD_PARTY_PATH}/protobuf/bin/:${ORIGIN_ENV_PATH}")
else()
if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
set(PATCHELF_URL https://bj.bcebos.com/fastdeploy/third_libs/protobuf-linux-aarch64-3.16.0.tgz)
else()
set(PATCHELF_URL https://bj.bcebos.com/fastdeploy/third_libs/protobuf-linux-x64-3.16.0.tgz)
endif()
set(ORIGIN_ENV_PATH "$ENV{PATH}")
download_and_decompress(${PATCHELF_URL} ${CMAKE_CURRENT_BINARY_DIR}/protobuf-linux-3.16.0.tgz ${THIRD_PARTY_PATH}/protobuf)
set(ENV{PATH} "${THIRD_PARTY_PATH}/protobuf/bin/:${ORIGIN_ENV_PATH}")
endif()
endfunction()
1 change: 1 addition & 0 deletions cmake/paddle_inference.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ else()
endif(PADDLEINFERENCE_DIRECTORY)

if(UNIX AND (NOT APPLE) AND (NOT ANDROID))
message("?????????????? ${PATCHELF_EXE}")
add_custom_target(patchelf_paddle_inference ALL COMMAND bash -c "PATCHELF_EXE=${PATCHELF_EXE} python ${PROJECT_SOURCE_DIR}/scripts/patch_paddle_inference.py ${PADDLEINFERENCE_INSTALL_DIR}/paddle/lib/libpaddle_inference.so" DEPENDS ${LIBRARY_NAME})
endif()

Expand Down
26 changes: 0 additions & 26 deletions cmake/patchelf.cmake

This file was deleted.

1 change: 0 additions & 1 deletion examples/runtime/cpp/infer_paddle_paddle_inference.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ int main(int argc, char* argv[]) {
// https://baidu-paddle.github.io/fastdeploy-api/cpp/html/structfastdeploy_1_1RuntimeOption.html
fd::RuntimeOption runtime_option;
runtime_option.SetModelPath(model_file, params_file);
runtime_option.UsePaddleInferBackend();
runtime_option.UseCpu();

// If need to configure Paddle Inference backend for more option, we can configure runtime_option.paddle_infer_option
Expand Down
Empty file added paddle2onnx/CMakeLists.txt
Empty file.
65 changes: 65 additions & 0 deletions paddle2onnx/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from paddle2onnx.utils import logging
from . import command
from .convert import dygraph2onnx
from .convert import program2onnx
from .version import version
from .version import git_version

__version__ = version
__commit_id__ = git_version


def run_convert(model, input_shape_dict=None, scope=None, opset_version=9):
logging.warning(
"[Deprecated] `paddle2onnx.run_convert` will be deprecated in the future version, the recommended usage is `paddle2onnx.export`"
)
from paddle2onnx.legacy import run_convert
return run_convert(model, input_shape_dict, scope, opset_version)


def export(model_file,
params_file="",
save_file=None,
opset_version=11,
auto_upgrade_opset=True,
verbose=True,
enable_onnx_checker=True,
enable_experimental_op=True,
enable_optimize=True,
custom_op_info=None,
deploy_backend="onnxruntime",
calibration_file="",
external_file="",
export_fp16_model=False):
import paddle2onnx.paddle2onnx_cpp2py_export as c_p2o
deploy_backend = deploy_backend.lower()
if custom_op_info is None:
onnx_model_str = c_p2o.export(
model_file, params_file, opset_version, auto_upgrade_opset, verbose,
enable_onnx_checker, enable_experimental_op, enable_optimize, {},
deploy_backend, calibration_file, external_file, export_fp16_model)
else:
onnx_model_str = c_p2o.export(
model_file, params_file, opset_version, auto_upgrade_opset, verbose,
enable_onnx_checker, enable_experimental_op, enable_optimize,
custom_op_info, deploy_backend, calibration_file, external_file,
export_fp16_model)
if save_file is not None:
with open(save_file, "wb") as f:
f.write(onnx_model_str)
else:
return onnx_model_str
Loading

0 comments on commit 6343b0d

Please sign in to comment.