Unable to build CUDA libtorch

Hi all,

I followed the tutorial of libtorch https://pytorch.org/cppdocs/installing.html but using a CUDA 12.4 version libtorch instead of CPU. However I failed to build the code using the CMakeLists provided by the tutorial and here is the error:

cmake -DCMAKE_PREFIX_PATH=/usr/include/libtorch ..
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- 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
-- Found CUDA: /usr/local/cuda (found version "12.6") 
-- The CUDA compiler identification is NVIDIA 12.6.77
-- Check for working CUDA compiler: /usr/local/cuda/bin/nvcc
-- Check for working CUDA compiler: /usr/local/cuda/bin/nvcc -- works
-- Detecting CUDA compiler ABI info
-- Detecting CUDA compiler ABI info - done
-- Found CUDAToolkit: /usr/local/cuda/include (found version "12.6.77") 
-- 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  
-- Caffe2: CUDA detected: 12.6
-- Caffe2: CUDA nvcc is: /usr/local/cuda/bin/nvcc
-- Caffe2: CUDA toolkit directory: /usr/local/cuda
-- Caffe2: Header version is: 12.6
-- Found Python: /usr/bin/python3.8 (found version "3.8.10") found components: Interpreter 
CMake Warning at /usr/include/libtorch/share/cmake/Caffe2/public/cuda.cmake:140 (message):
  Failed to compute shorthash for libnvrtc.so
Call Stack (most recent call first):
  /usr/include/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:86 (include)
  /usr/include/libtorch/share/cmake/Torch/TorchConfig.cmake:68 (find_package)
  CMakeLists.txt:5 (find_package)


-- Could NOT find nvtx3 (missing: nvtx3_dir) 
CMake Warning at /usr/include/libtorch/share/cmake/Caffe2/public/cuda.cmake:180 (message):
  Cannot find NVTX3, find old NVTX instead
Call Stack (most recent call first):
  /usr/include/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:86 (include)
  /usr/include/libtorch/share/cmake/Torch/TorchConfig.cmake:68 (find_package)
  CMakeLists.txt:5 (find_package)


-- USE_CUDNN is set to 0. Compiling without cuDNN support
-- USE_CUSPARSELT is set to 0. Compiling without cuSPARSELt support
-- USE_CUDSS is set to 0. Compiling without cuDSS support
-- USE_CUFILE is set to 0. Compiling without cuFile support
-- Autodetected CUDA architecture(s):  7.5 7.5
-- Added CUDA NVCC flags for: -gencode;arch=compute_75,code=sm_75
-- Found Torch: /usr/include/libtorch/lib/libtorch.so  
-- Configuring done
-- Generating done

My CMakeLists:

cmake_minimum_required(VERSION 3.16 FATAL_ERROR)

project(libtorch_example)

find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(example example.cpp)
target_link_libraries(example "${TORCH_LIBRARIES}")
set_property(TARGET example PROPERTY CXX_STANDARD 17)

The PC’s driver version: 550.127.08 and CUDA Version: 12.4.

I am new to c++ and libtorch and have no idea how to fix it. Any suggestions are appreciated! Thank you!

What’s the error? It seems the config step was done so did you try to build it afterwards?

Hi, the errors seem to be Failed to compute shorthash for libnvrtc.so and Cannot find NVTX3, find old NVTX instead, how can I fix them? But as you said I build it afterwards and it successed. Thank you!

Hey,

Failed to compute shorthash for libnvrtc.so

this should not affect the compiling and it is that CMake does not recognize “Python::Interpreter”.

Regarding the “NVTX3”, you can find the solution at Libtorch, Cannot find NVTX3, find old NVTX instead - #2 by weitaoliu.

Also, according to your log, the CUDA version is 12.6 not 12.4.

Best, Weitao.

Thank you Weitao. I thought it was an error but turned out to be fine. Anyway thank you for your help!