I encountered difficulties when I tried to call an external function


My code is as follows (which is basically the same as TVM’s tutorial):

import tvm
import numpy as np
from tvm.contrib import cblas

n = 1024
l = 128
m = 235
bias = tvm.var("bias", dtype=tvm.float32)
A = tvm.placeholder((n, l), dtype="float32", name="A")
B = tvm.placeholder((l, m), dtype="float32", name="B")

C = tvm.extern(
    (n, m),
    [A, B],
    lambda ins, outs: tvm.call_packed(
        "tvm.contrib.cblas.matmul", ins[0], ins[1], outs[0], False, False
D = tvm.compute(C.shape, lambda i, j: C[i, j] + bias, name="D")
S = tvm.create_schedule(D.op)

ctx = tvm.cpu(0)
FMul = tvm.build(
    S, [A, B, D, bias], "llvm"
)  # target="llvm", target_host="llvm", name="FMul")

a = tvm.nd.array(np.random.uniform(size=(n, l)).astype(A.dtype), ctx)
b = tvm.nd.array(np.random.uniform(size=(l, m)).astype(B.dtype), ctx)
d = tvm.nd.array(np.zeros(shape=(n, m), dtype=D.dtype), ctx)
bb = 10.0

FMul(a, b, d, bb)

But no matter how I modify this code (I tried to modify C: C = cblas.matmul(A,B)), I always get the error shown below:

Traceback (most recent call last):

  File "tvm2.2.py", line 53, in <module>
    FMul(a, b, d, bb)

  File "/home/leuckart/ENVIRONMENT/tvm/python/tvm/_ffi/function.py", line 145, in __call__
    return f(*args)

  File "/home/leuckart/ENVIRONMENT/tvm/python/tvm/_ffi/_ctypes/function.py", line 210, in __call__
    raise get_last_ffi_error()

tvm._ffi.base.TVMError: Traceback (most recent call last):
  [bt] (6) /home/leuckart/ENVIRONMENT/tvm/build/libtvm.so(TVMFuncCall+0x65) [0x7f4400542a25]
  [bt] (5) /home/leuckart/ENVIRONMENT/tvm/build/libtvm.so(+0x1013b90) [0x7f4400558b90]
  [bt] (4) [0x7f441108d23e]
  [bt] (3) [0x7f441108dbb0]
  [bt] (2) /home/leuckart/ENVIRONMENT/tvm/build/libtvm.so(TVMBackendGetFuncFromEnv+0x61) [0x7f4400542931]
  [bt] (1) /home/leuckart/ENVIRONMENT/tvm/build/libtvm.so(tvm::runtime::ModuleNode::GetFuncFromEnv(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)+0x2d1) [0x7f4400556721]
  [bt] (0) /home/leuckart/ENVIRONMENT/tvm/build/libtvm.so(dmlc::LogMessageFatal::~LogMessageFatal()+0x43) [0x7f43ffd9fda3]
  File "/home/leuckart/ENVIRONMENT/tvm/src/runtime/module.cc", line 111
  File "/home/leuckart/ENVIRONMENT/tvm/src/runtime/module_util.cc", line 73
TVMError: Check failed: ret == 0 (-1 vs. 0) : Check failed: f != nullptr: Cannot find function tvm.contrib.cblas.matmul in the imported modules or global registry

terminate called after throwing an instance of 'dmlc::Error'
  what():  [14:04:32] /home/leuckart/ENVIRONMENT/tvm/src/runtime/workspace_pool.cc:116: Check failed: allocated_.size() == 1 (2 vs. 1) : 
Stack trace:
  [bt] (0) /home/leuckart/ENVIRONMENT/tvm/build/libtvm.so(tvm::runtime::WorkspacePool::Pool::Release(DLContext, tvm::runtime::DeviceAPI*)+0x7d7) [0x7f4400585c77]
  [bt] (1) /home/leuckart/ENVIRONMENT/tvm/build/libtvm.so(tvm::runtime::WorkspacePool::~WorkspacePool()+0x37) [0x7f4400584087]
  [bt] (2) /home/leuckart/anaconda3/lib/libstdc++.so.6(+0x9b64a) [0x7f43ddd4e64a]
  [bt] (3) /lib/x86_64-linux-gnu/libc.so.6(+0x43041) [0x7f4410a96041]
  [bt] (4) /lib/x86_64-linux-gnu/libc.so.6(+0x4313a) [0x7f4410a9613a]
  [bt] (5) /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xee) [0x7f4410a74b9e]
  [bt] (6) python(+0x1dfed2) [0x5555ef3f6ed2]

The error message indicates that “Cannot find function tvm.contrib.cblas.matmul in the imported modules or global registry”, but I am able to get “cblas.matmul”'s information through “help(cblas.matmul)”, and I have already set the USE_CUBLAS to ON when compiling TVM.

I am a beginner of TVM, this problem has been bothering me for a long time. Maybe this question is too simple, or the way I ask questions is not standardized, but I still hope to get answers from all of you, thank you very much!


a way to tested whether the packed func is available is
help(cblas.matmul) is test the python side, which is different than what we need


Just wanna make sure. It seems you want to use CBLAS (MKL) on CPU.
In this case, you set “USE_BLAS” to “mkl” and specify “USE_MKL_PATH” to your installation path when building TVM. Is that what you did? Cuz you mentioned you set “USE_CUBLAS” to “ON” but that’s for CUDA.


Thank you very much! I did confuse the role of help, here should use the function you mentioned.


Thank you very much for your help! I did not open the cublas, and this problem has been solved. So stupid am I…