Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import all initialized tensors as dense constants #30

Merged
merged 8 commits into from
Mar 16, 2020

Conversation

tungld
Copy link
Collaborator

@tungld tungld commented Mar 13, 2020

This patch is to:

  • Fix value attribute format for ConstantOp by using dense tensor,
  • Import all initialized tensors as dense constants

By this patch, I successfully ran MNIST model (which was obtained from: https://github.com/onnx/models/tree/master/vision/classification/mnist)

Python script (Implementation of DummyBackend is not shown here):

import onnx
from onnx import numpy_helper
import numpy as np
test_data_dir = '/home/tungld/dl/dlc-misc/mnist/'
model = onnx.load(test_data_dir + 'model.onnx')

inputs = []
input_file = test_data_dir + 'test_data_set_0/input_0.pb'
tensor = onnx.TensorProto()
with open(input_file, 'rb') as f:
    tensor.ParseFromString(f.read())
inputs.append(numpy_helper.to_array(tensor))

# Run inference with onnx-mlir
outputs = DummyBackend.run_model(model, inputs)
print("Predicted output: \n")
print(outputs)

true_outputs = []
output_file = test_data_dir + 'test_data_set_0/output_0.pb'
output_tensor = onnx.TensorProto()
with open(output_file, 'rb') as f:
    output_tensor.ParseFromString(f.read())
true_outputs.append(numpy_helper.to_array(output_tensor))
print("True output: \n")
print(true_outputs)

Here is output (ignored unrelated parts):

[  1%] Built target cruntime
[  3%] Built target pyruntime
[  5%] Built target gen_onnx_decompose
[  7%] Built target gen_shape_inference
[ 10%] Built target gen_onnx_combine
[ 15%] Built target gen_onnx_rewrite
[ 21%] Built target gen_onnx
[ 28%] Built target gen_krnl_ops
[ 35%] Built target compiler
[ 35%] Built target gen_onnx_proto
[ 40%] Built target onnx_proto
[ 75%] Built target onnx
[ 78%] Built target builder
[ 80%] Built target onnf_shape_inference
[ 94%] Built target onnf_lower_frontend
[ 96%] Built target onnf_onnx_decompose
[ 98%] Built target onnf_transform
[100%] Built target onnf
Predicted output:

[array([[  975.6703  ,  -618.7241  ,  6574.5684  ,   668.02795 ,
         -917.2716  , -1671.636   , -1952.7598  ,   -61.549263,
         -777.1762  , -1439.5306  ]], dtype=float32)]
True output:

[array([[  975.6701  ,  -618.72394 ,  6574.5684  ,   668.02893 ,
         -917.27094 , -1671.6359  , -1952.7599  ,   -61.549873,
         -777.17664 , -1439.5316  ]], dtype=float32)]

Copy link
Contributor

@tjingrant tjingrant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, congrats on getting mnist to work!

@doru1004
Copy link
Collaborator

I think it's great that we now have confirmation that the code we have been generating so far is correct and can correctly run MNIST.

I have some reservations in terms of the solution of emitting large constants in the source code. We can avoid that and we should try to do so.

I am now working on a more robust way of dealing with constant inputs.

Copy link
Collaborator

@doru1004 doru1004 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LG

@doru1004 doru1004 merged commit d86591d into onnx:master Mar 16, 2020
@tungld tungld deleted the import_initialized_tensor branch January 26, 2021 03:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants