Writing/reading using Output/InputArchive

username: mmoelle1
cooked: <p>I am trying to implement serialisation functionality into my libtorch code. As my top-level class comprises quite many different member attributes I have implemented two user-callable functions</p>
<pre><code class="lang-auto">    inline void save(const std::string&amp; filename,
                     const std::string&amp; key="") const
    {
      torch::serialize::OutputArchive archive;
      write(archive, key).save_to(filename);
    }

    inline void load(const std::string&amp; filename,
                     const std::string&amp; key="")
    {
      torch::serialize::InputArchive archive;
      archive.load_from(filename);
      read(archive, key);
    }
</code></pre>
<p>Furthermore, I have implemented <code>write</code> and <code>read</code> functions of the form</p>
<pre><code class="lang-auto">    inline torch::serialize::OutputArchive&amp; write(torch::serialize::OutputArchive&amp; archive,
                                                  const std::string&amp; key="") const
    {
      archive.write(key+"dim", torch::full({1}, dim_));

      geo_.write(archive, key+"geo");
      rhs_.write(archive, key+"rhs");
      sol_.write(archive, key+"sol");

      torch::serialize::OutputArchive archive_net;

      net_-&gt;save(archive_net);
      archive.write(key+"net", archive_net);

      torch::serialize::OutputArchive archive_opt;

      opt_.save(archive_opt);
      archive.write(key+"opt", archive_opt);

      return archive;
    }

inline torch::serialize::InputArchive&amp; read(torch::serialize::InputArchive&amp; archive,
                                                const std::string&amp; key="")
    {
      torch::Tensor tensor;

      archive.read(key+"dim", tensor);
      if (tensor.item&lt;int64_t&gt;() != dim_)
        throw std::runtime_error("dim mismatch");

      geo_.read(archive, key+"geo");
      rhs_.read(archive, key+"rhs");
      sol_.read(archive, key+"sol");

      torch::serialize::InputArchive archive_net;
      archive.read(key+"net", archive_net);

      net_-&gt;load(archive_net);
      
      torch::serialize::InputArchive archive_opt;
      archive.read(key+"opt", archive_opt);      

      opt_.load(archive_opt);
      
      return archive;
    }
</code></pre>
<p>Here, <code>net_</code> is of type <code>MyGenerator</code> using the generator approach described <a href="https://pytorch.org/tutorials/advanced/cpp_frontend.html#module-ownership" rel="noopener nofollow ugc">here</a>.</p>
<p>I have the following problems:</p>
<ol>
<li>The code runs up to the point that <code>opt_</code> is loaded, which gives rise to the exception<pre><code class="lang-auto">libc++abi: terminating with uncaught exception of type c10::Error: loaded state dict contains a parameter group that has a different size than the optimizer's parameter group
Exception raised from serialize at /tmp/libtorch-20220312-61658-dj98dd/torch/csrc/api/include/torch/optim/serialize.h:170 (most recent call first):
frame #0: c10::detail::torchCheckFail(char const*, char const*, unsigned int, char const*) + 92 (0x10040d4c0 in libc10.dylib)
frame #1: void torch::optim::serialize&lt;torch::optim::AdamParamState, torch::optim::AdamOptions&gt;(torch::serialize::InputArchive&amp;, torch::optim::Optimizer&amp;) + 832 (0x108880be8 in libtorch_cpu.dylib)
frame #2: torch::optim::Adam::load(torch::serialize::InputArchive&amp;) + 108 (0x10887f21c in libtorch_cpu.dylib)
</code></pre>
</li>
<li>The <code>net_</code> object is still empty after <code>net_-&gt;load(archive_net)</code>.</li>
</ol>
<p>Any help on any of the above issues is highly appreciated.</p>
<p>Kind regards,<br>
Matthias</p>