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 ...)安装之后都不能使用,网上搜索资料发现有遇到的,没解决的.....废话不多说了
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) 版本
编译选项 链接器参数 备注 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.7 Debug/Release -INCLUDE:?warp_size@cuda@at@@YAHXZ 1.5 Debug/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();
}