【Pytorch环境配置—Linux/Windows】: cuda、cuDNN、pytorch、torchvision、torchaudio 详细安装教程(报错解决、安装慢慢慢)
文章目录
0. 相关报错
- Error: The NVIDIA driver on your system is too old (found version 11040).
Please update your GPU driver by downloading and installing a new
version from the URL: http://www.nvidia.com/Download/index.aspx
Alternatively, go to: https://pytorch.org to install
a PyTorch version that has been compiled with your version
of the CUDA driver. - UserWarning: Failed to load image Python extension: libc10_cuda.so: cannot open shared object file: No such file or directory
warn(f"Failed to load image Python extension: {e}") - AttributeError: module ‘torch._C’ has no attribute ‘_cuda_setDevice’
这些个问题的原因基本都是 cuda 版本与 torch*不匹配,或者 torch与torchvision 不匹配等,它们的解决办法都一样。
- 建议重新创建一个环境(当然你也可以卸载所有与torch相关的依赖包,然后在原有环境上修复)
- 接下来按照下面的思路。
1. 创建新环境并激活
conda create -n test python=3.8
conda activate test (source activate test)
2. 查看机器支持的CUDA版本
- nvidia-smi
该电脑可以支持最高CUDA版本为11.7,驱动可以向下兼容,所以小于等于11.7的CUDA版本都可以安装。
3. 安装 cuda、cuDNN、pytorch、torchvision、torchaudio(两种方式)
3.1 pip 安装 (这个我跑通了,所以推荐这种方法)
可以直接运行这条命令下载PyTorch相应版本:
pip install torch==x.x.x+cu113 torchvision==x.x.x+cu113 torchaudio==x.x.x+cu113 -f \
https://download.pytorch.org/whl/torch_stable.html
因为我的CUDA版本用的11.4,但是下面3.3中没有找到11.4相关的,由于向下兼容,我选择了11.3。所以下载CUDA版本需要加上+cu113,但是有的版本没有区分CPU和CUDA,不需要加+cu113(其他版本类似,如+cu111等),建议先加上,不行再去掉。
- 对应的 x.x.x 参考 3.3 内容进行选择,选择之后如下
- 觉得下载的很慢,可以加个 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install torch==1.10.0+cu113 torchvision==0.11.1+cu113 torchaudio==0.10.0+cu113 -f \
https://download.pytorch.org/whl/torch_stable.html -i https://pypi.tuna.tsinghua.edu.cn/simple
- 如果对于torchaudio没需求,可以把它去掉。
3.2 conda 安装
这个貌似不用加上cu113–默认cuda版本(好像不太确定),使用 conda 安装CUDA Toolkit、PyTorch、torchvision和torchaudio:
conda install torch==x.x.x torchvision==x.x.x torchaudio==x.x.x cudatoolkit=x.x -c python
如果想用国内镜像,conda需要添加通道,推荐使用清华源:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --set show_channel_urls yes
然后把上边的命令后的-c python删除:
conda install torch==x.x.x torchvision==x.x.x torchaudio==x.x.x cudatoolkit=x.x
而对应的 x.x.x 参考 下面 3.3 内容 进行选择。
3.3 版本匹配选择
- CUDA与cuDNN对应版本,参考来源:cuDNN Archive
- cuda、cudatoolkit 与 torch
- PyTorch与torchvision、Python对应版本,参考来源:Pytorch
- PyTorch与torchaudio、Python对应版本,参考来源:github
参考
【1】https://blog.csdn.net/qq_42026580/article/details/126538010