2.torch、torchvision、cuda之间的对应关系
1.问题分析
3090显卡在使用cuda == 10.2,torch == 1.9.0,torchvision == 0.10.0时,报错提示:
【1】NVIDIA GeForce RTX 3090 with CUDA capability sm_86 is not compatible with the current PyTorch installation.
The current PyTorch install supports CUDA capabilities sm_37 sm_50 sm_60 sm_70.
【2】RuntimeError: CUDA error: no kernel image is available for execution on the device
CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect.
翻译一下【1】:RTX 3090的算力是8.6,但是当前的PyTorch依赖的CUDA版本支持的算力只有3.7、5.0、6.0、7.0、7.5。
由于cuda版本是直接对应电脑显卡的,因此上面表面上是说pytorch版本不对应,但实际上是pytorch依赖的cuda版本的问题。
而根据对应的算力关系,算力为8.x的显卡使用的cuda版本应该 >= 11.0。因此需要重新安装cuda和对应cuda可用版本的pytorch。
2.torch、torchvision、cuda之间的对应关系
三者的对应关系可以参考这个。下面是常见版本的对应关系图:
【注】cuda是向下兼容的,因此可以选择 >= 项目要求版本的cuda。但是也不建议直接上最新版,因为可能找不到最新版cuda对应的pytorch。
3.重装torch、torchvision、cuda
- 打开终端,激活需要修改的虚拟环境
conda activate 虚拟环境名
- 卸载原本的torch和torchvision
pip uninstall torch
pip uninstall torchvision
- 重新安装 torch、torchvision、cuda
安装方法参考这个。该方法超级好用,可以一次性安装好cuda(包含cudnn)、torch、torchvision。我下载的是cuda == 11.1,torch == 1.9.0,torchvision == 0.10.0,参考指令如下:
pip3 install torch==1.9.0+cu111 torchvision==0.10.0+cu111 torchaudio==0.9.0 -f https://download.pytorch.org/whl/torch_stable.html
4.检测torch、cuda、cudnn安装是否成功
- 激活虚拟环境后进入python
conda activate 虚拟环境名
python
- 导入torch,并查看torch版本
import torch
print(torch.__version__) # 如果显示版本号,则安装成功
- 检查cuda是否安装成功
print(torch.version.cuda) # 如果显示版本号,则说明成功
- 检查cudnn是否安装成功
print(torch.backends.cudnn.version()) # 如果显示版本号,则说明成功
- 检查cuda和GPU是否匹配
torch.cuda.is_available() # 如果显示True,则说明cuda和GPU是匹配的