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_load
to dynamically load the ONNX Interface for Framework Integration library.Call
onnxGetBackendIDs
to get stable identifiers of available backends. Note: it is possible there are no backends installed in the system.Call
onnxGetBackendInfo
to check additional information about any available backend.Call
onnxGetBackendCompatibility
to check which operations within your model can run on the backend.Call
onnxInitBackend
to initialize a backend, then callonnxInitGraph
to offload one or more model graphs to the backend.Call
onnxSetGraphIO
to set locations and shapes for inputs and outputs of a graph.Initialize an
inputFence
structure of typeonnxMemoryFenceV1
: settag
toONNXIFI_TAG_MEMORY_FENCE_V1
,type
toONNXIFI_SYNCHRONIZATION_EVENT
, and callonnxInitEvent
to initiaze theevent
member.Initialize an
outputFence
structure of typeonnxMemoryFenceV1
: settag
toONNXIFI_TAG_MEMORY_FENCE_V1
,type
toONNXIFI_SYNCHRONIZATION_EVENT
, andevent
to null.Call
onnxRunGraph
with the initializedinputFence
andoutputFence
structures to enable execution of the graph. The call toonnxRunGraph
will populateevent
member of theoutputFence
with a newly created event object, asynchronously execute the graph onceinputFence
’sevent
is signalled, and then signal theoutputFence
’sevent
.Call
onnxSignalEvent
withevent
member ofinputFence
to signal to the backend that the inputs are ready to be consumed.Call
onnxWaitEvent
(alternatively, repeatedly callonnxGetEventState
in a loop until the event state isONNXIFI_EVENT_STATE_SIGNALLED
) withevent
member ofoutputFence
to 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
onnxSetGraphIO
is sufficient for multipleonnxRunGraph
calls. The previous call toonnxRunGraph
, however, must have finished before a user callsonnxRunGraph
again, 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 callonnxSetGraphIO
before eachonnxRunGraph
call.When done using the model, release the model graph(s) with
onnxReleaseGraph
, then release the backend withonnxReleaseBackend
and 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-*.dll
On macOS, library filename must match wildcard
libonnxifi-*.dylib
On 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.