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.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (988.1 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

onnxruntime_extensions-0.3.0-cp39-cp39-macosx_10_14_x86_64.whl (732.9 kB view details)

Uploaded CPython 3.9 macOS 10.14+ x86-64

onnxruntime_extensions-0.3.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (987.9 kB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

onnxruntime_extensions-0.3.0-cp38-cp38-macosx_10_14_x86_64.whl (733.0 kB view details)

Uploaded CPython 3.8 macOS 10.14+ x86-64

onnxruntime_extensions-0.3.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (990.2 kB view details)

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

onnxruntime_extensions-0.3.0-cp37-cp37m-macosx_10_14_x86_64.whl (731.5 kB view details)

Uploaded CPython 3.7m macOS 10.14+ x86-64

onnxruntime_extensions-0.3.0-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (990.2 kB view details)

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

onnxruntime_extensions-0.3.0-cp36-cp36m-macosx_10_14_x86_64.whl (731.5 kB view details)

Uploaded CPython 3.6m macOS 10.14+ x86-64

onnxruntime_extensions-0.3.0-3-cp39-cp39-win_amd64.whl (473.8 kB view details)

Uploaded CPython 3.9 Windows x86-64

onnxruntime_extensions-0.3.0-3-cp38-cp38-win_amd64.whl (473.8 kB view details)

Uploaded CPython 3.8 Windows x86-64

onnxruntime_extensions-0.3.0-3-cp37-cp37m-win_amd64.whl (474.8 kB view details)

Uploaded CPython 3.7m Windows x86-64

onnxruntime_extensions-0.3.0-3-cp36-cp36m-win_amd64.whl (474.8 kB view details)

Uploaded CPython 3.6m Windows x86-64

File details

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

File metadata

File hashes

Hashes for onnxruntime_extensions-0.3.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 98b045141b54e2c18b99cec1adf34f23103902105c314e3daeedd7fce55568df
MD5 27ac65c1cfa4cab4b57d166bfeaae712
BLAKE2b-256 31b105d77e11067970d496d4bf5268c2dd1957be3e3b25c0ac232a181d8a5aae

See more details on using hashes here.

File details

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

File metadata

  • Download URL: onnxruntime_extensions-0.3.0-cp39-cp39-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 732.9 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.0-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 b37851a389148602ace8189901f6f50e5cd2e0ab7f98f93839554c1ed23dfae1
MD5 91f9f385a7f4145c43614b79f53b7c1e
BLAKE2b-256 87a345291d29c6adfb6a011c316799d7b909e5a1642c59ffcb394ff91cec2d23

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnxruntime_extensions-0.3.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c15a06bf1d7638966b2067cc8d44b76f1b4bc1ad4ed7fa5aa2fadf42aae854a6
MD5 8a8dfd90ecb246f975326821b392fe95
BLAKE2b-256 8207ab422228a43cfa8654405d593ac19970b384ec8a8a37063ce7752d293493

See more details on using hashes here.

File details

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

File metadata

  • Download URL: onnxruntime_extensions-0.3.0-cp38-cp38-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 733.0 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.0-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 825e3aa5b742b40a3fc5919f99d09f5a2e31975eda41e8e4152900f38f109742
MD5 f35d13d0a161c5be84561f49cc795b20
BLAKE2b-256 a2a42728324ad0c77777481fbff23f722a16359929c7d1c15a2a0d92177ff662

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnxruntime_extensions-0.3.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 abea3179004c02995010cbd9c3496064038692ae570b424d1ca5d33d43b8279e
MD5 4a5706c345779b1001e9158bab677e7e
BLAKE2b-256 571f05fe8771acfc9c43fdb3a2a5902ac3e5fa27c826721d3e71ab65619b0797

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnxruntime_extensions-0.3.0-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 cb8aab73cbdc6a7684925645f0054b170516f2d52c99e96f3129b1c95dfecb4c
MD5 2e53d9d0080854cecb2c41679ce69144
BLAKE2b-256 f3f50dbe76d919d589b01892d07266bdc6ccf32f931c34f2c1a4ed0ac69e1d12

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnxruntime_extensions-0.3.0-cp36-cp36m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 95084d8e9abc21b306e74e223bff4ef8ee8fa1e3a05ed0adfacb34e390a826c9
MD5 f5d1a33cac556d3c3a8bb96df667bd38
BLAKE2b-256 5009f00202eb622ffbd255b8d36aeed88cc6737474f6620ce1efee715150cb5d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for onnxruntime_extensions-0.3.0-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 b062be67fac809cdeb338d01317ce99bd12467a49ce16dc95b24edda8bd5cb29
MD5 ccd7c0c41d952ba9b69138058c0f796b
BLAKE2b-256 ee7f8fc2186880d2e12179ff04ff0f1d14402e7fa2c35dcc73cbd00fcbc1da64

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.3.0-3-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: onnxruntime_extensions-0.3.0-3-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 473.8 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.0-3-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 931a553a16daebc718865d1a239f5bfad9155d832803483299706f7336aeedf2
MD5 50dbb870f3150712e211df0f02bf9fb5
BLAKE2b-256 3f0c840bfcaa6476b55e40d39b793aaabddb7fd6d0fe9a208c4f95ecb3193d35

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.3.0-3-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: onnxruntime_extensions-0.3.0-3-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 473.8 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.0-3-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 706ec1a21a23abda550423c826e37e85753950c2e75fdde3d9ab5a38a69b9310
MD5 36a80af06750d8ee281bf0d642c75452
BLAKE2b-256 6ddf51d4924f9b9439844dd86a295e3423d5ba184427b76a28d97c2f4131366b

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.3.0-3-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: onnxruntime_extensions-0.3.0-3-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 474.8 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.0-3-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 9f18b4523e8a559c4d5948d2e05c5ae5b480cebef0c185270eb170eeea3fcf81
MD5 602cc75506fba310ce9b6f690ff7c50e
BLAKE2b-256 7ad9c38a5ed3b2de18c1534c3aed46ef0ec579e4ba45c1ad30a7362cedb5c030

See more details on using hashes here.

File details

Details for the file onnxruntime_extensions-0.3.0-3-cp36-cp36m-win_amd64.whl.

File metadata

  • Download URL: onnxruntime_extensions-0.3.0-3-cp36-cp36m-win_amd64.whl
  • Upload date:
  • Size: 474.8 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.0-3-cp36-cp36m-win_amd64.whl
Algorithm Hash digest
SHA256 8eb3e929a9ea8cedcad63113efbafd1bf738ed72af0b4b7ae9ef5f21df80521a
MD5 01f089e7bc58aa978433b9389d7a84af
BLAKE2b-256 e172b6436517590573404d024522a54d06ab21d912a87d5d5432debc67ac7989

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