Torch::cuda::is_available() is throwing compilation error

I am trying out the c++ frontend with a simple program.
Compilation of this code block is failing -

    torch::DeviceType device_type;
    if (torch::cuda::is_available())
        std::cout << "CUDA is available!" << std::endl;
        device_type = torch::kCUDA;

And throws this error -

error: ‘is_available’ is not a member of ‘at::cuda’
     if (torch::cuda::is_available())

Any idea what might be wrong here?

1 Like

hm that is strange. Can you please provide your CMakeLists.txt and the command with which you are trying to build your code? Where did you get your libtorch from?

CMakeList is a minimal:


find_package(Torch REQUIRED)

add_executable(jit_minimal jit_minimal.cpp)
target_link_libraries(jit_minimal "${TORCH_LIBRARIES}")
set_property(TARGET jit_minimal PROPERTY CXX_STANDARD 11)

Command used for build -

mkdir -p build/
cd build/
# cmake -DCMAKE_PREFIX_PATH=~/libs/libtorch/ ..
cmake -DCMAKE_PREFIX_PATH=~/libs/nightly/libtorch/ ..

And I got the libtorch using the download url on pytorch website.

that should work, please provide your full jit_minimal.cpp
what version of CUDA do you have?

I met this error too, any idea?

Including the following should solve it.
#include <torch/torch.h>

You are possibly only including #include <torch/script.h>


Thanks @pratyush911. That was the solution.

Thanks for the information. It is really helpful for us. Please keep suggesting such a content.