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

Uploaded Source

Built Distributions

onnx_extended-0.2.3-cp311-cp311-win_amd64.whl (5.1 MB view details)

Uploaded CPython 3.11 Windows x86-64

onnx_extended-0.2.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (21.2 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

onnx_extended-0.2.3-cp311-cp311-macosx_10_15_x86_64.whl (39.2 MB view details)

Uploaded CPython 3.11 macOS 10.15+ x86-64

onnx_extended-0.2.3-cp310-cp310-win_amd64.whl (4.8 MB view details)

Uploaded CPython 3.10 Windows x86-64

onnx_extended-0.2.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.9 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

onnx_extended-0.2.3-cp310-cp310-macosx_10_15_x86_64.whl (38.9 MB view details)

Uploaded CPython 3.10 macOS 10.15+ x86-64

File details

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

File metadata

  • Download URL: onnx-extended-0.2.3.tar.gz
  • Upload date:
  • Size: 369.8 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.3.tar.gz
Algorithm Hash digest
SHA256 b752c76f9f601e52f86bc9a63a8b2f8ab0ec44345f9dc362b947bf47de657a6c
MD5 b3aa0ba002ea7ece804c90659d44d06d
BLAKE2b-256 29cbe6dffb87c5e9e9a4bcfae160b894d1df7f370923ddb342fa03a602ccd4d4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_extended-0.2.3-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ae109a9481c627216ef5359ce87daa5bdf174f56a14f3df7d2d0c0897788d149
MD5 8db80619133663ff73ba0f2937a23510
BLAKE2b-256 be0e75fccd7ac226fc6a37bf042a0e290e5d14ae4b4f2154a6e66f930b9fb95a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_extended-0.2.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ad009faa83e9a9d11d962b19b5f15d26946c1a6ac930138fa78425c5ce34c4de
MD5 d7110c55f393f2a449d86d62b05b1598
BLAKE2b-256 7c33e38049ef9c02f39c68773371b0a1a2106abb9bd9b05cc253085806cab0e7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_extended-0.2.3-cp311-cp311-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 1bb0f02eadc618cbb812eb076f33551540421dd77134466be59ed18ccec3db38
MD5 8cda83c5f7c2ba7b342d69f9f14c40c3
BLAKE2b-256 862d7a5ae773599a81d42837c1b413761e5809f07c142d7dcbac86ec00389458

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_extended-0.2.3-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 c28b59e8f422a766bb9a4ec93c8e5bfaeb267fac43f93fc3c0f1016d0ab5de93
MD5 c53bd1be9e619950c3998d8154e52b9e
BLAKE2b-256 c0d0e17a06540363bd0eda3b8108d2f2672e08861e1d4bf0f78927b8e3744299

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_extended-0.2.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 790cf85e98313a5380b15f4cb90a830aaf784426cb0fe43eb5369ab1edbd6433
MD5 918d9cc17e93a8ab93a35242a14e4cbb
BLAKE2b-256 f052afff9eae529b9e5c67283baecc91d43a6009a98388dbeb57ecc339526efe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnx_extended-0.2.3-cp310-cp310-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 50591fb23ec4c5d9766d0e11cd8f3217f896d405426a1250448948679f9d0a9e
MD5 3ff1e8f0780c7e1712770c4153b0ac90
BLAKE2b-256 7e60d3a5ba0c0f2dd9b91c8d402ee7ddd1b3e00bce4fb1a6dbc62aa73af07ce1

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