Windows下PyTorch(LibTorch)配置cuda加速

PyTorch is not linked with support for cuda devices (getDeviceGuardImpl at C:\w\b\windows\pytorch\c10/core/impl/DeviceGuardImplInterface.h:216)
(no backtrace available)

?set_fw_grad@AutogradMeta@autograd@torch@@UEAAXAEBVTensor@at@@0_K_N@Z

Could not run 'aten::empty_strided' with arguments from the 'CUDA' backend. This could be because the operator doesn't exist for this .....

LibTorch 1.10,LibTorch 1.9.1,LibTorch 1.8.1,LibTorch 1.8.1,LibTorch 1.7.0,LibTorch1.6.0 

PyTorch 在Windows下配置cuda加速似乎变得有些诡异

测试1.4,1.5.0,1.5.1,nightly,python(pip install ...)安装之后都不能使用,网上搜索资料发现有遇到的,没解决的.....废话不多说了

2023-4-28更新,增加2.0支持

2021-7-30更新,增加1.9支持

2021-5-30更新,增加1.8.1支持(博主1.8.0出现找不到?set_fw_grad@AutogradMeta@autograd@torch@@UEAAXAEBVTensor@at@@0_K_N@Z,于是跳过...)

2020-9-6更新:增加1.6版本支持

如果方法好用,请点赞收藏支持一下,如果不好用,请在评论区告诉我

链接器选项中添加以下内容(各版本有所不同),不熟悉的小伙伴们可以参考填写位置如下图(以1.5版本为例)

PyTorch

(LibTorch) 版本

编译选项链接器参数备注
2.0

Release正常

Debug未测

-INCLUDE:?ignore_this_library_placeholder@@YAHXZ

依赖的torch_cuda_cpp.lib被取消

与1.13参数一致

1.13(评论区贡献)release -INCLUDE:?ignore_this_library_placeholder@@YAHXZ

1.11(评论区贡献)

1.10

1.9/1.9.1 

Release正常

Debug未测

-INCLUDE:?warp_size@cuda@at@@YAHXZ 是的,1.6-1.7的参数又行了
1.8.1

Debug/Release

有2套参数,部分版本组合可能需要使用备选(便于显示加了换行,粘贴时需去除)

-INCLUDE:?wait@Future@ivalue@c10@@QEAAXXZ 

静态库换成了torch_cuda_cpp.lib,torch_cuda_cu.lib

-INCLUDE:?mutate@OptOutMutator@cuda@

fuser@jit@torch@@UEAAPEAVStatement@

2345@PEAVForLoop@kir@2345@@Z 

1.6/1.7Debug/Release-INCLUDE:?warp_size@cuda@at@@YAHXZ 
1.5Debug/Release-INCLUDE:THCudaCharTensor_zero

参数填写示意

填写该参数后需要链接torch_cuda.lib(1.8以下版本)文件,随后就能体验飞一般的速度提升啦.

附:

cuda测试代码(1.8及以下版本使用)

torch::Device deviceGPU(torch::kCUDA);
torch::Device deviceCPU(torch::kCPU);

struct Net : torch::nn::Module {
    Net(int64_t N, int64_t M) {
        W = register_parameter("W", torch::randn({ N, M }));
        b = register_parameter("b", torch::randn(M));
    }
    torch::Tensor forward(torch::Tensor input) {
        return torch::addmm(b, input, W);
    }
    torch::Tensor W, b;
};
void testCuda() {
    Net lmodule(4096, 4096);

    try
    {
        torch::Tensor tensor = torch::eye(4096, torch::kFloat).to(deviceGPU);
        lmodule.to(deviceGPU);
        for (size_t i = 0; i < 1024 * 64; i++)
            lmodule.forward(tensor);
        //tensor1* tensor2;
    }
    catch (const std::exception& ex)
    {
        std::cout << ex.what();
    }
    getchar();
}

评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值