How to serialize a tensor or vector of tensors in libtorch?

Shisho_Sama: <p>Hello everyone,<br>
I wanted to serialize a <code>vector&lt;std::tuple&lt;std::string, torch::Tensor&gt;&gt;</code> and a vector of that meaning a <code>std::vector&lt;std::vector&lt;std::tuple&lt;std::string, torch::Tensor&gt;&gt;&gt;</code> in libtorch.<br>
What are my options here? does libtorch offer a serialization capability such as pickle or not? what should I be doing in this case?<br>
Thanks a lot in advance</p>

Shisho_Sama: <p>Seems like both tensor and vector of tensors are supported and can be easily serialized using <code>torch::save</code> and <code>torch::load</code> respectively.</p>
<details>
<summary>
how it works!</summary>
<pre><code class="lang-auto">torch::save(tensor, "mytensor.pt");
torch::save(tensor_vec "myvector.pt")
</code></pre>
<p>for loading :</p>
<pre><code class="lang-auto">torch::load(tensor_vec, "myvector.pt")
</code></pre>
</details>
<p>However, I could really appreciate if anyone could give me a hint on how to add support for more types such as the one I mentioned above.</p>

Shisho_Sama: <p>I could save the file using but I have difficulty loading from it :<br>
for saving I simply did :</p>
<pre><code class="lang-cpp">template &lt;typename... SaveToArgs&gt;
void save(const std::vector&lt;std::tuple&lt;std::string, torch::Tensor&gt;&gt;&amp; tensor_vec, SaveToArgs&amp;&amp;... args) 
{
    torch::serialize::OutputArchive archive(std::make_shared&lt;torch::jit::CompilationUnit&gt;());
    for (size_t i = 0; i &lt; tensor_vec.size(); i+=2) 
    {
        auto&amp; key_value = tensor_vec[i];
        auto key_name= std::get&lt;0&gt;(key_value);
        auto value_tensor = std::get&lt;1&gt;(key_value);

        archive.write(c10::to_string(i), key_name);
        archive.write(c10::to_string(i+1), value_tensor);
    }
    archive.save_to(std::forward&lt;SaveToArgs&gt;(args)...);

}
</code></pre>
<p>and similarly for load:</p>
<pre><code class="lang-cpp">template &lt;typename... LoadFromArgs&gt;
void load(std::vector&lt;std::tuple&lt;std::string, torch::Tensor&gt;&gt;&amp; tensor_vec, LoadFromArgs&amp;&amp;... args)
{
    torch::serialize::InputArchive archive;
    archive.load_from(std::forward&lt;LoadFromArgs&gt;(args)...);

    // NOTE: The number of elements in the serialized `std::vector&lt;torch::Tensor&gt;`
    // is not known ahead of time, so we need a while-loop to increment the index,
    // and use `archive.try_read(...)` to check whether we have reached the end of
    // the serialized `std::vector&lt;torch::Tensor&gt;`.
    size_t index = 0;
    torch::Tensor value;
    std::string key_value;
    while (archive.try_read(c10::to_string(index), key_value))
    {
        archive.try_read(c10::to_string(index+1), value);
        tensor_vec.push_back(std::make_tuple(key_value, std::move(value)));
        value = torch::Tensor();
        key_value = "";
        index+=2;
    }
}
</code></pre>
<p>the prblem that I’m facing here is that the <code>InputArchive</code> currently doesnt have support for reading <code>std::string</code> and the <code>InputArchive</code> is final and I can not extend it.<br>
any help in this regard is greatly appreciated.<br>
<a class="mention" href="/u/ptrblck">@ptrblck</a> <a class="mention" href="/u/alband">@albanD</a></p>

albanD: <p>Hey.</p>
<p>If the issue is only with InputArchive not having the features you want, you can open an feature request on github.<br>
I am not very familiar with c++ serialization, but it sound like something we want to support! (or at least make it so that users can add support on their side).</p>

Shisho_Sama: <p>Thanks really appreciate your feedback. I ultimately ended up switching to protobuf for serialization but it’d be great to see much better support for  this in libtorch natively.</p>

JamesDickens: <p>What is the type of tensor_vec in your example?</p>

Shisho_Sama: <pre><code class="lang-auto">std::vector&lt;std::tuple&lt;std::string, torch::Tensor&gt;&gt;&amp; tensor_vec
</code></pre>
<p>You can see its type in the function definition, up in the example I provided at the time.</p>