Skip to content

Commit e90c53f

Browse files
authored
Merge branch 'main' into andife-patch-9
2 parents d25a2cd + 73364eb commit e90c53f

File tree

10 files changed

+169
-141
lines changed

10 files changed

+169
-141
lines changed

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ jobs:
2222
CIBW_BEFORE_BUILD_LINUX: pip install protobuf
2323
CIBW_BEFORE_BUILD_WINDOWS: python -m pip install protobuf
2424
CIBW_BEFORE_BUILD_MACOS: pip install protobuf
25-
CIBW_TEST_REQUIRES_LINUX: pytest pytest-xdist flake8 mypy onnxruntime
25+
CIBW_TEST_REQUIRES_LINUX: pytest pytest-xdist flake8-pyproject mypy onnxruntime
2626
CIBW_TEST_REQUIRES_MACOS: pytest pytest-xdist
2727
CIBW_TEST_REQUIRES_WINDOWS: pytest pytest-xdist
2828
CIBW_BEFORE_TEST_LINUX: pip install torch==2.6.0+cpu torchvision==0.21.0+cpu --index-url https://download.pytorch.org/whl/cpu
2929
CIBW_TEST_COMMAND: pytest {project}/onnxoptimizer/test
30-
CIBW_TEST_COMMAND_LINUX: cd {project} && flake8 && pytest
30+
CIBW_TEST_COMMAND_LINUX: cd {project} && flake8p && pytest
3131
CIBW_TEST_SKIP: " *_arm64"
3232
CIBW_ENVIRONMENT: CMAKE_ARGS="-DONNX_USE_PROTOBUF_SHARED_LIBS=OFF -DProtobuf_USE_STATIC_LIBS=ON -DONNX_USE_LITE_PROTO=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5"
3333
CIBW_ENVIRONMENT_WINDOWS: USE_MSVC_STATIC_RUNTIME=0 CMAKE_ARGS="-DONNX_USE_PROTOBUF_SHARED_LIBS=OFF -DProtobuf_USE_STATIC_LIBS=ON -DONNX_USE_LITE_PROTO=ON"
@@ -45,8 +45,13 @@ jobs:
4545
with:
4646
submodules: recursive
4747
- name: Build wheels
48-
uses: pypa/cibuildwheel@v3.2.0
49-
- uses: actions/upload-artifact@v4
48+
uses: pypa/cibuildwheel@v3.2.1
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) }}
54+
- uses: actions/upload-artifact@v5
5055
with:
5156
name: artifact-${{ matrix.os }}-${{ matrix.python }}
5257
path: ./wheelhouse/*.whl
@@ -59,8 +64,6 @@ jobs:
5964
with:
6065
submodules: recursive
6166

62-
- run: python3 -m pip install protobuf
63-
6467
- name: Build sdist
6568
run: pipx run build --sdist
6669

@@ -71,7 +74,7 @@ jobs:
7174
python3 -m pip install *.tar.gz
7275
python3 -c "import onnxoptimizer; print(onnxoptimizer.get_fuse_and_elimination_passes())"
7376
74-
- uses: actions/upload-artifact@v4
77+
- uses: actions/upload-artifact@v5
7578
with:
7679
path: dist/*.tar.gz
7780

@@ -87,7 +90,7 @@ jobs:
8790
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v')
8891
needs: [build_wheels, build_sdist]
8992
steps:
90-
- uses: actions/download-artifact@v5
93+
- uses: actions/download-artifact@v6
9194
with:
9295
name: artifact
9396
path: dist

.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

MANIFEST.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
recursive-include onnxoptimizer *.h *.c *.cc *.proto
1+
recursive-include onnxoptimizer *.h *.cc
22
recursive-include examples *
33
recursive-include cmake *
44
recursive-include tools *
55
recursive-include third_party *
66
include VERSION_NUMBER
77
include CMakeLists.txt
8+
9+
recursive-exclude third_party *.onnx *.pb *.cs *.rs

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(

pyproject.toml

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
[project]
2+
dynamic = ["version"]
3+
name = "onnxoptimizer"
4+
description = "ONNX Optimizer"
5+
license = 'Apache-2.0'
6+
license-files = [
7+
"LICENSE"
8+
]
9+
authors = [
10+
{name = 'ONNX Optimizer Authors', email = "onnx-technical-discuss@lists.lfai.foundation"},
11+
]
12+
readme = "README.md"
13+
keywords = [
14+
"deep-learning",
15+
"ONNX",
16+
]
17+
dependencies = [
18+
"onnx",
19+
]
20+
21+
[project.urls]
22+
Homepage = 'https://github.com/onnx/optimizer'
23+
24+
[console_scripts]
25+
onnxoptimizer = "onnxoptimizer:main"
26+
27+
[build-system]
28+
requires = [
29+
"setuptools>=68",
30+
"wheel",
31+
"cmake>=3.22",
32+
"protobuf>=4.25.1"
33+
]
34+
build-backend = "setuptools.build_meta"
35+
36+
[tool.pytest.ini_options]
37+
# addopts = --nbval --current-env
38+
addopts = "-n auto"
39+
testpaths = [
40+
"onnxoptimizer/test/"
41+
]
42+
43+
[[tool.mypy]]
44+
# follow-imports = silent # TODO remove this
45+
mypy_path = "stubs:third_party/onnx/third_party/pybind11"
46+
strict_optional = true
47+
warn_return_any = true
48+
warn_no_return = true
49+
# TODO warn_unused_ignores = true
50+
warn_redundant_casts = true
51+
warn_incomplete_stub = true
52+
# TODO disallow_untyped_calls = true
53+
check_untyped_defs = true
54+
disallow_any_generics = true
55+
no_implicit_optional = true
56+
# TODO disallow_incomplete_defs = true
57+
# TODO disallow_subclassing_any = true
58+
disallow_untyped_decorators = true
59+
warn_unused_configs = true
60+
61+
[[tool.mypy.overrides]]
62+
module = "onnxoptimizer.*"
63+
disallow_untyped_defs = true
64+
65+
[[tool.mypy.overrides]]
66+
module = "onnx_opt_cpp2py_export"
67+
ignore_missing_imports = true
68+
69+
[[tool.mypy.overrides]]
70+
module = "onnx.*"
71+
disallow_untyped_defs = true
72+
ignore_missing_imports = true
73+
74+
[[tool.mypy.overrides]]
75+
module = "tools.*"
76+
disallow_untyped_defs = true
77+
78+
# Ignore errors in setup.py
79+
[[tool.mypy.overrides]]
80+
module = "setup"
81+
ignore_errors = true
82+
83+
[tool.flake8]
84+
select = ["B","C","E","F","P","T4","W","B9"]
85+
max-line-length = 80
86+
### DEFAULT IGNORES FOR 4-space INDENTED PROJECTS ###
87+
# E127, E128 are hard to silence in certain nested formatting situations.
88+
# E265, E266 talk about comment formatting which is too opinionated.
89+
# E402 warns on imports coming after statements. There are important use cases
90+
# like demandimport (https://fburl.com/demandimport) that require statements
91+
# before imports.
92+
# E501 is not flexible enough, we're using B950 instead.
93+
# E722 is a duplicate of B001.
94+
# F405 is hard to silence since we indeed do star import
95+
# P207 is a duplicate of B003.
96+
# P208 is a duplicate of C403.
97+
# W503 talks about operator formatting which is too opinionated.
98+
# F401 clashes with PEP484 requiring us to import types that are only used in
99+
# type comments.
100+
ignore = ["E127", "E128", "E265", "E266", "E402", "E501", "E722", "F405", "P207", "P208", "W503", "F401"]
101+
exclude = [
102+
".git",
103+
"__pycache__",
104+
"build/*",
105+
"third_party/*",
106+
"*_pb2.py",
107+
".cache/*",
108+
".eggs",
109+
".setuptools-cmake-build*/*",
110+
]

setup.cfg

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

0 commit comments

Comments
 (0)