microsoft/onnxruntime-extensions

Public

mirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.3.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

README.md

110lines · modecode

1# ONNXRuntime Extensions
2[![Build Status](https://dev.azure.com/aiinfra/ONNX%20Converters/_apis/build/status/microsoft.ort-customops?repoName=microsoft%2Fonnxruntime-extensions&branchName=main)](https://dev.azure.com/aiinfra/ONNX%20Converters/_build/latest?definitionId=907&repoName=microsoft%2Fonnxruntime-extensions&branchName=main)
3
4# Introduction
5ONNXRuntime Extensions is a comprehensive package to extend the capability of the ONNX conversion and inference.
61. The CustomOp C++ library for [ONNX Runtime](http://onnxruntime.ai) on ONNXRuntime CustomOp API.
72. Support PyOp feature to implement the custom op with a Python function.
83. Build all-in-one ONNX model from the pre/post processing code, go to [docs/pre_post_processing.md](https://github.com/microsoft/onnxruntime-extensions/blob/main/docs/pre_post_processing.md) for details.
94. Support Python per operator debugging, checking ```hook_model_op``` in onnxruntime_extensions Python package.
10
11# Quick Start
12The following code shows how to run ONNX model and ONNXRuntime customop more straightforwardly.
13```python
14import numpy
15from onnxruntime_extensions import PyOrtFunction, VectorToString
16# <ProjectDir>/tutorials/data/gpt-2/gpt2_tok.onnx
17encode = PyOrtFunction.from_model('gpt2_tok.onnx')
18# https://github.com/onnx/models/blob/master/text/machine_comprehension/gpt-2/model/gpt2-lm-head-10.onnx
19gpt2_core = PyOrtFunction.from_model('gpt2-lm-head-10.onnx')
20decode = PyOrtFunction.from_customop(VectorToString, map={' a': [257]}, unk='<unknown>')
21
22input_text = ['It is very cool to have']
23output, *_ = gpt2_core(input_ids)
24next_id = numpy.argmax(output[:, :, -1, :], axis=-1)
25print(input_text[0] + decode(next_id).item())
26```
27This 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](https://github.com/microsoft/onnxruntime-extensions/blob/main/tutorials/gpt2bs.py) to the experimental.
28
29## Android/iOS
30The 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](https://github.com/microsoft/onnxruntime-extensions/blob/main/tutorials/gpt2bs.py)
31
32## CustomOp Conversion
33The 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.
341. [CustomOp conversion by pytorch.onnx.exporter](https://github.com/microsoft/onnxruntime-extensions/blob/main/tutorials/pytorch_custom_ops_tutorial.ipynb)
352. [CustomOp conversion by tf2onnx](https://github.com/microsoft/onnxruntime-extensions/blob/main/tutorials/tf2onnx_custom_ops_tutorial.ipynb)
36
37## Inference with CustomOp library
38The 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.
39```C++
40 // The line loads the customop library into ONNXRuntime engine to load the ONNX model with the custom op
41 Ort::ThrowOnError(Ort::GetApi().RegisterCustomOpsLibrary((OrtSessionOptions*)session_options, custom_op_library_filename, &handle));
42
43 // The regular ONNXRuntime invoking to run the model.
44 Ort::Session session(env, model_uri, session_options);
45 RunSession(session, inputs, outputs);
46```
47Of 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
48```python
49import onnxruntime as _ort
50from onnxruntime_extensions import get_library_path as _lib_path
51
52so = _ort.SessionOptions()
53so.register_custom_ops_library(_lib_path())
54
55# Run the ONNXRuntime Session.
56# sess = _ort.InferenceSession(model, so)
57# sess.run (...)
58```
59
60## More CustomOp
61Welcome 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
62```python
63import numpy
64from onnxruntime_extensions import PyOp, onnx_op
65
66# Implement the CustomOp by decorating a function with onnx_op
67@onnx_op(op_type="Inverse", inputs=[PyOp.dt_float])
68def inverse(x):
69 # the user custom op implementation here:
70 return numpy.linalg.inv(x)
71
72# Run the model with this custom op
73# model_func = PyOrtFunction(model_path)
74# outputs = model_func(inputs)
75# ...
76```
77
78# Build and Development
79This project supports Python and can be built from source easily, or a simple cmake build without Python dependency.
80## Python package
81- 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_)
82- Prepare Python env and install the pip packages in the requirements.txt.
83- `python setup.py install` to build and install the package.
84- 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.
85
86Test:
87- run `pytest test` in the project root directory.
88
89## The share library for non-Python
90If only DLL/shared library is needed without any Python dependencies, please run `build.bat` or `bash ./build.sh` to build the library.
91By 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.
92
93## The static library and link with ONNXRuntime
94For sake of the binary size, the project can be built as a static library and link into ONNXRuntime. Here is [the script](https://github.com/microsoft/onnxruntime-extensions/blob/main/ci_build/onnxruntime_integration/build_with_onnxruntime.sh) to this, which is especially usefully on building the mobile release.
95
96# Contributing
97This project welcomes contributions and suggestions. Most contributions require you to agree to a
98Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us
99the rights to use your contribution. For details, visit https://cla.microsoft.com.
100
101When you submit a pull request, a CLA-bot will automatically determine whether you need to provide
102a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions
103provided by the bot. You will only need to do this once across all repos using our CLA.
104
105This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/).
106For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or
107contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
108
109# License
110[MIT License](LICENSE)
111