Skip to content

Commit b9241fc

Browse files
committed
feat: remove git submodules, use CMake FetchContent for C++ deps
Replace git submodules (onnx, optimizer, pybind11, glog) with: - FetchContent for ONNX v1.20.1 and onnx-optimizer v0.3.19 - find_package for pybind11 and glog (system-installed) Also: update tests for PaddlePaddle 3.x, add uv.lock, update docs.
1 parent 4588c04 commit b9241fc

40 files changed

+1368
-443
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ paddle2onnx/*.so
2020
paddle2onnx/__pycache_*
2121
protobuf-*
2222

23+
# Python / uv
24+
.venv
25+
__pycache__/
26+
*.egg-info/
27+
.hypothesis/
28+
2329
# Temporary files automatically generated when executing Paddle2ONNX unit tests
2430
*.pdmodel
2531
*.pdiparams

.gitmodules

Lines changed: 0 additions & 15 deletions
This file was deleted.

CMakeLists.txt

Lines changed: 106 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
cmake_minimum_required(VERSION 3.16)
22
project(paddle2onnx C CXX)
3-
# ONNX 1.20 requires C++ 17
43
set(CMAKE_CXX_STANDARD 17)
54
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
6-
# Build the libraries with - fPIC
75
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
8-
# Always link with libstdc++ fs.a when using GCC 8.
96
link_libraries(
107
"$<$<AND:$<CXX_COMPILER_ID:GNU>,$<VERSION_LESS:$<CXX_COMPILER_VERSION>,9.0>>:-lstdc++fs>"
118
)
129

13-
option(WITH_STATIC "Compile Paddle2ONNX with STATIC" OFF)
10+
option(WITH_STATIC "Compile Paddle2ONNX with STATIC" OFF)
1411
option(PADDLE2ONNX_DEBUG "If open the debug log while converting model" OFF)
15-
option(MSVC_STATIC_CRT "Compile Paddle2ONNX with MSVC STATIC CRT" OFF)
12+
option(MSVC_STATIC_CRT "Compile Paddle2ONNX with MSVC STATIC CRT" OFF)
1613
if(MSVC)
1714
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
1815
add_definitions(-DPADDLE_WITH_TESTING)
@@ -22,12 +19,9 @@ if(PADDLE2ONNX_DEBUG)
2219
add_definitions(-DPADDLE2ONNX_DEBUG)
2320
endif()
2421

25-
# Set max opset version for onnx if you build from other version of onnx this
26-
# should be modified.
2722
add_definitions(-DMAX_ONNX_OPSET_VERSION=25)
2823
add_definitions(-DPADDLE2ONNX_LIB)
2924

30-
# Internal flags for convert.h.in
3125
set(WITH_PADDLE2ONNX_STATIC_INTERNAL OFF)
3226
if(WITH_STATIC)
3327
set(WITH_PADDLE2ONNX_STATIC_INTERNAL
@@ -40,115 +34,135 @@ include(utils)
4034
configure_file(${PROJECT_SOURCE_DIR}/paddle2onnx/mappers_registry.h.in
4135
${PROJECT_SOURCE_DIR}/paddle2onnx/mappers_registry.h)
4236

43-
# Third dependency : onnx
44-
if(NOT TARGET onnx_proto)
45-
if(NOT ONNX_NAMESPACE)
46-
set(ONNX_NAMESPACE "onnx")
47-
endif()
48-
add_definitions("-DONNX_NAMESPACE=${ONNX_NAMESPACE}")
49-
add_subdirectory(${PROJECT_SOURCE_DIR}/third_party/onnx)
37+
# --- Dependencies ---
38+
39+
# Protobuf (system-installed, CONFIG mode for proper targets)
40+
find_package(Protobuf REQUIRED CONFIG)
41+
42+
include(FetchContent)
43+
44+
# ONNX (built from source for protobuf compatibility)
45+
if(NOT ONNX_NAMESPACE)
46+
set(ONNX_NAMESPACE "onnx")
47+
endif()
48+
add_definitions("-DONNX_NAMESPACE=${ONNX_NAMESPACE}")
49+
50+
set(ONNX_USE_LITE_PROTO OFF CACHE BOOL "" FORCE)
51+
set(ONNX_ML ON CACHE BOOL "" FORCE)
52+
set(ONNX_USE_PROTOBUF_SHARED_LIBS OFF CACHE BOOL "" FORCE)
53+
set(ONNX_BUILD_TESTS OFF CACHE BOOL "" FORCE)
54+
set(ONNX_BUILD_BENCHMARKS OFF CACHE BOOL "" FORCE)
55+
56+
FetchContent_Declare(
57+
onnx
58+
GIT_REPOSITORY https://github.com/onnx/onnx.git
59+
GIT_TAG v1.20.1
60+
GIT_SHALLOW TRUE
61+
)
62+
FetchContent_MakeAvailable(onnx)
63+
64+
# onnx-optimizer: fetch source only, compile directly
65+
FetchContent_Declare(
66+
onnx_optimizer
67+
GIT_REPOSITORY https://github.com/onnx/optimizer.git
68+
GIT_TAG b3a4611861734e0731bbcc2bed1f080139e4988b # v0.3.19
69+
GIT_SHALLOW FALSE
70+
PATCH_COMMAND ${CMAKE_COMMAND} -P
71+
${CMAKE_CURRENT_SOURCE_DIR}/cmake/patch_optimizer.cmake
72+
)
73+
FetchContent_Populate(onnx_optimizer)
74+
75+
# pybind11 (system-installed)
76+
find_package(pybind11 REQUIRED)
77+
78+
# glog (system-installed)
79+
if(MSVC)
80+
set(WITH_GFLAGS
81+
OFF
82+
CACHE BOOL "Disable gflags support in glog")
5083
endif()
84+
find_package(glog REQUIRED)
5185

5286
# generate Paddle2ONNX proto files
5387
add_subdirectory(${PROJECT_SOURCE_DIR}/paddle2onnx/proto)
5488

5589
include_directories(${PROJECT_SOURCE_DIR})
5690
include_directories(${CMAKE_CURRENT_BINARY_DIR})
57-
include_directories(${PROJECT_SOURCE_DIR}/third_party/optimizer)
91+
include_directories(${onnx_optimizer_SOURCE_DIR})
5892

59-
file(GLOB_RECURSE ALL_SRCS ${PROJECT_SOURCE_DIR}/paddle2onnx/*.cc
60-
${PROJECT_SOURCE_DIR}/third_party/optimizer/onnx/optimizer/*.cc)
93+
# Collect sources: paddle2onnx + onnx-optimizer (compiled in)
94+
file(GLOB_RECURSE OPTIMIZER_SRCS
95+
${onnx_optimizer_SOURCE_DIR}/onnxoptimizer/*.cc)
96+
file(GLOB_RECURSE ALL_SRCS ${PROJECT_SOURCE_DIR}/paddle2onnx/*.cc)
97+
list(APPEND ALL_SRCS ${OPTIMIZER_SRCS})
6198
list(REMOVE_ITEM ALL_SRCS ${PROJECT_SOURCE_DIR}/paddle2onnx/cpp2py_export.cc)
99+
list(REMOVE_ITEM ALL_SRCS
100+
${onnx_optimizer_SOURCE_DIR}/onnxoptimizer/cpp2py_export.cc)
62101

63102
file(READ "${PROJECT_SOURCE_DIR}/VERSION_NUMBER" PADDLE2ONNX_VERSION)
64103
string(STRIP "${PADDLE2ONNX_VERSION}" PADDLE2ONNX_VERSION)
65104

105+
# find Python site-packages and PaddlePaddle
106+
set(PYTHON_SITE_PACKAGES "")
107+
execute_process(
108+
COMMAND ${PYTHON_EXECUTABLE} -c
109+
"import site; print(';'.join(site.getsitepackages()))"
110+
OUTPUT_VARIABLE SITE_PACKAGES_LIST
111+
OUTPUT_STRIP_TRAILING_WHITESPACE)
112+
string(REPLACE ";" " " SITE_PACKAGES_LIST "${SITE_PACKAGES_LIST}")
113+
separate_arguments(SITE_PACKAGES_LIST)
114+
foreach(SITE_PACKAGES IN LISTS SITE_PACKAGES_LIST)
115+
if(EXISTS "${SITE_PACKAGES}" AND SITE_PACKAGES MATCHES "site-packages")
116+
string(REPLACE "\\" "/" PYTHON_SITE_PACKAGES "${SITE_PACKAGES}")
117+
message(STATUS "Found valid site-packages path: ${PYTHON_SITE_PACKAGES}.")
118+
break()
119+
endif()
120+
endforeach()
121+
if(NOT PYTHON_SITE_PACKAGES)
122+
message(FATAL_ERROR "No valid site-packages path found.")
123+
endif()
124+
125+
if(MSVC)
126+
set(PADDLE_LIB "${PYTHON_SITE_PACKAGES}/paddle/base/libpaddle.lib")
127+
set(COMMON_LIB "${PYTHON_SITE_PACKAGES}/paddle/libs/common.lib")
128+
else()
129+
set(PADDLE_LIB "${PYTHON_SITE_PACKAGES}/paddle/base/libpaddle.so")
130+
endif()
131+
if(EXISTS ${PADDLE_LIB})
132+
message(STATUS "Found libpaddle: ${PADDLE_LIB}.")
133+
else()
134+
message(
135+
FATAL_ERROR
136+
"Can not find libpaddle at ${PYTHON_SITE_PACKAGES}/paddle/base!")
137+
endif()
138+
set(PADDLE_INCLUDE_DIR ${PYTHON_SITE_PACKAGES}/paddle/include/)
139+
include_directories(${PADDLE_INCLUDE_DIR})
140+
141+
if(MSVC)
142+
add_custom_target(
143+
pir_patch ALL
144+
COMMAND
145+
${CMAKE_COMMAND} -E copy_if_different
146+
${CMAKE_SOURCE_DIR}/patch/paddle/interface_support.h
147+
${PADDLE_INCLUDE_DIR}/paddle/pir/include/core/interface_support.h
148+
COMMENT "PIR patch for MSVC")
149+
endif()
150+
66151
if(WITH_STATIC)
67-
# Here, we use a dummy target (paddle2onnx_dummy) to form a build dependency
68-
# tree for paddle2onnx_static lib.
69152
add_library(paddle2onnx_dummy STATIC ${ALL_SRCS})
70-
71-
if(APPLE)
72-
set_target_properties(paddle2onnx_dummy PROPERTIES COMPILE_FLAGS
73-
"-fvisibility=hidden")
74-
elseif(MSVC)
75-
message("------ BUILD WITH MSVC --------")
76-
else()
153+
if(NOT MSVC)
77154
set_target_properties(paddle2onnx_dummy PROPERTIES COMPILE_FLAGS
78155
"-fvisibility=hidden")
79156
endif()
80157
target_link_libraries(paddle2onnx_dummy p2o_paddle_proto onnx)
81-
# Bundle paddle2onnx static lib here
82158
bundle_static_library(paddle2onnx_dummy paddle2onnx_static bundle_paddle2onnx)
83159
else()
84160
add_library(paddle2onnx SHARED ${ALL_SRCS})
85-
86-
# add pybind11
87-
add_subdirectory(third_party/pybind11)
88-
89-
# add glog
90-
if(MSVC)
91-
set(WITH_GFLAGS
92-
OFF
93-
CACHE BOOL "Disable gflags support in glog")
94-
endif()
95-
add_subdirectory(third_party/glog)
96-
97-
# find Python site-packages dir
98-
set(PYTHON_SITE_PACKAGES "")
99-
execute_process(
100-
COMMAND ${PYTHON_EXECUTABLE} -c
101-
"import site; print(';'.join(site.getsitepackages()))"
102-
OUTPUT_VARIABLE SITE_PACKAGES_LIST
103-
OUTPUT_STRIP_TRAILING_WHITESPACE)
104-
string(REPLACE ";" " " SITE_PACKAGES_LIST "${SITE_PACKAGES_LIST}")
105-
separate_arguments(SITE_PACKAGES_LIST)
106-
foreach(SITE_PACKAGES IN LISTS SITE_PACKAGES_LIST)
107-
if(EXISTS "${SITE_PACKAGES}" AND SITE_PACKAGES MATCHES "site-packages")
108-
string(REPLACE "\\" "/" PYTHON_SITE_PACKAGES "${SITE_PACKAGES}")
109-
message(STATUS "Found valid site-packages path: ${PYTHON_SITE_PACKAGES}.")
110-
break()
111-
endif()
112-
endforeach()
113-
if(NOT PYTHON_SITE_PACKAGES)
114-
message(FATAL_ERROR "No valid site-packages path found.")
115-
endif()
116-
117-
if(MSVC)
118-
set(PADDLE_LIB "${PYTHON_SITE_PACKAGES}/paddle/base/libpaddle.lib")
119-
set(COMMON_LIB "${PYTHON_SITE_PACKAGES}/paddle/libs/common.lib")
120-
else()
121-
set(PADDLE_LIB "${PYTHON_SITE_PACKAGES}/paddle/base/libpaddle.so")
122-
endif()
123-
if(EXISTS ${PADDLE_LIB})
124-
message(STATUS "Found libpaddle: ${PADDLE_LIB}.")
125-
else()
126-
message(
127-
FATAL_ERROR
128-
"Can not find libpaddle at ${PYTHON_SITE_PACKAGES}/paddle/base!")
129-
endif()
130-
# add paddle include dir
131-
set(PADDLE_INCLUDE_DIR ${PYTHON_SITE_PACKAGES}/paddle/include/)
132-
include_directories(${PADDLE_INCLUDE_DIR})
133-
if(MSVC)
134-
# PIR patch
135-
add_custom_target(
136-
pir_patch ALL
137-
COMMAND
138-
${CMAKE_COMMAND} -E copy_if_different
139-
${CMAKE_SOURCE_DIR}/patch/paddle/interface_support.h
140-
${PADDLE_INCLUDE_DIR}/paddle/pir/include/core/interface_support.h
141-
COMMENT
142-
"Replace file ${PADDLE_INCLUDE_DIR}/paddle/pir/include/core/interface_support.h with ${CMAKE_SOURCE_DIR}/patch/paddle/interface_support.h"
143-
)
144-
endif()
145161
if(APPLE)
146162
set_target_properties(paddle2onnx PROPERTIES LINK_FLAGS
147163
"-undefined dynamic_lookup")
148164
elseif(MSVC)
149165
message("------ BUILD WITH MSVC --------")
150-
# remove `interface` macro defined in MSVC
151-
# target_compile_options(paddle2onnx PRIVATE /Uinterface)
152166
else()
153167
set_target_properties(paddle2onnx PROPERTIES COMPILE_FLAGS
154168
"-fvisibility=hidden")
@@ -211,32 +225,20 @@ if(BUILD_PADDLE2ONNX_PYTHON)
211225
PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
212226
target_include_directories(
213227
paddle2onnx_cpp2py_export
214-
PRIVATE $<BUILD_INTERFACE:${ONNX_ROOT}>
215-
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
228+
PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
216229
$<INSTALL_INTERFACE:include>
217230
${PYTHON_INCLUDE_DIR}
218231
${PADDLE_INCLUDE_DIR})
219232

220-
if(EXISTS
221-
${PROJECT_SOURCE_DIR}/third_party/pybind11/include/pybind11/pybind11.h)
222-
target_include_directories(
223-
paddle2onnx_cpp2py_export
224-
PUBLIC ${PROJECT_SOURCE_DIR}/third_party/pybind11/include)
225-
else()
226-
message(FATAL_ERROR "cannot find pybind")
227-
endif()
228-
229233
if(APPLE)
230234
set_target_properties(paddle2onnx_cpp2py_export
231235
PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
232236
target_link_libraries(paddle2onnx_cpp2py_export
233237
PRIVATE -Wl,-force_load,$<TARGET_FILE:onnx>)
234238
elseif(MSVC)
235-
# In MSVC, we will add whole archive in default
236239
target_link_libraries(paddle2onnx_cpp2py_export
237240
PRIVATE -WHOLEARCHIVE:$<TARGET_FILE:onnx>)
238241
else()
239-
# Assume everything else is like gcc
240242
target_link_libraries(
241243
paddle2onnx_cpp2py_export
242244
PRIVATE "-Wl,--whole-archive" $<TARGET_FILE:onnx>
@@ -253,20 +255,6 @@ if(BUILD_PADDLE2ONNX_PYTHON)
253255
target_link_libraries(paddle2onnx_cpp2py_export PRIVATE ${PYTHON_LIBRARIES})
254256
target_compile_options(
255257
paddle2onnx_cpp2py_export
256-
PRIVATE /MP
257-
/wd4244 # 'argument': conversion from 'google::protobuf::uint64'
258-
# to 'int', possible loss of data
259-
/wd4267 # Conversion from 'size_t' to 'int', possible loss of data
260-
/wd4996 # The second parameter is ignored.
261-
${EXTRA_FLAGS})
262-
if(ONNX_USE_PROTOBUF_SHARED_LIBS)
263-
target_compile_options(
264-
onnx_cpp2py_export
265-
PRIVATE /wd4251 # 'identifier' : class 'type1' needs to have
266-
# dll-interface to be used by clients of class 'type2'
267-
)
268-
endif()
269-
add_msvc_runtime_flag(paddle2onnx_cpp2py_export)
270-
add_onnx_global_defines(paddle2onnx_cpp2py_export)
258+
PRIVATE /MP /wd4244 /wd4267 /wd4996 ${EXTRA_FLAGS})
271259
endif()
272260
endif()

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ Paddle2ONNX 支持将 **PaddlePaddle** 模型格式转化到 **ONNX** 模型格
88

99
## 2 Paddle2ONNX 环境依赖
1010

11-
Paddle2ONNX 依赖PaddlePaddle3.0,我们建议您在以下环境下使用 Paddle2ONNX :
11+
Paddle2ONNX 依赖 PaddlePaddle 3.3+,我们建议您在以下环境下使用 Paddle2ONNX :
1212

13-
- PaddlePaddle == 3.0.0
13+
- Python >= 3.10
14+
- PaddlePaddle >= 3.3
15+
- ONNX >= 1.20.1
16+
- protobuf >= 34.0
1417
- onnxruntime >= 1.10.0
1518

1619
## 3 安装 Paddle2ONNX

README_en.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ Paddle2ONNX supports the conversion of PaddlePaddle model format to ONNX model f
88

99
## 2 Paddle2ONNX Environment Dependencies
1010

11-
Paddle2ONNX depends on paddle3.0, we recommend using Paddle2ONNX in the following environments:
11+
Paddle2ONNX depends on PaddlePaddle 3.3+, we recommend using Paddle2ONNX in the following environments:
1212

13-
- PaddlePaddle == 3.0.0b2
13+
- Python >= 3.10
14+
- PaddlePaddle >= 3.3
15+
- ONNX >= 1.20.1
16+
- protobuf >= 34.0
1417
- onnxruntime >= 1.10.0
1518

1619
## 3 Install Paddle2ONNX

cmake/patch_optimizer.cmake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Patch onnx-optimizer for compatibility with ONNX 1.20.1
2+
# Dimension constructor is now explicit, so fix implicit conversions
3+
4+
file(READ "onnxoptimizer/passes/fuse_add_bias_into_conv.h" content)
5+
string(REPLACE
6+
"std::vector<Dimension> s = {1};"
7+
"std::vector<Dimension> s = {Dimension(1)};"
8+
content "${content}")
9+
file(WRITE "onnxoptimizer/passes/fuse_add_bias_into_conv.h" "${content}")

0 commit comments

Comments
 (0)