You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows10 Ubuntu Shell运行深度学习算法遇CUDA及PyTorch报错求助

解决PyTorch CUDA相关的两个常见错误

针对你在Linux和Windows环境下遇到的不同CUDA错误,我分别给出具体的排查和解决步骤:

一、Linux环境:CUDA驱动版本不足错误

你遇到的错误:

THCudaCheck FAIL file=/pytorch/torch/csrc/cuda/Module.cpp line=34 error=35 : CUDA driver version is insufficient for CUDA runtime version
Traceback (most recent call last):
File "train_nli.py", line 61, in
torch.cuda.set_device(params.gpu_id)
...
RuntimeError: cuda runtime error (35) : CUDA driver version is insufficient for CUDA runtime version at /pytorch/torch/csrc/cuda/Module.cpp:34

这个错误的核心是你安装的PyTorch所依赖的CUDA Runtime版本,高于当前GPU驱动支持的最高CUDA版本。更新驱动无效可能是你装的驱动版本仍未匹配要求,或者驱动安装不彻底。

解决步骤:

  1. 先确认你的PyTorch对应的CUDA Runtime版本:
    打开Python终端,运行:
    import torch
    print(torch.version.cuda)
    
    会得到类似11.7这样的版本号。
  2. 查找该CUDA版本对应的最低驱动版本:
    比如CUDA 11.7需要驱动版本≥515.43.04,CUDA 11.8需要≥520.61.05,驱动版本必须满足这个最低要求才能适配对应的CUDA Runtime。
  3. 彻底卸载并重装对应版本的GPU驱动:
    • Ubuntu系统下,先卸载现有驱动:
      sudo apt purge nvidia*
      sudo apt autoremove
      
    • 下载对应版本的.run驱动文件(NVIDIA官网可获取),关闭图形界面后安装:
      sudo init 3
      sudo chmod +x NVIDIA-Linux-x86_64-xxx.xx.run
      sudo ./NVIDIA-Linux-x86_64-xxx.xx.run --no-x-check --no-nouveau-check --no-opengl-files
      
    • 安装完成后重启,用nvidia-smi命令确认驱动版本是否符合要求。
  4. 替代方案:如果不想更换驱动,可以降级PyTorch到与当前驱动匹配的版本:
    比如当前驱动支持CUDA 11.3,就安装对应版本的PyTorch:
    pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cu113
    

二、Windows环境:PyTorch无CUDA支持错误

你遇到的错误:

Traceback (most recent call last):
File "train_nli.py", line 61, in
torch.cuda.set_device(params.gpu_id)
...
AttributeError: module 'torch._C' has no attribute '_cuda_setDevice'

这个错误很明确:你安装的是纯CPU版本的PyTorch,完全没有CUDA相关的功能模块。

解决步骤:

  1. 先卸载现有的CPU版PyTorch:
    如果用pip安装的:
    pip uninstall torch torchvision torchaudio
    
    如果用conda安装的:
    conda uninstall torch torchvision torchaudio
    
  2. 安装带CUDA支持的PyTorch版本:
    针对CUDA 11.8的pip安装命令示例:
    pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
    
    conda安装命令示例:
    conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
    
  3. 验证安装是否成功:
    打开Python终端,运行:
    import torch
    print(torch.cuda.is_available())
    
    如果输出True,说明CUDA功能已经正常启用。

通用检查点

  • 确认你的显卡是NVIDIA且支持CUDA:AMD显卡目前需要额外配置ROCm环境,PyTorch默认不支持。
  • Linux下不需要单独安装CUDA Toolkit:PyTorch自带了CUDA Runtime,只要驱动版本匹配即可。
  • Windows下要注意Python版本与PyTorch版本的兼容性,比如Python 3.11需要PyTorch 1.13及以上版本。

内容的提问来源于stack exchange,提问作者JakobThePharao

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:36:11