PyTorch无法识别CUDA(错误803):Ubuntu系统驱动组合不兼容问题
问题重现
Python 3.10.6 (main, Nov 14 2022, 16:10:14) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.import torch
torch.cuda.is_available()
[PATH]/.local/lib/python3.10/site-packages/torch/cuda/init.py:88: UserWarning: CUDA initialization: Unexpected error from cudaGetDeviceCount(). Did you run some cuda functions before calling NumCudaDevices() that might have already set an error? Error 803: system has unsupported display driver / cuda driver combination (Triggered internally at ../c10/cuda/CUDAFunctions.cpp:109.)
return torch._C._cuda_getDeviceCount() > 0
False
问题原因
核心矛盾是PyTorch依赖的CUDA runtime与系统NVIDIA驱动版本不兼容:你安装的PyTorch基于CUDA 11.7编译,但系统当前驱动仅支持CUDA 11.5,驱动版本未达到CUDA 11.7的最低要求,导致CUDA初始化失败。
对应版本要求:
- CUDA 11.5 最低驱动版本:495.29.05
- CUDA 11.7 最低驱动版本:515.43.04
解决方案(二选一)
方案一:升级NVIDIA驱动适配CUDA 11.7
- 卸载现有NVIDIA驱动
sudo apt purge nvidia* sudo apt autoremove sudo apt autoclean - 添加NVIDIA官方驱动PPA源
sudo add-apt-repository ppa:graphics-drivers/ppa sudo apt update - 安装适配CUDA 11.7的515系列驱动
sudo apt install nvidia-driver-515 - 重启系统后,执行
nvidia-smi确认驱动版本≥515.43.04,再启动Python测试torch.cuda.is_available()。
方案二:降级PyTorch适配系统CUDA 11.5
- 卸载当前PyTorch包
pip3 uninstall torch torchvision torchaudio - 安装适配CUDA 11.5的PyTorch版本(对应Python3.10)
pip3 install torch==1.13.1+cu115 torchvision==0.14.1+cu115 torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/cu115 - 安装完成后启动Python,执行
torch.cuda.is_available()验证是否返回True。
额外注意事项
nvidia-smi显示的是驱动支持的最高CUDA版本,nvcc --version是系统安装的CUDA Toolkit版本,PyTorch仅要求驱动版本满足其依赖的CUDA runtime最低要求,无需完全匹配系统CUDA Toolkit版本。- 若使用conda环境,需切换到对应环境后执行卸载和安装命令。
内容的提问来源于stack exchange,提问作者Raiyan Chowdhury

