ONNX Interface for Framework Integration (ONNXIFI)¶
ONNXIFI is a cross-platform API for loading and executing ONNX graphs on optimized backends. High-level frameworks and applications can use this API to execute neural network and machine learning models. Hardware vendors can implement this API to expose specialized hardware accelerators and highly optimized software infrastructure to the users.
Core Features¶
Standardized interface for neural network inference on special-purpose accelerators (NPUs), CPUs, GPUs, DSPs, and FPGAs
Based on widely supported technologies
C API for function calls
ONNX format for passing model graphs
NCHW tensor layout for passing inputs and outputs
Dynamic discovery of available backends for model execution
Multiple backends from different vendors can co-exist on the same system
Dynamic discovery of supported ONNX Operators on each backend
Optional Features:¶
Graphs with variable-shape inputs and/or outputs
Graphs with data-dependendent output shapes
How to Use ONNX Interface for Framework Integration¶
(Optional) Use
onnxifi_loadto dynamically load the ONNX Interface for Framework Integration library.Call
onnxGetBackendIDsto get stable identifiers of available backends. Note: it is possible there are no backends installed in the system.Call
onnxGetBackendInfoto check additional information about any available backend.Call
onnxGetBackendCompatibilityto check which operations within your model can run on the backend.Call
onnxInitBackendto initialize a backend, then callonnxInitGraphto offload one or more model graphs to the backend.Call
onnxSetGraphIOto set locations and shapes for inputs and outputs of a graph.Initialize an
inputFencestructure of typeonnxMemoryFenceV1: settagtoONNXIFI_TAG_MEMORY_FENCE_V1,typetoONNXIFI_SYNCHRONIZATION_EVENT, and callonnxInitEventto initiaze theeventmember.Initialize an
outputFencestructure of typeonnxMemoryFenceV1: settagtoONNXIFI_TAG_MEMORY_FENCE_V1,typetoONNXIFI_SYNCHRONIZATION_EVENT, andeventto null.Call
onnxRunGraphwith the initializedinputFenceandoutputFencestructures to enable execution of the graph. The call toonnxRunGraphwill populateeventmember of theoutputFencewith a newly created event object, asynchronously execute the graph onceinputFence’seventis signalled, and then signal theoutputFence’sevent.Call
onnxSignalEventwitheventmember ofinputFenceto signal to the backend that the inputs are ready to be consumed.Call
onnxWaitEvent(alternatively, repeatedly callonnxGetEventStatein a loop until the event state isONNXIFI_EVENT_STATE_SIGNALLED) witheventmember ofoutputFenceto wait until graph outputs are ready to be consumed. Release events for inputs and outputs usingonnxReleaseEvent.If your model works with fixed-size inputs and outputs, and shape and location of inputs and outputs does not change, one call to
onnxSetGraphIOis sufficient for multipleonnxRunGraphcalls. The previous call toonnxRunGraph, however, must have finished before a user callsonnxRunGraphagain, because concurrent execution with the same input and output locations is not allowed. For models with variable-size inputs or outputs, you’d need to callonnxSetGraphIObefore eachonnxRunGraphcall.When done using the model, release the model graph(s) with
onnxReleaseGraph, then release the backend withonnxReleaseBackendand backend ID withonnxReleaseBackendID.
How to Implement ONNX Interface for Framework Integration¶
The minimum functionality an ONNXIFI implementation must provide is the following:
Support ONNX 1.0 model format.
There is no minimum list of Operators a backend has to support.
Support graph inputs / outputs in CPU memory.
Support graph inputs / outputs with fixed shape, specified in GraphProto message.
Discovery¶
Vendor-provided libraries should adhere to some rules to ensure discovery by ONNX-supported frameworks and applications:
The libraries must be installed in the following directories:
GNU/Linux: user-installed system library directory (typically /usr/lib)
macOS: /opt/onnx/lib
Windows: system directory (typically C:\Windows\System32)
Filenames of vendor-specific libraries must follow the rule below:
On Windows, library filename must match wildcard
onnxifi-*.dllOn macOS, library filename must match wildcard
libonnxifi-*.dylibOn Linux and other OSes, library filename must match wildcard
libonnxifi-*.so
Extensions¶
Hardware vendors are welcome to add their own extensions to ONNX backend interface. The backend interface offers several extension mechanisms:
Experimental, exotic, or vendor-specific operators can be supported in a private domain using NodeProto.domain attribute.
Vendor-provided ONNXIFI implementation can expose additional functions.