Skip to main content

Introduction

ONNXRuntime Extensions is a comprehensive package to extend the capability of the ONNX conversion and inference.

  1. The CustomOp C++ library for ONNX Runtime on ONNXRuntime CustomOp API.
  2. Support PyOp feature to implement the custom op with a Python function.
  3. Build all-in-one ONNX model from the pre/post processing code, go to docs/pre_post_processing.md for details.
  4. Support Python per operator debugging, checking hook_model_op in onnxruntime_extensions Python package.

Quick Start

The following code shows how to run ONNX model and ONNXRuntime customop more straightforwardly.

import numpy
from onnxruntime_extensions import PyOrtFunction, VectorToString
# <ProjectDir>/tutorials/data/gpt-2/gpt2_tok.onnx
encode = PyOrtFunction.from_model('gpt2_tok.onnx')
# https://github.com/onnx/models/blob/master/text/machine_comprehension/gpt-2/model/gpt2-lm-head-10.onnx
gpt2_core = PyOrtFunction.from_model('gpt2-lm-head-10.onnx')
decode = PyOrtFunction.from_customop(VectorToString, map={' a': [257]}, unk='<unknown>')

input_text = ['It is very cool to have']
output, *_ = gpt2_core(input_ids)
next_id = numpy.argmax(output[:, :, -1, :], axis=-1)
print(input_text[0] + decode(next_id).item())

This is a simplified version of GPT-2 inference for the demonstration only, The comprehensive solution on the GPT-2 model and its deviants are under development, and here is the link to the experimental.

Android/iOS

The previous processing python code can be translated into all-in-one model to be run in Android/iOS mobile platform, without any Python runtime and the 3rd-party dependencies requirement. Here is the tutorial

CustomOp Conversion

The mainstream ONNX converters support the custom op generation if there is the operation from the original framework cannot be interpreted as ONNX standard operators. Check the following two examples on how to do this.

  1. CustomOp conversion by pytorch.onnx.exporter
  2. CustomOp conversion by tf2onnx

Inference with CustomOp library

The CustomOp library was written with C++, so that it supports run the model in the native binaries. The following is the example of C++ version.

  // The line loads the customop library into ONNXRuntime engine to load the ONNX model with the custom op
  Ort::ThrowOnError(Ort::GetApi().RegisterCustomOpsLibrary((OrtSessionOptions*)session_options, custom_op_library_filename, &handle));

  // The regular ONNXRuntime invoking to run the model.
  Ort::Session session(env, model_uri, session_options);
  RunSession(session, inputs, outputs);

Of course, with Python language, the thing becomes much easier since PyOrtFunction will directly translate the ONNX model into a python function. But if the ONNXRuntime Custom Python API want to be used, the inference process will be

import onnxruntime as _ort
from onnxruntime_extensions import get_library_path as _lib_path

so = _ort.SessionOptions()
so.register_custom_ops_library(_lib_path())

# Run the ONNXRuntime Session.
# sess = _ort.InferenceSession(model, so)
# sess.run (...)

More CustomOp

Welcome to contribute the customop C++ implementation directly in this repository, which will widely benefit other users. Besides C++, if you want to quickly verify the ONNX model with some custom operators with Python language, PyOp will help with that

import numpy
from onnxruntime_extensions import PyOp, onnx_op

# Implement the CustomOp by decorating a function with onnx_op
@onnx_op(op_type="Inverse", inputs=[PyOp.dt_float])
def inverse(x):
    # the user custom op implementation here:
    return numpy.linalg.inv(x)

# Run the model with this custom op
# model_func = PyOrtFunction(model_path)
# outputs = model_func(inputs)
# ...

Build and Development

This project supports Python and can be built from source easily, or a simple cmake build without Python dependency.

