[SOVLED] DLPACK ERROR: AttributeError: Module has no function ‘__tvm_main__’

I am working on a case: compile a pytorch model using tvm on g4dn.

But I encountered a problem that the tensor conversion between tvm and pytorch is too slow, since some pre-process and post-process are implemented using pytorch.

I found that DLPACK maybe helpful in this case, thus I tried the example here (https://tvm.apache.org/2018/08/10/DLPack-Bridge). The example works well, but when it comes to my pytorch model, I got the error when calling the converted model: [AttributeError: Module has no function ‘tvm_main’].

Can anybody help me on this case? Thank you!

Sample code:

from tvm.contrib.dlpack import to_pytorch_func
m_pytorch = to_pytorch_func(lib\['default'\](ctx))

torch_output = torch.empty(1, 40, 120, 256)
m_pytorch(input1, input2, input3, torch_output)

I removed the (ctx), and the error has been solved. But I got another error: TVMError: Check failed: type_code_ == kTVMContext (13 vs. 6) : expected TVMContext but get NDArrayContainer

I found that the example use [tvm.build], but my code use [tvm.relay.build]. I think it is the reason. But if I use [tvm.build] in my code, it may not work. So, what’s the difference between [tvm.build] and [tvm.relay.build]?

I found that I can simply use DLPACK to convert tvm tensor to pytorch tensor and vice versa. The problem has been solved!

from torch.utils.dlpack import to_dlpack, from_dlpack
from tvm.runtime import ndarray
...
m.set_input('input1', ndarray.from_dlpack(to_dlpack(input1)))
...
pytorch_output = from_dlpack(tvm_output.to_dlpack())
1 Like

Would you like to kindly post where the confusing part is, and possibly submit a PR to improve the documentation? That would be super helpful! Thanks!

I think the confusion part is mainly coming from different use cases. The tutorial uses the TE functions while @whn09 is working on a Relay graph.

I see…That makes sense