[Relay][ONNX]Excute ONNX model to to TVM failed

when I run the following code:
model_path = ‘mlpsimple.onnx’
onnx_model = onnx.load(model_path)

target = ‘llvm’

input_name = ‘input’
test_input = np.random.rand(1,1)
shape_dict = {input_name: test_input.shape}

sym, params = relay.frontend.from_onnx(onnx_model,shape=shape_dict, dtype=‘float32’)

print (‘sym’,sym)
print (‘params’,params)

with relay.build_config(opt_level=1):
intrp = relay.build_module.create_executor(‘graph’, sym, tvm.cpu(0), target)

Execute on TVM

dtype = ‘float32’
tvm_output = intrp.evaluate(sym)(tvm.nd.array(test_input.astype(dtype)), **params).asnumpy()

I will excounter the following error:
sym v0.0.1
%6 = fn (%input: Tensor[(1, 1), float32], %v1: Tensor[(1, 1), float32], %v2: Tensor[(1,), float32]) {
%0 = nn.batch_flatten(%input)
%1 = multiply(1f, %0)
%2 = nn.dense(%1, %v1, units=1)
%3 = multiply(1f, %v2)
%4 = nn.bias_add(%2, %3)
%5 = nn.relu(%4)
%5
}
%6
params {‘1’: <tvm.NDArray shape=(1, 1), cpu(0)>
array([[-0.46104646]], dtype=float32), ‘2’: <tvm.NDArray shape=(1,), cpu(0)>
array([-0.3470515], dtype=float32)}
-> tvm_output = intrp.evaluate(sym)(tvm.nd.array(test_input.astype(dtype)), **params).asnumpy()
*** stack smashing detected ***: python terminated
core dump

Can you attach mlpsimple.onnx of not confidential ?

Build a Mock Model in PyTorch with a convolution and a reduceMean layer

import torch
import torch.onnx as torch_onnx
from torch.autograd import Variable

class MLP(torch.nn.Module):
def init(self):
super(MLP,self).init()
self.fc1 = torch.nn.Linear(1,1)

def forward(self,din):
    dout = torch.nn.functional.relu(self.fc1(din))
    return dout

Use this an input trace to serialize the model

model_onnx_path = “mlpsimple1.onnx”
model = MLP()
print (model.state_dict())

Export the model to an ONNX file

dummy_input = Variable(torch.randn(1,1))

output = torch.onnx.export(model,
dummy_input,
model_onnx_path,
export_params=True,
verbose=True)

print(“Export of torch_model.onnx complete!”)

I attach the mlpsimple.onnx, please chech it. thanks

I change the server to run it, it’s success.

1 Like