Runtimeerror : running errors on libtorch about torch::jit::load

Chen0729: <p>Hi, All</p>
<p>I have received a running time in C++ on libtorch about torch::jit as:<br>
terminate called after throwing an instance of ‘c10::Error’<br>
what():  <code>torch::jit::load()</code> received a file from <code>torch.save()</code>, but <code>torch::jit::load()</code> can only load files produced by <code>torch.jit.save()</code><br>
Exception raised from load at /pytorch/torch/csrc/jit/serialization/import.cpp:347 (most recent call first):</p>
<p>but in the C++ code, I have only used torch::load and torch::save. I didnot use torch::jit::load()<br>
Any suggestions on how to fix the issue please ?</p>
<p>Thanks,</p>

ptrblck: <p>The error message seems misleading in this case. Could you post a minimal code snippet to reproduce this issue?</p>

Chen0729: <p>Hi, ptrblck</p>
<p>Thanks in advance</p>
<p>This is the minimal code snippet that triggers the errors:<br>
int main(){<br>
CustArgsSSL args1;</p>
<pre><code>auto newgan = ppGAN(MnistLabel(10), MnistUnlabel(), MnistTest(), args1);//
</code></pre>
<p>}</p>
<p>struct ppGANImpl: torch::nn::Module{</p>
<pre><code>ppGANImpl(torch::data::datasets::MNIST dataset1, torch::data::datasets::MNIST dataset2, torch::data::datasets::MNIST dataset3, CustArgsSSL &amp;args)
        : discriminator(register_module("discriminator", Discriminator1())),doptim(torch::optim::Adam(discriminator-&gt;parameters(),torch::optim::AdamOptions(args.lr_default).betas({args.momentum,0.999})) )
        ,
        generator(register_module("generator", Generator1(100, 28*28))),goptim(torch::optim::Adam(generator-&gt;parameters(),torch::optim::AdamOptions(args.lr_default).betas({args.momentum,0.999}))
</code></pre>
<p>// generator(generator1)<br>
)<br>
{<br>
if (!args.savedir.empty()){</p>
<pre><code>        torch::load(generator,"/path/G.pkl");

        torch::load(discriminator,"/path/D.pkl");
    }
    else {


        torch::save(generator,"/path/G.pkl");
        torch::save(discriminator,"/path/D.pkl");
    }</code></pre>

Chen0729: <p>And this is the debug information</p>
<p>terminate called after throwing an instance of ‘c10::Error’<br>
what():  <code>torch::jit::load()</code> received a file from <code>torch.save()</code>, but <code>torch::jit::load()</code> can only load files produced by <code>torch.jit.save()</code><br>
Exception raised from load at /pytorch/torch/csrc/jit/serialization/import.cpp:347 (most recent call first):</p>
<p>template &lt;typename Value, typename… LoadFromArgs&gt;<br>
void load(Value&amp; value, LoadFromArgs&amp;&amp;… args) {<br>
serialize::InputArchive archive;<br>
archive.load_from(std::forward(args)…);<br>
archive &gt;&gt; value;<br>
}</p>

ptrblck: <p>Thanks for the update. I don’t know, how your models are defined, but this minimal code snippet works fine:</p>
<pre><code class="lang-auto">#include &lt;torch/torch.h&gt;
#include &lt;iostream&gt;

struct NetImpl : torch::nn::Module {
  NetImpl(int64_t N, int64_t M)
      : linear(register_module("linear", torch::nn::Linear(N, M))) {
    another_bias = register_parameter("b", torch::randn(M));
  }
  torch::Tensor forward(torch::Tensor input) {
    return linear(input) + another_bias;
  }
  torch::nn::Linear linear;
  torch::Tensor another_bias;
};
TORCH_MODULE(Net);


int main() {
  auto net = Net(4, 5);
  for (const auto&amp; p : net-&gt;parameters()) {
    std::cout &lt;&lt; p &lt;&lt; std::endl;
  }

  auto net2 = Net(4, 5);

  torch::save(net, "tmp.pt");
  torch::load(net2, "tmp.pt");
  for (const auto&amp; p : net2-&gt;parameters()) {
    std::cout &lt;&lt; p &lt;&lt; std::endl;
  }
}
</code></pre>
<p>and I can properly save and load the model.</p>

Chen0729: <p>Thank you very much ptrblck for the working example.</p>
<p>I double checked it and find that it is because I try to load the python “.pkl” file. It seems to me that in C++, only “.pt” file can be loaded and saved.</p>
<p>Thanks again</p>

ptrblck: <p>Did you get the error just by changing the file extension? I.e. is my code snippet also failing, if you use <code>.pkl</code>?<br>
<code>pickle</code> files are serialized Python objects, but I’m unsure if PyTorch checks the file extension or not.</p>

Chen0729: <p>Actually your code is good even if change the file extension.</p>
<p>So it is probably due to other reasons …</p>