Install LibTorch Error [PyTorch C++ API]

Hi,
I have Installed torch_nightly via pip3 command successfully. Now, I want to Install PyTorch C++ API (i.e., LibTorch). I have followed the instructions from Installing C++ Distributions of PyTorch exactly. However, the following error occurred after the cmake command on the build folder:

-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.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
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- 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  
-- Found CUDA: /usr/local/cuda (found suitable version "9.0", minimum required is "7.0") 
-- Caffe2: CUDA detected: 9.0
-- Caffe2: CUDA nvcc is: /usr/local/cuda/bin/nvcc
-- Caffe2: CUDA toolkit directory: /usr/local/cuda
-- Caffe2: Header version is: 9.0
-- Found CUDNN: /usr/local/cuda/include  
-- Found cuDNN: v7.1.2  (include: /usr/local/cuda/include, library: /usr/lib/x86_64-linux-gnu/libcudnn.so)
CMake Error at libtorch/share/cmake/Caffe2/public/utils.cmake:192 (cuda_select_nvcc_arch_flags):
  Unknown CMake command "cuda_select_nvcc_arch_flags".
Call Stack (most recent call first):
  libtorch/share/cmake/Caffe2/public/cuda.cmake:332 (torch_cuda_get_nvcc_gencode_flag)
  libtorch/share/cmake/Caffe2/Caffe2Config.cmake:88 (include)
  libtorch/share/cmake/Torch/TorchConfig.cmake:37 (find_package)
  CMakeLists.txt:4 (find_package)


-- Configuring incomplete, errors occurred!
See also "/data/Codes/CPP/ClionProjects/Torch1/build/CMakeFiles/CMakeOutput.log".
See also "/data/Codes/CPP/ClionProjects/Torch1/build/CMakeFiles/CMakeError.log".

Could anyone help me to address this issue?

I met the same problem and solved it via updating CMake to latest version.

2 Likes

Dear @kaixin,
Thank you very much. The problem has been addressed. I have updated the CMake (from 3.5.1 to 3.12.3 version) via this link. Then the problem has been addressed.
Just as a note, in some PyTorch docs, wrongly used this #include <torch/torch.h>. however, there isn’t any torch.h. So one can use #include <torch/script.h> instead.

Good to know your problem has been solved.

I am not sure what do you mean by "there isn’t any torch.h". I implemented a ResNet with #include <torch/torch.h> and everything seems to work fine. Is there anything I missed in the doc?

Dear @kaixin,
Thank you for your response. I have installed LibTorch successfully via CMake. But when I want to use LibTorch in another C++ project via #include <torch/torch.h> error occurs that there isn’t such header file. But when I have used #include <torch/script.h> then the code has been run correctly.
Actually I am newbie in using CMake. Maybe it is because I haven’t used CMake correctly. My used CMake is something like this (CMake for using LibTorch in another C++ projects):

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(Torch2)

find_package(Torch REQUIRED)

add_executable(Torch2 main.cpp)
include_directories("/data/Codes/CPP/ClionProjects/Torch1/libtorch/include/ATen")
target_link_libraries(Torch2 "/data/Codes/CPP/ClionProjects/Torch1/libtorch/lib/libtorch.so")
target_link_libraries(Torch2 "${TORCH_LIBRARIES}")
set_property(TARGET Torch2 PROPERTY CXX_STANDARD 11)

It is worth noting that, I have used Ubuntu 16.04 LTS, CUDA 9.0 and this my C++ code:

#include <torch/script.h>
#include <iostream>
using namespace std;

int main()
{
    std::cout << "Hello, World!" << std::endl;

    at::Tensor tensor = torch::rand({2, 3});
    std::cout << tensor << std::endl;
    
    return 0;
}

I will be appreciate you if tell me about incorrect things in the CMake file.

Sorry @ahkarami, I am also a newbie in using CMake (maybe C++ too). I just copy the basic CMakeLists.txt in the doc and it runs fine.

Never mind @kaixin.
Thank you for your response.

That’s because you didn’t use torch.h in this demo.
I am meeting the same error in another demo from https://github.com/iamhankai/cpp-pytorch
there’s no Tensor.h in libtorch/include/torch
Damn it and I really didn’t know how to go on…

2 Likes