libtorch 无法检测到 cuda,而 cuda 运行时可以

问题描述 投票:0回答:1
#include <cuda_runtime.h>
#include <torch/torch.h>
#include <iostream>

int main() {
    std::cout << "CUDA device count: " << torch::cuda::device_count() << std::endl;
    std::cout << "CUDA is available: " << (torch::cuda::is_available() ? "Yes" : "No") << std::endl;
    std::cout << "cuDNN is available: " << (torch::cuda::cudnn_is_available() ? "Yes" : "No") <<        std::endl;
    int deviceCount;
    cudaGetDeviceCount(&deviceCount);
    std::cout << "Number of CUDA devices: " << deviceCount << std::endl;
    return 0;
}

CUDA 设备数量:0 CUDA 是否可用: 否 cuDNN 是否可用: 否 CUDA 设备数量:1

运行时似乎可以工作,火炬不会显示cuda 我正在使用 msvc 2022、windows 11。我安装了最新的 pytorch 并支持 cuda libtorch-win-shared-with-deps-2.4.0+cu124.zip(我也尝试过发布),我的 nvcc --version 显示:

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2024 NVIDIA Corporation
Built on Fri_Jun_14_16:44:19_Pacific_Daylight_Time_2024
Cuda compilation tools, release 12.6, V12.6.20
Build cuda_12.6.r12.6/compiler.34431801_0

我尝试了不同版本的 pytorch 但没有成功。我可以使用 pytorch 从 python 代码访问 cuda,没有任何问题,但我需要在 c++ 中访问它

c++ pytorch cuda torch
1个回答
0
投票

手动加载 torch_cuda.dll 确实解决了问题:

    HMODULE torchCudaDll = LoadLibraryA("torch_cuda.dll");
    if (torchCudaDll == NULL) {
        std::cerr << "Failed to load torch_cuda.dll. Error code: " << GetLastError() << std::endl;

}
    else {
        std::cout << "Successfully loaded torch_cuda.dll" << std::endl;
        //  free the library when you're done:
        // FreeLibrary(torchCudaDll);
    }

显然这是一个老错误

© www.soinside.com 2019 - 2024. All rights reserved.