Mhm i tried it as well as with llvm as target. The output then is just an grey image…
I have build the module in python with:
json, lib, params = relay.build(mod, target=target, params=params)
lib.export_library("./my_lib.so")
with open(os.path.join("./", 'graph.json'), 'w') as f_graph_json:
f_graph_json.write(json)
with open(os.path.join("./", "deploy.params"), "wb") as fo:
fo.write(tvm.relay.save_param_dict(params))
Then i just run some inference in python:
runtime = tvm.contrib.graph_runtime.create(json, lib, tvm.context(target, 0))
runtime.set_input(**params)
runtime.set_input("input", tvm_array)
image_tensor_relay_module_lib, _ = runtime.get_output(0).asnumpy(), runtime
In C++ i just load the so lib and retrive the runtime:
tvm::runtime::Module mod_dylib = tvm::runtime::Module::LoadFromFile(...)
std::ifstream json_in("./graph.json", std::ios::in);
std::string json_data((std::istreambuf_iterator<char>(json_in)), std::istreambuf_iterator<char>());
json_in.close();
std::ifstream params_in("./deploy.params", std::ios::binary);
std::string params_data((std::istreambuf_iterator<char>(params_in)), std::istreambuf_iterator<char>());
arams_in.close();
TVMByteArray params_arr;
params_arr.data = params_data.c_str();
params_arr.size = params_data.length();
auto graph_runtime = tvm::runtime::Registry::Get("tvm.graph_runtime.create");
tvm::runtime::Module mod = (*graph_runtime)(json_data, mod_dylib, kDLOpenCl, cpu_dev_id);
tvm::runtime::PackedFunc set_input = mod.GetFunction("set_input");
int64_t in_shape[4] = {1, channel, width, height}; // NCWH
int dtype_code = kDLFloat;
int dtype_bits = 32;
int dtype_bytes = dtype_bits / 8;
int dtype_lanes = 1;
TVMArrayAlloc(in_shape, in_ndim, dtype_code, dtype_bits, dtype_lanes, kDLOpenCL, cpu_dev_id, &x);
TVMArrayCopyFromBytes(x, normalized_image, sizeof(float) * width * height * channel);
tvm::runtime::PackedFunc run = mod.GetFunction("run");
tvm::runtime::PackedFunc get_output = mod.GetFunction("get_output");
TVMSynchronize(kDLOpenCL, 0, NULL);
get_output(0, y);
TVMArrayCopyToBytes(y, output_image, sizeof(float) * width * height * channel)