Skip to main content

Extends the list of supported operators in onnx reference implementation and onnxruntime, or implements faster versions in C++.

Project description

https://github.com/sdpython/onnx-extended/raw/main/_doc/_static/logo.png

onnx-extended: extensions for onnx and onnxruntime

https://dev.azure.com/xavierdupre3/onnx-extended/_apis/build/status/sdpython.onnx-extended https://badge.fury.io/py/onnx-extended.svg GitHub Issues MIT License size https://img.shields.io/badge/code%20style-black-000000.svg

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"]
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

onnx_extended-0.5.0.tar.gz (17.9 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

onnx_extended-0.5.0-cp313-cp313-win_amd64.whl (81.5 MB view details)

Uploaded CPython 3.13Windows x86-64

onnx_extended-0.5.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (36.0 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

onnx_extended-0.5.0-cp313-cp313-macosx_15_0_arm64.whl (66.9 MB view details)

Uploaded CPython 3.13macOS 15.0+ ARM64

onnx_extended-0.5.0-cp312-cp312-win_amd64.whl (81.5 MB view details)

Uploaded CPython 3.12Windows x86-64

onnx_extended-0.5.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (36.0 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

onnx_extended-0.5.0-cp312-cp312-macosx_15_0_arm64.whl (66.9 MB view details)

Uploaded CPython 3.12macOS 15.0+ ARM64

onnx_extended-0.5.0-cp311-cp311-win_amd64.whl (81.5 MB view details)

Uploaded CPython 3.11Windows x86-64

onnx_extended-0.5.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (36.0 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

onnx_extended-0.5.0-cp311-cp311-macosx_15_0_arm64.whl (66.9 MB view details)

Uploaded CPython 3.11macOS 15.0+ ARM64

onnx_extended-0.5.0-cp310-cp310-win_amd64.whl (81.3 MB view details)

Uploaded CPython 3.10Windows x86-64

onnx_extended-0.5.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (35.8 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

onnx_extended-0.5.0-cp310-cp310-macosx_15_0_arm64.whl (66.7 MB view details)

Uploaded CPython 3.10macOS 15.0+ ARM64

File details

Details for the file onnx_extended-0.5.0.tar.gz.

File metadata

  • Download URL: onnx_extended-0.5.0.tar.gz
  • Upload date:
  • Size: 17.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for onnx_extended-0.5.0.tar.gz
Algorithm Hash digest
SHA256 2bb17f4cec65bee998508837276100a33df5b5e6ea0b84a1eaebda1cbd8eb56d
MD5 14073463b98f85642534ea664e202ad9
BLAKE2b-256 e1c218b854b017bd95c693fe12b04f3528dd0de3ae0621b39281f2290ea589f5

See more details on using hashes here.

File details

Details for the file onnx_extended-0.5.0-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for onnx_extended-0.5.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 19c9c79d732ecd513c3d23059d6b374ba941a1df4710a402c03da76c70daf155
MD5 bfbd04bdcee083a9ff9f8fb89c041fae
BLAKE2b-256 62d8d96598cda174d600b174bd23fd51dc26a4ea6d5b0b4ae95494dde47b4e60

See more details on using hashes here.

File details

Details for the file onnx_extended-0.5.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for onnx_extended-0.5.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 c88eaa9e526821bf7ed3d5867ae585eba6c5d845b6a4f8c38b203972b0d719f7
MD5 0d0295c6a23eae384d991c713e76077e
BLAKE2b-256 d3d5d7ad528e374c4f14d0169d5e006d3cf7df0543defcdfb10d38a0414f2e1c

See more details on using hashes here.

File details

Details for the file onnx_extended-0.5.0-cp313-cp313-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for onnx_extended-0.5.0-cp313-cp313-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 3b5fb2eaa7ec7ba39380410bfc537dfa1e11fdfd245cccf34b51d1f6f33b9f97
MD5 4ba057df10035f1eabcc5d8fcfc385dc
BLAKE2b-256 d574b23b2fc32d3f34a78f695955f344d48cbec4b16e787bb3c76ec89c5776af

See more details on using hashes here.

File details

Details for the file onnx_extended-0.5.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for onnx_extended-0.5.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 8490b39af9da9dd439d03b0213c3d9c20a660f3aba3590ac0e9fdad6342f437e
MD5 2e07c821a03ba9a395d55949c8079bbb
BLAKE2b-256 a800e3cd9d3f50b901f48a832c78d94cf1cee73769d927206ae4aedd1ea1ee10

See more details on using hashes here.

File details

Details for the file onnx_extended-0.5.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for onnx_extended-0.5.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0c301e23dfe30da636c36047bc3bdadc1423370a622643976cae8f91e0fe288a
MD5 63be49f52008a5ba39580329065ea92d
BLAKE2b-256 501cc74d1b4acf602b7405a3f18c5bbd2df35ebf8642b653a6663fa4f14a1f24

See more details on using hashes here.

File details

Details for the file onnx_extended-0.5.0-cp312-cp312-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for onnx_extended-0.5.0-cp312-cp312-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 36d727b97cf1ba5d6d2684619349815947725f822a2ff1d90336c935fe5d6a5c
MD5 357a3e733a33875f189fc9c5ae17384d
BLAKE2b-256 605b0e6228b4acae68eeaa0311fabeb67f0ab5cae911a84ed3e83704eb8bdddd

See more details on using hashes here.

File details

Details for the file onnx_extended-0.5.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for onnx_extended-0.5.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 4a60e8e311b09d43c9d4276f258336d12ec9fd94f8d36134b3d3dc9a3a66694c
MD5 12de7badc15b466dbb3e31458276089b
BLAKE2b-256 ae2434c8fc69fba0be568abb17aad742f1b53773bb3ed0e7cf3a37e26a477aaa

See more details on using hashes here.

File details

Details for the file onnx_extended-0.5.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for onnx_extended-0.5.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 b92ffca9d4505996b47ba0c41feec8ec1a69a0a7a502e83a9229ec876c428b18
MD5 1d01174f41f85496875e64cb1c89d3f7
BLAKE2b-256 b76681d11ba6af057c37177c714032299d43b47037dd390b3355b1a58bde88ae

See more details on using hashes here.

File details

Details for the file onnx_extended-0.5.0-cp311-cp311-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for onnx_extended-0.5.0-cp311-cp311-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 3d31d147061a9153d83c8d0b2c9843543c5c021db9f0ead7aa739e5ce6b51c6d
MD5 182e1a05df1ba0762a803eacb76c84fd
BLAKE2b-256 37c9a8906146a6cc83d23b5bde460228bb9e78a9ef57fd5c9a0290bee106d59e

See more details on using hashes here.

File details

Details for the file onnx_extended-0.5.0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for onnx_extended-0.5.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 5003b54c2540c62fb579394a487eebb8c7b5873198d281c73217a867411dc88e
MD5 e8cb9f80017d4c58cbdb2fdd8659f3cf
BLAKE2b-256 ac89ec4b1a716c9d782a960b7faedf91895e7fcfeed7b11b5cf303355d67a991

See more details on using hashes here.

File details

Details for the file onnx_extended-0.5.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for onnx_extended-0.5.0-cp310-cp310-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 eddfbf3f98bbaa944179918474ddfd7325ea77785ec44c3a0bc613df25ca7f18
MD5 61c8024ec6b17242b16b133284ddc03d
BLAKE2b-256 ef6c7289e0f1ef8c9a9bbe0c66f172e5d3fde2d19534faf2c6703aaa0ef04f2c

See more details on using hashes here.

File details

Details for the file onnx_extended-0.5.0-cp310-cp310-macosx_15_0_arm64.whl.

File metadata

File hashes

Hashes for onnx_extended-0.5.0-cp310-cp310-macosx_15_0_arm64.whl
Algorithm Hash digest
SHA256 bcc15549e8f199131e569f5a676cde321ea50517a2e87da412d19abbe29800b5
MD5 1a5522648803a28370835fd819408727
BLAKE2b-256 19cc84a75ac8c3665a65cf7e651893a289b374d7c7b72ab22617650f32c10015

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page