(libtorch) Problem saving cpp model

ub216: <p>Hey,<br>
I’m trying to use libtorch to create and save models. Following the <a href="https://pytorch.org/tutorials/advanced/cpp_frontend.html#checkpointing-and-recovering-the-training-state" rel="nofollow noopener">DCGAN</a> tutorial and <a href="https://discuss.pytorch.org/t/libtorch-how-to-save-model-in-mnist-cpp-example/34234/5">this</a> I create a basic script as given here</p>
<pre><code class="lang-auto">class TestImpl: torch::nn::Module
{
public:
  TestImpl(int inPlanes, int outClasses)
    : m_score(torch::nn::Conv2dOptions(inPlanes, outClasses, 1))
  {
    m_score-&gt;to(at::kCUDA);
    register_module("conv",m_score);
  }
  double forward(at::Tensor&amp; input, at::Tensor&amp; output)
  {
    cudaDeviceSynchronize();
    auto start = std::chrono::system_clock::now();

    output = m_score-&gt;forward(input);

    cudaDeviceSynchronize();
    auto end = std::chrono::system_clock::now();
    std::chrono::duration&lt;double&gt; elapsed_seconds = end-start;
    return double(elapsed_seconds.count());
  }
private:
  torch::nn::Conv2d m_score{nullptr};
};
TORCH_MODULE(Test);

int main(int argc, char** argv )
{
  Test net(3,10);
  int loop = 100;
  int skip = 20;
  float timing;
  at::Tensor outputs;
  at::Tensor inputs = torch::ones({1, 3, 1024, 1024},torch::TensorOptions().device(torch::kCUDA));

  for (auto i = 0; i &lt; loop+skip; i++)
  {
      auto tmp = net-&gt;forward(inputs, outputs);//_pass(inputs, outputs);

      if(i &gt;= skip)
          timing += tmp;
  }
  std::cout&lt;&lt; "Average forward pass in: " &lt;&lt; timing/loop &lt;&lt;std::endl;
  torch::save(net, "chkValues.pt");
}
</code></pre>
<p>When I comment out the last line (<code>torch::save</code>) it compiles fine but un-commenting this throws out a lot of errors starting with:</p>
<pre><code class="lang-auto">In file included from libtorch_stable/include/torch/csrc/api/include/torch/nn/module.h:3:0,
                 from libtorch_stable/include/torch/csrc/api/include/torch/nn/cloneable.h:3,
                 from libtorch_stable/include/torch/csrc/api/include/torch/nn.h:3,
                 from libtorch_stable/include/torch/csrc/api/include/torch/all.h:7,
                 fromlibtorch_stable/include/torch/csrc/api/include/torch/torch.h:3,
                 from timing.cpp:36:
libtorch_stable/include/torch/csrc/api/include/torch/nn/pimpl.h: In instantiation of ‘torch::serialize::OutputArchive&amp; torch::nn::operator&lt;&lt;(torch::serialize::OutputArchive&amp;, const torch::nn::ModuleHolder&lt;ModuleType&gt;&amp;) [with ModuleType = TestImpl]’:
libtorch_stable/include/torch/csrc/api/include/torch/serialize.h:43:11:   required from ‘void torch::save(const Value&amp;, SaveToArgs&amp;&amp; ...) [with Value = Test; SaveToArgs = {const char (&amp;)[13]}]’
timing.cpp:127:34:   required from here
libtorch_stable/include/torch/csrc/api/include/torch/nn/pimpl.h:179:18: error: no match for ‘operator&lt;&lt;’ (operand types are ‘torch::serialize::OutputArchive’ and ‘const std::shared_ptr&lt;TestImpl&gt;’)
   return archive &lt;&lt; module.ptr();

</code></pre>
<p>Could someone please help me understand why this is happening?<br>
I’m using libtorch-stable 1.4 version with cuda 9.2.</p>

ub216: <p>Replying to my question… it looks like replacing <code>class TestImpl</code> with <code>struct TestImpl</code> things work fine. Not sure why this is the case.</p>