在Google Colab运行PyTorch时遭遇cuDNN版本不兼容错误
Google Colab中PyTorch的cuDNN版本不兼容问题解决方法
自昨日起,在Google Colab中使用GPU运行PyTorch时出现cuDNN版本不兼容错误,此前操作一直正常。尝试安装不同版本PyTorch后仍出现其他错误。
版本检查代码
# 使用PyTorch检查版本、CUDA版本和cuDNN版本 import torch print("PyTorch version: ") print(torch.__version__) print("CUDA Version: ") print(torch.version.cuda) print("cuDNN version is: ") print(torch.backends.cudnn.version())
执行结果
PyTorch version:
1.12.0+cu113
CUDA Version:
11.3
cuDNN version is:
--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-23-93b5c974c4be> in <module>() 8 print(torch.version.cuda) 9 print("cuDNN version is: ") ---> 10 print(torch.backends.cudnn.version()) 1 frames /usr/local/lib/python3.7/dist-packages/torch/backends/cudnn/__init__.py in version() 48 def version(): 49 """Returns the version of cuDNN""" ---> 50 if not _init(): 51 return None 52 return __cudnn_version /usr/local/lib/python3.7/dist-packages/torch/backends/cudnn/__init__.py in _init() 39 raise RuntimeError( 40 'cuDNN version incompatibility: PyTorch was compiled against {} ' ---> 41 'but linked against {}'.format(compile_version, runtime_version)) 42 return True 43 else: RuntimeError: cuDNN版本不兼容:PyTorch编译时依赖的版本为(8, 3, 2),但运行时链接的版本为(8, 0, 5)
解决方案
方法1:重置Colab环境
Colab基础环境可能因自动更新导致依赖冲突,重置可恢复默认兼容配置:
- 点击界面顶部「Runtime」→「Factory reset runtime」
- 重置后重新运行代码,默认的PyTorch与CUDA/cuDNN版本会保持匹配
方法2:手动安装匹配版本的PyTorch
当前环境cuDNN版本为8.0.5、CUDA为11.3,安装编译时依赖cuDNN 8.0.x的PyTorch版本:
!pip uninstall -y torch torchvision torchaudio !pip install torch==1.11.0+cu113 torchvision==0.12.0+cu113 torchaudio==0.11.0+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html
安装完成后重新运行版本检查代码,确认cuDNN版本正常输出。
方法3:临时禁用cuDNN(应急用)
仅需临时运行代码时,可禁用cuDNN(会降低GPU性能):
import torch torch.backends.cudnn.enabled = False
内容的提问来源于stack exchange,提问作者Katarzyna B
相关产品推荐
相关产品推荐

