问题描述
今天遇到这么一个问题: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了!
相关链接
Previous PyTorch Versions | PyTorchInstalling previous versions of PyTorchhttps://pytorch.org/get-started/previous-versions/conda install 和pip install有什么区别?-CSDN博客文章浏览阅读1k次,点赞3次,收藏12次。本篇为分享贴,截图部分选自,部分选自,文字内容是结合自己实践进行总结。_conda installhttps://blog.csdn.net/BetrayFree/article/details/132115996?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522171652690116800222883586%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=171652690116800222883586&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~rank_v31_ecpm-1-132115996-null-null.nonecase&utm_term=pip%20install&spm=1018.2226.3001.4450
完结撒花
就让一切随风~