Python package

  • Install Visual Studio with C++ development tools on Windows, or gcc for Linux or xcode for MacOS, and cmake on the unix-like platform. (hints: in Windows platform, if cmake bundled in Visual Studio was used, please specify the set VCVARS=%ProgramFiles(x86)%\Microsoft Visual Studio\2019<Edition>\VC\Auxiliary\Build\vcvars64.bat)
  • Prepare Python env and install the pip packages in the requirements.txt.
  • python setup.py install to build and install the package.
  • OR python setup.py develop to install the package in the development mode, which is more friendly for the developer since (re)installation is not needed with every build.

Test:

  • run pytest test in the project root directory.

The share library for non-Python

If only DLL/shared library is needed without any Python dependencies, please run build.bat or bash ./build.sh to build the library. By default the DLL or the library will be generated in the directory out/<OS>/<FLAVOR>. There is a unit test to help verify the build.

For sake of the binary size, the project can be built as a static library and link into ONNXRuntime. Here is the script to this, which is especially usefully on building the mobile release.

Metadata

Release files for onnxruntime_extensions 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for onnxruntime_extensions 0.4.0
File Actions
onnxruntime_extensions-0.4.0-pp37-pypy37_pp73-manylinux_2_12_x86_64.manylinux2010_x86_64.whl PyPy 3.7 PyPy 3.7 7.3 Linux glibc 2.12+ x86-64 Details for onnxruntime_extensions-0.4.0-pp37-pypy37_pp73-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
onnxruntime_extensions-0.4.0-pp37-pypy37_pp73-manylinux_2_12_i686.manylinux2010_i686.whl PyPy 3.7 PyPy 3.7 7.3 Linux glibc 2.12+ x86-32 Details for onnxruntime_extensions-0.4.0-pp37-pypy37_pp73-manylinux_2_12_i686.manylinux2010_i686.whl
onnxruntime_extensions-0.4.0-cp39-cp39-win_amd64.whl CPython 3.9 CPython 3.9 Windows x86-64 Details for onnxruntime_extensions-0.4.0-cp39-cp39-win_amd64.whl
onnxruntime_extensions-0.4.0-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl CPython 3.9 CPython 3.9 Linux glibc 2.12+ x86-64 Details for onnxruntime_extensions-0.4.0-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
onnxruntime_extensions-0.4.0-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl CPython 3.9 CPython 3.9 Linux glibc 2.12+ x86-32 Details for onnxruntime_extensions-0.4.0-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl
onnxruntime_extensions-0.4.0-cp38-cp38-win_amd64.whl CPython 3.8 CPython 3.8 Windows x86-64 Details for onnxruntime_extensions-0.4.0-cp38-cp38-win_amd64.whl
onnxruntime_extensions-0.4.0-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl CPython 3.8 CPython 3.8 Linux glibc 2.12+ x86-64 Details for onnxruntime_extensions-0.4.0-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
onnxruntime_extensions-0.4.0-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl CPython 3.8 CPython 3.8 Linux glibc 2.12+ x86-32 Details for onnxruntime_extensions-0.4.0-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl
onnxruntime_extensions-0.4.0-cp37-cp37m-win_amd64.whl CPython 3.7 CPython 3.7 pymalloc Windows x86-64 Details for onnxruntime_extensions-0.4.0-cp37-cp37m-win_amd64.whl
onnxruntime_extensions-0.4.0-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.12+ x86-64 Details for onnxruntime_extensions-0.4.0-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
onnxruntime_extensions-0.4.0-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.whl CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.12+ x86-32 Details for onnxruntime_extensions-0.4.0-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.whl
onnxruntime_extensions-0.4.0-cp36-cp36m-win_amd64.whl CPython 3.6 CPython 3.6 pymalloc Windows x86-64 Details for onnxruntime_extensions-0.4.0-cp36-cp36m-win_amd64.whl
onnxruntime_extensions-0.4.0-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.12+ x86-64 Details for onnxruntime_extensions-0.4.0-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
onnxruntime_extensions-0.4.0-cp36-cp36m-manylinux_2_12_i686.manylinux2010_i686.whl CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.12+ x86-32 Details for onnxruntime_extensions-0.4.0-cp36-cp36m-manylinux_2_12_i686.manylinux2010_i686.whl

