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.3.1-cp39-cp39-win_amd64.whl (474.0 kB view details)

Uploaded CPython 3.9 Windows x86-64

onnxruntime_extensions-0.3.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (988.2 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

onnxruntime_extensions-0.3.1-cp39-cp39-macosx_10_14_x86_64.whl (733.1 kB view details)

Uploaded CPython 3.9 macOS 10.14+ x86-64

onnxruntime_extensions-0.3.1-cp38-cp38-win_amd64.whl (473.9 kB view details)

Uploaded CPython 3.8 Windows x86-64

onnxruntime_extensions-0.3.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (988.1 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

onnxruntime_extensions-0.3.1-cp38-cp38-macosx_10_14_x86_64.whl (733.1 kB view details)

Uploaded CPython 3.8 macOS 10.14+ x86-64

onnxruntime_extensions-0.3.1-cp37-cp37m-win_amd64.whl (475.0 kB view details)

Uploaded CPython 3.7m Windows x86-64

onnxruntime_extensions-0.3.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (990.6 kB view details)

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

onnxruntime_extensions-0.3.1-cp37-cp37m-macosx_10_14_x86_64.whl (731.6 kB view details)

Uploaded CPython 3.7m macOS 10.14+ x86-64

onnxruntime_extensions-0.3.1-cp36-cp36m-win_amd64.whl (475.0 kB view details)

Uploaded CPython 3.6m Windows x86-64

onnxruntime_extensions-0.3.1-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (990.4 kB view details)

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

onnxruntime_extensions-0.3.1-cp36-cp36m-macosx_10_14_x86_64.whl (731.6 kB view details)

Uploaded CPython 3.6m macOS 10.14+ x86-64

File details

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

File metadata

  • Download URL: onnxruntime_extensions-0.3.1-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 474.0 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.3.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 3b5b7e76ace2edb05e69a86de4ee2ceef5e17fff2c280a9c1bcbf3293cab32d8
MD5 d7ea1fc879860c459366f0c1f5aa2a7b
BLAKE2b-256 84efb7cdefd890dde1f87cc43f47709864944a1b76bf6d9edbdbaf023ecf33a2

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.3.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for onnxruntime_extensions-0.3.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 40f1ca5c4724b5c5bd6d301e855402ff03fb41c4e647d6c2edfce4fb84280e4d
MD5 b54605064f1e5aca1c38e4284e16b2c9
BLAKE2b-256 999ccf298d83eedb871c4b6b6efee0002057af3e1afcca1c69fe14e1e086dba2

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.3.1-cp39-cp39-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: onnxruntime_extensions-0.3.1-cp39-cp39-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 733.1 kB
  • Tags: CPython 3.9, macOS 10.14+ 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.3.1-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 1e92836bd733a57ac221c19b20248bc2350f2cad6dcb36e43f461503caa80cae
MD5 5d88120da821ae29bcbe710e1cc63606
BLAKE2b-256 38537b1925267ff0e26da87ee6b479bf9ca257656ae4d51e7be8fad550cc4ed6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: onnxruntime_extensions-0.3.1-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 473.9 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.3.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 3f8aad95f257776a311e8c1d32c61a25d12b5d3f09b7608fe144f7ef46648d7b
MD5 9b2254505292e0145cc8597406f2ab2f
BLAKE2b-256 123c503011c37ef2efada5fd3345e910ea89de03e4bbcdcb07b178dd2b81913e

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.3.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for onnxruntime_extensions-0.3.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 461268201d817ce44800da4533c9903b3357aca225df8f2ea5f5ecb8ecf839d0
MD5 6df5133023aa7f6445a8670467078bc0
BLAKE2b-256 9a428397a7f20d0408be725696d4a6d95bfaf9445b76f08878aca473454639e0

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.3.1-cp38-cp38-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: onnxruntime_extensions-0.3.1-cp38-cp38-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 733.1 kB
  • Tags: CPython 3.8, macOS 10.14+ 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.3.1-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 a8c8c3e0a6dc7f172b05a388990a8f9b9b6c3940664cef56d969731a156e824c
MD5 7291adc94ec40a95cd7055226404dd81
BLAKE2b-256 842f058d6337e0b79ff88b31fd570afdb54af142ddb413893e99ece8fe72c3c5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: onnxruntime_extensions-0.3.1-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 475.0 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.3.1-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 7803b96b63fb6d7a826aab0034e3f56d1047030ce730d4478cb40cfd319e82a5
MD5 39cb9e3b899c64fb4ee61477ec56f527
BLAKE2b-256 52ac69aa44b83bed89f9382693ebeaff3a98ed49463375334398474876431bf8

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.3.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for onnxruntime_extensions-0.3.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6d8c06a43c22147bdeb1e7826f01ff60a48e801074220da43ff8446aba1f7da0
MD5 5382ac043b424feb6b460217efdc84a2
BLAKE2b-256 41e58eb8cba2cf07ff76e5a0e6446e5ba1d57d605c8f9fc3b53f3e5383d32798

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.3.1-cp37-cp37m-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for onnxruntime_extensions-0.3.1-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 dc2c8774eaaf442bf0b5c56173774b938287066d219e430833ce93a801e704c7
MD5 5a0174f8935d058c44b4345fd8ba0ec8
BLAKE2b-256 f06b2af6789ac3ed0610582f574d2d6d8149c04ded4526b7b27e8f6f1b7d02c3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: onnxruntime_extensions-0.3.1-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 475.0 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.3.1-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 572972c743601d8e04ab5007f8ee941843208680cd2d0afe531637a21b9d3797
MD5 020ee10dd27bd8cc83456639a16dbe77
BLAKE2b-256 3c7d051ad32818d4c2225fcfe0ffa6aabbbe2ce2d72c7ec602951156f2c552f4

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.3.1-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for onnxruntime_extensions-0.3.1-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 40c77f77b2dbb7aed8f92187f7bec6355c158806ef2e45e305ab7a7515929844
MD5 ebe4553cb383984b3466ef37ea7dc9b5
BLAKE2b-256 88b1b4c69abac615c1d7e37d8faa670ac66bd990b55a3f7bf40e4c389a362138

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.3.1-cp36-cp36m-macosx_10_14_x86_64.whl.

File metadata

File hashes

Hashes for onnxruntime_extensions-0.3.1-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 1233c1e468b3be21354997c5fe9ea7b58f23b058ccecaa81d38d888f19bee065
MD5 eec51c6b83726f9d64ef1c4233447d3b
BLAKE2b-256 77764102fce1a949975841e51682447117fa7fc3918dca52a5997c8d8e7ca1d0

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