RTX 5090 CUDA 错误解决方案

报错信息

RuntimeError: CUDA error: no kernel image is available for execution on the device
Compile with `TORCH_USE_CUDA_DSA` to enable device-side assertions.

1. 报错原因

问题根源

RTX 5090 显卡使用的是 Blackwell 架构,计算能力为 sm_120 (12.0),这是一个非常新的 GPU 架构。而稳定版的 PyTorch 通常只支持到 sm_90 或更早的计算能力,因此无法在 RTX 5090 上运行。

技术细节

  • RTX 5090 计算能力: sm_120 (12.0)
  • 我项目中使用的稳定版 PyTorch 支持: 只到 sm_90 (9.0)
  • 问题: PyTorch 预编译的 CUDA kernels 不包含 sm_120 的代码,导致无法在 RTX 5090 上执行

验证方法

运行以下命令可以验证问题:

python -c "import torch; print(f'Compute Capability: {torch.cuda.get_device_capability(0)}'); print(f'CUDA Arch: {torch.cuda.get_arch_list()}')"

如果看到警告信息:

NVIDIA GeForce RTX 5090 D with CUDA capability sm_120 is not compatible with the current PyTorch installation.
The current PyTorch install supports CUDA capabilities sm_50 sm_60 sm_70 sm_75 sm_80 sm_86 sm_90.

说明当前 PyTorch 版本不支持 RTX 5090。

2. 解决方案

方案:安装 PyTorch Nightly 版本

PyTorch Nightly 版本通常包含对最新 GPU 架构的支持。使用以下命令安装:

# 激活 conda 环境
# 卸载旧版本(可选,如果已安装)
pip uninstall torch torchvision torchaudio -y

# 安装 PyTorch Nightly 版本(使用清华镜像源加速)
pip install --pre torch torchvision torchaudio \
  -f https://mirrors.tuna.tsinghua.edu.cn/pytorch-wheels/nightly/cu126/torch_nightly.html \
  --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple

验证安装

安装完成后,运行以下命令验证:

python -c "import torch; print(f'PyTorch: {torch.__version__}'); print(f'CUDA available: {torch.cuda.is_available()}'); print(f'GPU: {torch.cuda.get_device_name(0)}'); print(f'Compute Capability: {torch.cuda.get_device_capability(0)}')"

如果不再出现警告信息,且 Compute Capability 显示 (12, 0),说明安装成功。

测试 GPU 是否可用

python -c "import torch; x = torch.randn(3, 3).cuda(); print('GPU test passed!')"

如果没有报错,说明 GPU 可以正常使用。

3. 运行环境

硬件配置

  • GPU: NVIDIA GeForce RTX 5090 D
  • GPU 架构: Blackwell
  • 计算能力: sm_120 (12.0)
  • 显存: 32GB

环境检查命令

# 检查 GPU 信息
nvidia-smi

# 检查 PyTorch 和 CUDA
python -c "import torch; print(f'PyTorch: {torch.__version__}'); print(f'CUDA: {torch.version.cuda}'); print(f'GPU: {torch.cuda.get_device_name(0)}')"

# 检查计算能力
python -c "import torch; print(f'Compute Capability: {torch.cuda.get_device_capability(0)}')"

注意事项

  1. Nightly 版本稳定性: Nightly 版本是开发版本,可能不如稳定版稳定,但通常包含对最新硬件的支持。

  2. 版本更新: 当 PyTorch 官方发布支持 sm_120 的稳定版本后,建议升级到稳定版。

相关链接

更新日志

  • 2025-11-22: 初始版本,记录 RTX 5090 CUDA 错误解决方案
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