start problem for a C++ program using libtorch

environment:
system: linux centos 7.6
compiler: gcc 4.8.5
libtorch: CPU version [https://download.pytorch.org/libtorch/cpu/libtorch-shared-with-deps-latest.zip] 1.1.0
demo:[https://pytorch.org/tutorials/advanced/cpp_export.html]
#include <torch/script.h> // One-stop header.

#include
#include

int main(int argc, const char* argv[]) {
if (argc != 2) {
std::cerr << “usage: example-app \n”;
return -1;
}

// Deserialize the ScriptModule from a file using torch::jit::load().
std::shared_ptrtorch::jit::script::Module module = torch::jit::load(argv[1]);

assert(module != nullptr);
std::cout << “ok\n”;
// Create a vector of inputs.
std::vectortorch::jit::IValue inputs;
inputs.push_back(torch::ones({1, 3, 224, 224}));

// Execute the model and turn its output into a tensor.
auto output = module->forward(inputs).toTensor();

std::cout << output.slice(/dim=/1, /start=/0, /end=/5) << ‘\n’;
}

  1. I use cmake to build the demo, CMakeLists.txt:
    project(torchd_emo)

find_package(Torch REQUIRED)

message(STATUS “Pytorch status:”)
message(STATUS " libraries: ${TORCH_LIBRARIES}")

add_executable(demo demo.cpp)
target_link_libraries(demo “${TORCH_LIBRARIES}”)
set_property(TARGET demo PROPERTY CXX_STANDARD 11)

  1. build the demo
    [root@iZwz9fj8vtpopvk9atx03nZ build]# cmake -DCMAKE_PREFIX_PATH=/home/julianyang/softwares/libtorch …/
    – The C compiler identification is GNU 4.8.5
    – The CXX compiler identification is GNU 4.8.5
    – Check for working C compiler: /usr/bin/cc
    – Check for working C compiler: /usr/bin/cc – works
    – Detecting C compiler ABI info
    – Detecting C compiler ABI info - done
    – Detecting C compile features
    – Detecting C compile features - done
    – Check for working CXX compiler: /usr/bin/c++
    – Check for working CXX compiler: /usr/bin/c++ – works
    – Detecting CXX compiler ABI info
    – Detecting CXX compiler ABI info - done
    – Detecting CXX compile features
    – Detecting CXX compile features - done
    – Looking for pthread.h
    – Looking for pthread.h - found
    – Performing Test CMAKE_HAVE_LIBC_PTHREAD
    – Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
    – Looking for pthread_create in pthreads
    – Looking for pthread_create in pthreads - not found
    – Looking for pthread_create in pthread
    – Looking for pthread_create in pthread - found
    – Found Threads: TRUE
    – Found torch: /home/julianyang/softwares/libtorch/lib/libtorch.so
    – Pytorch status:
    – libraries: torch;caffe2_library;/home/julianyang/softwares/libtorch/lib/libc10.so
    – Configuring done
    – Generating done
    – Build files have been written to: /home/julianyang/work/torch/cpp/build
    [root@iZwz9fj8vtpopvk9atx03nZ build]# make
    Scanning dependencies of target demo
    [ 50%] Building CXX object CMakeFiles/demo.dir/demo.cpp.o
    [100%] Linking CXX executable demo
    [100%] Built target demo

  2. run the demo
    [root@iZwz9fj8vtpopvk9atx03nZ build]# ./demo
    *** Error in `./demo’: free(): invalid pointer: 0x00007fffcafbd600 ***
    ======= Backtrace: =========
    /lib64/libc.so.6(+0x81489)[0x7f188fd47489]
    ./demo(_ZN9__gnu_cxx13new_allocatorIPNSt8__detail15_Hash_node_baseEE10deallocateEPS3_m+0x20)[0x43c44e]
    ./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]
    ./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]
    /home/julianyang/softwares/libtorch/lib/libcaffe2.so(+0x83d5c8)[0x7f1890ded5c8]
    /home/julianyang/softwares/libtorch/lib/libcaffe2.so(_ZN5torch3jit6script16SchemaTypeParser9parseTypeEv+0xc13)[0x7f1890e2b733]
    /home/julianyang/softwares/libtorch/lib/libcaffe2.so(+0x865d75)[0x7f1890e15d75]
    /home/julianyang/softwares/libtorch/lib/libcaffe2.so(+0x8688c6)[0x7f1890e188c6]
    /home/julianyang/softwares/libtorch/lib/libcaffe2.so(+0x864723)[0x7f1890e14723]
    /home/julianyang/softwares/libtorch/lib/libcaffe2.so(_ZN5torch3jit17parseSchemaOrNameERKSs+0x6f7)[0x7f1890e17cf7]
    /home/julianyang/softwares/libtorch/lib/libcaffe2.so(_ZN5torch3jit11parseSchemaERKSs+0x23)[0x7f1890e18623]
    /home/julianyang/softwares/libtorch/lib/libtorch.so.1(+0xaa770b)[0x7f189b68e70b]
    /home/julianyang/softwares/libtorch/lib/libtorch.so.1(_ZN5torch3jit16registerOperatorEONS0_8OperatorE+0x15)[0x7f189b69b185]
    /home/julianyang/softwares/libtorch/lib/libtorch.so.1(+0x278990)[0x7f189ae5f990]
    /lib64/ld-linux-x86-64.so.2(+0xf8f3)[0x7f189bfff8f3]
    /lib64/ld-linux-x86-64.so.2(+0x115a)[0x7f189bff115a]
    ======= Memory map: ========
    00400000-00451000 r-xp 00000000 fd:11 5383578 /mnt/workspace/julianyang/work/torch/cpp/build/demo
    00650000-00651000 r–p 00050000 fd:11 5383578 /mnt/workspace/julianyang/work/torch/cpp/build/demo
    00651000-00652000 rw-p 00051000 fd:11 5383578 /mnt/workspace/julianyang/work/torch/cpp/build/demo
    00c28000-00ec2000 rw-p 00000000 00:00 0 [heap]
    7f1888000000-7f1888021000 rw-p 00000000 00:00 0
    7f1888021000-7f188c000000 —p 00000000 00:00 0
    7f188f172000-7f188f179000 r-xp 00000000 fd:01 666759 /usr/lib64/librt-2.17.so
    7f188f179000-7f188f378000 —p 00007000 fd:01 666759 /usr/lib64/librt-2.17.so
    7f188f378000-7f188f379000 r–p 00006000 fd:01 666759 /usr/lib64/librt-2.17.so
    7f188f379000-7f188f37a000 rw-p 00007000 fd:01 666759 /usr/lib64/librt-2.17.so
    7f188f37a000-7f188f37c000 r-xp 00000000 fd:01 666752 /usr/lib64/libdl-2.17.so
    7f188f37c000-7f188f57c000 —p 00002000 fd:01 666752 /usr/lib64/libdl-2.17.so
    7f188f57c000-7f188f57d000 r–p 00002000 fd:01 666752 /usr/lib64/libdl-2.17.so
    7f188f57d000-7f188f57e000 rw-p 00003000 fd:01 666752 /usr/lib64/libdl-2.17.so
    7f188f57e000-7f188f67f000 r-xp 00000000 fd:01 666753 /usr/lib64/libm-2.17.so
    7f188f67f000-7f188f87e000 —p 00101000 fd:01 666753 /usr/lib64/libm-2.17.so
    7f188f87e000-7f188f87f000 r–p 00100000 fd:01 666753 /usr/lib64/libm-2.17.so
    7f188f87f000-7f188f880000 rw-p 00101000 fd:01 666753 /usr/lib64/libm-2.17.so
    7f188f880000-7f188f897000 r-xp 00000000 fd:01 658682 /usr/lib64/libpthread-2.17.so
    7f188f897000-7f188fa96000 —p 00017000 fd:01 658682 /usr/lib64/libpthread-2.17.so
    7f188fa96000-7f188fa97000 r–p 00016000 fd:01 658682 /usr/lib64/libpthread-2.17.so
    7f188fa97000-7f188fa98000 rw-p 00017000 fd:01 658682 /usr/lib64/libpthread-2.17.so
    7f188fa98000-7f188fa9c000 rw-p 00000000 00:00 0
    7f188fa9c000-7f188fac1000 r-xp 00000000 fd:11 4597940 /mnt/workspace/julianyang/softwares/libtorch/lib/libgomp-8bba0e50.so.1
    7f188fac1000-7f188fcc0000 —p 00025000 fd:11 4597940 /mnt/workspace/julianyang/softwares/libtorch/lib/libgomp-8bba0e50.so.1
    7f188fcc0000-7f188fcc1000 r–p 00024000 fd:11 4597940 /mnt/workspace/julianyang/softwares/libtorch/lib/libgomp-8bba0e50.so.1
    7f188fcc1000-7f188fcc6000 rw-p 00025000 fd:11 4597940 /mnt/workspace/julianyang/softwares/libtorch/lib/libgomp-8bba0e50.so.1
    7f188fcc6000-7f188fe88000 r-xp 00000000 fd:01 658656 /usr/lib64/libc-2.17.so
    7f188fe88000-7f1890088000 —p 001c2000 fd:01 658656 /usr/lib64/libc-2.17.so
    7f1890088000-7f189008c000 r–p 001c2000 fd:01 658656 /usr/lib64/libc-2.17.so
    7f189008c000-7f189008e000 rw-p 001c6000 fd:01 658656 /usr/lib64/libc-2.17.so
    7f189008e000-7f1890093000 rw-p 00000000 00:00 0
    7f1890093000-7f18900a8000 r-xp 00000000 fd:01 707424 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
    7f18900a8000-7f18902a7000 —p 00015000 fd:01 707424 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
    7f18902a7000-7f18902a8000 r–p 00014000 fd:01 707424 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
    7f18902a8000-7f18902a9000 rw-p 00015000 fd:01 707424 /usr/lib64/libgcc_s-4.8.5-20150702.so.1
    7f18902a9000-7f1890392000 r-xp 00000000 fd:01 659019 /usr/lib64/libstdc++.so.6.0.19
    7f1890392000-7f1890591000 —p 000e9000 fd:01 659019 /usr/lib64/libstdc++.so.6.0.19
    7f1890591000-7f1890599000 r–p 000e8000 fd:01 659019 /usr/lib64/libstdc++.so.6.0.19
    7f1890599000-7f189059b000 rw-p 000f0000 fd:01 659019 /usr/lib64/libstdc++.so.6.0.19
    7f189059b000-7f18905b0000 rw-p 00000000 00:00 0
    7f18905b0000-7f189a2c9000 r-xp 00000000 fd:11 4597808 /mnt/workspace/julianyang/softwares/libtorch/lib/libcaffe2.so
    7f189a2c9000-7f189a4c8000 —p 09d19000 fd:11 4597808 /mnt/workspace/julianyang/softwares/libtorch/lib/libcaffe2.so
    7f189a4c8000-7f189a5a6000 r–p 09d18000 fd:11 4597808 /mnt/workspace/julianyang/softwares/libtorch/lib/libcaffe2.so已放弃
    [root@iZwz9fj8vtpopvk9atx03nZ build]#

Is there anyone can show me some clue to solve this problem.

Did you solve the problem? I met the same issue.

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.

Best regards

Thomas

(Same problem/solution as here:
The problem about Loading a Pytorch Model in C++ )

If i were to rebuilt pytorch from source, which checked out version would be compatible with gcc 4.8.5?

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).

Best regards

Thomas

Is there truth to this? i just managed to build v1.2.0 with 4.8.5.Would i have problems linking?

Oh, congrats! Then I might have misremembered.

No worries ! I got it to work with 1.3.0