onnx-extended: extensions for onnx and onnxruntime
onnx-extended extends the list of supported operators in onnx reference implementation and onnxruntime, or implements faster versions in C++. Documentation onnx-extended. Source are available on github/onnx-extended.
Use a C++ implementation of existing operators
import timeit
import numpy as np
from onnx import TensorProto
from onnx.helper import (
make_graph,
make_model,
make_node,
make_opsetid,
make_tensor_value_info,
)
from onnx.reference import ReferenceEvaluator
from onnxruntime import InferenceSession
from onnx_extended.ext_test_case import measure_time
from onnx_extended.reference import CReferenceEvaluator
X = make_tensor_value_info("X", TensorProto.FLOAT, [None, None, None, None])
Y = make_tensor_value_info("Y", TensorProto.FLOAT, [None, None, None, None])
B = make_tensor_value_info("B", TensorProto.FLOAT, [None, None, None, None])
W = make_tensor_value_info("W", TensorProto.FLOAT, [None, None, None, None])
node = make_node(
"Conv",
["X", "W", "B"],
["Y"],
pads=[1, 1, 1, 1],
dilations=[1, 1],
strides=[2, 2],
)
graph = make_graph([node], "g", [X, W, B], [Y])
onnx_model = make_model(graph, opset_imports=[make_opsetid("", 16)])
sH, sW = 64, 64
X = np.arange(sW * sH).reshape((1, 1, sH, sW)).astype(np.float32)
W = np.ones((1, 1, 3, 3), dtype=np.float32)
B = np.array([[[[0]]]], dtype=np.float32)
sess1 = ReferenceEvaluator(onnx_model)
sess2 = CReferenceEvaluator(onnx_model) # 100 times faster
expected = sess1.run(None, {"X": X, "W": W, "B": B})[0]
got = sess2.run(None, {"X": X, "W": W, "B": B})[0]
diff = np.abs(expected - got).max()
print(f"difference: {diff}")
f1 = lambda: sess1.run(None, {"X": X, "W": W, "B": B})[0]
f2 = lambda: sess2.run(None, {"X": X, "W": W, "B": B})[0]
print("onnx:", timeit.timeit(f1, globals=globals(), number=5))
print("onnx-extended:", timeit.timeit(f2, globals=globals(), number=5))
difference: 0.0 onnx: 0.024006774998269975 onnx-extended: 0.0002316169993719086
Build with CUDA, openmp, eigen, onnxruntime
The package also contains some dummy examples on how to build with C++ functions (pybind11, cython), with openmp, eigen with or without CUDA. It also shows how to create a custom operator for onnxruntime in C++.
The version released on pypi/onnx-extended only works on CPU. It needs to be manually built to enable the code using CUDA. The build will automatically link with CUDA if it is found. If not, some extensions might not be available.
python setup.py build_ext --inplace # pip install -e .
It is possible to use a specific version of CUDA:
python setup.py build_ext --inplace --cuda-version=13.0 --use-cuda=1 # or (not working yet) # CMAKE_CXX_COMPILER_LAUNCHER=ccache USE_CUDA=1 CUDA_VERSION=13.0 pip install -e . -v --no-build-isolation --no-clean pip install -e . -v --no-build-isolation --no-clean
NVTX can be enabled with the following command:
python setup.py build_ext --inplace --use_nvtx 1 # or (not working yet) USE_NVTX=1 pip install -e . -v
Experimental cython binding for onnxruntime
The python onnxruntime package relies on pybind11 to expose its functionalities. onnx-extended tries to build a cython wrapper around the C/C++ API of onnxruntime. cython relies on python C API and is faster than pybind11. This different may be significant when onnxruntime is used on small graphs and tensors.
Custom kernels for onnxruntime
onnxruntime provides an API to add custom implementation for existing or new onnx operators. An example for CPU.
from onnxruntime import InferenceSession, SessionOptions
from onnx_extended.ortops.optim.cpu import get_ort_ext_libs
r = get_ort_ext_libs()
opts = SessionOptions()
if r is not None:
opts.register_custom_ops_library(r[0])
sess_cus = InferenceSession(
onx_modified.SerializeToString(), opts, providers=["CPUExecutionProvider"]
)
Metadata
Release files for onnx-extended 0.5.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| onnx_extended-0.5.0.tar.gz | 17.9 MB | Details |
Built distributions (wheels)
Total release size: 754.8 MB
Release files / onnx_extended-0.5.0.tar.gz
| Download URL | onnx_extended-0.5.0.tar.gz |
|---|---|
| Size | 17.9 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2bb17f4cec65bee998508837276100a33df5b5e6ea0b84a1eaebda1cbd8eb56d
|
|
BLAKE2b-256 checksum How to use checksums |
e1c218b854b017bd95c693fe12b04f3528dd0de3ae0621b39281f2290ea589f5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / onnx_extended-0.5.0-cp313-cp313-win_amd64.whl
| Download URL | onnx_extended-0.5.0-cp313-cp313-win_amd64.whl |
|---|---|
| Size | 81.5 MB |
| Tags | CPython 3.13 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
19c9c79d732ecd513c3d23059d6b374ba941a1df4710a402c03da76c70daf155
|
|
BLAKE2b-256 checksum How to use checksums |
62d8d96598cda174d600b174bd23fd51dc26a4ea6d5b0b4ae95494dde47b4e60
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / onnx_extended-0.5.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | onnx_extended-0.5.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 36.0 MB |
| Tags | CPython 3.13 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
c88eaa9e526821bf7ed3d5867ae585eba6c5d845b6a4f8c38b203972b0d719f7
|
|
BLAKE2b-256 checksum How to use checksums |
d3d5d7ad528e374c4f14d0169d5e006d3cf7df0543defcdfb10d38a0414f2e1c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / onnx_extended-0.5.0-cp313-cp313-macosx_15_0_arm64.whl
| Download URL | onnx_extended-0.5.0-cp313-cp313-macosx_15_0_arm64.whl |
|---|---|
| Size | 66.9 MB |
| Tags | CPython 3.13 macOS 15.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
3b5fb2eaa7ec7ba39380410bfc537dfa1e11fdfd245cccf34b51d1f6f33b9f97
|
|
BLAKE2b-256 checksum How to use checksums |
d574b23b2fc32d3f34a78f695955f344d48cbec4b16e787bb3c76ec89c5776af
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / onnx_extended-0.5.0-cp312-cp312-win_amd64.whl
| Download URL | onnx_extended-0.5.0-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 81.5 MB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
8490b39af9da9dd439d03b0213c3d9c20a660f3aba3590ac0e9fdad6342f437e
|
|
BLAKE2b-256 checksum How to use checksums |
a800e3cd9d3f50b901f48a832c78d94cf1cee73769d927206ae4aedd1ea1ee10
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / onnx_extended-0.5.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | onnx_extended-0.5.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 36.0 MB |
| Tags | CPython 3.12 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
0c301e23dfe30da636c36047bc3bdadc1423370a622643976cae8f91e0fe288a
|
|
BLAKE2b-256 checksum How to use checksums |
501cc74d1b4acf602b7405a3f18c5bbd2df35ebf8642b653a6663fa4f14a1f24
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / onnx_extended-0.5.0-cp312-cp312-macosx_15_0_arm64.whl
| Download URL | onnx_extended-0.5.0-cp312-cp312-macosx_15_0_arm64.whl |
|---|---|
| Size | 66.9 MB |
| Tags | CPython 3.12 macOS 15.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
36d727b97cf1ba5d6d2684619349815947725f822a2ff1d90336c935fe5d6a5c
|
|
BLAKE2b-256 checksum How to use checksums |
605b0e6228b4acae68eeaa0311fabeb67f0ab5cae911a84ed3e83704eb8bdddd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / onnx_extended-0.5.0-cp311-cp311-win_amd64.whl
| Download URL | onnx_extended-0.5.0-cp311-cp311-win_amd64.whl |
|---|---|
| Size | 81.5 MB |
| Tags | CPython 3.11 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
4a60e8e311b09d43c9d4276f258336d12ec9fd94f8d36134b3d3dc9a3a66694c
|
|
BLAKE2b-256 checksum How to use checksums |
ae2434c8fc69fba0be568abb17aad742f1b53773bb3ed0e7cf3a37e26a477aaa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / onnx_extended-0.5.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | onnx_extended-0.5.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 36.0 MB |
| Tags | CPython 3.11 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
b92ffca9d4505996b47ba0c41feec8ec1a69a0a7a502e83a9229ec876c428b18
|
|
BLAKE2b-256 checksum How to use checksums |
b76681d11ba6af057c37177c714032299d43b47037dd390b3355b1a58bde88ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / onnx_extended-0.5.0-cp311-cp311-macosx_15_0_arm64.whl
| Download URL | onnx_extended-0.5.0-cp311-cp311-macosx_15_0_arm64.whl |
|---|---|
| Size | 66.9 MB |
| Tags | CPython 3.11 macOS 15.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
3d31d147061a9153d83c8d0b2c9843543c5c021db9f0ead7aa739e5ce6b51c6d
|
|
BLAKE2b-256 checksum How to use checksums |
37c9a8906146a6cc83d23b5bde460228bb9e78a9ef57fd5c9a0290bee106d59e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / onnx_extended-0.5.0-cp310-cp310-win_amd64.whl
| Download URL | onnx_extended-0.5.0-cp310-cp310-win_amd64.whl |
|---|---|
| Size | 81.3 MB |
| Tags | CPython 3.10 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
5003b54c2540c62fb579394a487eebb8c7b5873198d281c73217a867411dc88e
|
|
BLAKE2b-256 checksum How to use checksums |
ac89ec4b1a716c9d782a960b7faedf91895e7fcfeed7b11b5cf303355d67a991
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / onnx_extended-0.5.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
| Download URL | onnx_extended-0.5.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl |
|---|---|
| Size | 35.8 MB |
| Tags | CPython 3.10 Linux glibc 2.27+ x86-64 Linux glibc 2.28+ x86-64 |
|
SHA-256 checksum How to use checksums |
eddfbf3f98bbaa944179918474ddfd7325ea77785ec44c3a0bc613df25ca7f18
|
|
BLAKE2b-256 checksum How to use checksums |
ef6c7289e0f1ef8c9a9bbe0c66f172e5d3fde2d19534faf2c6703aaa0ef04f2c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|
Release files / onnx_extended-0.5.0-cp310-cp310-macosx_15_0_arm64.whl
| Download URL | onnx_extended-0.5.0-cp310-cp310-macosx_15_0_arm64.whl |
|---|---|
| Size | 66.7 MB |
| Tags | CPython 3.10 macOS 15.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
bcc15549e8f199131e569f5a676cde321ea50517a2e87da412d19abbe29800b5
|
|
BLAKE2b-256 checksum How to use checksums |
19cc84a75ac8c3665a65cf7e651893a289b374d7c7b72ab22617650f32c10015
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.3
|