Model cannot be loaded with torch::jit::load

username: kostas
cooked: <p>Hi dear Team<br>
I am trying to load a saved, serialized model in C++ but the model cannot be loaded with error:</p>
<p>open file failed because of errno 2 on fopen: No such file or directory, file path:<br>
Exception raised from RAIIFile at …\caffe2\serialize\file_adapter.cc:27 (most recent call first):<br>
00007FFA4A1EA29200007FFA4A1EA230 c10.dll!c10::Error::Error [ @ ]<br>
00007FFA4A1E9D1E00007FFA4A1E9CD0 c10.dll!c10::detail::torchCheckFail [ @ ]<br>
00007FF9DAE717B300007FF9DAE716C0 torch_cpu.dll!caffe2::serialize::FileAdapter::FileAdapter [ @ ]<br>
00007FF9DC09E4EF00007FF9DC09E450 torch_cpu.dll!torch::jit::load [ @ ]<br>
00007FF7811F6B9E00007FF7811F6A40 dcgan.exe!main [c:\users\mskfi\downloads\examples-master\cpp\dcgan\dcgan.cpp @ 24]<br>
00007FF7812251D400007FF7812251A0 dcgan.exe!invoke_main [d:\agent_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 79]<br>
00007FF78122507E00007FF781224F50 dcgan.exe!__scrt_common_main_seh [d:\agent_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 288]<br>
00007FF781224F3E00007FF781224F30 dcgan.exe!__scrt_common_main [d:\agent_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 331]<br>
00007FF78122526900007FF781225260 dcgan.exe!mainCRTStartup [d:\agent_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp @ 17]<br>
00007FFA6938703400007FFA69387020 KERNEL32.DLL!BaseThreadInitThunk [ @ ]<br>
00007FFA6ACE265100007FFA6ACE2630 ntdll.dll!RtlUserThreadStart [ @ ]<br>
T       _                               pG¢B┬☺    ▓h·⌂  ÿ♣      ƒ♣       M¢B┬☺  [­▓h·⌂</p>
<p>Following installation / implementation:<br>
Using the example in <a href="https://pytorch.org/tutorials/advanced/cpp_export.html" class="inline-onebox" rel="noopener nofollow ugc">Loading a TorchScript Model in C++ — PyTorch Tutorials 1.10.0+cu102 documentation</a></p>
<p>Installed pytorch with<br>
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch</p>
<p>Saved the model, as described in the example, with traced_script_module = torch.jit.trace(model, example)</p>
<p>Installed locally CUDA 11.3.1 and respective Cudnn</p>
<p>Used the C++ example in <a href="https://github.com/pytorch/examples/tree/master/cpp/dcgan" class="inline-onebox" rel="noopener nofollow ugc">examples/cpp/dcgan at master · pytorch/examples · GitHub</a></p>
<p>Build based on instructions with cmake -DCMAKE_PREFIX_PATH=“libtorch-win-shared-with-deps-1.10.0+cu113\libtorch” … -G “Visual Studio 15 2017” -A x64<br>
Project compiles and starts</p>
<p>Changed the code to load the model in:</p>
<p><span class="hashtag">#include</span> &lt;torch/script.h&gt; // One-stop header.</p>
<p><span class="hashtag">#include</span> <br>
<span class="hashtag">#include</span> <br>
<span class="hashtag">#include</span> <br>
namespace fs = std::experimental::filesystem;</p>
<p>int main(int argc, const char* argv[]) {<br>
if (argc != 2) {<br>
std::cerr &lt;&lt; “usage: example-app \n”;<br>
return -1;<br>
}</p>
<p>torch::jit::script::Module module;<br>
bool ex = fs::exists(argv[1]);<br>
if (ex)<br>
{<br>
std::cerr &lt;&lt; “model exists\n”;<br>
} else {<br>
std::cerr &lt;&lt; “model missing\n”;<br>
}<br>
try {<br>
// Deserialize the ScriptModule from a file using torch::jit::load().<br>
module = torch::jit::load(argv[1]);<br>
} catch (const c10::Error&amp; e) {<br>
std::cerr &lt;&lt; “error loading the model\n”;<br>
std::cerr &lt;&lt; e.what();<br>
std::cerr &lt;&lt; e.msg();<br>
return -1;<br>
}</p>
<p>std::cout &lt;&lt; “ok\n”;<br>
}</p>
<p>I run the project by calling:<br>
dcgan.exe “&lt;path_to&gt;/traced_resnet_model.pt”</p>
<p>filesystem::exists is true</p>
<p>The error I get changed from time to time between:<br>
open file failed because of errno 2 on fopen: No such file or directory, file path:<br>
open file failed because of errno 22 on fopen: Invalid argument, file path:</p>
<p>file path: is always empty</p>
<p>Any hints?</p>
<p>Thank you very much</p>
<p>Best</p>

username: kostas
cooked: <p>Solved: unfortunately I was just mixing build configurations, so when using the torchlib release package, the projects need to be built in release!</p>

username: Odegard
cooked: <p>Thanks for sharing the hints. Could you please briefly explain the way you’ve modified it? I am currently having the same errors.</p>

username: rbaleksandar
cooked: <p>I’m not sure that’s the case. I have libtorch 2.0.1 GPU (CUDA 11.8) both release and debug binaries. In my CMake I make absolutely sure that I pick the correct one (can see it in the logs both locally as well as in my CI pipeline). Not to mention that the produced EXE of my application is different in size (release is approx. 4 times smaller).</p>
<p>Interestingly enough I have a remote system (where my GitLab runner is building the stuff) as well as a local one. The debug build of my application (simple one, loads a PNG with OpenCV, does some transformations and then loads a TorchScript traced SRCNN model) works on both systems but the release shows the error 22. Surprisingly enough this happens on the very system (remote one), where it was built. <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=12" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p>

username: 1847198823
cooked: <p>thank you for sharing!</p>