Skip to main content

ONNXRuntime Extensions

Project description

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.

The static library and link with ONNXRuntime

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

onnxruntime_extensions-0.4.2-cp39-cp39-win_amd64.whl (556.2 kB view details)

Uploaded CPython 3.9 Windows x86-64

onnxruntime_extensions-0.4.2-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.12+ x86-64

onnxruntime_extensions-0.4.2-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl (1.2 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.12+ i686

onnxruntime_extensions-0.4.2-cp38-cp38-win_amd64.whl (556.0 kB view details)

Uploaded CPython 3.8 Windows x86-64

onnxruntime_extensions-0.4.2-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.12+ x86-64

onnxruntime_extensions-0.4.2-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl (1.2 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.12+ i686

onnxruntime_extensions-0.4.2-cp37-cp37m-win_amd64.whl (556.9 kB view details)

Uploaded CPython 3.7m Windows x86-64

onnxruntime_extensions-0.4.2-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.12+ x86-64

onnxruntime_extensions-0.4.2-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.whl (1.2 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.12+ i686

onnxruntime_extensions-0.4.2-cp36-cp36m-win_amd64.whl (556.9 kB view details)

Uploaded CPython 3.6m Windows x86-64

onnxruntime_extensions-0.4.2-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.6m manylinux: glibc 2.12+ x86-64

onnxruntime_extensions-0.4.2-cp36-cp36m-manylinux_2_12_i686.manylinux2010_i686.whl (1.2 MB view details)

Uploaded CPython 3.6m manylinux: glibc 2.12+ i686

File details

Details for the file onnxruntime_extensions-0.4.2-pp37-pypy37_pp73-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for onnxruntime_extensions-0.4.2-pp37-pypy37_pp73-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 9b3ba191d09f9ccd95f1a7333ba7495f6506385ea815adc04c6aa12be6b7b0d8
MD5 d691c7947ac673382d398604cbebb603
BLAKE2b-256 37ee2a1ee982d369fcf07ec65f7ebe08ca1eabcfbdc7acee64a317bc94cfa47c

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.4.2-pp37-pypy37_pp73-manylinux_2_12_i686.manylinux2010_i686.whl.

File metadata

File hashes

Hashes for onnxruntime_extensions-0.4.2-pp37-pypy37_pp73-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm Hash digest
SHA256 83d16ba3434813aedecfdfa5fbd84b7ca78c7f1c203e6208532a683092b585db
MD5 2f63547cd382bc83a398f533cca6b4db
BLAKE2b-256 c2e79581692c9619c42fa0d130c6d08da5069783c39e1c1daa20b8b961513d7f

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.4.2-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: onnxruntime_extensions-0.4.2-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 556.2 kB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using 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

File hashes

Hashes for onnxruntime_extensions-0.4.2-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 bb0e563078bdcee39b86d898318f688593d530e03e2bd88c49d528ae864a999d
MD5 199dcbf0b9cbc2a7508a409e1feec2fa
BLAKE2b-256 b3688426deef1a317a7a2e57fe4fac0fbf5621994e2eb4ab70b8e267131e3dc1

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.4.2-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for onnxruntime_extensions-0.4.2-cp39-cp39-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 6929f5c97f67ef4e71b5a336ec84a736439cb2027292e67ce8eee0f97d11ca7b
MD5 689d22e38354644ace1831dade7d1e75
BLAKE2b-256 3ce1482046c069e734996b53a78aa6f93335ca698ffed5b8c952cd6b68bfba7b

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.4.2-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl.

File metadata

File hashes

Hashes for onnxruntime_extensions-0.4.2-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm Hash digest
SHA256 47e94121e2b7f751746440289a7b5100fddfb78d71efb29e609c5ff5a82814e0
MD5 7c7b891fea270f93428e475c956da119
BLAKE2b-256 11e9ac4828f1318cf190bca6f77c915a54250f4fed2036fcc9784aef91eb1473

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.4.2-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: onnxruntime_extensions-0.4.2-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 556.0 kB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using 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

File hashes

Hashes for onnxruntime_extensions-0.4.2-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 cfaa25b441c5505834b62082ed2846258de0d142ecf3afd8298e3ffeca3739da
MD5 fa532b5ffec9585e36be17d3b14c6d90
BLAKE2b-256 a2e4ead296ab83fd716c99d18689f5843d501084ca790115ce4fca796cb6ff22

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.4.2-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for onnxruntime_extensions-0.4.2-cp38-cp38-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 f51f17f85b92ccd35e3af0b7192f09be6b4bc8962d78a884b95b5e0ed7f32ecf
MD5 b1ae7fa78714986196c71bc55ed18f85
BLAKE2b-256 1a9afb0eadee5b1456cb5cbf6f397d384b6ceab328e209c85f6a06f8c029efa4

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.4.2-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl.

File metadata

File hashes

Hashes for onnxruntime_extensions-0.4.2-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm Hash digest
SHA256 edc4a847f2c76085bb3d00518490f557691e8377071444099a86621492e1b328
MD5 5cf9735a69cf62ccd9d6a2771015ca47
BLAKE2b-256 df2d8758636ed0f86254519969229196854bc557abe8099b80698ed690b1c92d

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.4.2-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: onnxruntime_extensions-0.4.2-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 556.9 kB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using 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

File hashes

Hashes for onnxruntime_extensions-0.4.2-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 824d7b3e9f7da80640964e2f838fb2dad36c0bf1429c8c34aa8ca561eb459bad
MD5 f6cb9593a128943c58245e31de41f6fa
BLAKE2b-256 b8d99ad7064771f6b1eb3acb860f8f901bb9f4135a9e8f3590872b2850bc8eeb

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.4.2-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for onnxruntime_extensions-0.4.2-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 1a81054a27271e7845de1480a4187436b96f788a0cb818cbacc680de693f81c4
MD5 c60bd6d3a14c8628fd5e14bc8ead31d3
BLAKE2b-256 b1e1c73c5131e0fe40a2b3708bf562aea139ff669a290b9d6a170c918e79dd88

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.4.2-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.whl.

File metadata

File hashes

Hashes for onnxruntime_extensions-0.4.2-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm Hash digest
SHA256 2f0f893eb0cbff26ee52247191240286c0d4a1ebe1db0ac17c318569b5f71200
MD5 cf3b94bb565b325278c48ddd316d2f8a
BLAKE2b-256 b313cb4800207cbe9510b2d39e1e5ca69ff328a45466145c45621a9f762bc5aa

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.4.2-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: onnxruntime_extensions-0.4.2-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 556.9 kB
  • Tags: CPython 3.6m, Windows x86-64
  • Uploaded using 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

File hashes

Hashes for onnxruntime_extensions-0.4.2-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 24c70e3fb13b4aa66e6aaa7421ede8dd5d65e828bdc8df9f4cf0b32755333e60
MD5 8d638619a187c7180438fdb540d2e16c
BLAKE2b-256 d818311789e5ce5afb096bd5a1c8b320cbab1fc5b95b305ac5de4d68f1c43215

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.4.2-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.

File metadata

File hashes

Hashes for onnxruntime_extensions-0.4.2-cp36-cp36m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl
Algorithm Hash digest
SHA256 5489a6714facef9488d4551fa4c222d9613f127b944b4da6a5939c0effc19728
MD5 cf35786e8975424811703b929ef4981e
BLAKE2b-256 264e3160bc13a0be9223c9c8b8dea2a306f1f88c39c06ad74befbdf418cdafa8

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.4.2-cp36-cp36m-manylinux_2_12_i686.manylinux2010_i686.whl.

File metadata

File hashes

Hashes for onnxruntime_extensions-0.4.2-cp36-cp36m-manylinux_2_12_i686.manylinux2010_i686.whl
Algorithm Hash digest
SHA256 10397e546b718d6a1b14cf39e80e4decce936687584d8051443a6407b5386abb
MD5 0bc1f36a5ebdd2e19a0a27e0d37192fa
BLAKE2b-256 c843f4e00ffa7d18c8b7bdec085cb968d1321d429daa3f03d8074315214dc6eb

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