Pytorch model compilation fails in dense_strategy_cpu

I made/trained a simple model that predicts a sin() value (based on an example that I found on the web). It works in pytorch (v1.4). I then tried to compile it into a TVM model with llvm on Ubuntu with a x86 cpu (I followed the TVM setup guide and some tutorials for onnx that work). I mainly followed existing tutorials on TVM on pytorch and onnx to figure out the procedure below. Note that I’m not a ML nor DataScience engineer. These were my steps:

import tvm, torch, os
from tvm import relay
state = torch.load("/home/dude/tvm/tst_state.pt") # load the trained pytorch state
import tst
m = tst.Net()
m.load_state_dict(state) # init the model with its trained state
m.eval()

sm = torch.jit.trace(m, torch.tensor([3.1415 / 4]))  # convert to a scripted model

# the model only takes 1 input for inference hence [("input0", (1,))]
mod, params = tvm.relay.frontend.from_pytorch(sm, [("input0", (1,))])
mod.astext # outputs some small relay(?) script

with tvm.transform.PassContext(opt_level=1):
  lib = relay.build(mod, target="llvm", target_host="llvm", params=params)

The last step gives me this error that I don’t know how to solve nor where I went wrong:

... removed some lines here ...
  [bt] (3) /home/dude/tvm/build/libtvm.so(TVMFuncCall+0x5f) [0x7f5cd65660af]
  [bt] (2) /home/dude/tvm/build/libtvm.so(+0xb4f8a7) [0x7f5cd5f318a7]
  [bt] (1) /home/dude/tvm/build/libtvm.so(tvm::GenericFunc::CallPacked(tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*) const+0x1ab) [0x7f5cd5f315cb]
  [bt] (0) /home/tvm/build/libtvm.so(+0x1180cab) [0x7f5cd6562cab]
  File "/home/tvm/python/tvm/_ffi/_ctypes/packed_func.py", line 81, in cfun
    rv = local_pyfunc(*pyargs)
  File "/home/tvm/python/tvm/relay/op/strategy/x86.py", line 311, in dense_strategy_cpu
    m, _ = inputs[0].shape
ValueError: not enough values to unpack (expected 2, got 1)

I think if you change the input shape to (1, 1), it would work. The error is happening because the input to dense op is supposed to be 2 dimensional, while your input is 1D.

1 Like

You are right, I tried next with torch.nn.Linear, giving me a similar same issue. After making that input more dimensional, it passed!

1 Like