现象
在使用Torch时报错RuntimeError: CUDA error: no kernel image is available for execution on the device
,具体代码如下:
import torch
torch.cuda.is_availabel() # 显示:True
torch.ones(1).cuda()
打印信息如下:
/xx/xx/anaconda3/envs/xx/lib/python3.8/site-packages/torch/cuda/__init__.py:146: UserWarning:
NVIDIA GeForce RTX 3060 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_61 sm_70 sm_75 compute_37.
If you want to use the NVIDIA GeForce RTX 3060 GPU with PyTorch, please check the instructions at https://pytorch.org/get-started/locally/
原因
打印信息的字面意思是PyTorch版本不支持当前显卡的算力。
我的显卡是GTX3060,算力8.6(可以在nvidia官网查询)
我的PyTorch版本是1.12.1,显然是老版本了。
解决
Step1. 卸载torch
pip uninstall torch torchvision torchaudio
Step2. 在torch官网下载更高或更低的版本,以匹配显卡(CUDA)算力
pip install torch==2.0.0+cu117 torchvision==0.15.1+cu117 torchaudio==2.0.1 --index-url https://download.pytorch.org/whl/cu117
# 我的情况是选择安装torch2.0,因为不匹配CUDA10.2,先重新安装了更高版本的nvidia显卡驱动与CUDA11.7,然后再安装了torch2.0。
最后解决了该问题。
PS:关于如何在Ubuntu上安装nvidia显卡驱动、CUDA、cudnn,请移步我的其他文章。