<h2 dir="auto">🐛 Bug</h2>
<p dir="auto">Load pytorch tensor created by torch.save(tensor_name, tensor_path) in c++ libtorch failed.<br>
How can I save some tensor in python, but load it in libtorch?</p>
<h2 dir="auto">To Reproduce</h2>
<p dir="auto">using the following code:</p>
<p dir="auto">I save tensor named piror using python, using the code:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="prior = torch.ones(32145, 4)
torch.save(prior,  'prior.pth')"><pre class="notranslate"><code class="notranslate">prior = torch.ones(32145, 4)
torch.save(prior,  'prior.pth')
</code></pre></div>
<p dir="auto">And I load the tensor in libtorch using C++, by the following code:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="torch::Tensor priors = torch::ones({32145, 4});
torch::load(priors , &quot;/app/model/prior.pth&quot;);"><pre class="notranslate"><code class="notranslate">torch::Tensor priors = torch::ones({32145, 4});
torch::load(priors , "/app/model/prior.pth");
</code></pre></div>
<h2 dir="auto">Expected behavior</h2>
<p dir="auto">load the tensor successfully.<br>
And get exact same value as in pytorch python-api.</p>
<h2 dir="auto">Environment</h2>
<p dir="auto">Collecting environment information...<br>
PyTorch version: N/A<br>
Is debug build: N/A<br>
CUDA used to build PyTorch: N/A</p>
<p dir="auto">OS: Ubuntu 16.04.5 LTS<br>
GCC version: (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609<br>
CMake version: version 3.13.2</p>
<p dir="auto">Python version: 3.5<br>
Is CUDA available: N/A<br>
CUDA runtime version: 10.0.130<br>
GPU models and configuration:<br>
GPU 0: TITAN X (Pascal)<br>
GPU 1: TITAN X (Pascal)</p>
<p dir="auto">Nvidia driver version: 430.26<br>
cuDNN version: /usr/lib/x86_64-linux-gnu/libcudnn.so.7.4.2</p>
<p dir="auto">Versions of relevant libraries:<br>
[pip] Could not collect<br>
[conda] Could not collect</p>
<h2 dir="auto">Additional context</h2>
<p dir="auto">But I got the error:<br>
terminate called after throwing an instance of 'c10::Error'<br>
what():  <code class="notranslate">torch::jit::load()</code> received a file from <code class="notranslate">torch.save()</code>, but <code class="notranslate">torch::jit::load()</code> can only load files produced by <code class="notranslate">torch.jit.save()</code> (load at ../torch/csrc/jit/serialization/import.cpp:285)</p>
<p dir="auto">Why is that? I do not use torch::jit::load but torch::load, so how to load tensor saved in pytorch?</p>
<p dir="auto">Thanks in advance.</p>
<p dir="auto">cc <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/suo/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/suo">@suo</a> <a class="user-mention notranslate" data-hovercard-type="user" data-hovercard-url="/users/yf225/hovercard" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" href="https://github.com/yf225">@yf225</a></p>
<h2 dir="auto">🐛 Bug</h2>
<p dir="auto">I don't manage to import a tensor saved in PyTorch from C++. Any help on this is welcome <g-emoji class="g-emoji" alias="smile" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f604.png">😄</g-emoji></p>
<h2 dir="auto">To Reproduce</h2>
<p dir="auto">Save a tensor to a file in python.</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.save(torch.tensor([1., 2., 3.]), &quot;tensor.pt&quot;)
&gt;&gt;&gt; torch.load(&quot;tensor.pt&quot;)
tensor([1., 2., 3.])"><pre class="notranslate"><span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-k">import</span> <span class="pl-s1">torch</span>
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">torch</span>.<span class="pl-en">save</span>(<span class="pl-s1">torch</span>.<span class="pl-en">tensor</span>([<span class="pl-c1">1.</span>, <span class="pl-c1">2.</span>, <span class="pl-c1">3.</span>]), <span class="pl-s">"tensor.pt"</span>)
<span class="pl-c1">&gt;&gt;</span><span class="pl-c1">&gt;</span> <span class="pl-s1">torch</span>.<span class="pl-en">load</span>(<span class="pl-s">"tensor.pt"</span>)
<span class="pl-en">tensor</span>([<span class="pl-c1">1.</span>, <span class="pl-c1">2.</span>, <span class="pl-c1">3.</span>])</pre></div>
<p dir="auto">Build this small C++ program</p>
<div class="highlight highlight-source-c++ notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="#include &lt;torch/torch.h&gt;

int main() {
  torch::Tensor tensor;
  torch::load(tensor, &quot;tensor.pt&quot;);
  std::cout &lt;&lt; tensor &lt;&lt; std::endl;
}"><pre class="notranslate">#<span class="pl-k">include</span> <span class="pl-s"><span class="pl-pds">&lt;</span>torch/torch.h<span class="pl-pds">&gt;</span></span>

<span class="pl-k">int</span> <span class="pl-en">main</span>() {
  torch::Tensor tensor;
  <span class="pl-c1">torch::load</span>(tensor, <span class="pl-s"><span class="pl-pds">"</span>tensor.pt<span class="pl-pds">"</span></span>);
  std::cout &lt;&lt; tensor &lt;&lt; std::endl;
}</pre></div>
<p dir="auto">At runtime I get:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="terminate called after throwing an instance of 'c10::Error'
  what():  [enforce fail at inline_container.cc:137] . PytorchStreamReader failed reading zip archive: failed finding central directory
frame #0: std::function&lt;std::string ()&gt;::operator()() const + 0x11 (0x7f1da515e691 in /___/libtorch/lib/libc10.so)
frame #1: c10::ThrowEnforceNotMet(char const*, int, char const*, std::string const&amp;, void const*) + 0x49 (0x7f1da515e4a9 in /__/libtorch/lib/libc10.so)
frame #2: caffe2::serialize::PyTorchStreamReader::valid(char const*) + 0x6b (0x7f1d9c69735b in /__/libtorch/lib/libcaffe2.so)
frame #3: caffe2::serialize::PyTorchStreamReader::init() + 0x9d (0x7f1d9c69912d in /___/libtorch/lib/libcaffe2.so)
frame #4: caffe2::serialize::PyTorchStreamReader::PyTorchStreamReader(std::unique_ptr&lt;caffe2::serialize::ReadAdapterInterface, std::default_delete&lt;caffe2::serialize::ReadAdapterInterface&gt; &gt;) + 0x3b (0x7f1d9c69ab8b in /__/libtorch/lib/libcaffe2.so)
frame #5: &lt;unknown function&gt; + 0xa9b5bf (0x7f1da5e125bf in /__/libtorch/lib/libtorch.so.1)
frame #6: torch::jit::load(std::unique_ptr&lt;caffe2::serialize::ReadAdapterInterface, std::default_delete&lt;caffe2::serialize::ReadAdapterInterface&gt; &gt;, c10::optional&lt;c10::Device&gt;, std::unordered_map&lt;std::string, std::string, std::hash&lt;std::string&gt;, std::equal_to&lt;std::string&gt;, std::allocator&lt;std::pair&lt;std::string const, std::string&gt; &gt; &gt;&amp;) + 0x9a (0x7f1da5e15d2a in /__/libtorch/lib/libtorch.so.1)
frame #7: torch::jit::load(std::string const&amp;, c10::optional&lt;c10::Device&gt;, std::unordered_map&lt;std::string, std::string, std::hash&lt;std::string&gt;, std::equal_to&lt;std::string&gt;, std::allocator&lt;std::pair&lt;std::string const, std::string&gt; &gt; &gt;&amp;) + 0x68 (0x7f1da5e15ec8 in /___/libtorch/lib/libtorch.so.1)
frame #8: torch::serialize::InputArchive::load_from(std::string const&amp;, c10::optional&lt;c10::Device&gt;) + 0x38 (0x7f1da604c058 in /___/libtorch/lib/libtorch.so.1)
frame #9: void torch::load&lt;at::Tensor, char const (&amp;) [10]&gt;(at::Tensor&amp;, char const (&amp;) [10]) + 0x86 (0x55a502f5fe97 in ./load)
frame #10: main + 0x37 (0x55a502f5fb51 in ./load)
frame #11: __libc_start_main + 0xe7 (0x7f1d9a338b97 in /lib/x86_64-linux-gnu/libc.so.6)
frame #12: _start + 0x2a (0x55a502f5f96a in ./load)

[1]    30788 abort      ./load"><pre class="notranslate"><code class="notranslate">terminate called after throwing an instance of 'c10::Error'
  what():  [enforce fail at inline_container.cc:137] . PytorchStreamReader failed reading zip archive: failed finding central directory
frame #0: std::function&lt;std::string ()&gt;::operator()() const + 0x11 (0x7f1da515e691 in /___/libtorch/lib/libc10.so)
frame #1: c10::ThrowEnforceNotMet(char const*, int, char const*, std::string const&amp;, void const*) + 0x49 (0x7f1da515e4a9 in /__/libtorch/lib/libc10.so)
frame #2: caffe2::serialize::PyTorchStreamReader::valid(char const*) + 0x6b (0x7f1d9c69735b in /__/libtorch/lib/libcaffe2.so)
frame #3: caffe2::serialize::PyTorchStreamReader::init() + 0x9d (0x7f1d9c69912d in /___/libtorch/lib/libcaffe2.so)
frame #4: caffe2::serialize::PyTorchStreamReader::PyTorchStreamReader(std::unique_ptr&lt;caffe2::serialize::ReadAdapterInterface, std::default_delete&lt;caffe2::serialize::ReadAdapterInterface&gt; &gt;) + 0x3b (0x7f1d9c69ab8b in /__/libtorch/lib/libcaffe2.so)
frame #5: &lt;unknown function&gt; + 0xa9b5bf (0x7f1da5e125bf in /__/libtorch/lib/libtorch.so.1)
frame #6: torch::jit::load(std::unique_ptr&lt;caffe2::serialize::ReadAdapterInterface, std::default_delete&lt;caffe2::serialize::ReadAdapterInterface&gt; &gt;, c10::optional&lt;c10::Device&gt;, std::unordered_map&lt;std::string, std::string, std::hash&lt;std::string&gt;, std::equal_to&lt;std::string&gt;, std::allocator&lt;std::pair&lt;std::string const, std::string&gt; &gt; &gt;&amp;) + 0x9a (0x7f1da5e15d2a in /__/libtorch/lib/libtorch.so.1)
frame #7: torch::jit::load(std::string const&amp;, c10::optional&lt;c10::Device&gt;, std::unordered_map&lt;std::string, std::string, std::hash&lt;std::string&gt;, std::equal_to&lt;std::string&gt;, std::allocator&lt;std::pair&lt;std::string const, std::string&gt; &gt; &gt;&amp;) + 0x68 (0x7f1da5e15ec8 in /___/libtorch/lib/libtorch.so.1)
frame #8: torch::serialize::InputArchive::load_from(std::string const&amp;, c10::optional&lt;c10::Device&gt;) + 0x38 (0x7f1da604c058 in /___/libtorch/lib/libtorch.so.1)
frame #9: void torch::load&lt;at::Tensor, char const (&amp;) [10]&gt;(at::Tensor&amp;, char const (&amp;) [10]) + 0x86 (0x55a502f5fe97 in ./load)
frame #10: main + 0x37 (0x55a502f5fb51 in ./load)
frame #11: __libc_start_main + 0xe7 (0x7f1d9a338b97 in /lib/x86_64-linux-gnu/libc.so.6)
frame #12: _start + 0x2a (0x55a502f5f96a in ./load)

[1]    30788 abort      ./load
</code></pre></div>
<h2 dir="auto">Expected behavior</h2>
<p dir="auto">I would expect to be able to load a tensor from the PyTorch dump in the C++ program.</p>
<h2 dir="auto">Environment</h2>
<p dir="auto">PyTorch version: 1.1.0<br>
Is debug build: No<br>
CUDA used to build PyTorch: 9.0.176</p>
<p dir="auto">OS: Ubuntu 18.04.2 LTS<br>
GCC version: (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0<br>
CMake version: version 3.10.2</p>
<p dir="auto">Python version: 3.7<br>
Is CUDA available: Yes<br>
CUDA runtime version: 10.1.105<br>
GPU models and configuration:<br>
GPU 0: GeForce GTX TITAN X<br>
GPU 1: GeForce GTX TITAN X<br>
GPU 2: GeForce GTX TITAN X<br>
GPU 3: GeForce GTX TITAN X<br>
GPU 4: GeForce GTX TITAN X<br>
GPU 5: GeForce GTX TITAN X<br>
GPU 6: GeForce GTX TITAN X<br>
GPU 7: GeForce GTX TITAN X</p>
<p dir="auto">Nvidia driver version: 418.56<br>
cuDNN version: Could not collect</p>
<p dir="auto">Versions of relevant libraries:<br>
[pip] msgpack-numpy==0.4.3.2<br>
[pip] numpy==1.15.4<br>
[pip] numpydoc==0.8.0<br>
[pip] pytorch-nlp==0.4.1<br>
[pip] torch==1.1.0<br>
[pip] torchvision==0.2.2<br>
[conda] blas                      1.0                         mkl<br>
[conda] mkl                       2018.0.3                      1<br>
[conda] mkl-service               1.1.2            py37h90e4bf4_5<br>
[conda] mkl_fft                   1.0.6            py37h7dd41cf_0<br>
[conda] mkl_random                1.0.1            py37h4414c95_1<br>
[conda] pytorch                   1.1.0           py3.7_cuda9.0.176_cudnn7.5.1_0    pytorch<br>
[conda] pytorch-nlp               0.4.1                    pypi_0    pypi<br>
[conda] torchvision               0.2.2                      py_3    pytorch</p>
<h2 dir="auto">Additional context</h2>
<p dir="auto">I downloaded the C++ frontend from <a href="https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip" rel="nofollow">https://download.pytorch.org/libtorch/nightly/cpu/libtorch-shared-with-deps-latest.zip</a> yesterday.</p>