Load tensors saved in python from c++ and vice versa

vfibyrf007: <p>Hello,<br>
I noticed that I can’t read tensor from .pt file saved in python with torch.save() from c++ with torch::load() and I can’t read tensor from file saved in c++ with torch::save() from python with torch.load()<br>
I read that PyTorch uses different formats to save tensors in python with pickle and in c++ it seems to be zip with tensors inside, but maybe are there any ways to transfer tensors between python and c++?<br>
Thanks a lot</p>

justusschock: <p>As far as I know, C++ uses the <code>torch.jit</code> serialization. Have you tried saving/loading it via <code>torch.jit</code>?</p>

vfibyrf007: <p>It is not possible. torch.jit.save() can only save ScriptModule but not a tensor</p>

Oktai15: <p>Any idea about this? Very important feature for integration</p>

kwanUm: <p>Joining the question, important feature indeed.</p>

tom: <p>So I think we’re lacking a bit here, but you could wrap your tensors into a module and save and load that though JIT.<br>
As this is clumsy, it would be nice to have something better. Options seem to be</p>
<ul>
<li>Read pickle. Unfortunately, the Python pickle format torch.save is Python-specific. There seems to be an a <a href="http://www.picklingtools.com/home" rel="nofollow noopener">library</a> capable of reading some pickle files, but I’m not sure about it’s usability here or the maintenance status.</li>
<li>Or you could write a model’s state dict to a standard format, say <a href="https://www.hdfgroup.org/solutions/hdf5/" rel="nofollow noopener">HDF5</a>, and use that to read and write tensors. HDF5 is generally well supported.</li>
</ul>
<p>Ideally, we would have a ready utility for this, but I’m afraid we don’t at this point.</p>
<p>Best regards</p>
<p>Thomas</p>

tom: <p>This seems seems to be a plan:<br>
<aside class="onebox githubissue">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/issues/18286" target="_blank" rel="nofollow noopener">github.com/pytorch/pytorch</a>
  </header>
  <article class="onebox-body">
    <a href="https://github.com/driazati" rel="nofollow noopener">
<img src="https://avatars1.githubusercontent.com/u/9407960?v=2&amp;s=96" class="thumbnail onebox-avatar" width="60" height="60">
</a>

<h4><a href="https://github.com/pytorch/pytorch/issues/18286" target="_blank" rel="nofollow noopener">Issue: [jit] Save IValues from Python and load them in C++</a></h4>

<div class="date" style="margin-top:10px;">
	<div class="user" style="margin-top:10px;">
	opened by <a href="https://github.com/driazati" target="_blank" rel="nofollow noopener">driazati</a>
	on <a href="https://github.com/pytorch/pytorch/issues/18286" target="_blank" rel="nofollow noopener">2019-03-21</a>
	</div>
	<div class="user">
	</div>
</div>

<pre class="content" style="white-space: pre-wrap;">It would be nice if users could create values in Python, serialize them, then load them in C++ and pass them...</pre>

<div class="labels">
 	<span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">jit</span>
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>

elvisszhang: <p>Below code works.</p>
<p>python code:</p>
<pre><code class="lang-auto">import torch
from torch import nn
 
x = torch.ones(4,5)
m = nn.Module()
par = nn.Parameter(x)
m.register_parameter("0",par)
tensors = torch.jit.script(m)
tensors.save('x.ts')
</code></pre>
<p>c++ code:</p>
<pre><code class="lang-auto">try
{
    torch::Tensor x;
    torch::load(x, "x.ts");
    std::cout &lt;&lt; x;
    return 0;
}
catch (const c10::Error&amp; e) {
    std::cerr &lt;&lt; "error loading the tensor " &lt;&lt; std::endl;
    std::cerr &lt;&lt; e.msg() &lt;&lt; std::endl;
    return 0;
}
</code></pre>

elvisszhang: <p>another solution.</p>
<p>python code:</p>
<pre><code class="lang-auto">import torch
from torch import nn
 
class TensorContainer(nn.Module):
    def __init__(self, tensor_dict):
        super().__init__()
        for key,value in tensor_dict.items():
            setattr(self, key, value)
 
x = torch.ones(4, 4)
tensor_dict = {'x': x}
tensors = TensorContainer(tensor_dict)
tensors = torch.jit.script(tensors)
tensors.save('x.pth')
</code></pre>
<p>c++ code:</p>
<pre><code class="lang-auto">try
{
    torch::jit::script::Module tensors = torch::jit::load("x.pth");
    c10::IValue iv = tensors.attr("x");
    torch::Tensor ts = iv.toTensor();
    std::cout &lt;&lt; ts;
}catch (const c10::Error&amp; e) {
    std::cerr &lt;&lt; "error loading the tensor " &lt;&lt; std::endl;
    std::cerr &lt;&lt; e.msg() &lt;&lt; std::endl;
}
</code></pre>