【已解决】RuntimeError: PyTorch has CUDA version 12.1 and torch_sparse has CUDA version 11.8
【已解决】RuntimeError: PyTorch has CUDA version 12.1 and torch_sparse has CUDA version 11.8
问题描述
今天遇到这么一个问题:RuntimeError: Detected that PyTorch and torch_sparse were compiled with different CUDA versions. PyTorch has CUDA version 12.1 and torch_sparse has CUDA version 11.8. Please reinstall the torch_sparse that matches your PyTorch install.
详细内容如下:
/home/visionx/anaconda3/envs/gdem/lib/python3.8/site-packages/torch_geometric/typing.py:101: UserWarning: An issue occurred while importing 'torch-sparse'. Disabling its usage. Stacktrace: Detected that PyTorch and torch_sparse were compiled with different CUDA versions. PyTorch has CUDA version 12.1 and torch_sparse has CUDA version 11.8. Please reinstall the torch_sparse that matches your PyTorch install.
warnings.warn(f"An issue occurred while importing 'torch-sparse'. "
Traceback (most recent call last):
File "preprocess.py", line 7, in <module>
from dataset import get_dataset, get_largest_cc, get_eigh, Transd2Ind, DataGraphSAINT
File "/media/visionx/monica/project24/GDEM/dataset.py", line 23, in <module>
from utils import normalize_adj
File "/media/visionx/monica/project24/GDEM/utils.py", line 14, in <module>
from torch_sparse import SparseTensor
File "/home/visionx/anaconda3/envs/gdem/lib/python3.8/site-packages/torch_sparse/__init__.py", line 32, in <module>
raise RuntimeError(
RuntimeError: Detected that PyTorch and torch_sparse were compiled with different CUDA versions. PyTorch has CUDA version 12.1 and torch_sparse has CUDA version 11.8. Please reinstall the torch_sparse that matches your PyTorch install.
原因分析与解决
一开始看到这个的时候我是懵逼的,为什么呢?我明明按照requirements.txt
安装了
pip install -r requirements.txt
requirements.txt
内容如下:
deeprobust==0.2.9
gdown==4.7.3
networkx==3.2.1
numpy==1.26.3
ogb==1.3.6
pandas==2.1.4
scikit-learn==1.3.2
scipy==1.11.4
torch==2.1.2
torch_geometric==2.4.0
torch-sparse==0.6.18
但首先是报错如下图(解决办法可以看我的另一篇博客:点击这里):
解决完上面这个问题之后呢,又会出现新的问题:RuntimeError: PyTorch has CUDA version 12.1 and torch_sparse has CUDA version 11.8
,显然就是版本version
不对,但是为什么会这样呢?我们从pytorch
故那王找到了答案:
我们会发现同样是torch2.1.2
,但是却对应了不同的cuda版本
,也就是我们在pip install -r requirements.txt
的时候torch
里的2.1.2
并不是cuda11.8
的而是cuda12.1
的,这个大家也熟,pip install
一般就是说安装最新版本的,所以出了问题。那,怎么解决呢?
很多人的想法就是直接:
pip install torch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 --index-url https://download.pytorch.org/whl/cu118
但别忘了,pip install是安装最新的,这个版本比我们现在已有的旧,所以安装不上!!!
所以需要先卸载:
pip uninstall torch
然后再安装:
pip install torch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 --index-url https://download.pytorch.org/whl/cu118
这样就ok了!
相关链接
完结撒花
就让一切随风~
【已解决】RuntimeError: PyTorch has CUDA version 12.1 and torch_sparse has CUDA version 11.8