Total release size: 14.2 MB

Release files / onnxruntime_extensions-0.4.0-pp37-pypy37_pp73-manylinux_2_12_x86_64.manylinux2010_x86_64.whl

Download URL onnxruntime_extensions-0.4.0-pp37-pypy37_pp73-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Size 1.2 MB
Tags Linux glibc 2.12+ x86-64 PyPy 3.7 PyPy 3.7 7.3
SHA-256 checksum
How to use checksums
a8857693efa1fc54b1fe731af127d66ec3d9f0fddb32a8df8134c21ec8f3fdde
BLAKE2b-256 checksum
How to use checksums
e94d0cf1899143cbe83f5ed03578ad4c346087742980c33f4aad3cbbdf12a7ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.5

Release files / onnxruntime_extensions-0.4.0-pp37-pypy37_pp73-manylinux_2_12_i686.manylinux2010_i686.whl

Download URL onnxruntime_extensions-0.4.0-pp37-pypy37_pp73-manylinux_2_12_i686.manylinux2010_i686.whl
Size 1.2 MB
Tags Linux glibc 2.12+ x86-32 PyPy 3.7 PyPy 3.7 7.3
SHA-256 checksum
How to use checksums
2f5017a19fd6cd5b3de54c03240039ee4ca7f054e4b9fe5f621382c1a5cca721
BLAKE2b-256 checksum
How to use checksums
cee619d8c384350cd623f37a387e86f0de60ae68b518a2ac6f7d908ecec78b7c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.5

Release files / onnxruntime_extensions-0.4.0-cp39-cp39-win_amd64.whl

Download URL onnxruntime_extensions-0.4.0-cp39-cp39-win_amd64.whl
Size 556.4 kB
Tags CPython 3.9 Windows x86-64
SHA-256 checksum
How to use checksums
257d81750409df5e226f50bb7a1e46f072f1715ac18ded9374003886c93ea4c8
BLAKE2b-256 checksum
How to use checksums
a63d1f0068f4f1507dbf772e7d2e2e636125049a776550bfc174aa47dfc0d4ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.5

Release files / onnxruntime_extensions-0.4.0-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl

Download URL onnxruntime_extensions-0.4.0-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Size 1.2 MB
Tags CPython 3.9 Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
62ac310da928dfcd57c51e672745081681fcf78a6cae5d2cb2c43976ee0aed03
BLAKE2b-256 checksum
How to use checksums
43f6fe1e18716598844b04c24715d062a43a9184089edcecc57d9ccd5c97c7b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.5

Release files / onnxruntime_extensions-0.4.0-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl

Download URL onnxruntime_extensions-0.4.0-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl
Size 1.2 MB
Tags CPython 3.9 Linux glibc 2.12+ x86-32
SHA-256 checksum
How to use checksums
880fbeead2361d4661bff178f9e8c8f8cc8e719acc23dc8afbd58b548cd9a535
BLAKE2b-256 checksum
How to use checksums
fbae6beccf5b07c4406fc9711dcbcbcf3378874c141fe1791554d02ad91c478f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.5

Release files / onnxruntime_extensions-0.4.0-cp38-cp38-win_amd64.whl

Download URL onnxruntime_extensions-0.4.0-cp38-cp38-win_amd64.whl
Size 556.2 kB
Tags CPython 3.8 Windows x86-64
SHA-256 checksum
How to use checksums
448ae422127369842726d56d46d20998949ce8f878ad4e48751055fa51cf6052
BLAKE2b-256 checksum
How to use checksums
ca12a53c3f5e2993ae58c1b50c2d9656351a465d7b138acebf5a68a74764b434
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.5

Release files / onnxruntime_extensions-0.4.0-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl

Download URL onnxruntime_extensions-0.4.0-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Size 1.2 MB
Tags CPython 3.8 Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
4c92c7319253486b2a4d453d42633d79620ecf38d02c331ab9c1509bed9f742d
BLAKE2b-256 checksum
How to use checksums
f1036da2ac92c5c25eb2ceaaeb1c374bf3b8436e257dbd27653450b1e1afa238
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.5

