pytorch / pytorch

Tensors and Dynamic neural networks in Python with strong GPU acceleration
https://pytorch.org
Other
83.38k stars 22.49k forks source link

Caffe2 CMake exported target has hard-coded path under linux #33928

Closed cmpute closed 4 years ago

cmpute commented 4 years ago

🐛 Bug

Caffe2Targets.cmake from pytorch installed by conda has hard-coded library path, which will make targets depending on torch unable to build, if the CUDA installation directory is different from /usr/local/cuda. Specifically in my case, I installed pytorch py3.6_cuda9.2.148_cudnn7.6.3_0 and the Caffe2 target file contains:

set_target_properties(torch PROPERTIES
  INTERFACE_COMPILE_DEFINITIONS "_THP_CORE;AT_PARALLEL_OPENMP=1"
  INTERFACE_COMPILE_OPTIONS "-Wall;-Wextra;-Wno-unused-parameter;-Wno-missing-field-initializers;-Wno-write-strings;-Wno-unknown-pragmas;-Wno-missing-braces;-fopenmp"
  INTERFACE_INCLUDE_DIRECTORIES "${_IMPORT_PREFIX}/include;${_IMPORT_PREFIX}/include"
  INTERFACE_LINK_LIBRARIES "protobuf::libprotobuf;c10;Threads::Threads;caffe2::mkl;caffe2::mkldnn;torch::cudart;c10_cuda;/usr/local/cuda/lib64/libnvToolsExt.so;/usr/local/cuda/lib64/libcudart.so;caffe2::cufft;caffe2::curand;caffe2::cudnn;/usr/local/cuda/lib64/libculibos.a;dl;caffe2::cublas"
)

To Reproduce

I'm working on an example for pytorch with CMake, you can find it here: https://github.com/cmpute/pytorch-cmake-example.

If I remove the hard-coded libraries in Caffe2Targets.cmake, then the compilation of my extension will succeed.

Expected behavior

The example is fairly simple, but when you try to build the package, there will be an error:

...
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jacobz/pytorch-cmake-example/_skbuild/linux-x86_64-3.6/cmake-build
ninja: error: '/usr/local/cuda/lib64/libnvToolsExt.so', needed by 'lltm_ext.cpython-36m-x86_64-linux-gnu.so', missing and no known rule to make it
Traceback (most recent call last):
  File "/home/jacobz/.conda/envs/lidar/lib/python3.6/site-packages/skbuild/setuptools_wrap.py", line 577, in setup
    cmkr.make(make_args, env=env)
  File "/home/jacobz/.conda/envs/lidar/lib/python3.6/site-packages/skbuild/cmaker.py", line 482, in make
    os.path.abspath(CMAKE_BUILD_DIR())))

An error occurred while building with CMake.
  Command:
    "cmake" "--build" "." "--target" "install" "--config" "Release" "--"
  Source directory:
    /home/jacobz/pytorch-cmake-example
  Working directory:
    /home/jacobz/pytorch-cmake-example/_skbuild/linux-x86_64-3.6/cmake-build
Please see CMake's output for more information.

Environment

This issue is also reported here: https://github.com/facebookresearch/DensePose/issues/185

vincentqb commented 4 years ago

Thanks for reporting this. Would you be interested in opening a pull request?

cmpute commented 4 years ago

@vincentqb actually I have no experience with Caffe2, so I don't know which part in the CMakeLists.txt of Caffe2 lead to this. Anyone could help?

cmpute commented 4 years ago

And I'm also a little bit confused, why is the exported torch target depends heavily on Caffe2 targets?

prclibo commented 4 years ago

Met similar issue. This hard coded target link will results in problems when compiling pytorch extension with custom cuda location.

For example, I installed pytorch and cudatookit via conda. So I compile my custom pytorch extension with CUDA from cudatookit (not in /usr/local/cuda). But the compiler will try to link /usr/local/cuda/lib64/libcudart.so which does not exist.