LibTorch CMake build tutorial

Hi, I am trying this tutorial but having a difficulties building the C++ file. Installing C++ Distributions of PyTorch β€” PyTorch main documentation

  1. I downloaded LibTorch from PyTorch website.
  2. I wrote a simple C++ file (CNN_Cpp.cpp)
#include <torch/torch.h>
#include <iostream>

int main() {
  torch::Tensor tensor = torch::rand({2, 3});
  std::cout << tensor << std::endl;
}
  1. Wrote a CMakeLists.txt and saved in the same directory where CNN_Cpp.cpp is saved.
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example-app)

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

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

# 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 example-app
                     POST_BUILD
                     COMMAND ${CMAKE_COMMAND} -E copy_if_different
                     ${TORCH_DLLS}
                     $<TARGET_FILE_DIR:example-app>)
endif (MSVC)
  1. Now, my directory looks like below
CNN_Cpp/
  CMakeLists.txt
  CNN_Cpp.cpp
  build/
    CMakeLists.txt
  1. Opened Windows cmd and went to the build/ directory above and executed a command below.

C:\Users\eugene.roh\Desktop\CNN_Cpp\build>cmake -DCMAKE_PREFIX_PATH=β€œC:\Users\eugene.roh\Downloads\libtorch-win-shared-with-deps-latest\libtorch”
CMake Warning:
No source or binary directory provided. Both will be assumed to be the
same as the current working directory, but note that this warning will
become a fatal error in future CMake releases.

– Building for: Visual Studio 16 2019
– Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19042.
– The C compiler identification is MSVC 19.29.30136.0
– The CXX compiler identification is MSVC 19.29.30136.0
– Detecting C compiler ABI info
– Detecting C compiler ABI info - done
– Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - 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: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
– Detecting CXX compile features
– Detecting CXX compile features - done
– Looking for pthread.h
– Looking for pthread.h - not found
– Found Threads: TRUE
– Found CUDA: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.5 (found version β€œ11.5”)
– Caffe2: CUDA detected: 11.5
– Caffe2: CUDA nvcc is: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.5/bin/nvcc.exe
– Caffe2: CUDA toolkit directory: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.5
– Caffe2: Header version is: 11.5
– Could NOT find CUDNN (missing: CUDNN_LIBRARY_PATH CUDNN_INCLUDE_PATH)
CMake Warning at C:/Users/eugene.roh/Downloads/libtorch-win-shared-with-deps-latest/libtorch/share/cmake/Caffe2/public/cuda.cmake:111 (message):
Caffe2: Cannot find cuDNN library. Turning the option off
Call Stack (most recent call first):
C:/Users/eugene.roh/Downloads/libtorch-win-shared-with-deps-latest/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:88 (include)
C:/Users/eugene.roh/Downloads/libtorch-win-shared-with-deps-latest/libtorch/share/cmake/Torch/TorchConfig.cmake:68 (find_package)
CMakeLists.txt:4 (find_package)

Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
CMake Warning at C:/Users/eugene.roh/Downloads/libtorch-win-shared-with-deps-latest/libtorch/share/cmake/Caffe2/public/cuda.cmake:205 (message):
Failed to compute shorthash for libnvrtc.so
Call Stack (most recent call first):
C:/Users/eugene.roh/Downloads/libtorch-win-shared-with-deps-latest/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:88 (include)
C:/Users/eugene.roh/Downloads/libtorch-win-shared-with-deps-latest/libtorch/share/cmake/Torch/TorchConfig.cmake:68 (find_package)
CMakeLists.txt:4 (find_package)

– Autodetected CUDA architecture(s): 7.5
– Added CUDA NVCC flags for: -gencode;arch=compute_75,code=sm_75
CMake Warning (dev) at C:/Users/eugene.roh/Downloads/libtorch-win-shared-with-deps-latest/libtorch/share/cmake/Caffe2/public/cuda.cmake:467 (if):
Policy CMP0054 is not set: Only interpret if() arguments as variables or
keywords when unquoted. Run β€œcmake --help-policy CMP0054” for policy
details. Use the cmake_policy command to set the policy and suppress this
warning.

Quoted variables like β€œMSVC” will no longer be dereferenced when the policy
is set to NEW. Since the policy is not set the OLD behavior will be used.
Call Stack (most recent call first):
C:/Users/eugene.roh/Downloads/libtorch-win-shared-with-deps-latest/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:88 (include)
C:/Users/eugene.roh/Downloads/libtorch-win-shared-with-deps-latest/libtorch/share/cmake/Torch/TorchConfig.cmake:68 (find_package)
CMakeLists.txt:4 (find_package)
This warning is for project developers. Use -Wno-dev to suppress it.

CMake Error at C:/Users/eugene.roh/Downloads/libtorch-win-shared-with-deps-latest/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:96 (message):
Your installed Caffe2 version uses cuDNN but I cannot find the cuDNN
libraries. Please set the proper cuDNN prefixes and / or install cuDNN.
Call Stack (most recent call first):
C:/Users/eugene.roh/Downloads/libtorch-win-shared-with-deps-latest/libtorch/share/cmake/Torch/TorchConfig.cmake:68 (find_package)
CMakeLists.txt:4 (find_package)

– Configuring incomplete, errors occurred!
See also β€œC:/Users/eugene.roh/Desktop/CNN_Cpp/build/CMakeFiles/CMakeOutput.log”.
See also β€œC:/Users/eugene.roh/Desktop/CNN_Cpp/build/CMakeFiles/CMakeError.log”.

Question.
It seems like I need cuDNN. I downloaded a .zip file from NVidia website. Now what should I do to make it work? Or, is there any way that I can do cuDNN with anaconda?


cudnn-10.2-windows10-x64-v8.2.4.15.zip/
  cuda/
       bin/
       include/
       lib/
       NVIDIA_SLA_cuDNN_Support/

Hello, I could finally solve the problem.

Firstly, I would like to correct the cmake command execution from

cmake -DCMAKE_PREFIX_PATH=β€œC:\Users\eugene.roh\Downloads\libtorch-win-shared-with-deps-latest\libtorch”

to

cmake -DCMAKE_PREFIX_PATH=β€œC:\Users\eugene.roh\Downloads\libtorch-win-shared-with-deps-latest\libtorch” ..

so that I do not have to copy the CMakeLists.txt file in the build/ folder.

After executing this command, I still get the error
Could NOT find CUDNN (missing: CUDNN_LIBRARY_PATH CUDNN_INCLUDE_PATH)

Solution:
Navigate build/ folder and you will find the CMakeCache.txt file. Inside the file, search CUDNN_ROOT:PATH= and fill the path with the downloaded NVIDIA cudnn upzipped directory.

//Folder containing NVIDIA cuDNN

CUDNN_ROOT:PATH=C:\Users\eugene.roh\Desktop\cnn_cpp\cudnn-10.2-windows10-x64-v8.2.4.15\cuda

and everything works like a charm.

2 Likes