본문 바로가기

A.I.

libtorch CUDA GPU 인식 안된 경우 해결방법 "libtorch: torch::cuda::is_available()"

현상
 

visual studio 2019에서 libtorch cuda 버전을 정상적으로 link했음에도 불구하고 CUDA 인식이 안되었음.

std::cout << torch::cuda::is_available() << std::endl;
0

 

해결방법

아래와 같이 Windows.h을 include해서 해결함.

#include <ATen/ATen.h>
#include <torch/torch.h>
#include <iostream>
#include <Windows.h>

int main() {
    LoadLibraryA("torch_cuda.dll");
    try {    
        std::cout << torch::cuda::is_available() << std::endl;
        torch::Tensor tensor = at::tensor({ -1, 1 }, at::kCUDA);
    }
    catch (exception& ex) {
        std::cout << ex.what() << std::endl;
    }
}

 

참고

https://github.com/pytorch/pytorch/issues/72396