Is torch::load able to load the bytestring saved in Python (getting file not found: archive/constants.pkl error)?

username: Shisho_Sama
cooked: <p>Hello everyone,<br>
I have a module in Python that converts a pytorch tensor into a bytestring and then serialize it using protobuf.<br>
I then try to load it in C++ using libtorch’s torch::load like  this :</p>
<pre><code class="lang-cpp">torch::Tensor deserialize_tensor(std::string input)
{
    torch::Tensor tensor;
    std::stringstream stream(input);
    torch::load(tensor, stream);
    return tensor;
}
</code></pre>
<pre><code class="lang-cpp">//use protobuf to load the vector
std::vector&lt;std::tuple&lt;std::string, torch::Tensor&gt;&gt; embeddings;
libtorch::EmbeddingVector vec;
std::ifstream stream(file_path, std::ios::in | std::ios::binary);
if (!vec.ParseFromIstream(&amp;stream))
{
    cout &lt;&lt; "Failed to parse embedding.";
    return embeddings;
}

for (size_t i = 0; i &lt; vec.row_size(); i++)
{
    auto tuple = vec.row(i);
    std::string key = tuple.key();
    auto tensor =  deserialize_tensor(tuple.tensor());
    //...    
}
</code></pre>
<p>and this is the snippet that I use to deserialize the tensors. this works just fine for C++. But it fails with an exception message when I try to load a string that was created in Python.<br>
This is the exception I get when I try to do this :</p>
<pre><code class="lang-auto">[enforce fail at ..\..\caffe2\serialize\inline_container.cc:209] . file not found: archive/constants.pkl
00007FFD13E87BC800007FFD13E87A30 c10.dll!c10::MessageLogger::~MessageLogger [&lt;unknown file&gt; @ &lt;unknown line number&gt;]
00007FFD13E880B300007FFD13E88080 c10.dll!c10::ThrowEnforceNotMet [&lt;unknown file&gt; @ &lt;unknown line number&gt;]
00007FFCF4E51F8C00007FFCF4E51EA0 torch_cpu.dll!caffe2::serialize::PyTorchStreamReader::getRecordID [&lt;unknown file&gt; @ &lt;unknown line number&gt;]
00007FFCF4E51DA600007FFCF4E51D70 torch_cpu.dll!caffe2::serialize::PyTorchStreamReader::getRecord [&lt;unknown file&gt; @ &lt;unknown line number&gt;]
00007FFCF5DB5F7D00007FFCF5DB5EB0 torch_cpu.dll!torch::jit::readArchiveAndTensors [&lt;unknown file&gt; @ &lt;unknown line number&gt;]
00007FFCF5DB5E7F00007FFCF5DB4A00 torch_cpu.dll!torch::jit::load [&lt;unknown file&gt; @ &lt;unknown line number&gt;]
00007FFCF5DB3BC400007FFCF5DA3D50 torch_cpu.dll!torch::jit::hasGradientInfoForSchema [&lt;unknown file&gt; @ &lt;unknown line number&gt;]
00007FFCF5DB4BB300007FFCF5DB4A00 torch_cpu.dll!torch::jit::load [&lt;unknown file&gt; @ &lt;unknown line number&gt;]
00007FFCF5DB495B00007FFCF5DB48E0 torch_cpu.dll!torch::jit::load [&lt;unknown file&gt; @ &lt;unknown line number&gt;]
00007FFCF5FBEF5C00007FFCF5FBEF20 torch_cpu.dll!torch::serialize::InputArchive::load_from [&lt;unknown file&gt; @ &lt;unknown line number&gt;]
00007FFD171EE48B00007FFD171EE2B0 FV.dll!deserialize_tensor [D:\Codes\fac_ver\cpp\port\LibtorchPort\Dependencies\include\Utility.h @ 1014]
00007FFD171F311100007FFD171F2E00 FV.dll!FVImpl::ReadEmbeddings [D:\Codes\fac_ver\cpp\port\LibtorchPort\Dependencies\include\FV\FV.cpp @ 729]
00007FFD171F35A700007FFD171F3380 FV.dll!FVImpl::LoadEmbeddings [D:\Codes\fac_ver\cpp\port\LibtorchPort\Dependencies\include\FV\FV.cpp @ 752]
00007FFD171EECCA00007FFD171EE5E0 FV.dll!FVImpl::FVImpl [D:\Codes\fac_ver\cpp\port\LibtorchPort\Dependencies\include\FV\FV.cpp @ 345]
00007FFD171FC93C00007FFD171FC8F0 FV.dll!FV::FV [D:\Codes\fac_ver\cpp\port\LibtorchPort\Dependencies\include\FV\FV.cpp @ 1749]
00007FF66F13BA6A00007FF66F13B5F0 FV_Test.exe!test_qmodel [D:\Codes\fac_ver\cpp\port\LibtorchPort\FV_Test\FV_Test.cpp @ 847]
00007FF66F13BFA500007FF66F13BDA0 FV_Test.exe!main [D:\Codes\fac_ver\cpp\port\LibtorchPort\FV_Test\FV_Test.cpp @ 937]
00007FF66F1CF58400007FF66F1CF478 FV_Test.exe!__scrt_common_main_seh [D:\agent\_work\9\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 288]
00007FFD82187BD400007FFD82187BC0 KERNEL32.DLL!BaseThreadInitThunk [&lt;unknown file&gt; @ &lt;unknown line number&gt;]
00007FFD8288CE5100007FFD8288CE30 ntdll.dll!RtlUserThreadStart [&lt;unknown file&gt; @ &lt;unknown line number&gt;]
</code></pre>
<p>Note:<br>
this is how I save the bytestring in python, if its of any help:</p>
<pre><code class="lang-python"># my protobuf module thats used both in c++ and 
# python to serialize and deserialize tensors.
import io
import torch 
from Manager import TorchSerializer_pb2 as ts

rec = ts.EmbeddingVector()

features = self.model(tensor_img.unsqueeze(0)).cpu().detach()
buffer = io.BytesIO()
torch.save(features, buffer)
tpl = rec.row.add()
tpl.key = key
buffer.seek(0)
tpl.tensor = buffer.read()
#...
</code></pre>
<p>Whats going on?<br>
Any help is greatly apprecaited</p>

username: tom
cooked: <p>I think this means you’re not having the right format.<br>
Note that PyTorch 1.6 changed the default serialization format of Python’s <code>torch.save</code>, which may bring a better experience moving between Python and C++.</p>
<p>Best regards</p>
<p>Thomas</p>

username: Shisho_Sama
cooked: <p>Thanks a lot really apprecaite it. but I’m using 1.6 both on Python and C++ side. and the process is the same Could you kindly elaborate a bit more on the <em>not</em> right format bit ?</p>

username: Rahul_Agrawal
cooked: <p><a class="mention" href="/u/shisho_sama">@Shisho_Sama</a> I am facing the exact same issue while loading a model saved using Python using a Java library. Were you able to find a solution?</p>

username: Shisho_Sama
cooked: <p>If my memory serves me right, I went the prototype route! you save your model/object as a protobuf object and then load it using protobuf in the other language like Java.<br>
In your Python code, export your model as a protobuf object, and in Java simply read it and do as you wish</p>