Unable to load model with liborch

username: Ian_Kennedy
cooked: <p>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:</p>
<p>python code:<br>
traced_script_module.jit.save(“net_0.pt”)<br>
traced_scrip_module = torch.jit.script(net)</p>
<p>C++ code:<br>
torch::jit::script::Module open_net = torch::jit::load("…/net_0.pt");</p>
<p>terminate called after throwing an instance of ‘c10::Error’<br>
what():  [enforce fail at inline_container.cc:144] . PytorchStreamReader failed reading zip archive: failed finding central directory<br>
frame <span class="hashtag">#0:</span> c10::ThrowEnforceNotMet(char const*, int, char const*, std::__cxx11::basic_string&lt;char, std::char_traits, std::allocator &gt; const&amp;, void const*) + 0x67 (0x7f119c091fc7 in /home/ubuntu/cfr/libtorch/lib/libc10.so)</p>

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

username: Ian_Kennedy
cooked: <p>Sorry the model was saved like this</p>
<p>Python code:<br>
traced_scrip_module = torch.jit.script(net)<br>
traced_script_module.jit.save(“net_0.pt”)</p>
<p>C++ error:<br>
terminate called after throwing an instance of ‘c10::Error’<br>
what(): [enforce fail at inline_container.cc:144] . PytorchStreamReader failed reading zip archive: failed finding central directory<br>
frame <span class="hashtag">#0:</span> c10::ThrowEnforceNotMet(char const*, int, char const*, std::__cxx11::basic_string&lt;char, std::char_traits, std::allocator &gt; const&amp;, void const*) + 0x67 (0x7f3ceac4cfc7 in /home/ubuntu/cfr/libtorch/lib/libc10.so)<br>
frame <span class="hashtag">#1:</span> caffe2::serialize::PyTorchStreamReader::valid(char const*, char const*) + 0xac (0x7f3cdccebf5c in /home/ubuntu/cfr/libtorch/lib/libtorch_cpu.so)<br>
frame <span class="hashtag">#2:</span> caffe2::serialize::PyTorchStreamReader::init() + 0xb0 (0x7f3cdccee110 in /home/ubuntu/cfr/libtorch/lib/libtorch_cpu.so)<br>
frame <span class="hashtag">#3:</span> caffe2::serialize::PyTorchStreamReader::PyTorchStreamReader(std::unique_ptr&lt;caffe2::serialize::ReadAdapterInterface, std::default_deletecaffe2::serialize::ReadAdapterInterface &gt;) + 0x6d (0x7f3cdccf04bd in /home/ubuntu/cfr/libtorch/lib/libtorch_cpu.so)<br>
frame <span class="hashtag">#4:</span> + 0x36cb15f (0x7f3cde0f515f in /home/ubuntu/cfr/libtorch/lib/libtorch_cpu.so)<br>
frame <span class="hashtag">#5:</span> torch::jit::load(std::unique_ptr&lt;caffe2::serialize::ReadAdapterInterface, std::default_deletecaffe2::serialize::ReadAdapterInterface &gt;, c10::optionalc10::Device, std::unordered_map&lt;std::__cxx11::basic_string&lt;char, std::char_traits, std::allocator &gt;, std::__cxx11::basic_string&lt;char, std::char_traits, std::allocator &gt;, std::hash&lt;std::__cxx11::basic_string&lt;char, std::char_traits, std::allocator &gt; &gt;, std::equal_to&lt;std::__cxx11::basic_string&lt;char, std::char_traits, std::allocator &gt; &gt;, std::allocator&lt;std::pair&lt;std::__cxx11::basic_string&lt;char, std::char_traits, std::allocator &gt; const, std::__cxx11::basic_string&lt;char, std::char_traits, std::allocator &gt; &gt; &gt; &gt;&amp;) + 0x67 (0x7f3cde0f1697 in /home/ubuntu/cfr/libtorch/lib/libtorch_cpu.so)<br>
frame <span class="hashtag">#6:</span> torch::jit::load(std::__cxx11::basic_string&lt;char, std::char_traits, std::allocator &gt; const&amp;, c10::optionalc10::Device, std::unordered_map&lt;std::__cxx11::basic_string&lt;char, std::char_traits, std::allocator &gt;, std::__cxx11::basic_string&lt;char, std::char_traits, std::allocator &gt;, std::hash&lt;std::__cxx11::basic_string&lt;char, std::char_traits, std::allocator &gt; &gt;, std::equal_to&lt;std::__cxx11::basic_string&lt;char, std::char_traits, std::allocator &gt; &gt;, std::allocator&lt;std::pair&lt;std::__cxx11::basic_string&lt;char, std::char_traits, std::allocator &gt; const, std::__cxx11::basic_string&lt;char, std::char_traits, std::allocator &gt; &gt; &gt; &gt;&amp;) + 0x75 (0x7f3cde0f4355 in /home/ubuntu/cfr/libtorch/lib/libtorch_cpu.so)<br>
frame <span class="hashtag">#7:</span> + 0xc4e0 (0x5609e3aa64e0 in ./cfr)<br>
frame <span class="hashtag">#8:</span> + 0xff11 (0x5609e3aa9f11 in ./cfr)<br>
frame <span class="hashtag">#9:</span> __libc_start_main + 0xe7 (0x7f3cd98fab97 in /lib/x86_64-linux-gnu/libc.so.6)<br>
frame <span class="hashtag">#10:</span> + 0x88da (0x5609e3aa28da in ./cfr)</p>
<p>Aborted (core dumped)</p>

username: ptrblck
cooked: <p>As explained before, you would need to provide more information where <code>traced_script_module.jit.save</code> 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.</p>

username: Laleh
cooked: <p>I think it was meant to be <code>traced_script_module.save</code>. Then it should work?</p>

username: ptrblck
cooked: <p>Yes, <code>save</code> is a method of a traced module and should work. Loading it in <code>libtorch</code> should also work.<br>
Since <a class="mention" href="/u/ian_kennedy">@Ian_Kennedy</a> did not follow up in this thread and the code wasn’t executable it’s still unclear what was causing the error.</p>