onnx-mlir icon indicating copy to clipboard operation
onnx-mlir copied to clipboard

Modify RunONNXModel.py for pip install

Open chentong319 opened this issue 1 year ago • 4 comments

The goal is to create a python package so that we can run an inference like onnxruntime in python script. Since the RunONNXModel.py provides most of the necessary functionality, I added extra support in that script. The pip package related code will be checked later. I tested with a local pip install. We can upload the package later.

Major changes:

  1. Add the function interface to call the main in RunONNXModel.py with the parameters similar to onnxruntime:
  2. Accept list of arrays as input.
  3. Return the outputs

Example:

import numpy as np
import onnxmlirrun

a = np.zeros((3,4,5), dtype=np.float32)
b = a + 4
r = onnxmlirrun.RunONNXModel.onnxmlirrun(compiled_so="/Users/chentong/Projects/onnx-mlir/build/test_add.so", inputs=[a,b])
print(r)
r = onnxmlirrun.RunONNXModel.onnxmlirrun(onnx_model="/Users/chentong/Projects/onnx-mlir/build/test_add.onnx", inputs=[a,b])
print(r)

We may have different name for the functions and the package. Comments are welcome.

chentong319 avatar Jun 27 '24 15:06 chentong319

@chentong319 Can you add a reference to the ORT interface that this PR is imitating.

In your first example

r = onnxmlirrun.RunONNXModel.onnxmlirrun(compiled_so="/Users/chentong/Projects/onnx-mlir/build/test_add.so", inputs=[a,b])

was the onnx_model parameter omitted on purpose? Just for me to understand better the interface.

AlexandreEichenberger avatar Jun 27 '24 16:06 AlexandreEichenberger

@chentong319 Can you add a reference to the ORT interface that this PR is imitating.

In your first example

r = onnxmlirrun.RunONNXModel.onnxmlirrun(compiled_so="/Users/chentong/Projects/onnx-mlir/build/test_add.so", inputs=[a,b])

was the onnx_model parameter omitted on purpose? Just for me to understand better the interface.

Yes, onnx_model is not needed if the compiled .so is provided. By the way, the two onnxmilrrun.RunONNXModel.onnxmlirrun commands are two independent ways to do inference.

chentong319 avatar Jun 27 '24 18:06 chentong319

Got it, so we can provide a onnx file or a pre-compiled binary, smart, thanks.

AlexandreEichenberger avatar Jun 27 '24 18:06 AlexandreEichenberger

onnxmlirrun.RunONNXModel.onnxmlirrun

My two cents here. For the package name (the first onnxmlirrun), just onnxmlir is enough to avoid typing two Rs. I would remove RunONNXModel, but it seems not straightforward unless we reorganize RunONNXModel.py.

Anyway, we have several python utilities, perhaps it's time to reorganize them into a single python package.

tungld avatar Jul 01 '24 07:07 tungld

I changed the init.py for the package to simplify onnxmlirrun.RunONNXModel.onnxmlirrun to onnxmlir.onnxmlir. I am thinking to add the code for package into onnx-mlir in future.

chentong319 avatar Jul 18 '24 19:07 chentong319

Can you copy/link a description of the ORT runtime interface and update ours with the new names, so that we can see how they relate?

Is it something on this page? https://onnxruntime.ai/docs/get-started/with-python.html

AlexandreEichenberger avatar Jul 18 '24 19:07 AlexandreEichenberger

The Ort code is like

import onnxruntime as ort
import numpy as np
x, y = test_data[0][0], test_data[0][1]
ort_sess = ort.InferenceSession('fashion_mnist_model.onnx')
outputs = ort_sess.run(None, {'input': x.numpy()})

Since the current RunModelPython does not separate the session creation and run, our code will be like:

import onnxmlir
import numpy as np
x, y = test_data[0][0], test_data[0][1]
outputs = onnxmlir.run(onnx_model='fashion_mnist_model.onnx', inputs = [a, b])

It is doable to totally mimic ORT in separating the session creation and run, and using dictionary for input.

chentong319 avatar Jul 20 '24 19:07 chentong319

Ideally

import onnxmlir as ort
import numpy as np
x, y = test_data[0][0], test_data[0][1]
ort_sess = ort.InferenceSession('fashion_mnist_model.onnx')
outputs = ort_sess.run(None, {'input': x.numpy()})

with additional parameters ok

import onnxruntime as ort
import numpy as np
x, y = test_data[0][0], test_data[0][1]
ort_sess = ort.InferenceSession('fashion_mnist_model.onnx', compile_flags="-O3")
outputs = ort_sess.run(None, {'input': x.numpy()})

reusing flags that already exists, if any.

AlexandreEichenberger avatar Jul 23 '24 15:07 AlexandreEichenberger

Check error message if file does not exist.

onnx-mlir will report the error if file does not exist. Leave this to the TODO list to catch all the errors gracefully.

chentong319 avatar Aug 06 '24 18:08 chentong319

@jenkins-droid test it!

chentong319 avatar Aug 13 '24 15:08 chentong319

@jenkins-droid test this please

hamptonm1 avatar Aug 13 '24 15:08 hamptonm1

Jenkins Linux ppc64le Build #14467 [push] Modify RunONNXModel.py f... started at 11:56

jenkins-droid avatar Aug 26 '24 15:08 jenkins-droid

Jenkins Linux amd64 Build #15437 [push] Modify RunONNXModel.py f... started at 10:46

jenkins-droid avatar Aug 26 '24 15:08 jenkins-droid

Jenkins Linux s390x Build #15444 [push] Modify RunONNXModel.py f... started at 11:46

jenkins-droid avatar Aug 26 '24 15:08 jenkins-droid

Jenkins Linux amd64 Build #15437 [push] Modify RunONNXModel.py f... passed after 1 hr 7 min

jenkins-droid avatar Aug 26 '24 16:08 jenkins-droid

Jenkins Linux s390x Build #15444 [push] Modify RunONNXModel.py f... passed after 1 hr 29 min

jenkins-droid avatar Aug 26 '24 17:08 jenkins-droid

Jenkins Linux ppc64le Build #14467 [push] Modify RunONNXModel.py f... passed after 2 hr 1 min

jenkins-droid avatar Aug 26 '24 17:08 jenkins-droid