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=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.3.0.tar.gz (14.7 MB view details)

Uploaded Source

Built Distributions

onnx_extended-0.3.0-cp311-cp311-win_amd64.whl (67.1 MB view details)

Uploaded CPython 3.11 Windows x86-64

onnx_extended-0.3.0-cp311-cp311-manylinux_2_28_x86_64.whl (25.8 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.28+ x86-64

onnx_extended-0.3.0-cp310-cp310-win_amd64.whl (67.0 MB view details)

Uploaded CPython 3.10 Windows x86-64

onnx_extended-0.3.0-cp310-cp310-manylinux_2_28_x86_64.whl (25.7 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.28+ x86-64

File details

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

File metadata

  • Download URL: onnx_extended-0.3.0.tar.gz
  • Upload date:
  • Size: 14.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.12

File hashes

Hashes for onnx_extended-0.3.0.tar.gz
Algorithm Hash digest
SHA256 add7f769b7b5d6590a807ec40a4abdb192ee4e2899222480f5182699fb34bb19
MD5 015587a58c5a311292051390e63b0da0
BLAKE2b-256 cb7dcd45358a907c8cd6de3db91e8881e1d5915292ffbdf3365705e0e5b48960

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_extended-0.3.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 6c30631cb7b71919e4bd7437ef8fe13215ffbb362ed7883d93af8541f520032f
MD5 e67d328d89374fc12fc3dfbf71e51158
BLAKE2b-256 1301b9db129a7b9e2b36d7f82648bac2cc633835420c596f4685c591e8faefe4

See more details on using hashes here.

File details

Details for the file onnx_extended-0.3.0-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for onnx_extended-0.3.0-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 cc6b0d22fccc6e0657c25d8e11a40f4383ffae88854ca9d0caab043ab03fb6d8
MD5 69df873540a5f4949045820c65319c7f
BLAKE2b-256 d3c817f8012bb2acbeb350f257ba9b60b1c521d360c8a79c4e07210018c9c55d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_extended-0.3.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 8ddb75ef99f7fc795be4ab7b717f78dd5455df6c54605865e9dec69799632f3d
MD5 76cb02339547e2402c72dc16617de9a1
BLAKE2b-256 58112e6738f7529e0056fe3d45cfa53ce701fae2e11f4a96793e3993e9a3d0f8

See more details on using hashes here.

File details

Details for the file onnx_extended-0.3.0-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for onnx_extended-0.3.0-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 a4929cf2469bf54e414466bf012ff5b57d6526321682cecde64ce900944ae9d4
MD5 e527dc0da4b8639ca736a8dca8e68411
BLAKE2b-256 b92d5fc4e338e47513f5c23a867611e984148b5c8be52f349ba1f77381c08e13

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