Release files / onnxruntime_extensions-0.4.0-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl

Download URL onnxruntime_extensions-0.4.0-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl
Size 1.2 MB
Tags CPython 3.8 Linux glibc 2.12+ x86-32
SHA-256 checksum
How to use checksums
b42b75fc05f382a475e849ebf9b428596e111716e91764cfca70769e10f981a0
BLAKE2b-256 checksum
How to use checksums
b12ce4e73aa949b21ea651a96d75eeba384c138d7ec96dc6277799fc27749f95
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.5

Release files / onnxruntime_extensions-0.4.0-cp37-cp37m-win_amd64.whl

Download URL onnxruntime_extensions-0.4.0-cp37-cp37m-win_amd64.whl
Size 557.3 kB
Tags CPython 3.7 CPython 3.7 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
348a81f9b234b6c6c7684623e8e8d0254c145c64f9a3a47427e93b9f22171bc7
BLAKE2b-256 checksum
How to use checksums
9b185bf905e1ef8f93a6f352a620ae68ff31e97a7ed55576165e4a9994eee60e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.5

Release files / onnxruntime_extensions-0.4.0-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl

Download URL onnxruntime_extensions-0.4.0-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Size 1.2 MB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
e31ab29251eceb23a282d3cdee949108e75ea227cddd4bb12e4dba167488c1d2
BLAKE2b-256 checksum
How to use checksums
98c31a99cf4450e18da98af433b092013aa0981ced27d86fc4732b35b603035d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.5

Release files / onnxruntime_extensions-0.4.0-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.whl

Download URL onnxruntime_extensions-0.4.0-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.whl
Size 1.2 MB
Tags CPython 3.7 CPython 3.7 pymalloc Linux glibc 2.12+ x86-32
SHA-256 checksum
How to use checksums
06680bab9afe2eb84cb550ad66980b430d84911b3eb02c0395c1a4f68931203a
BLAKE2b-256 checksum
How to use checksums
277e68ee24a807c65749a41fea8da6f6ada4b19fbe953a9c9dce144568672ae1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.5

Release files / onnxruntime_extensions-0.4.0-cp36-cp36m-win_amd64.whl

Download URL onnxruntime_extensions-0.4.0-cp36-cp36m-win_amd64.whl
Size 557.3 kB
Tags CPython 3.6 CPython 3.6 pymalloc Windows x86-64
SHA-256 checksum
How to use checksums
00e0f1bc0937a0f81fee35dfe1da7ca0302434d6e3f3f434205ae3df7f3297fc
BLAKE2b-256 checksum
How to use checksums
85d484fbee44f1b57f3d67dddf1b9a4c293e19fbd2e12648fe48f6b5f3d16c32
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.5

Release files / onnxruntime_extensions-0.4.0-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl

Download URL onnxruntime_extensions-0.4.0-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Size 1.2 MB
Tags CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.12+ x86-64
SHA-256 checksum
How to use checksums
20f06ceab60403e9d4d6cb2f220272d732a3aca99fa6f1b845ef1d453957c90b
BLAKE2b-256 checksum
How to use checksums
e1efe1256b363b4a20436bf046cfa05559ad8b4a535b7a90085bed19f9745e4d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.5

Release files / onnxruntime_extensions-0.4.0-cp36-cp36m-manylinux_2_12_i686.manylinux2010_i686.whl

Download URL onnxruntime_extensions-0.4.0-cp36-cp36m-manylinux_2_12_i686.manylinux2010_i686.whl
Size 1.2 MB
Tags CPython 3.6 CPython 3.6 pymalloc Linux glibc 2.12+ x86-32
SHA-256 checksum
How to use checksums
c342886c25c7c86d90ff5c8f9c35cf1e0874b905957b9d3484651db235742108
BLAKE2b-256 checksum
How to use checksums
dca2785f3b7e45077fa8172993b8309b265872649da535d8c9678954f2be6db5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.5

Security for onnxruntime_extensions 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page