LibTorch CMake build tutorial

dalseeroh: <p>Hi, I am trying this tutorial but having a difficulties building the C++ file. <a href="https://pytorch.org/cppdocs/installing.html" class="inline-onebox" rel="noopener nofollow ugc">Installing C++ Distributions of PyTorch — PyTorch main documentation</a></p>
<ol>
<li>I downloaded LibTorch from PyTorch website.</li>
<li>I wrote a simple C++ file (CNN_Cpp.cpp)</li>
</ol>
<pre><code class="lang-auto">#include &lt;torch/torch.h&gt;
#include &lt;iostream&gt;

int main() {
  torch::Tensor tensor = torch::rand({2, 3});
  std::cout &lt;&lt; tensor &lt;&lt; std::endl;
}
</code></pre>
<ol start="3">
<li>Wrote a CMakeLists.txt and saved in the same directory where CNN_Cpp.cpp is saved.</li>
</ol>
<pre><code class="lang-auto">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}
                     $&lt;TARGET_FILE_DIR:example-app&gt;)
endif (MSVC)
</code></pre>
<ol start="4">
<li>Now, my directory looks like below</li>
</ol>
<pre><code class="lang-auto">CNN_Cpp/
  CMakeLists.txt
  CNN_Cpp.cpp
  build/
    CMakeLists.txt
