Failed to build libtorch because "Multiple definition of at::naive::DispatchStub"

Hi, I want to build libtorch from source without cuda because the official binary is using old C++ABI. The steps i did is as follows:

  1. cd pytorch
  2. mkdir build && cd build
  3. BUILD_TORCH=ON ONNX_NAMESPACE=onnx_torch bash …/tools/build_pytorch_libs.sh --use-nnpack caffe2

And then i meet the following errors:

Linking CXX shared library ../lib/libcaffe2.so
CMakeFiles/caffe2.dir/__/aten/src/ATen/native/cpu/UnaryOpsKernel.cpp.DEFAULT.cpp.DEFAULT.cpp.DEFAULT.cpp.AVX2.cpp.AVX2.cpp.o:(.data.rel.local+0x0): multiple definition of `at::native::DispatchStub<void (*)(at::Tensor&, at::Tensor const&), at::native::truncImpl>::AVX2'
CMakeFiles/caffe2.dir/__/aten/src/ATen/native/cpu/UnaryOpsKernel.cpp.DEFAULT.cpp.DEFAULT.cpp.DEFAULT.cpp.DEFAULT.cpp.AVX2.cpp.o:(.data.rel.local+0x0): first defined here
CMakeFiles/caffe2.dir/__/aten/src/ATen/native/cpu/UnaryOpsKernel.cpp.DEFAULT.cpp.DEFAULT.cpp.DEFAULT.cpp.AVX2.cpp.AVX2.cpp.o:(.data.rel.local+0x8): multiple definition of `at::native::DispatchStub<void (*)(at::Tensor&, at::Tensor const&), at::native::tanhImpl>::AVX2'
CMakeFiles/caffe2.dir/__/aten/src/ATen/native/cpu/UnaryOpsKernel.cpp.DEFAULT.cpp.DEFAULT.cpp.DEFAULT.cpp.DEFAULT.cpp.AVX2.cpp.o:(.data.rel.local+0x8): first defined here
//many errors like above
//many errors like above
collect2: error: ld returned 1 exit status
caffe2/CMakeFiles/caffe2.dir/build.make:58764: recipe for target 'lib/libcaffe2.so' failed
make[2]: *** [lib/libcaffe2.so] Error 1
CMakeFiles/Makefile2:1305: recipe for target 'caffe2/CMakeFiles/caffe2.dir/all' failed
make[1]: *** [caffe2/CMakeFiles/caffe2.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2

There are a lot of similar errors like this. According to the comments in https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/DispatchStub.h, this file is compiled for multiple times and then linked. But this information still cannot help me figure out how to fix it.

My environment:
OS: Ubuntu18.04 LTS (Windows Linux Subsystem)
Compiler: GCC7.3
CMake: 3.12
pytorch master: eeb1467

Is there any idea? Thanks very much!!

1 Like

I got different errors:

multiple definition of `omp_get_place_num'
FAILED: bin/dead_code_elim_test 
: && /usr/bin/c++  -fvisibility-inlines-hidden -fopenmp -DUSE_FBGEMM -O2 -fPIC -Wno-narrowing -Wall -Wextra -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -fdiagnostics-color=always -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -DHAVE_AVX_CPU_DEFINITION -DHAVE_AVX2_CPU_DEFINITION -O3  -rdynamic -L/usr/lib -pthread caffe2/CMakeFiles/dead_code_elim_test.dir/opt/dead_code_elim_test.cc.o  -o bin/dead_code_elim_test -L/media/jintain/sg/ai/libs/pytorch/third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib -Wl,-rpath,/media/jintain/sg/ai/libs/pytorch/third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib:/media/jintain/sg/ai/libs/pytorch/build_libtorch/build/lib:/usr/local/cuda/lib64: lib/libgtest_main.a -Wl,--no-as-needed,/media/jintain/sg/ai/libs/pytorch/build_libtorch/build/lib/libtorch.so -Wl,--as-needed lib/libprotobuf.a /usr/lib/x86_64-linux-gnu/libiomp5.so lib/libmkldnn.a -lmklml_intel ../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so lib/libc10_cuda.so lib/libc10.so /usr/local/cuda/lib64/libnvToolsExt.so /usr/local/cuda/lib64/libcudart.so /usr/local/cuda/lib64/libcufft.so /usr/local/cuda/lib64/libcurand.so /usr/local/cuda/lib64/libcudnn.so.7 /usr/local/cuda/lib64/libcublas.so lib/libgtest.a -lpthread && :
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_place_num'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_partition_num_places'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_partition_place_nums'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_max_task_priority_'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_place_num_procs'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_num_places_'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_place_proc_ids_'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_place_num_procs_'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_partition_place_nums_'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_partition_num_places_'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_max_task_priority'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_place_num_'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_num_places'
../../third_party/ideep/mkl-dnn/external/mklml_lnx_2019.0.3.20190220/lib/libiomp5.so:(*IND*+0x0): multiple definition of `omp_get_place_proc_ids'
collect2: error: ld returned 1 exit status
[2297/2451] Linking CXX executable bin/event_gpu_test

Could you try a clean build of libtorch following https://gist.github.com/mhubii/1c1049fb5043b8be262259efac4b89d5 ?