Here is what I do:
// on thread 1 (configuration thread)
Filter::init()
{
mod_factory = tvm::runtime::Module::LoadFromFile (model_path);
gmod = mod_factory.GetFunction ("default") (ctx);
tvm_get_input = gmod.GetFunction ("get_input");
tvm_set_input = gmod.GetFunction ("set_input");
tvm_get_output = gmod.GetFunction ("get_output");
tvm_run = gmod.GetFunction ("run");
}
// on thread 2 (runtime thread)
Filter::invoke(input, output) {
// tvm::runtime::Module gmod = mod_factory.GetFunction ("default") (ctx);
// tvm::runtime::PackedFunc tvm_set_input = gmod.GetFunction ("set_input");
// tvm::runtime::PackedFunc tvm_get_output = gmod.GetFunction ("get_output");
// tvm::runtime::PackedFunc tvm_run = gmod.GetFunction ("run");
tvm_set_input (0, input->data);
tvm_run();
tvm_get_output(i, output->data);
}
if I uncomment the code in invoke(), then it works. Otherwise, the cached PackedFunc are invalid, output->data
contains whatever was there before calling invoke()
. And when the object is destroyed, these cached PackedFunc are dangling pointers and crash.
I tried various ways to protect the cache of PackedFunc but with no success than just using commented code in invoke()
.
Thanks for your help.