</code></pre>
<ol start="5">
<li>Opened Windows cmd and went to the build/ directory above and executed a command below.</li>
</ol>
<blockquote>
<p>C:\Users\eugene.roh\Desktop\CNN_Cpp\build&gt;cmake -DCMAKE_PREFIX_PATH=“C:\Users\eugene.roh\Downloads\libtorch-win-shared-with-deps-latest\libtorch”<br>
CMake Warning:<br>
No source or binary directory provided.  Both will be assumed to be the<br>
same as the current working directory, but note that this warning will<br>
become a fatal error in future CMake releases.</p>
<p>– Building for: Visual Studio 16 2019<br>
– Selecting Windows SDK version 10.0.19041.0 to target Windows 10.0.19042.<br>
– The C compiler identification is MSVC 19.29.30136.0<br>
– The CXX compiler identification is MSVC 19.29.30136.0<br>
– Detecting C compiler ABI info<br>
– Detecting C compiler ABI info - done<br>
– 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<br>
– Detecting C compile features<br>
– Detecting C compile features - done<br>
– Detecting CXX compiler ABI info<br>
– Detecting CXX compiler ABI info - done<br>
– 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<br>
– Detecting CXX compile features<br>
– Detecting CXX compile features - done<br>
– Looking for pthread.h<br>
– Looking for pthread.h - not found<br>
– Found Threads: TRUE<br>
– Found CUDA: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.5 (found version “11.5”)<br>
– Caffe2: CUDA detected: 11.5<br>
– Caffe2: CUDA nvcc is: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.5/bin/nvcc.exe<br>
– Caffe2: CUDA toolkit directory: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.5<br>
– Caffe2: Header version is: 11.5<br>
– Could NOT find CUDNN (missing: CUDNN_LIBRARY_PATH CUDNN_INCLUDE_PATH)<br>
CMake Warning at C:/Users/eugene.roh/Downloads/libtorch-win-shared-with-deps-latest/libtorch/share/cmake/Caffe2/public/cuda.cmake:111 (message):<br>
Caffe2: Cannot find cuDNN library.  Turning the option off<br>
Call Stack (most recent call first):<br>
C:/Users/eugene.roh/Downloads/libtorch-win-shared-with-deps-latest/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:88 (include)<br>
C:/Users/eugene.roh/Downloads/libtorch-win-shared-with-deps-latest/libtorch/share/cmake/Torch/TorchConfig.cmake:68 (find_package)<br>
CMakeLists.txt:4 (find_package)</p>
<p>Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings &gt; Manage App Execution Aliases.<br>
CMake Warning at C:/Users/eugene.roh/Downloads/libtorch-win-shared-with-deps-latest/libtorch/share/cmake/Caffe2/public/cuda.cmake:205 (message):<br>
Failed to compute shorthash for libnvrtc.so<br>
Call Stack (most recent call first):<br>
C:/Users/eugene.roh/Downloads/libtorch-win-shared-with-deps-latest/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:88 (include)<br>
C:/Users/eugene.roh/Downloads/libtorch-win-shared-with-deps-latest/libtorch/share/cmake/Torch/TorchConfig.cmake:68 (find_package)<br>
CMakeLists.txt:4 (find_package)</p>
<p>– Autodetected CUDA architecture(s):  7.5<br>
– Added CUDA NVCC flags for: -gencode;arch=compute_75,code=sm_75<br>
CMake Warning (dev) at C:/Users/eugene.roh/Downloads/libtorch-win-shared-with-deps-latest/libtorch/share/cmake/Caffe2/public/cuda.cmake:467 (if):<br>
Policy CMP0054 is not set: Only interpret if() arguments as variables or<br>
keywords when unquoted.  Run “cmake --help-policy CMP0054” for policy<br>
details.  Use the cmake_policy command to set the policy and suppress this<br>
warning.</p>
<p>Quoted variables like “MSVC” will no longer be dereferenced when the policy<br>
is set to NEW.  Since the policy is not set the OLD behavior will be used.<br>
Call Stack (most recent call first):<br>
C:/Users/eugene.roh/Downloads/libtorch-win-shared-with-deps-latest/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:88 (include)<br>
C:/Users/eugene.roh/Downloads/libtorch-win-shared-with-deps-latest/libtorch/share/cmake/Torch/TorchConfig.cmake:68 (find_package)<br>
CMakeLists.txt:4 (find_package)<br>
This warning is for project developers.  Use -Wno-dev to suppress it.</p>
<p>CMake Error at C:/Users/eugene.roh/Downloads/libtorch-win-shared-with-deps-latest/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:96 (message):<br>
Your installed Caffe2 version uses cuDNN but I cannot find the cuDNN<br>
libraries.  Please set the proper cuDNN prefixes and / or install cuDNN.<br>
Call Stack (most recent call first):<br>
C:/Users/eugene.roh/Downloads/libtorch-win-shared-with-deps-latest/libtorch/share/cmake/Torch/TorchConfig.cmake:68 (find_package)<br>
CMakeLists.txt:4 (find_package)</p>
<p>– Configuring incomplete, errors occurred!<br>
See also “C:/Users/eugene.roh/Desktop/CNN_Cpp/build/CMakeFiles/CMakeOutput.log”.<br>
See also “C:/Users/eugene.roh/Desktop/CNN_Cpp/build/CMakeFiles/CMakeError.log”.</p>
</blockquote>
<p>Question.<br>
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?</p>
<pre><code class="lang-auto">
cudnn-10.2-windows10-x64-v8.2.4.15.zip/
  cuda/
       bin/
       include/
       lib/
       NVIDIA_SLA_cuDNN_Support/
</code></pre>

dalseeroh: <p>Hello, I could finally solve the problem.</p>
<p>Firstly, I would like to correct the cmake command execution from</p>
<p><code>cmake -DCMAKE_PREFIX_PATH=“C:\Users\eugene.roh\Downloads\libtorch-win-shared-with-deps-latest\libtorch”</code></p>
<p>to</p>
<p><code>cmake -DCMAKE_PREFIX_PATH=“C:\Users\eugene.roh\Downloads\libtorch-win-shared-with-deps-latest\libtorch” ..</code></p>
<p>so that I do not have to copy the CMakeLists.txt file in the build/ folder.</p>
<p>After executing this command, I still get the error<br>
<code>Could NOT find CUDNN (missing: CUDNN_LIBRARY_PATH CUDNN_INCLUDE_PATH)</code></p>
<p>Solution:<br>
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.</p>
<blockquote>
<p>//Folder containing NVIDIA cuDNN</p>
<blockquote>
<p>CUDNN_ROOT:PATH=C:\Users\eugene.roh\Desktop\cnn_cpp\cudnn-10.2-windows10-x64-v8.2.4.15\cuda</p>
</blockquote>
</blockquote>
<p>and everything works like a charm.</p>