Ubuntu20.04安装pytorch检查cuda

安装pytorch

pytorch官网:https://pytorch.org/get-started/locally/

pytorch

torch-torchvision-python版本对应关系

torch-torchvision-python

cuda-torch对应关系

cuda-torch

pip安装(推荐)

用anaconda安装torch。新建虚拟环境后,直接在pytorch官网官网链接找到“Install”按钮。这里一键搞定torch,torchvision,cudatoolkit等等,不需要另外安装cuda(笔者在没有单独安装CUDA情况下,成功运行了torch-gpu,很丝滑),并且版本都会自己对于对应好,原因是anaconda都集成在虚拟环境里面了,统一管理各个依赖包。有镜像源的情况下,去掉末尾的“-c pytorch”会更快,否则经常容易下载超时。不过conda下载超时时间可以设置。

默认cuda版本
pip3 install torch torchvision torchaudio
更改cuda版本
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116

如果不是用anaconda来管理环境,就要仔细对照torch、torchvision、cuda之间的对应关系。

下载安装包安装

安装包下载地址:https://download.pytorch.org/whl/torch_stable.html
根据cuda版本-torch版本-python版本找寻相应的下载文件,进行下载。然后到whl文件下载的位置,使用 pip install xxx.whl 进行安装即可。

安装完成后,我们要进行检查,以确保torch和cuda可以正常使用。

检查cuda

  1. 是否可以用显卡:
torch.cuda.is_available()

output

True
  1. 显卡个数:
torch.cuda.device_count()

output

1
  1. 显卡名字:
torch.cuda.get_device_name(0)

output

'NVIDIA GeForce RTX 3080'
  1. 测试cuda版本和gpu是否匹配
torch.zeros(1).cuda()

如果是不匹配的话,就会得到一开始的这个报错信息:

NVIDIA GeForce RTX 3080 with CUDA capability sm_86 is not compatible with the current PyTorch installation.
The current PyTorch install supports CUDA capabilities sm_37 sm_50 sm_60 sm_70.
If you want to use the NVIDIA GeForce RTX 3080 GPU with PyTorch, please check the instructions at https://pytorch.org/get-started/locally/

通过最近装cuda环境研究发下仅仅通过前三步命令是不能够测试cuda是否可以正常使用的,第四步才是最关键的。
解决方法:新建一个conda环境,使用pip安装pytorch时指定cuda版本:

pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113

这样安装出来cuda就是11.3的,可以正常使用RTX3080。
值得一提的是,若非新建conda环境而是在原有conda环境的基础上卸载pytorch再重新运行上面的指令进行安装,即使是cu113的也无法解决该错误。猜测是conda在本地缓存了torch的安装包文件而非从cu113的网址上重新下载。因此最好的解决方案是新建conda环境。
之后遇到了一个新的报错:

    model = PPO.load("/Volumes/easystore/rerun/pong_v1/policy")
  File "/Users/dd/miniconda3/envs/tf_new/lib/python3.8/site-packages/stable_baselines3/common/base_class.py", line 645, in load
    model._setup_model()
  File "/Users/dd/miniconda3/envs/tf_new/lib/python3.8/site-packages/stable_baselines3/ppo/ppo.py", line 152, in _setup_model
    self.clip_range = get_schedule_fn(self.clip_range)
  File "/Users/dd/miniconda3/envs/tf_new/lib/python3.8/site-packages/stable_baselines3/common/utils.py", line 88, in get_schedule_fn
    assert callable(value_schedule)
AssertionError

也就是在PPO加载模型的时候出现了一个从未见过的报错,于是搜了一波
见github issue:
https://github.com/DLR-RM/stable-baselines3/issues/403
简单来说,因为训练模型时的python版本和加载模型时的python版本不一致,所以在调用stable_baselines3的时候报错了。应该是用来保证训练环境和测试环境的一致性的。
解决方法:按照训练时的环境配置重新配置测试环境

卸载pytorch

使用conda卸载pytorch

conda uninstall pytorch
conda uninstall libtorch

使用pip卸载pytorch

pip uninstall torch torchvision torchaudio

reference
https://www.cnblogs.com/huangshiyu13/p/16467225.html
https://blog.csdn.net/qq_39763246/article/details/122250062
https://blog.csdn.net/mao_hui_fei/article/details/112078113

你可能感兴趣的:(Ubuntu20.04安装pytorch检查cuda)