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

Uploaded Source

Built Distributions

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

Uploaded CPython 3.11 Windows x86-64

onnx_extended-0.2.2-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.2-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.2-cp310-cp310-win_amd64.whl (678.9 kB view details)

Uploaded CPython 3.10 Windows x86-64

onnx_extended-0.2.2-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.2-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.2.tar.gz.

File metadata

  • Download URL: onnx-extended-0.2.2.tar.gz
  • Upload date:
  • Size: 169.1 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.2.tar.gz
Algorithm Hash digest
SHA256 b00861e68056f13052084b70574b7916ad8fb92289778806855a6a7fede5ef43
MD5 b5510855f74a0eb81bed0ce57978d077
BLAKE2b-256 4e5a58738257e7b2c0a8927436d4864556e52c7584be7315567df45ebf558bb0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_extended-0.2.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ca207c674bea772a0cb0387fcc9a6c6a9f14113f59404cbba64c596fb515426e
MD5 d088c40822c5ffca29d033b29c7a7d19
BLAKE2b-256 8fc6d3667c90698ea355fc7d7948125131711ad17a72991438cb7cdcf5bcc8e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_extended-0.2.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7ef3d19cdd9e2b01315e856ea04b449adc8e261600d607517cb6eb9a37685c66
MD5 8037289e2d62c4d64e923ac3e0b8539b
BLAKE2b-256 b2e0aee2c27e187fcde3a602f9b945eff1b6f35528814fc92101d21ccc970759

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_extended-0.2.2-cp311-cp311-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 3ba88bba0739285003f3b3dc298b499bed4cb6977b734d98eea324112c5ce387
MD5 2d397d120d1543a8b239d15b8b851ea2
BLAKE2b-256 73fd6f2582e09ede623766d3a9e38257cfb86a99c99c5a6f1903b4c7c1d7dfb5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_extended-0.2.2-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 6181671c30261f40537082dd59471789e9c6353bf7c9e4948fabc5ea3e4762c4
MD5 78844865c0d830319b331c5188c6eea3
BLAKE2b-256 28fe24346e5aa3d2c06a7f0a347f88b5e56197af3599c0dbc59208be47c5c7ef

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_extended-0.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2e22cc4ff3a34056c32e5a17919b95902a2310495b31d7a6e03f33fdd8c666f8
MD5 f9f309bf3363326ba99e03be22c2dad1
BLAKE2b-256 3add0dcbdffcb4293f54dc937d4160eee3f1142d5a74f9c58c52c0951788bf65

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_extended-0.2.2-cp310-cp310-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 babcd4d6d1fa9b0988eeedb859401d92026ef8d6accbafc47f23f79fb4084594
MD5 a7f0548a23ac219dac635e88bb6c5e66
BLAKE2b-256 816e8da4a3ada0782c011208af8a90b1c6d798f69f02982a2a3f098b49896ea3

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