Skip to main content

ONNXRuntime Extensions

Project description

ONNXRuntime-Extensions

Build Status

What's ONNXRuntime-Extensions

Introduction: ONNXRuntime-Extensions is a library that extends the capability of the ONNX models and inference with ONNX Runtime, via ONNX Runtime Custom Operator ABIs. It includes a set of ONNX Runtime Custom Operator to support the common pre- and post-processing operators for vision, text, and nlp models. And it supports multiple languages and platforms, like Python on Windows/Linux/macOS, some mobile platforms like Android and iOS, and Web-Assembly etc. The basic workflow is to enhance a ONNX model firstly and then do the model inference with ONNX Runtime and ONNXRuntime-Extensions package.

Quickstart

Python installation

pip install onnxruntime-extensions

nightly build

on Windows

pip install --index-url https://aiinfra.pkgs.visualstudio.com/PublicPackages/_packaging/ORT-Nightly/pypi/simple/ onnxruntime-extensions

Please ensure that you have met the prerequisites of onnxruntime-extensions (e.g., onnx and onnxruntime) in your Python environment.

on Linux/macOS

the packages are not ready yet, so it could be installed from source. Please make sure the compiler toolkit like gcc(later than g++ 8.0) or clang, and the tool cmake are installed before the following command

python -m pip install git+https://github.com/microsoft/onnxruntime-extensions.git

Usage

1. Augment an ONNX model with a pre- and post-processing pipeline

check tutorial for a couple of examples on how to do it.

2. Using Extensions for ONNX Runtime inference

Python

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, as ONNXRuntime docs suggested.
# sess = _ort.InferenceSession(model, so)
# sess.run (...)

C++

  // 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);

Java

var env = OrtEnvironment.getEnvironment();
var sess_opt = new OrtSession.SessionOptions();

/* Register the custom ops from onnxruntime-extensions */
sess_opt.registerCustomOpLibrary(OrtxPackage.getLibraryPath());

Use exporters to generate graphs with custom operators

The PyTorch and TensorFlow converters support custom operator generation if the operation from the original framework cannot be interpreted as a standard ONNX operators. Check the following two examples on how to do this.

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

Add a new custom operator to onnxruntime-extensions

You can contribute customop C++ implementations directly in this repository if they have general applicability to other users. In addition, if you want to quickly verify the ONNX model with Python, you can wrap the custom operator with PyOp.

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)
# ...

Check development.md for build and test

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.8.0-cp310-cp310-win_amd64.whl (1.7 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

onnxruntime_extensions-0.8.0-cp310-cp310-musllinux_1_1_x86_64.whl (4.8 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

onnxruntime_extensions-0.8.0-cp310-cp310-musllinux_1_1_i686.whl (4.5 MB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

onnxruntime_extensions-0.8.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.4 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

onnxruntime_extensions-0.8.0-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl (4.0 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686

onnxruntime_extensions-0.8.0-cp310-cp310-macosx_11_0_arm64.whl (2.6 MB view hashes)

Uploaded CPython 3.10 macOS 11.0+ ARM64

onnxruntime_extensions-0.8.0-cp310-cp310-macosx_10_9_x86_64.whl (3.8 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

onnxruntime_extensions-0.8.0-cp310-cp310-macosx_10_9_universal2.whl (5.3 MB view hashes)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

onnxruntime_extensions-0.8.0-cp39-cp39-win_amd64.whl (1.7 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

onnxruntime_extensions-0.8.0-cp39-cp39-musllinux_1_1_x86_64.whl (4.8 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

onnxruntime_extensions-0.8.0-cp39-cp39-musllinux_1_1_i686.whl (4.5 MB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

onnxruntime_extensions-0.8.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.4 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

onnxruntime_extensions-0.8.0-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl (4.0 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686

onnxruntime_extensions-0.8.0-cp39-cp39-macosx_11_0_arm64.whl (2.6 MB view hashes)

Uploaded CPython 3.9 macOS 11.0+ ARM64

onnxruntime_extensions-0.8.0-cp39-cp39-macosx_10_9_x86_64.whl (3.8 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

onnxruntime_extensions-0.8.0-cp39-cp39-macosx_10_9_universal2.whl (5.3 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

onnxruntime_extensions-0.8.0-cp38-cp38-win_amd64.whl (1.7 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

onnxruntime_extensions-0.8.0-cp38-cp38-musllinux_1_1_x86_64.whl (4.8 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

onnxruntime_extensions-0.8.0-cp38-cp38-musllinux_1_1_i686.whl (4.5 MB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ i686

onnxruntime_extensions-0.8.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.4 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

onnxruntime_extensions-0.8.0-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl (4.0 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686

onnxruntime_extensions-0.8.0-cp38-cp38-macosx_11_0_arm64.whl (2.6 MB view hashes)

Uploaded CPython 3.8 macOS 11.0+ ARM64

onnxruntime_extensions-0.8.0-cp38-cp38-macosx_10_9_x86_64.whl (3.8 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

onnxruntime_extensions-0.8.0-cp38-cp38-macosx_10_9_universal2.whl (5.3 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64)

onnxruntime_extensions-0.8.0-cp37-cp37m-win_amd64.whl (1.7 MB view hashes)

Uploaded CPython 3.7m Windows x86-64

onnxruntime_extensions-0.8.0-cp37-cp37m-musllinux_1_1_x86_64.whl (4.8 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

onnxruntime_extensions-0.8.0-cp37-cp37m-musllinux_1_1_i686.whl (4.5 MB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ i686

onnxruntime_extensions-0.8.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.4 MB view hashes)

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

onnxruntime_extensions-0.8.0-cp37-cp37m-manylinux_2_17_i686.manylinux2014_i686.whl (4.0 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686

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