start problem for a C++ program using libtorch

username: julianyang
cooked: <p>environment:<br>
system: linux centos 7.6<br>
compiler: gcc 4.8.5<br>
libtorch: CPU version [<a href="https://download.pytorch.org/libtorch/cpu/libtorch-shared-with-deps-latest.zip" rel="nofollow noopener">https://download.pytorch.org/libtorch/cpu/libtorch-shared-with-deps-latest.zip</a>] 1.1.0<br>
demo:[<a href="https://pytorch.org/tutorials/advanced/cpp_export.html" rel="nofollow noopener">https://pytorch.org/tutorials/advanced/cpp_export.html</a>]<br>
<span class="hashtag">#include</span> &lt;torch/script.h&gt; // One-stop header.</p>
<p><span class="hashtag">#include</span> <br>
<span class="hashtag">#include</span> </p>
<p>int main(int argc, const char* argv[]) {<br>
if (argc != 2) {<br>
std::cerr &lt;&lt; “usage: example-app \n”;<br>
return -1;<br>
}</p>
<p>// Deserialize the ScriptModule from a file using torch::jit::load().<br>
std::shared_ptr<a>torch::jit::script::Module</a> module = torch::jit::load(argv[1]);</p>
<p>assert(module != nullptr);<br>
std::cout &lt;&lt; “ok\n”;<br>
// Create a vector of inputs.<br>
std::vector<a>torch::jit::IValue</a> inputs;<br>
inputs.push_back(torch::ones({1, 3, 224, 224}));</p>
<p>// Execute the model and turn its output into a tensor.<br>
auto output = module-&gt;forward(inputs).toTensor();</p>
<p>std::cout &lt;&lt; output.slice(/<em>dim=</em>/1, /<em>start=</em>/0, /<em>end=</em>/5) &lt;&lt; ‘\n’;<br>
}</p>
<ol>
<li>I use cmake to build the demo, CMakeLists.txt:<br>
project(torchd_emo)</li>
</ol>
<p>find_package(Torch REQUIRED)</p>
<p>message(STATUS “Pytorch status:”)<br>
message(STATUS "    libraries: ${TORCH_LIBRARIES}")</p>
<p>add_executable(demo demo.cpp)<br>
target_link_libraries(demo “${TORCH_LIBRARIES}”)<br>
set_property(TARGET demo PROPERTY CXX_STANDARD 11)</p>
<ol start="2">
<li>
<p>build the demo<br>
[root@iZwz9fj8vtpopvk9atx03nZ build]# cmake -DCMAKE_PREFIX_PATH=/home/julianyang/softwares/libtorch …/<br>
– The C compiler identification is GNU 4.8.5<br>
– The CXX compiler identification is GNU 4.8.5<br>
– Check for working C compiler: /usr/bin/cc<br>
– Check for working C compiler: /usr/bin/cc – works<br>
– Detecting C compiler ABI info<br>
– Detecting C compiler ABI info - done<br>
– Detecting C compile features<br>
– Detecting C compile features - done<br>
– Check for working CXX compiler: /usr/bin/c++<br>
– Check for working CXX compiler: /usr/bin/c++ – works<br>
– Detecting CXX compiler ABI info<br>
– Detecting CXX compiler ABI info - done<br>
– Detecting CXX compile features<br>
– Detecting CXX compile features - done<br>
– Looking for pthread.h<br>
– Looking for pthread.h - found<br>
– Performing Test CMAKE_HAVE_LIBC_PTHREAD<br>
– Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed<br>
– Looking for pthread_create in pthreads<br>
– Looking for pthread_create in pthreads - not found<br>
– Looking for pthread_create in pthread<br>
– Looking for pthread_create in pthread - found<br>
– Found Threads: TRUE<br>
– Found torch: /home/julianyang/softwares/libtorch/lib/libtorch.so<br>
– Pytorch status:<br>
–     libraries: torch;caffe2_library;/home/julianyang/softwares/libtorch/lib/libc10.so<br>
– Configuring done<br>
– Generating done<br>
– Build files have been written to: /home/julianyang/work/torch/cpp/build<br>
[root@iZwz9fj8vtpopvk9atx03nZ build]# make<br>
Scanning dependencies of target demo<br>
[ 50%] Building CXX object CMakeFiles/demo.dir/demo.cpp.o<br>
[100%] Linking CXX executable demo<br>
[100%] Built target demo</p>
</li>
<li>
<p>run the demo<br>
[root@iZwz9fj8vtpopvk9atx03nZ build]# ./demo<br>
*** Error in `./demo’: free(): invalid pointer: 0x00007fffcafbd600 ***<br>
======= Backtrace: =========<br>
/lib64/libc.so.6(+0x81489)[0x7f188fd47489]<br>
./demo(_ZN9__gnu_cxx13new_allocatorIPNSt8__detail15_Hash_node_baseEE10deallocateEPS3_m+0x20)[0x43c44e]<br>
./demo(_ZNSt10_HashtableIN3c106SymbolES1_SaIS1_ENSt8__detail9_IdentityESt8equal_toIS1_ESt4hashIS1_ENS3_18_Mod_range_hashingENS3_20_Default_ranged_hashENS3_20_Prime_rehash_policyENS3_17_Hashtable_traitsILb1ELb1ELb1EEEE21_M_deallocate_bucketsEPPNS3_15_Hash_node_baseEm+0x49)[0x43a0f1]<br>
./demo(_ZNSt10_HashtableIN3c106SymbolES1_SaIS1_ENSt8__detail9_IdentityESt8equal_toIS1_ESt4hashIS1_ENS3_18_Mod_range_hashingENS3_20_Default_ranged_hashENS3_20_Prime_rehash_policyENS3_17_Hashtable_traitsILb1ELb1ELb1EEEED1Ev+0x36)[0x436dd0]<br>
/home/julianyang/softwares/libtorch/lib/libcaffe2.so(+0x83d5c8)[0x7f1890ded5c8]<br>
/home/julianyang/softwares/libtorch/lib/libcaffe2.so(_ZN5torch3jit6script16SchemaTypeParser9parseTypeEv+0xc13)[0x7f1890e2b733]<br>
/home/julianyang/softwares/libtorch/lib/libcaffe2.so(+0x865d75)[0x7f1890e15d75]<br>
/home/julianyang/softwares/libtorch/lib/libcaffe2.so(+0x8688c6)[0x7f1890e188c6]<br>
/home/julianyang/softwares/libtorch/lib/libcaffe2.so(+0x864723)[0x7f1890e14723]<br>
/home/julianyang/softwares/libtorch/lib/libcaffe2.so(_ZN5torch3jit17parseSchemaOrNameERKSs+0x6f7)[0x7f1890e17cf7]<br>
/home/julianyang/softwares/libtorch/lib/libcaffe2.so(_ZN5torch3jit11parseSchemaERKSs+0x23)[0x7f1890e18623]<br>
/home/julianyang/softwares/libtorch/lib/libtorch.so.1(+0xaa770b)[0x7f189b68e70b]<br>
/home/julianyang/softwares/libtorch/lib/libtorch.so.1(_ZN5torch3jit16registerOperatorEONS0_8OperatorE+0x15)[0x7f189b69b185]<br>
/home/julianyang/softwares/libtorch/lib/libtorch.so.1(+0x278990)[0x7f189ae5f990]<br>
/lib64/ld-linux-x86-64.so.2(+0xf8f3)[0x7f189bfff8f3]<br>
/lib64/ld-linux-x86-64.so.2(+0x115a)[0x7f189bff115a]<br>
======= Memory map: ========<br>
00400000-00451000 r-xp 00000000 fd:11 5383578                            /mnt/workspace/julianyang/work/torch/cpp/build/demo<br>
00650000-00651000 r–p 00050000 fd:11 5383578                            /mnt/workspace/julianyang/work/torch/cpp/build/demo<br>
00651000-00652000 rw-p 00051000 fd:11 5383578                            /mnt/workspace/julianyang/work/torch/cpp/build/demo<br>
00c28000-00ec2000 rw-p 00000000 00:00 0                                  [heap]<br>
7f1888000000-7f1888021000 rw-p 00000000 00:00 0<br>
7f1888021000-7f188c000000 —p 00000000 00:00 0<br>
7f188f172000-7f188f179000 r-xp 00000000 fd:01 666759                     /usr/lib64/librt-2.17.so<br>
7f188f179000-7f188f378000 —p 00007000 fd:01 666759                     /usr/lib64/librt-2.17.so<br>
7f188f378000-7f188f379000 r–p 00006000 fd:01 666759                     /usr/lib64/librt-2.17.so<br>
7f188f379000-7f188f37a000 rw-p 00007000 fd:01 666759                     /usr/lib64/librt-2.17.so<br>
7f188f37a000-7f188f37c000 r-xp 00000000 fd:01 666752                     /usr/lib64/libdl-2.17.so<br>
7f188f37c000-7f188f57c000 —p 00002000 fd:01 666752                     /usr/lib64/libdl-2.17.so<br>
7f188f57c000-7f188f57d000 r–p 00002000 fd:01 666752                     /usr/lib64/libdl-2.17.so<br>
7f188f57d000-7f188f57e000 rw-p 00003000 fd:01 666752                     /usr/lib64/libdl-2.17.so<br>
7f188f57e000-7f188f67f000 r-xp 00000000 fd:01 666753                     /usr/lib64/libm-2.17.so<br>
7f188f67f000-7f188f87e000 —p 00101000 fd:01 666753                     /usr/lib64/libm-2.17.so<br>
7f188f87e000-7f188f87f000 r–p 00100000 fd:01 666753                     /usr/lib64/libm-2.17.so<br>
7f188f87f000-7f188f880000 rw-p 00101000 fd:01 666753                     /usr/lib64/libm-2.17.so<br>
7f188f880000-7f188f897000 r-xp 00000000 fd:01 658682                     /usr/lib64/libpthread-2.17.so<br>
7f188f897000-7f188fa96000 —p 00017000 fd:01 658682                     /usr/lib64/libpthread-2.17.so<br>
7f188fa96000-7f188fa97000 r–p 00016000 fd:01 658682                     /usr/lib64/libpthread-2.17.so<br>
7f188fa97000-7f188fa98000 rw-p 00017000 fd:01 658682                     /usr/lib64/libpthread-2.17.so<br>
7f188fa98000-7f188fa9c000 rw-p 00000000 00:00 0<br>
7f188fa9c000-7f188fac1000 r-xp 00000000 fd:11 4597940                    /mnt/workspace/julianyang/softwares/libtorch/lib/libgomp-8bba0e50.so.1<br>
7f188fac1000-7f188fcc0000 —p 00025000 fd:11 4597940                    /mnt/workspace/julianyang/softwares/libtorch/lib/libgomp-8bba0e50.so.1<br>
7f188fcc0000-7f188fcc1000 r–p 00024000 fd:11 4597940                    /mnt/workspace/julianyang/softwares/libtorch/lib/libgomp-8bba0e50.so.1<br>
7f188fcc1000-7f188fcc6000 rw-p 00025000 fd:11 4597940                    /mnt/workspace/julianyang/softwares/libtorch/lib/libgomp-8bba0e50.so.1<br>
7f188fcc6000-7f188fe88000 r-xp 00000000 fd:01 658656                     /usr/lib64/libc-2.17.so<br>
7f188fe88000-7f1890088000 —p 001c2000 fd:01 658656                     /usr/lib64/libc-2.17.so<br>
7f1890088000-7f189008c000 r–p 001c2000 fd:01 658656                     /usr/lib64/libc-2.17.so<br>
7f189008c000-7f189008e000 rw-p 001c6000 fd:01 658656                     /usr/lib64/libc-2.17.so<br>
7f189008e000-7f1890093000 rw-p 00000000 00:00 0<br>
7f1890093000-7f18900a8000 r-xp 00000000 fd:01 707424                     /usr/lib64/libgcc_s-4.8.5-20150702.so.1<br>
7f18900a8000-7f18902a7000 —p 00015000 fd:01 707424                     /usr/lib64/libgcc_s-4.8.5-20150702.so.1<br>
7f18902a7000-7f18902a8000 r–p 00014000 fd:01 707424                     /usr/lib64/libgcc_s-4.8.5-20150702.so.1<br>
7f18902a8000-7f18902a9000 rw-p 00015000 fd:01 707424                     /usr/lib64/libgcc_s-4.8.5-20150702.so.1<br>
7f18902a9000-7f1890392000 r-xp 00000000 fd:01 659019                     /usr/lib64/libstdc++.so.6.0.19<br>
7f1890392000-7f1890591000 —p 000e9000 fd:01 659019                     /usr/lib64/libstdc++.so.6.0.19<br>
7f1890591000-7f1890599000 r–p 000e8000 fd:01 659019                     /usr/lib64/libstdc++.so.6.0.19<br>
7f1890599000-7f189059b000 rw-p 000f0000 fd:01 659019                     /usr/lib64/libstdc++.so.6.0.19<br>
7f189059b000-7f18905b0000 rw-p 00000000 00:00 0<br>
7f18905b0000-7f189a2c9000 r-xp 00000000 fd:11 4597808                    /mnt/workspace/julianyang/softwares/libtorch/lib/libcaffe2.so<br>
7f189a2c9000-7f189a4c8000 —p 09d19000 fd:11 4597808                    /mnt/workspace/julianyang/softwares/libtorch/lib/libcaffe2.so<br>
7f189a4c8000-7f189a5a6000 r–p 09d18000 fd:11 4597808                    /mnt/workspace/julianyang/softwares/libtorch/lib/libcaffe2.so已放弃<br>
[root@iZwz9fj8vtpopvk9atx03nZ build]#</p>
</li>
</ol>
<p>Is there anyone can show me some clue to solve this problem.</p>

username: Huan_YANG
cooked: <p>Did you solve the problem? I met the same issue.</p>

username: tom
cooked: <p>Here, the C++ compiler is too old. You need at least gcc 4.9.5 or so, I have been using gcc 6 without trouble for a long time now.</p>
<p>Best regards</p>
<p>Thomas</p>
<p>(Same problem/solution as here:<br>
<a href="https://discuss.pytorch.org/t/the-problem-about-loading-a-pytorch-model-in-c/49589" class="inline-onebox">The problem about Loading a Pytorch Model in C++</a> )</p>

username: copythatpasta
cooked: <p>If i were to rebuilt pytorch from source, which checked out version would be compatible with gcc 4.8.5?</p>

username: tom
cooked: <p>I don’t know and I can heartily recommend taking a newer GCC over older PyTorch. (You could try with 0.4 and then go backwards, I doubt that 1.0 would work. But really, you’d be doing yourself a favour by using newer PyTorch – and everyone around here, too, in case you have questions).</p>
<p>Best regards</p>
<p>Thomas</p>

username: copythatpasta
cooked: <p>Is there truth to this? i just managed to build v1.2.0 with 4.8.5.Would i have problems linking?</p>

username: tom
cooked: <p>Oh, congrats! Then I might have misremembered.</p>

username: copythatpasta
cooked: <p>No worries ! I got it to work with 1.3.0</p>