Skip to content

Commit d3145f5

Browse files
committed
Support ABI3
Signed-off-by: take-cheeze <[email protected]>
1 parent f74bb6c commit d3145f5

File tree

7 files changed

+50
-15
lines changed

7 files changed

+50
-15
lines changed

.github/workflows/build-and-test.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ jobs:
4646
submodules: recursive
4747
- name: Build wheels
4848
uses: pypa/[email protected]
49+
- name: Check ABI3
50+
run: |
51+
pip install abi3audit
52+
abi3audit --strict -v --report ./wheelhouse/*.whl
53+
if: ${{ contains(fromJSON('["cp312", "cp313"]'), matrix.python) }}
4954
- uses: actions/upload-artifact@v4
5055
with:
5156
name: artifact-${{ matrix.os }}-${{ matrix.python }}

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
[submodule "third_party/protobuf"]
55
path = third_party/protobuf
66
url = https://github.com/protocolbuffers/protobuf.git
7+
[submodule "third_party/nanobind"]
8+
path = third_party/nanobind
9+
url = https://github.com/wjakob/nanobind.git

CMakeLists.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,12 @@ target_include_directories(onnx_optimizer_c_api PUBLIC
6767
)
6868

6969
if(ONNX_BUILD_PYTHON)
70-
find_package(Python3 REQUIRED COMPONENTS Interpreter Development.Module)
71-
find_package(pybind11)
72-
pybind11_add_module(onnx_opt_cpp2py_export "onnxoptimizer/cpp2py_export.cc")
70+
find_package(
71+
Python 3
72+
REQUIRED COMPONENTS Interpreter Development.Module
73+
OPTIONAL_COMPONENTS Development.SABIModule)
74+
add_subdirectory(third_party/nanobind EXCLUDE_FROM_ALL)
75+
nanobind_add_module(onnx_opt_cpp2py_export "onnxoptimizer/cpp2py_export.cc" STABLE_ABI)
7376
target_link_libraries(onnx_opt_cpp2py_export PRIVATE onnx_optimizer)
7477
endif()
7578

cmake/utils.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ include(${PROJECT_SOURCE_DIR}/third_party/onnx/cmake/Utils.cmake)
33
# Poor man's FetchContent
44
function(add_subdirectory_if_no_target dir target)
55
if (NOT TARGET ${target})
6-
add_subdirectory(${dir})
6+
add_subdirectory(${dir} EXCLUDE_FROM_ALL)
77
endif()
88
endfunction()
99

onnxoptimizer/cpp2py_export.cc

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,50 @@
22
* SPDX-License-Identifier: Apache-2.0
33
*/
44

5-
#include <pybind11/pybind11.h>
6-
#include <pybind11/stl.h>
5+
#include <nanobind/nanobind.h>
6+
#include <nanobind/stl/string.h>
7+
#include <nanobind/stl/vector.h>
78

8-
#include "onnx/py_utils.h"
99
#include "onnxoptimizer/model_util.h"
1010
#include "onnxoptimizer/optimize.h"
1111

1212
namespace ONNX_NAMESPACE {
13-
namespace py = pybind11;
14-
using namespace pybind11::literals;
15-
PYBIND11_MODULE(onnx_opt_cpp2py_export, onnx_opt_cpp2py_export) {
13+
namespace nb = nanobind;
14+
using namespace nanobind::literals;
15+
16+
template <typename Proto>
17+
bool ParseProtoFromPyBytes(Proto* proto, const nb::bytes& bytes) {
18+
// Get the buffer from Python bytes object
19+
char* buffer = nullptr;
20+
Py_ssize_t length = 0;
21+
PyBytes_AsStringAndSize(bytes.ptr(), &buffer, &length);
22+
23+
return ParseProtoFromBytes(proto, buffer, length);
24+
}
25+
26+
NB_MODULE(onnx_opt_cpp2py_export, onnx_opt_cpp2py_export) {
1627
onnx_opt_cpp2py_export.doc() = "ONNX Optimizer";
1728

1829
onnx_opt_cpp2py_export.def(
1930
"optimize",
20-
[](const py::bytes& bytes, const std::vector<std::string>& names) {
31+
[](const nb::bytes& bytes, const std::vector<std::string>& names) {
2132
ModelProto proto{};
2233
ParseProtoFromPyBytes(&proto, bytes);
2334
auto const result = optimization::Optimize(proto, names);
2435
std::string out;
2536
result.SerializeToString(&out);
26-
return py::bytes(out);
37+
return nb::bytes(out.data(), out.size());
2738
});
2839

2940
onnx_opt_cpp2py_export.def(
3041
"optimize_fixedpoint",
31-
[](const py::bytes& bytes, const std::vector<std::string>& names) {
42+
[](const nb::bytes& bytes, const std::vector<std::string>& names) {
3243
ModelProto proto{};
3344
ParseProtoFromPyBytes(&proto, bytes);
3445
auto const result = optimization::OptimizeFixed(proto, names);
3546
std::string out;
3647
result.SerializeToString(&out);
37-
return py::bytes(out);
48+
return nb::bytes(out.data(), out.size());
3849
});
3950

4051
onnx_opt_cpp2py_export.def(

setup.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,21 @@ def run(self):
288288
# Extensions
289289
################################################################################
290290

291+
py_limited_api = sys.version_info[0] >= 3 and sys.version_info[1] >= 12
292+
if py_limited_api:
293+
setup_opts = {
294+
'bdist_wheel': {
295+
'py_limited_api': 'cp312'
296+
},
297+
}
298+
else:
299+
setup_opts = {}
300+
291301
ext_modules = [
292302
setuptools.Extension(
293303
name=str('onnxoptimizer.onnx_opt_cpp2py_export'),
294-
sources=[])
304+
sources=[],
305+
py_limited_api=py_limited_api)
295306
]
296307

297308
################################################################################
@@ -348,4 +359,5 @@ def run(self):
348359
'onnxoptimizer=onnxoptimizer:main',
349360
],
350361
},
362+
options=setup_opts,
351363
)

third_party/nanobind

Submodule nanobind added at ab3456f

0 commit comments

Comments
 (0)