How can I save and load a vector of a vectors of tensors?

Alio: <p>How can I save and load a vector of a vectors of tensors?</p>
<p>This is my attempt to save to disk and retrieve. It just returns a random looking number when it should return the vector of vectors of tensors.</p>
<pre><code class="lang-auto">#include &lt;fstream&gt;
#include &lt;iterator&gt;
#include &lt;string&gt;

#include &lt;torch/torch.h&gt;
using namespace torch::indexing;
torch::Device device(torch::kCUDA);

std::vector&lt;std::vector&lt;torch::Tensor&gt;&gt; vec_of_vec_of_tensor;
std::vector&lt;torch::Tensor&gt; vec_of_tensor;

class ValueGet {
public:
   int data;
   ValueGet() {
   data = 0;
   }
};

int main() {

    auto foo = torch::rand({3, 3});
    auto foo_two = torch::rand({3, 3});

    vec_of_tensor.push_back(foo);
    vec_of_tensor.push_back(foo_two);
    vec_of_vec_of_tensor.push_back(vec_of_tensor);

    std::cout &lt;&lt; vec_of_vec_of_tensor;


    std::ofstream outfile("outfile.dat", std::ofstream::binary);
    outfile.write(reinterpret_cast&lt;const char*&gt;(vec_of_vec_of_tensor.data() /* or &amp;v[0] pre-C++11 */), sizeof(int) * vec_of_vec_of_tensor.size());
    outfile.close();

    ValueGet vg;
    std::ifstream file;
    file.open("outfile.dat", std::fstream::binary | std::fstream::out); // Opens a file in binary mode for input operations i.e., getting data from file.
    if (!file)
        std::cout &lt;&lt; "File Not Found.";
    else {
        file.seekg(0); // To make sure that the data is read from the starting position of the file.
        while (file.read((char *)&amp;vg, sizeof(vg))) // Iterates through the file till the pointer reads the last line of the file.
            std::cout&lt;&lt;vg.data;
    }
}
</code></pre>

Igor_Aherne: <aside class="onebox githubissue" data-onebox-src="https://github.com/pytorch/pytorch/issues/19025#issuecomment-489918420">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/19025#issuecomment-489918420" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/19025#issuecomment-489918420" target="_blank" rel="noopener nofollow ugc">C++ torch::Tensor serialization</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2019-04-08" data-time="17:14:03" data-timezone="UTC">05:14PM - 08 Apr 19 UTC</span>
      </div>

        <div class="date">
          closed <span class="discourse-local-date" data-format="ll" data-date="2019-05-07" data-time="05:52:23" data-timezone="UTC">05:52AM - 07 May 19 UTC</span>
        </div>

      <div class="user">
        <a href="https://github.com/semin-park" target="_blank" rel="noopener nofollow ugc">
          <img alt="semin-park" src="https://avatars.githubusercontent.com/u/30904140?v=4" class="onebox-avatar-inline" width="20" height="20">
          semin-park
        </a>
      </div>
    </div>

    <div class="labels">
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          high priority
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: docs
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: cpp
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          feature
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          module: serialization
        </span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">
          triaged
        </span>
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">## 🚀 Feature


I am building an application where there's basically a produce<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">r and a consumer of `std::tuple&lt;torch::Tensor, torch::Tensor&gt;`s.

The producer has to run a neural network to produce the tensors, and the consumer has to train from the output of the producer, occasionally checkpoint to file, which the producer then occasionally reads and updates its own network with the new one. (I'm implementing Alpha Zero)

The only problem is, it seems like pytorch is not thread safe in this regard. Even when I assign two separate (but identical) networks to producer and consumer, I get an error when I run the two concurrently.

Therefore, I'm trying to separate the two as processes. I'm trying to use a message queue so that the producer can send its output to the consumer. However, now I don't know how to serialize `torch::Tensor`s. I think it should be doable with `torch::serialize::InputArchive` and `torch::serialize::OutputArchive`, but the C++ API reference wasn't too kind for me to understand.

Since I wasn't too sure if this is doable or not, I decided to submit as a feature request.</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>