I want to load libtorch module once and then use it on several times when a class method is called.
I tried to do it this way:
class myClass {
void process();
bool module_loaded = false;
torch::jit::script::Module module;
};
void myClass::process() {
if (!module_loaded) {
module = torch::jit::load("model.pt");
module_loaded = true;
}
// .....
auto outputs = module.forward(inputs).toTensor();
// .....
}
This cause a crash on class destruction, and in any case copying of torch::jit::script::Module does not seem right, as it has TH_DISALLOW_COPY_AND_ASSIGN in the code.
Is there a better way to use torch::jit::script::Module so that it’s not loaded every time, for several consecutive calls?