The pre- and post- processing library for ONNX Runtime
Перейти к файлу
Scott McKay e3663fb110
Add build.py to make it easier for developers to build different variants (#318)
* Add python based build infrastructure to simplify developer builds for various platforms. Majority was copied from the ORT build script so usage is consistent with that.

Left the existing build.bat/build.sh but ideally the CI can be updated to use the new infrastructure so things are more consistent.

Updated gradle to 7.5.1 and Android gradle tools to 7.3.0.

Validated Windows and cross-compiling Android on Windows including builds with explicitly selected ops.
WASM and iOS builds aren't tested yet and might need minor tweaks.

* Update build.py to require Python 3.7, remove git submodule sync, reorder options.
* Use 'cmake -E remove' to remove file.
* Enable specifying the ORT version to fetch
* Add ability to enable Java bindings.


Co-authored-by: Wenbing Li <10278425+wenbingl@users.noreply.github.com>
Co-authored-by: edgchen1 <18449977+edgchen1@users.noreply.github.com>
2023-01-02 14:55:31 +10:00
.az Add initial android packaging pipeline (#332) 2022-12-28 15:56:19 -08:00
.config Update tsaoptions.json (#309) 2022-10-25 15:23:33 -07:00
.pipelines Add initial android packaging pipeline (#332) 2022-12-28 15:56:19 -08:00
cmake Add build.py to make it easier for developers to build different variants (#318) 2023-01-02 14:55:31 +10:00
docs Use `object` instead of `np.object` and `str` instead of `np.str`. (#337) 2022-12-19 18:45:07 -05:00
includes fix the ORT API version in header files. (#334) 2022-12-19 10:19:59 -08:00
java Add build.py to make it easier for developers to build different variants (#318) 2023-01-02 14:55:31 +10:00
onnxruntime_extensions a naive decoder for sentencepiece tokenization (#314) 2022-11-21 11:10:30 -08:00
operators Minor opencv tweaks (#316) 2022-12-20 13:47:17 +10:00
pyop Using the header files from the ONNXRuntime package (#322) 2022-12-09 14:30:24 -08:00
shared Using the header files from the ONNXRuntime package (#322) 2022-12-09 14:30:24 -08:00
test Add build.py to make it easier for developers to build different variants (#318) 2023-01-02 14:55:31 +10:00
tools Add build.py to make it easier for developers to build different variants (#318) 2023-01-02 14:55:31 +10:00
tutorials Minor opencv tweaks (#316) 2022-12-20 13:47:17 +10:00
.clang-format initial checkins 2020-10-12 10:52:52 -07:00
.clang-tidy initial checkins 2020-10-12 10:52:52 -07:00
.flake8 initial checkins 2020-10-12 10:52:52 -07:00
.gitignore Add version.txt and derive CMake version variables from it. (#333) 2022-12-16 10:14:28 -08:00
CMakeLists.txt Add build.py to make it easier for developers to build different variants (#318) 2023-01-02 14:55:31 +10:00
CODEOWNERS Create CODEOWNERS 2021-04-21 16:46:21 -07:00
CODE_OF_CONDUCT.md Initial CODE_OF_CONDUCT.md commit 2020-10-05 12:36:41 -07:00
LICENSE Updating LICENSE to template content 2020-10-05 12:36:43 -07:00
MANIFEST.in Prepare for 0.4.0 release (#151) 2021-09-25 00:40:12 -07:00
README.md Fix ci pipelines failure caused by the latest transformer package (#324) 2022-12-06 15:21:06 -08:00
SECURITY.md Initial SECURITY.md commit 2020-10-05 12:36:44 -07:00
build.android Add build.py to make it easier for developers to build different variants (#318) 2023-01-02 14:55:31 +10:00
build.bat update the main doc and add a developer doc (#321) 2022-12-02 16:27:58 -08:00
build.ios_xcframework Add iOS packaging pipeline. (#327) 2022-12-23 05:27:41 -08:00
build.sh support build_id on Python package building (#281) 2022-08-24 14:39:33 -07:00
build_lib.bat Add build.py to make it easier for developers to build different variants (#318) 2023-01-02 14:55:31 +10:00
build_lib.sh Add build.py to make it easier for developers to build different variants (#318) 2023-01-02 14:55:31 +10:00
cgmanifest.json Add `$schema` to `cgmanifest.json` (#284) 2022-08-30 15:45:58 -07:00
pyproject.toml Starter changes for supporting pre/post processing for vision models. (#312) 2022-11-24 07:40:56 +10:00
requirements-dev.txt Include requirements.txt from requirements-dev.txt. (#328) 2022-12-13 10:21:32 -08:00
requirements.txt suppress the protobuf warning. (#299) 2022-10-05 00:29:46 -07:00
setup.cfg Add two opencv operators as ONNX custom ops. (#249) 2022-06-15 23:22:10 -07:00
setup.py using the cmake bundled in visual studio (#319) 2022-11-29 09:54:49 -08:00
version.txt Add version.txt and derive CMake version variables from it. (#333) 2022-12-16 10:14:28 -08:00

README.md

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.

⚠️ NOTE: most ONNXRuntime-Extensions packages are in active development and most packages require building from source. The package information will be updated here if it is published.

Quickstart with the experimental Python package

on Windows

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

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

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

License

MIT License