c++ - 带有 Cuda 的 LibTorch(C++) 引发异常

标签 c++ windows libtorch

我正在尝试使用 Cuda 10.1 和 Windows 10 使用 LibTorch 1.3 和 C++ 创建 NN。对于构建,我使用的是 Visual Studio 2019。

到目前为止,我尝试了基本示例和 MNIST example使用正在工作的 CPU。但是我不能用 CUDA 运行它。我尝试按照描述将模型移动到 GPU here ,但它不起作用。

To move your model to GPU memory, you can write model.to(at::kCUDA);. Make sure the inputs to a model are also living in CUDA memory by calling tensor.to(at::kCUDA), which will return a new tensor in CUDA memory.

所以我尝试了简单的

int main(){
    auto net = std::make_shared<Net>();
    net->to(torch::kCUDA); //crashes here
}

然后我尝试将简单的张量移动到 gpu 内存,但它也崩溃了。

#include <torch/torch.h>

int main() 
{
    torch::Tensor a = torch::ones({ 2, 2 }, torch::requires_grad());
    torch::Tensor b = torch::randn({ 2, 2 });
    a.to(torch::kCUDA);    //Here it crashes
    b.to(torch::kCUDA);    //
    auto c = a + b;
}

我得到了:

Exception thrown at 0x00007FFB8263A839 in Resnet50.exe: Microsoft C++ exception: c10::Error at memory location 0x000000E574979F30.
Unhandled exception at 0x00007FFB8263A839 in Resnet50.exe: Microsoft C++ exception: c10::Error at memory location 0x000000E574979F30.

在 Debug模式下,它指向 KernelBase.dll

auto operator()(Parameters... args) -> decltype(std::declval<FuncType>()(std::forward<Parameters>(args)...)) {
  return kernel_func_(std::forward<Parameters>(args)...);
}

使用 torch::cuda::is_available() 显示它可以找到 cuda 设备。

我对异常没有太多经验。

最佳答案

您好,我遇到了同样的问题。我已经通过安装 libtorch 9.2 版解决了这个问题。 我已经从这里下载了发行版 https://pytorch.org/ Cuda 工具包 9.2 和 cudnn 9.2。

我使用的是 Visual Studio 2017。

如果您有其他 cuda 版本,我建议您从控制面板卸载。

Cuda 工具包 https://developer.nvidia.com/cuda-92-download-archive?target_os=Windows&target_arch=x86_64&target_version=10

在 Windows 上安装 cudnn https://docs.nvidia.com/deeplearning/sdk/cudnn-install/index.html#install-windows我有版本 cudnn-9.2-windows10-x64-v7.6.5.32

在我用这个命令编译项目之后

cmake -DCMAKE_PREFIX_PATH=path\to\libtorch -Ax64 .. 制作-- build 。 --config 发布

在我的代码中我可以做到

testModel->to(torch::DeviceType::CUDA);

一定要在Release中编译

关于c++ - 带有 Cuda 的 LibTorch(C++) 引发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58879638/

相关文章:

c++ - std::end 如何知道数组的结尾?

c++ - istream_iterator cin 初始化等待输入

c++ - Eclipse C++ 单元测试快捷方式

c - 运行此代码片段需要哪些头文件?

c++ - 如何使用 C++ 和 Windows API 将击键/消息发送到 Windows 7?

c++ - Raspberry 上的 Libtorch 无法加载 pt 文件但在 ubuntu 上工作

c++ - 如何在 LibTorch (C++) 中将 torch::Torch 的类型从 Float 更改为 Long

c++ - C++ 中的全局动态 Allcoated 变量

linux - 如何处理共享文件/区域的页面错误