美文网首页
Ubuntu PyTorch 配置GPU环境

Ubuntu PyTorch 配置GPU环境

作者: 程点 | 来源:发表于2022-07-09 18:44 被阅读0次

本文主要讲ubuntu18.04配置NVIDIA GPU环境, 并安装配置PyTorch。

先确认GPU型号

方式一:

$ lspci | grep -i nvidia
01:00.0 VGA compatible controller: NVIDIA Corporation Device 249d (rev a1)
01:00.1 Audio device: NVIDIA Corporation Device 228b (rev a1)

方式二:

$ ubuntu-drivers devices
== /sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0 ==
modalias : pci:v000010DEd0000249Dsv00001D05sd00001147bc03sc00i00
vendor   : NVIDIA Corporation
manual_install: True
driver   : nvidia-driver-510 - distro non-free
driver   : nvidia-driver-510-server - distro non-free
driver   : nvidia-driver-515-server - distro non-free
driver   : nvidia-driver-515 - distro non-free
driver   : nvidia-driver-470-server - distro non-free
driver   : nvidia-driver-470 - distro non-free recommended
driver   : xserver-xorg-video-nouveau - distro free builtin

方式二中, driver : nvidia-driver-470 - distro non-free recommended便是系统推荐的的GPU 驱动版本。

安装驱动

只安装驱动

直接安装推荐的版本:

sudo apt install nvidia-driver-470

驱动 + cuda 安装

一般我们做深度学习使用nvidia gpu都需要安装cuda,安装cuda之前一定要确认依赖cuda的版本,比如当前最新版的PyTorch支持到的cuda版本为11.6, 而cuda最新版已经到11.6了,故要安装对应的版本。

我们根据英伟达官网链接 安装。

根据需要选择适合的cuda版本,点击链接进入安装, 此处我选择11.6。


nvidia-cuda-install-01.jpg

点击链接之后进入下一步,依次选择操作系统、架构、发行版(版本)、安装方式后,便给出的安装信息。
ubuntu安装方式栏有三个选项,三个我都试过,没特殊原因推荐使用runfile(local)方式,我在使用deb(local)deb(network)安装的时候,会直接安装最新版的cuda,而我需要确定的一个版本11.6。

此处我选择"Linux-x86_64-Ubuntu-18.04-runfile(local)"

nvidia-cuda-install-02.jpg

复制安装命令依次执行:

$ wget https://developer.download.nvidia.com/compute/cuda/11.6.2/local_installers/cuda_11.6.2_510.47.03_linux.run
$ sudo sh cuda_11.6.2_510.47.03_linux.run
===========
= Summary =
===========

Driver:   Installed
Toolkit:  Installed in /usr/local/cuda-11.6/

Please make sure that
 -   PATH includes /usr/local/cuda-11.6/bin
 -   LD_LIBRARY_PATH includes /usr/local/cuda-11.6/lib64, or, add /usr/local/cuda-11.6/lib64 to /etc/ld.so.conf and run ldconfig as root

To uninstall the CUDA Toolkit, run cuda-uninstaller in /usr/local/cuda-11.6/bin
To uninstall the NVIDIA Driver, run nvidia-uninstall
Logfile is /var/log/cuda-installer.log

安装完成后根据提示配置环境变量

# cuda
export PATH="/usr/local/cuda-11.6/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda-11.6/lib64:$LD_LIBRARY_PATH"

重启系统

使用以上两种方式安装都需要重启系统

$ sudo reboot

查看安装是否成功:

$ nvidia-smi
Sat Jul  9 07:04:15 2022
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 510.47.03    Driver Version: 510.47.03    CUDA Version: 11.6     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ...  Off  | 00000000:01:00.0 Off |                  N/A |
| N/A   52C    P0    35W /  N/A |      5MiB /  8192MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A      1140      G   /usr/lib/xorg/Xorg                  4MiB |
+-----------------------------------------------------------------------------+

安装PyTorch

安装基础包

我一般使用conda管理python环境, 安装PyTorch之前先创建一个命名为dl-gpu的python环境:

$ conda create -y -n dl-gpu python=3.8

激活环境

$ conda activate dl-gpu

然后开始安装PyTorch,
首先进入官网安装链接, 依次选择PyTorch BuildYour OSPackageLanguageCompute Platform

pytorch-install-01.jpg

此处我选择的是"Stable(1.12.0)-Linux-Conda-Python-CUDA11.6",得到执行的命令

$ conda install pytorch torchvision torchaudio cudatoolkit=11.6 -c pytorch -c conda-forge

注意点:

  1. 如果是conda安装,官网有一个提示 NOTE: 'conda-forge' channel is required for cudatoolkit 11.6, 需要先安装conda-forge, 安装可参考链接
  2. cuda版本与PyTorch支持的版本要尽量一致

验证安装

执行上述安装命令

$ conda install pytorch torchvision torchaudio cudatoolkit=11.6 -c pytorch -c conda-forge

验证是否可用:

(dl-gpu) ➜  ~ python -c "import torch;print(torch.cuda.device_count())"
1

这里输出1则说明GPU配置成功了。

安装Jupyter

个人比较喜欢使用JupyterLab,所以本次安装JupyterLab, 安装细节见文档

使用conda安装

$ conda install -c conda-forge jupyterlab

使用pip安装

pip install jupyterlab

启动Jupyter并验证Torch和GPU

$ jupyter-lab
import torch
# 查看显卡数量
print('GPU count: ', torch.cuda.device_count())
# 获取第一个GPU
gpu1 = torch.device(f'cuda:0')
print('GPU one: ', gpu1)

# 创建使用GPU的张量X, Y
X = torch.rand(2, 3, device=gpu1)
Y = torch.rand(3, 4, device=gpu1)
# 分别打印X、Y、以及X和Y的乘积
X, Y, torch.mm(X, Y)
pytorch-gpu-py-example.jpg

其中device='cuda:0'表示当前张量数据存储在第0个GPU上。

常见问题

常见问题1: 重启系统后无法连接驱动了

$ nvidia-smi
NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.

大概率是更新Linux内核引起的,

  1. 先检查驱动和cuda版本
$ nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Tue_Mar__8_18:18:20_PST_2022
Cuda compilation tools, release 11.6, V11.6.124
Build cuda_11.6.r11.6/compiler.31057947_0

说明驱动和和cuda都是存在的。

  1. 查看已经安装的驱动的版本信息
$ ls /usr/src|grep nvidia
nvidia-510.47.03

这里我的驱动版本是: nvidia-510.47.03

  1. 使用dkms工具安装驱动
$ sudo apt install dkms
$ sudo dkms install -m nvidia -v 510.47.03
...
nvidia-peermem.ko:
Running module version sanity check.
 - Original module
   - No original module exists within this kernel
 - Installation
   - Installing to /lib/modules/5.4.0-121-generic/updates/dkms/

depmod...

DKMS: install completed.

等待完成后再次输入nvidia-smi命令就发现驱动好了

$ nvidia-smi
Sat Jul  9 08:14:07 2022
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 510.47.03    Driver Version: 510.47.03    CUDA Version: 11.6     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ...  Off  | 00000000:01:00.0 Off |                  N/A |
| N/A   56C    P0    34W /  N/A |      0MiB /  8192MiB |      0%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+

为了避免出现此问题,可关闭linux自动更新:

编辑文件/etc/apt/apt.conf.d/10periodic, 将其中的所有值改为0

相关文章

网友评论

      本文标题:Ubuntu PyTorch 配置GPU环境

      本文链接:https://www.haomeiwen.com/subject/djribrtx.html