USE_CUDNN always set to 0

Dear Developers:

I’m new to libtorch and I met a problem in setting the cudnn. I’m not sure what I did is wrong… :face_with_thermometer:

Environment:

  1. NVCC (/usr/local/cuda-12.1/bin/nvcc)
    Cuda compilation tools, release 12.1, V12.1.66
    Build cuda_12.1.r12.1/compiler.32415258_0

  2. CUDNN (/usr/include/cudnn.h)
    CUDNN_MAJOR 9

  3. cmake
    cmake version 3.28.1 (CMake; JetBrains IDE bundle; build 16)

  4. CLion
    2024.1.1

  5. Ubuntu
    22.04

I did these things below:

  1. Download libtorch, and unzip.

  2. Write some files:

  • CMakeLists.txt
cmake_minimum_required(VERSION 3.18)
project(Test)

set(CMAKE_CUDA_ARCHITECTURES "native")
set(PYTHON_EXECUTABLE "/usr/bin/python3")

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

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

# The following code block is suggested to be used on Windows.
# According to https://github.com/pytorch/pytorch/issues/25457,
# the DLLs need to be copied to avoid memory errors.
if (MSVC)
    file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
    add_custom_command(TARGET Test
            POST_BUILD
            COMMAND ${CMAKE_COMMAND} -E copy_if_different
            ${TORCH_DLLS}
            $<TARGET_FILE_DIR:example-app>)
endif (MSVC)
  • main.cpp
#include <torch/torch.h>
#include <iostream>

int main() {
    torch::Tensor tensor = torch::rand({2, 3});
    std::cout << tensor << std::endl;
    std::cout << torch::cuda::is_available();
}

  1. Set environment variables in CLion
CUDACXX=/usr/local/cuda-12.1/bin/nvcc;USE_CUDNN=1;CAFFE2_USE_CUDNN=1
  1. Set CMake Options
-DCMAKE_PREFIX_PATH=/path/to/libtorch

Results from CLion of cmake:

/snap/clion/275/bin/cmake/linux/x64/bin/cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_PREFIX_PATH=/path/to/libtorch -S /home/i/Documents/Test -B /home/i/Documents/Test/cmake-build-debug
-- The C compiler identification is GNU 11.4.0
-- The CXX compiler identification is GNU 11.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found CUDA: /usr/local/cuda (found version "12.1") 
-- The CUDA compiler identification is NVIDIA 12.1.66
-- Detecting CUDA compiler ABI info
-- Detecting CUDA compiler ABI info - done
-- Check for working CUDA compiler: /usr/local/cuda-12.1/bin/nvcc - skipped
-- Detecting CUDA compile features
-- Detecting CUDA compile features - done
-- Found CUDAToolkit: /usr/local/cuda-12.1/include (found version "12.1.66") 
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE  
-- Caffe2: CUDA detected: 12.1
-- Caffe2: CUDA nvcc is: /usr/local/cuda/bin/nvcc
-- Caffe2: CUDA toolkit directory: /usr/local/cuda
-- Caffe2: Header version is: 12.1
-- /usr/local/cuda/lib64/libnvrtc.so shorthash is d540eb83
-- USE_CUDNN is set to 0. Compiling without cuDNN support
-- USE_CUSPARSELT is set to 0. Compiling without cuSPARSELt support
-- Autodetected CUDA architecture(s):  7.5
-- Added CUDA NVCC flags for: -gencode;arch=compute_75,code=sm_75
-- Found Torch: /path/to/libtorch/lib/libtorch.so  
-- Configuring done (8.3s)
-- Generating done (0.0s)
-- Build files have been written to: /home/i/Documents/Test/cmake-build-debug

The USE_CUDNN still 0 even though I set USE_CUDNN=1;CAFFE2_USE_CUDNN=1. :exploding_head:

I have read https://discuss.pytorch.org/t/use-cudnn-set-to-zero-after-upgrade-to-libtorch-2-0/175413/4 and https://github.com/pytorch/pytorch/blob/main/cmake/public/cuda.cmake#L268 . I also tried libtorch 2.2.0. I tried to set environment variables in terminal. But it still not work.

Thank you for your patience to read my topic, and I would so appreciate if you could help me to solve this problem. Thanks! :smiling_face_with_three_hearts:

Could you check where your cuDNN libraries are located on your system?

Yes. cuDNN 9 is located in /usr/include/cudnn.h

That’s the header file not the library. Look for the .so files.

Thanks for your patience!
I found libcudnn.so in /usr/lib/x86_64-linux-gnu/libcudnn.so.
By the way, I install cudnn by deb package.

By adding “message(STATUS “CAFFE2_USE_CUDNN is: ${CAFFE2_USE_CUDNN}”)” in libtorch/share/cmake/Caffe2/public/cuda.cmake, I found CAFFE2_USE_CUDNN is None, even though I set CAFFE2_USE_CUDNN=1 in environment variables.

Finally, I add -DCAFFE2_USE_CUDNN=True in cmake option and make everything works fine.