ubuntu中安装pytorch版本与 RTX3090(4090) 对应关系

在RTX 3090 上判断,当前版本的的torch版本是否可以用,一般需要通过如下方式:

  1. conda activate torch1.8.1 ( 激活相关的虚拟环境)
  2. python 进入python 环境, import torch 导入torch 安装包;
  3. 测试 torch.cuda.is_available(),
  4. 测试 torch.zeros(1).cuda()

直到,第四步骤完成,才能说明当前版本的cuda 可以调用当前版本的pytorch;

问题的关键点:

  1. 安装pytorch 过程中, 需要两个注意点, 一个是当前安装的pytorch 版本, 该pytroch 版本官网指定包含了哪几个cuda 版本;
  2. 使用pip install torch==1.8.1 的方式安装,默认的是torch 版本+ 当前主机上的cuda 版本
  3. 可能出现的问题, 当前主机的cuda 版本 不兼容该torch版本中官方发布的几个cuda 版本;

1. 问题现象

>>> torch.zeros(1).cuda()
/home/respecting/anaconda3/envs/torch1.8.1/lib/python3.7/site-packages/torch/cuda/__init__.py:104: UserWarning: 
NVIDIA GeForce RTX 3090 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 3090 GPU with PyTorch, please check the instructions at https://pytorch.org/get-started/locally/

1.1 问题分析

表明当前的安装的pytorch 版本没有匹配上合适的cuda,  即当前pytorch 版本的 cuda 版本没有对应到自己主机上,安装的cuda 版本,

pytorch 环境中安装的cuda 版本, 需要满足以下两个条件:

  1. 当前pytorch版本的算力支持 当前机器上显卡的算力;
  2. pytorch  中的 cuda 版本 不能高于当前机器上已经安装的 cuda 版本;

具体讲来, 同一个pytorch 版本,比如 pytorch 1.8.1 会对应到不同版本的 cuda

# ROCM 4.0.1 (Linux only)
pip install torch==1.8.1+rocm4.0.1 torchvision==0.9.1+rocm4.0.1 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html

# ROCM 3.10 (Linux only)
pip install torch==1.8.1+rocm3.10 torchvision==0.9.1+rocm3.10 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html

# CUDA 11.1
pip install torch==1.8.1+cu111 torchvision==0.9.1+cu111 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html

# CUDA 10.2
pip install torch==1.8.1+cu102 torchvision==0.9.1+cu102 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html

# CUDA 10.1
pip install torch==1.8.1+cu101 torchvision==0.9.1+cu101 torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html

# CPU only
pip install torch==1.8.1+cpu torchvision==0.9.1+cpu torchaudio==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html

1.2 解决问题

pytorch 环境中安装的cuda 版本, 需要满足以下两个条件:

  1. 当前pytorch版本的算力支持 当前机器上显卡的算力;
  2. pytorch 中的 cuda 版本不能高于当前机器上已经安装的 cuda 版本;

知道了问题的原因之后, 我们便可以解决了:

  1. RTX3090 至少需要cuda 11.1 版本,才能够驱动该设备, 故我们可以安装cuda11.1 以上版本
  2. 所以在想要安装的 pytorch 版本中,找到大于  cuda11.1 <= pytorch-cuda --version <=  当前机器上安装的 cuda --version

由于笔者机器上安装的是 cuda11.2 , 而3090对应的cuda 版本必须大于等于cuda11.1,
故安装pytorch 1.8.1 中的 cuda11.1 版本,  卸载重新安装对应版本;

 pip install -i https://pypi.douban.com/simple torch-1.8.1+cu111-cp37-cp37m-linux_x86_64.whl 
Looking in indexes: https://pypi.douban.com/simple
Processing ./torch-1.8.1+cu111-cp37-cp37m-linux_x86_64.whl
Requirement already satisfied: numpy in /home/respecting/anaconda3/envs/torch1.8.1/lib/python3.7/site-packages (from torch==1.8.1+cu111) (1.21.6)
Requirement already satisfied: typing-extensions in /home/respecting/anaconda3/envs/torch1.8.1/lib/python3.7/site-packages (from torch==1.8.1+cu111) (4.2.0)
Installing collected packages: torch
  Attempting uninstall: torch
    Found existing installation: torch 1.8.1
    Uninstalling torch-1.8.1:
      Successfully uninstalled torch-1.8.1
Successfully installed torch-1.8.1+cu111

torchvision install

(torch1.8.1) respecting@respecting-B360M-GAMING-HD:/media/respecting/Ubuntu 18.0/June18$  pip install -i https://pypi.douban.com/simple torch-1.8.1+cu111-cp37-cp37m-linux_x86_64.whl 
Looking in indexes: https://pypi.douban.com/simple
Processing ./torch-1.8.1+cu111-cp37-cp37m-linux_x86_64.whl
Requirement already satisfied: numpy in /home/respecting/anaconda3/envs/torch1.8.1/lib/python3.7/site-packages (from torch==1.8.1+cu111) (1.21.6)
Requirement already satisfied: typing-extensions in /home/respecting/anaconda3/envs/torch1.8.1/lib/python3.7/site-packages (from torch==1.8.1+cu111) (4.2.0)
Installing collected packages: torch
  Attempting uninstall: torch
    Found existing installation: torch 1.8.1
    Uninstalling torch-1.8.1:
      Successfully uninstalled torch-1.8.1
