Unable to load model with liborch

I have trained a model in python and have saved it with the torch.jit and when I try to load the net into C++, I get an error:

python code:
traced_script_module.jit.save(“net_0.pt”)
traced_scrip_module = torch.jit.script(net)

C++ code:
torch::jit::script::Module open_net = torch::jit::load("…/net_0.pt");

terminate called after throwing an instance of ‘c10::Error’
what(): [enforce fail at inline_container.cc:144] . PytorchStreamReader failed reading zip archive: failed finding central directory
frame #0: c10::ThrowEnforceNotMet(char const*, int, char const*, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, void const*) + 0x67 (0x7f119c091fc7 in /home/ubuntu/cfr/libtorch/lib/libc10.so)

Your Python code looks a bit weird, since you are apparently saving the model before scripting it.
Also, it seems you are using a custom model.jit.save method, since nn.Module doesn’t implement a jit function by default. Could you clarify how the model was saved in Python?

Sorry the model was saved like this

Python code:
traced_scrip_module = torch.jit.script(net)
traced_script_module.jit.save(“net_0.pt”)

C++ error:
terminate called after throwing an instance of ‘c10::Error’
what(): [enforce fail at inline_container.cc:144] . PytorchStreamReader failed reading zip archive: failed finding central directory
frame #0: c10::ThrowEnforceNotMet(char const*, int, char const*, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, void const*) + 0x67 (0x7f3ceac4cfc7 in /home/ubuntu/cfr/libtorch/lib/libc10.so)
frame #1: caffe2::serialize::PyTorchStreamReader::valid(char const*, char const*) + 0xac (0x7f3cdccebf5c in /home/ubuntu/cfr/libtorch/lib/libtorch_cpu.so)
frame #2: caffe2::serialize::PyTorchStreamReader::init() + 0xb0 (0x7f3cdccee110 in /home/ubuntu/cfr/libtorch/lib/libtorch_cpu.so)
frame #3: caffe2::serialize::PyTorchStreamReader::PyTorchStreamReader(std::unique_ptr<caffe2::serialize::ReadAdapterInterface, std::default_deletecaffe2::serialize::ReadAdapterInterface >) + 0x6d (0x7f3cdccf04bd in /home/ubuntu/cfr/libtorch/lib/libtorch_cpu.so)
frame #4: + 0x36cb15f (0x7f3cde0f515f in /home/ubuntu/cfr/libtorch/lib/libtorch_cpu.so)
frame #5: torch::jit::load(std::unique_ptr<caffe2::serialize::ReadAdapterInterface, std::default_deletecaffe2::serialize::ReadAdapterInterface >, c10::optionalc10::Device, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits, std::allocator >, std::__cxx11::basic_string<char, std::char_traits, std::allocator >, std::hash<std::__cxx11::basic_string<char, std::char_traits, std::allocator > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits, std::allocator > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits, std::allocator > const, std::__cxx11::basic_string<char, std::char_traits, std::allocator > > > >&) + 0x67 (0x7f3cde0f1697 in /home/ubuntu/cfr/libtorch/lib/libtorch_cpu.so)
frame #6: torch::jit::load(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, c10::optionalc10::Device, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits, std::allocator >, std::__cxx11::basic_string<char, std::char_traits, std::allocator >, std::hash<std::__cxx11::basic_string<char, std::char_traits, std::allocator > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits, std::allocator > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits, std::allocator > const, std::__cxx11::basic_string<char, std::char_traits, std::allocator > > > >&) + 0x75 (0x7f3cde0f4355 in /home/ubuntu/cfr/libtorch/lib/libtorch_cpu.so)
frame #7: + 0xc4e0 (0x5609e3aa64e0 in ./cfr)
frame #8: + 0xff11 (0x5609e3aa9f11 in ./cfr)
frame #9: __libc_start_main + 0xe7 (0x7f3cd98fab97 in /lib/x86_64-linux-gnu/libc.so.6)
frame #10: + 0x88da (0x5609e3aa28da in ./cfr)

Aborted (core dumped)

As explained before, you would need to provide more information where traced_script_module.jit.save comes from as it’s not a built-in method of a scripted module so I assume it’s a custom function, which creates this error.

I think it was meant to be traced_script_module.save. Then it should work?

Yes, save is a method of a traced module and should work. Loading it in libtorch should also work.
Since @Ian_Kennedy did not follow up in this thread and the code wasn’t executable it’s still unclear what was causing the error.