Hi, I have been struggling quite a bit the BYOC example working.
You can follow my progression:
I am now using the codegen_c example from contrib, haven’t touched it really, just trying to get it to execute at runtime.
I have added operator registrations into python/tvm/relay/op/contrib/ccompiler.py
Here is the code I am executing:
import numpy as np
import tvm
from tvm import relay as R
from tvm.contrib import graph_executor
# build IR
dim = (2, 2)
x = R.var('x', shape=dim)
y = R.var('y', shape=dim)
output = R.multiply(x, y)
params = {}
# Set the TVM build target
target = "llvm"
func = R.Function(R.analysis.free_vars(output), output)
func = R.build_module.bind_params_by_name(func, params)
mod = tvm.IRModule()
mod["main"] = func
from tvm.relay.op.contrib.ccompiler import partition_for_ccompiler
pmod = partition_for_ccompiler(mod)
print(f'PMOD\n{pmod}')
lib = R.build(pmod, target, params=params)
# Generate graph executor
dev = tvm.device(target, 0)
m = graph_executor.GraphModule(lib["default"](dev))
dtype = 'float32'
x = np.array([
[1, 1],
[1, 1]
], dtype=dtype)
y = np.array([
[2, 2],
[2, 2]
], dtype=dtype)
m.set_input('x', tvm.nd.array(x))
m.set_input('y', tvm.nd.array(y))
m.run()
output = m.get_output(0)
print(f'Output:\n{output}')
Honestly, I am not quite sure of what I am doing here… I did my best to put the pieces together.
Here is the code for partition_for_ccompiler
:
def partition_for_ccompiler(mod, params=None):
if params:
mod["main"] = bind_params_by_name(mod["main"], params)
seq = tvm.transform.Sequential(
[
transform.CanonicalizeOps(),
transform.InferType(),
transform.SimplifyInference(),
transform.FoldConstant(),
transform.FoldScaleAxis(),
# fold consecutive add ops to simplify pattern `conv2d-bias_add-bn-relu`
transform.SimplifyExpr(),
transform.FoldConstant(),
# transform.MergeComposite(pattern_table()),
transform.AnnotateTarget("ccompiler"),
transform.MergeCompilerRegions(),
transform.PartitionGraph(),
]
)
with tvm.transform.PassContext(opt_level=3):
mod = seq(mod)
return mod
And this is the error:
Traceback (most recent call last):
File "tests/slai/relay_multiply.py", line 33, in <module>
m = graph_executor.GraphModule(lib["default"](dev))
File "/Users/.../tvm/python/tvm/_ffi/_ctypes/packed_func.py", line 237, in __call__
raise get_last_ffi_error()
tvm._ffi.base.TVMError: Traceback (most recent call last):
[bt] (8) 9 libffi.8.dylib 0x0000000103ac974c ffi_call_int + 1208
[bt] (7) 8 libffi.8.dylib 0x0000000103acc04c ffi_call_SYSV + 76
[bt] (6) 7 libtvm.dylib 0x000000011d9d503c TVMFuncCall + 60
[bt] (5) 6 libtvm.dylib 0x000000011da91198 tvm::runtime::PackedFuncObj::Extractor<tvm::runtime::PackedFuncSubObj<tvm::runtime::GraphExecutorFactory::GetFunction(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, tvm::runtime::ObjectPtr<tvm::runtime::Object> const&)::$_0> >::Call(tvm::runtime::PackedFuncObj const*, tvm::runtime::TVMArgs, tvm::runtime::TVMRetValue*) + 328
[bt] (4) 5 libtvm.dylib 0x000000011da8bd5c tvm::runtime::GraphExecutorFactory::ExecutorCreate(std::__1::vector<DLDevice, std::__1::allocator<DLDevice> > const&) + 264
[bt] (3) 4 libtvm.dylib 0x000000011da77f28 tvm::runtime::GraphExecutor::Init(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, tvm::runtime::Module, std::__1::vector<DLDevice, std::__1::allocator<DLDevice> > const&, tvm::runtime::PackedFunc) + 456
[bt] (2) 3 libtvm.dylib 0x000000011da7a418 tvm::runtime::GraphExecutor::SetupOpExecs() + 1780
[bt] (1) 2 libtvm.dylib 0x000000011da7f6f8 tvm::runtime::GraphExecutor::CreateTVMOp(tvm::runtime::TVMOpParam const&, std::__1::vector<DLTensor, std::__1::allocator<DLTensor> > const&) + 1228
[bt] (0) 1 libtvm.dylib 0x000000011c116d38 tvm::runtime::detail::LogFatal::Entry::Finalize() + 84
File "/Users/.../tvm/src/runtime/graph_executor/graph_executor.cc", line 529
TVMError:
---------------------------------------------------------------
An error occurred during the execution of TVM.
For more information, please see: https://tvm.apache.org/docs/errors.html
---------------------------------------------------------------
Check failed: (pf != nullptr) is false: no such function in module: tvmgen_default_ccompiler_main_0
If anyone can help me getting BYOC to work I will be massively thankful!