Successfully installed torch-1.8.1+cu111

1.2 重新测试

(torch1.8.1) respecting@respecting-B360M-GAMING-HD:~$ python
Python 3.7.13 (default, Mar 29 2022, 02:18:16) 
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.zeros(1).cuda()
tensor([0.], device='cuda:0')
>>> 

3090Ti 系列 nvidia 显卡驱动

Version:	515.57
Release Date:	2022.6.28
Operating System:	Linux 64-bit
Language:	English (US)
File Size:	346.53 MB

2. 4090 GPU 与cuda 之间版本匹配

这里给出,GPU 与 cuda, cudnn 之间的关系,

具体的环境的搭建参考这里,
安装步骤

2.1GPU 与 cuda, cudnn 之间的关系

https://docs.nvidia.com/deeplearning/cudnn/support-matrix/index.html#fntarg_4

在这里插入图片描述

从这里可以看出:官方已经规定了 基于 Ada Lovelace 的 4090 显卡, 要求cuda >= 11.8

而截至到 2023. 06.10 , Pytorch=2.0.1 官方版本最高支持到 cuda 11.8。
在这里插入图片描述

2.2cuda, cudnn 之间的关系

Collecting nvidia-cuda-cupti-cu11==11.7.101
  Downloading https://pypi.doubanio.com/packages/e6/9d/dd0cdcd800e642e3c82ee3b5987c751afd4f3fb9cc2752517f42c3bc6e49/nvidia_cuda_cupti_cu11-11.7.101-py3-none-manylinux1_x86_64.whl (11.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 11.8/11.8 MB 1.2 MB/s eta 0:00:00
INFO: pip is looking at multiple versions of <Python from Requires-Python> to determine which version is compatible with other requirements. This could take a while.
INFO: pip is looking at multiple versions of torch to determine which version is compatible with other requirements. This could take a while.
ERROR: Cannot install torch==2.0.0 and torchvision==0.15.0 because these package versions have conflicting dependencies.

The conflict is caused by:
    The user requested torch==2.0.0
    torchvision 0.15.0 depends on torch==2.0.0+cu117

To fix this you could try to:
1. loosen the range of package versions you've specified
2. remove package versions to allow pip attempt to solve the dependency conflict

ERROR: ResolutionImpossible: for help visit https://pip.pypa.io/en/latest/topics/dependency-resolution/#dealing-with-dependency-conflicts
### 安装 PyTorch 以支持 RTX 4090 GPU 为了在配备 NVIDIA RTX 4090 的环境中成功安装 PyTorch 并充分利用其硬件加速功能,以下是详细的配置方法: #### 环境准备 确保操作系统已更新至最新状态并满足最低要求。对于 Ubuntu 用户,建议使用较新的 LTS 版本(如 Ubuntu 20.04 或更高)。此外,确认驱动程序版本兼容 CUDA 和 RTX 4090。 ```bash sudo apt update && sudo apt upgrade -y ``` #### 安装 NVIDIA 驱动 RTX 4090 要求最新的 NVIDIA 显卡驱动程序以及对应的 CUDA 工具链。可以通过官方 PPA 添加并安装适合的驱动版本。 ```bash sudo add-apt-repository ppa:graphics-drivers/ppa sudo apt update sudo apt install nvidia-driver-535 ``` 完成驱动安装后重启计算机以加载新驱动。 #### 设置 CUDA 支持 PyTorch 提供预编译好的二进制文件来适配不同版本的 CUDA。由于 RTX 4090 基于 Ampere 架构,推荐使用 CUDA 11.x 或更高级别的版本[^1]。 验证当前系统中的 CUDA 是否可用: ```bash nvcc --version ``` 如果未检测到有效 CUDA,则需手动安装它。通过运行以下命令获取最新版 CUDA Toolkit: ```bash wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-keyring_1.0-1_all.deb sudo dpkg -i cuda-keyring_1.0-1_all.deb sudo apt-get update sudo apt-get -y install cuda ``` #### 创建 Conda 环境 创建一个新的虚拟环境可以避免其他项目发生冲突,并简化依赖管理过程。 ```bash conda create -n rtx4090_env python=3.9 conda activate rtx4090_env ``` #### 安装基础库 除了核心框架外,还需要一些辅助软件包以便更好地开发深度学习模型。 ```bash conda install numpy ninja pyyaml mkl mkl-include setuptools cmake cffi typing_extensions future six requests dataclasses ``` 针对特定 GPU 加速需求可能还需额外引入 MAGMA 库作为底层线性代数运算的支持组件。 ```bash conda install -c pytorch magma-cuda117 # 替换为匹配实际使用的 CUDA 版本号 ``` #### 下载安装 PyTorch 访问 [PyTorch官网](https://pytorch.org/get-started/locally/) 获取定制化安装指令。通常情况下可以直接复制粘贴对应选项下的终端脚本执行即可。 假设目标平台基于 Linux (CUDA-enabled),则采用如下方式快速部署稳定发行版: ```bash pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117 ``` 注意这里指定了 `cu117` 参数表示启用 CUDA 11.7 功能集;具体数值应依据前序步骤所选定之 CUDA 版本来调整。 最后测试一下是否能够正常调用 GPU 设备资源: ```python import torch print(torch.cuda.is_available()) # 输出 True 表明成功识别显卡 print(torch.__version__) # 查看当前激活的 PyTorch 版本信息 ``` --- ###
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值