pytorch bug解决:AssertionError: CUDA unavailable, invalid device 0 requested

注意,该报错同样可以是AssertionError: CUDA unavailable, invalid device 0/1/2 requested,取决于用的那个device编号的显卡。

大致看了一遍
RuntimeError: CUDA error: no kernel image is available for execution on the driver, when use pytorch 1.7 on linux with RTX 3090 · Issue #49161 · pytorch/pytorch (github.com)
关于该issue的讨论后,推测也是由于torch、torchvision版本不对应或者torch、torchvision对应了,却跟CUDA版本不对应的问题。

torch、torchvision、python三者的适配版本对应地址

pytorch/vision: Datasets, Transforms and Models specific to Computer Vision (github.com)

torch和CUDA 环境的对应地址

Previous PyTorch Versions | PyTorch
PyTorch和CUDA版本对应关系 - 墨莲玦 - 博客园 (cnblogs.com)

情况描述

先:

python
import torch
torch.cuda.is_available()

大概率是False,否则不会报这个错。
我的GPU环境:


image.png

因此安装CUDA为11.5对应的torch、torchvision即可。
先卸载原版本:

pip uninstall torch torchvision

再按照上面github的issue思路,安装适配11.5 version CUDA的torch和torchvision(注意,本人没有用到和安装torchaudio):

pip install torch==1.11.0+cu115 torchvision==0.12.0+cu115 -f https://download.pytorch.org/whl/torch_stable.html

之后程序成功运行。可以再测试一下torch.cuda.is_available()是否为true.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容