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 C++ a 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=11.8
# or (not working yet)
# pip install -e . --config-settings="--cuda-version=11.8"
# pip install -e . --global-option="--cuda-version=11.8"
export USE_CUDA=11.8
pip install -e .

NVTX can be enabled with the following command:

python setup.py build_ext --inplace --use_nvtx 1
# or (not working yet)
# pip install -e . --config-settings="--use_nvtx=1"
pip install -e . --global-option "--use_nvtx=1"

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.2.1.tar.gz (138.6 kB view details)

Uploaded Source

Built Distributions

onnx_extended-0.2.1-cp311-cp311-win_amd64.whl (4.9 MB view details)

Uploaded CPython 3.11 Windows x86-64

onnx_extended-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (21.0 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

onnx_extended-0.2.1-cp311-cp311-macosx_10_15_x86_64.whl (39.0 MB view details)

Uploaded CPython 3.11 macOS 10.15+ x86-64

onnx_extended-0.2.1-cp310-cp310-win_amd64.whl (675.5 kB view details)

Uploaded CPython 3.10 Windows x86-64

onnx_extended-0.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.5 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

onnx_extended-0.2.1-cp310-cp310-macosx_10_15_x86_64.whl (13.7 MB view details)

Uploaded CPython 3.10 macOS 10.15+ x86-64

File details

Details for the file onnx-extended-0.2.1.tar.gz.

File metadata

  • Download URL: onnx-extended-0.2.1.tar.gz
  • Upload date:
  • Size: 138.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for onnx-extended-0.2.1.tar.gz
Algorithm Hash digest
SHA256 77f753d763ff215c365c1a37402979a90e4de9c7afe97584ccf368ce2bd276b0
MD5 c65e6df65402013825e800291923f792
BLAKE2b-256 3f9fe337e6f6f717efe3a48b9d4c57dfd16183da72efb1420978ed48f77351e4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_extended-0.2.1-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 00f3c59e3f2f951fcbbdfbcb21812102c8d19028e07cbab29bd3d104a2976d74
MD5 f0e320b40ef83a9bfe193dc372a8a2a7
BLAKE2b-256 50d1b14616d22eb63e67ababc8ad42046ac30ab5b1edb5be897a007532a987fa

See more details on using hashes here.

File details

Details for the file onnx_extended-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for onnx_extended-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1d278e1e39f8b8f7d1997ebb06147590d1c2addb9c01a24ba2ab25ea880e7e31
MD5 b29bbb905f111580b7eae740cebaa9d7
BLAKE2b-256 bf196a10d676f003af405a4da8939a589895f00deef61fe33ff31b3647dcb748

See more details on using hashes here.

File details

Details for the file onnx_extended-0.2.1-cp311-cp311-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for onnx_extended-0.2.1-cp311-cp311-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 2f6e0827946b29a9d1467ab86c5c1b7cd35f02101d86df836a935061b2a690ac
MD5 bb94e9119f4fff3754468feee4ff7dbb
BLAKE2b-256 f1ca7dc164d0551d3015e3101850f32e690757dde78cc2b7f56b60113afa3a5c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_extended-0.2.1-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 1703df25e7582878e0240a9bdd1ae522c120c11fffa7fa8a1e0b5be1303fc79f
MD5 ad5e5ac24078c7294d0ba4d1ae5051dc
BLAKE2b-256 66603a4c3833cf70d3d219e7e92e9920ec92bf7f4f108193d5acd1de921c0146

See more details on using hashes here.

File details

Details for the file onnx_extended-0.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for onnx_extended-0.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b70f69cf269d668b78cde7cfcb1e7ad7d95760968d6e79fb42d917ae78744aa9
MD5 13c2712e25ae1234f256d51b9b8efb32
BLAKE2b-256 7a768c80a416cb83c76cce1c4f12fc5709cba3cfe0247846aa25c139fbce4cad

See more details on using hashes here.

File details

Details for the file onnx_extended-0.2.1-cp310-cp310-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for onnx_extended-0.2.1-cp310-cp310-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 726ae6a18bfdd72d65fc08622aab6c8153e8e52dea111face34d65d5fc326f17
MD5 971aa11914d260238077d23337e76cc7
BLAKE2b-256 fbe6a0ad193780183b537940ce768214e94c93b874dffce5fd6b351bdd4793e0

See more details on using hashes here.

Supported by

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