onnx-mlir
|
ONNX-MLIR project comes with an executable onnx-mlir
capable of compiling onnx models to a shared library. In this documentation, we demonstrate how to interact programmatically with the compiled shared library using ONNX-MLIR's Runtime API.
OMTensor
is the data structure used to describe the runtime information (rank, shape, data type, etc) associated with a tensor input or output.
OMTensorList
is the data structure used to hold a list of pointers to OMTensor so that they can be passed into and out of the compiled model as inputs and outputs.
All compiled model will have the same exact C function signature equivalent to:
Intuitively, the model takes a list of tensors as input and returns a list of ensors as output.
API
We demonstrate using the API functions to run a simple ONNX model consisting of an add operation. To create such an onnx model, use this python script
To compile the above model, run onnx-mlir add.onnx
and a binary library "add.so" should appear. We can use the following C code to call into the compiled function computing the sum of two inputs:
Compile with gcc main.c add.so -o add
, you should see an executable add
appearing. Run it, and the output should be:
Exactly as it should be.
For full reference to available C Runtime API, refer to include/onnx-mlir/Runtime/OMTensor.h
and include/onnx-mlir/Runtime/OMTensorList.h
.