Torch::load gives segmentation Segmentation fault

baris: <blockquote>
<p>std::stringstream stream;</p>
<p>auto tensor = torch::ones({3, 4});<br>
torch::save(tensor, stream);</p>
<p>torch::Tensor tensor2;<br>
torch::load(tensor2, stream);</p>
</blockquote>
<p>The very last line gives a Segmentation fault (core dumped).</p>
<p>Similar code was working fine on another machine, but in my current computer it doesn’t. What can be the problem?</p>

ptrblck: <p>If the same code is running fine on another machine, I would guess that you are using different PyTorch versions?<br>
If so, which one is working and which one is failing?<br>
Could you also post the batcktrace of <code>gdb</code> via:</p>
<pre><code class="lang-auto">gdb --args ./my_app
...
run
...
bt
</code></pre>

baris: <pre><code class="lang-auto">#include &lt;torch/torch.h&gt;
#include &lt;iostream&gt;

int main(){
	std::stringstream stream;

	std::cout &lt;&lt; "tensor1:" &lt;&lt; std::endl;
	torch::Tensor tensor1 = torch::eye(3);
	torch::save(tensor1, stream);
	std::cout &lt;&lt; tensor1 &lt;&lt; std::endl;

	std::cout &lt;&lt; "tensor2:" &lt;&lt; std::endl;
	torch::Tensor tensor2;
	torch::load(tensor2, stream);
	std::cout &lt;&lt; tensor2 &lt;&lt; std::endl;
}
</code></pre>
<p>this is the code that i run. btw it works fine using this CMakeList:</p>
<pre><code class="lang-auto">cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(dcgan)

find_package(Torch REQUIRED)

add_executable(dcgan dcgan.cpp)
target_link_libraries(dcgan "${TORCH_LIBRARIES}")
set_property(TARGET dcgan PROPERTY CXX_STANDARD 14)
</code></pre>
<p>and gives this output:</p>
<pre><code class="lang-auto">tensor1:
 1  0  0
 0  1  0
 0  0  1
[ CPUFloatType{3,3} ]
tensor2:
 1  0  0
 0  1  0
 0  0  1
[ CPUFloatType{3,3} ]
</code></pre>
<p>However within the project that I’m working, which has multiple CMakeLists in a way that’s too advanced for me, it gives me this:</p>
<pre><code class="lang-auto">tensor1:
 1  0  0
 0  1  0
 0  0  1
[ CPUFloatType{3,3} ]
tensor2:
Segmentation fault (core dumped)
</code></pre>
<p>and here is the backtrace you wanted:</p>
<pre><code class="lang-auto">GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later &lt;http://gnu.org/licenses/gpl.html&gt;
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
&lt;http://www.gnu.org/software/gdb/bugs/&gt;.
Find the GDB manual and other documentation resources online at:
&lt;http://www.gnu.org/software/gdb/documentation/&gt;.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./src/algos/simple...done.
(gdb) run
Starting program: /home/baris/sdms/build/src/algos/simple 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
tensor1:
 1  0  0
 0  1  0
 0  0  1
[ CPUFloatType{3,3} ]
tensor2:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff6a1b2cf in std::__detail::_Compiler&lt;std::__cxx11::regex_traits&lt;char&gt; &gt;::_Compiler(char const*, char const*, std::locale const&amp;, std::regex_constants::syntax_option_type) ()
   from /opt/libtorch/lib/libc10.so
(gdb) bt
#0  0x00007ffff6a1b2cf in std::__detail::_Compiler&lt;std::__cxx11::regex_traits&lt;char&gt; &gt;::_Compiler(char const*, char const*, std::locale const&amp;, std::regex_constants::syntax_option_type)
    () from /opt/libtorch/lib/libc10.so
#1  0x00007ffff6a09fa6 in c10::Device::Device(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;) () from /opt/libtorch/lib/libc10.so
#2  0x00007fffe916493d in torch::jit::Unpickler::readInstruction() () from /opt/libtorch/lib/libtorch_cpu.so
#3  0x00007fffe9166840 in torch::jit::Unpickler::run() () from /opt/libtorch/lib/libtorch_cpu.so
#4  0x00007fffe9166df1 in torch::jit::Unpickler::parse_ivalue() () from /opt/libtorch/lib/libtorch_cpu.so
#5  0x00007fffe910acc2 in torch::jit::readArchiveAndTensors(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, c10::optional&lt;std::function&lt;c10::StrongTypePtr (c10::QualifiedName const&amp;)&gt; &gt;, c10::optional&lt;std::function&lt;c10::intrusive_ptr&lt;c10::ivalue::Object, c10::detail::intrusive_target_default_null_type&lt;c10::ivalue::Object&gt; &gt; (c10::StrongTypePtr, c10::IValue)&gt; &gt;, c10::optional&lt;c10::Device&gt;, caffe2::serialize::PyTorchStreamReader&amp;) () from /opt/libtorch/lib/libtorch_cpu.so
#6  0x00007fffe910afdd in torch::jit::(anonymous namespace)::ScriptModuleDeserializer::readArchive(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;) [clone .constprop.789] () from /opt/libtorch/lib/libtorch_cpu.so
#7  0x00007fffe910d905 in torch::jit::(anonymous namespace)::ScriptModuleDeserializer::deserialize(c10::optional&lt;c10::Device&gt;, std::unordered_map&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::hash&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::equal_to&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::allocator&lt;std::pair&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt; &gt;&amp;) [clone .constprop.788] ()
   from /opt/libtorch/lib/libtorch_cpu.so
#8  0x00007fffe910ded9 in 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::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::hash&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::equal_to&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::allocator&lt;std::pair&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::all---Type &lt;return&gt; to continue, or q &lt;return&gt; to quit---q
</code></pre>

ptrblck: <p>Thanks for the update. Assuming you are using the latest stable release (<code>1.7.1</code>), could you update to the nightly release and rerun it?<br>
If you are still seeing the issue, please create an issue on GitHub with a minimal executable code snippet.</p>

baris: <p>Unfortunately with nightly release still it still crushes. Is my previous comment enough as minimal executable code, knowing that it doesn’t crush with the given CMakeList, but does crush with the setup that I can’t share?</p>

ptrblck: <p>I don’t think so, as we would need to reproduce it.<br>
Since the setup using the posted <code>CMakeLists.txt</code> with the code snippet works fine, I guess the issue is created by your original setup.<br>
If you cannot narrow it down, feel free to post the issue nevertheless, as someone might have already seen a similar issue and might be able to help.</p>

baris: <p>We solved the issue.</p>

ptrblck: <p>That’s good to hear! What was the issue?</p>

baris: <p>The order of inclusion of libraries was PROJECT, Boost, Torch. When we changed it to Torch, Boost, PROJECT the issue went away.</p>