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


(Yizhi Wang) #1

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!!