最近研究图神经网络,搞了几个conda环境,其中主要用到的库是torch、torch_geometric
dgl、ogb这几个库,我们根据git项目的引导安装时一般都不会报错,但是程序启动的使用就会报如下常见错误
ImportError: libc10_cuda.so: cannot open shared object file: No such file or directory
OSError: libcusparse.so.11: cannot open shared object file: No such file or directory
OSError: libcusparse.so.12: cannot open shared object file: No such file or directory
经过我的摸索,上述错误的原因都是因为torch-gpu版被torch-cpu版覆盖了
举个例子,比如说conda环境的安装指令如下
conda install pytorch==2.0.0 cudatoolkit=11.8 -c pytorch
conda install -c pyg pytorch-sparse
conda install -c pyg pytorch-scatter
conda install -c pyg pytorch-cluster
conda install -c pyg pyg
pip install ogb
conda install -c dglteam/label/cu118 dgl
pip install transformers
pip install --upgrade accelerate
pip install openai
pip install langchain
pip install gensim
pip install google-generativeai
pip install -U sentence-transformers
pip install editdistance
pip install InstructorEmbedding
pip install optuna
pip install tiktoken
pip install pytorch_warmup
这个指令能看torch和cuda的安装情况
python -c "import torch; print('torch={}, cuda={}'.format(torch.__version__, torch.cuda.is_available()))"
conda install pytorch==2.0.0 cudatoolkit=11.8 -c pytorch
# 安装pytorch 一般是不会报错的,用上面的指令会显示
torch=2.0.0+cu118, cuda=True
conda install -c pyg pytorch-sparse
# 当你安装过后再次查看的话
torch=2.0.0+cu118, cuda=False
这样后面无论再怎么安,都是错的了
分析:当你安装torch-sparse的时候,conda会提示你需要安装哪些前置库,如下图
最搞的来了,红圈里居然还需要安装pytorch???还是cpu版本??? 这说明刚才安装的pytorch-gpu根本就没检测到啊 如果你还是安装了,那么torch-cpu就会覆盖torch-gpu,导致错误。
解决方法
# 先正常安装
conda install pytorch==2.0.0 cudatoolkit=11.8 -c pytorch
conda install -c pyg pytorch-sparse
# 如果此时torch=2.0.0+cu118, cuda=True,那就继续安装,后面不用看了
先把pytorch-sparse安装上,
# 如果torch=2.0.0+cu118, cuda=False,证明torch被覆盖了,
# 将两次的pytorch都卸载掉,一个gpu,一个cpu
pip uninstall torch
pip uninstall torch
# 最后再重新装gpu版
conda install pytorch==2.0.0 cudatoolkit=11.8 -c pytorch
# 再检查一下pytorch-sparse,这样应该就没错了
conda install -c pyg pytorch-sparse
目前没啥更好的办法,真是蛋疼,为什么会找不到pytorch呢?要是我说的有错或者有更好的办法一定告诉我!