本文主要講ubuntu18.04配置NVIDIA GPU環(huán)境, 并安裝配置PyTorch丁屎。
先確認(rèn)GPU型號(hào)
方式一:
$ 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
便是系統(tǒng)推薦的的GPU 驅(qū)動(dòng)版本。
安裝驅(qū)動(dòng)
只安裝驅(qū)動(dòng)
直接安裝推薦的版本:
sudo apt install nvidia-driver-470
驅(qū)動(dòng) + cuda 安裝
一般我們做深度學(xué)習(xí)使用nvidia gpu都需要安裝cuda晨川,安裝cuda之前一定要確認(rèn)依賴cuda的版本节沦,比如當(dāng)前最新版的PyTorch支持到的cuda版本為11.6, 而cuda最新版已經(jīng)到11.6了,故要安裝對(duì)應(yīng)的版本础爬。
我們根據(jù)英偉達(dá)官網(wǎng)鏈接 安裝甫贯。
根據(jù)需要選擇適合的cuda版本朴皆,點(diǎn)擊鏈接進(jìn)入安裝笆制, 此處我選擇11.6。
點(diǎn)擊鏈接之后進(jìn)入下一步逞度,依次選擇操作系統(tǒng)、架構(gòu)渴逻、發(fā)行版(版本)疾党、安裝方式后,便給出的安裝信息惨奕。
ubuntu安裝方式欄有三個(gè)選項(xiàng)雪位,三個(gè)我都試過(guò),沒(méi)特殊原因推薦使用runfile(local)
方式梨撞,我在使用deb(local)
和deb(network)
安裝的時(shí)候雹洗,會(huì)直接安裝最新版的cuda,而我需要確定的一個(gè)版本11.6卧波。
此處我選擇"Linux-x86_64-Ubuntu-18.04-runfile(local)"
復(fù)制安裝命令依次執(zhí)行:
$ 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
安裝完成后根據(jù)提示配置環(huán)境變量
# cuda
export PATH="/usr/local/cuda-11.6/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda-11.6/lib64:$LD_LIBRARY_PATH"
重啟系統(tǒng)
使用以上兩種方式安裝都需要重啟系統(tǒng)
$ 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
安裝基礎(chǔ)包
我一般使用conda管理python環(huán)境, 安裝PyTorch之前先創(chuàng)建一個(gè)命名為dl-gpu
的python環(huán)境:
$ conda create -y -n dl-gpu python=3.8
激活環(huán)境
$ conda activate dl-gpu
然后開(kāi)始安裝PyTorch,
首先進(jìn)入官網(wǎng)安裝鏈接时肿, 依次選擇PyTorch Build
、Your OS
港粱、Package
螃成、Language
、Compute Platform
此處我選擇的是"Stable(1.12.0)-Linux-Conda-Python-CUDA11.6"查坪,得到執(zhí)行的命令
$ conda install pytorch torchvision torchaudio cudatoolkit=11.6 -c pytorch -c conda-forge
注意點(diǎn):
- 如果是conda安裝寸宏,官網(wǎng)有一個(gè)提示
NOTE: 'conda-forge' channel is required for cudatoolkit 11.6
, 需要先安裝conda-forge
, 安裝可參考鏈接 - cuda版本與PyTorch支持的版本要盡量一致
驗(yàn)證安裝
執(zhí)行上述安裝命令
$ conda install pytorch torchvision torchaudio cudatoolkit=11.6 -c pytorch -c conda-forge
驗(yàn)證是否可用:
(dl-gpu) ? ~ python -c "import torch;print(torch.cuda.device_count())"
1
這里輸出則說(shuō)明GPU配置成功了。
安裝Jupyter
個(gè)人比較喜歡使用JupyterLab偿曙,所以本次安裝JupyterLab, 安裝細(xì)節(jié)見(jiàn)文檔
使用conda安裝
$ conda install -c conda-forge jupyterlab
使用pip安裝
pip install jupyterlab
啟動(dòng)Jupyter并驗(yàn)證Torch和GPU
$ jupyter-lab
import torch
# 查看顯卡數(shù)量
print('GPU count: ', torch.cuda.device_count())
# 獲取第一個(gè)GPU
gpu1 = torch.device(f'cuda:0')
print('GPU one: ', gpu1)
# 創(chuàng)建使用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)
其中device='cuda:0'
表示當(dāng)前張量數(shù)據(jù)存儲(chǔ)在第0個(gè)GPU上遥昧。
常見(jiàn)問(wèn)題
常見(jiàn)問(wèn)題1: 重啟系統(tǒng)后無(wú)法連接驅(qū)動(dòng)了
$ 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內(nèi)核引起的覆醇,
- 先檢查驅(qū)動(dòng)和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
說(shuō)明驅(qū)動(dòng)和和cuda都是存在的。
- 查看已經(jīng)安裝的驅(qū)動(dòng)的版本信息
$ ls /usr/src|grep nvidia
nvidia-510.47.03
這里我的驅(qū)動(dòng)版本是: nvidia-510.47.03
- 使用dkms工具安裝驅(qū)動(dòng)
$ 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
命令就發(fā)現(xiàn)驅(qū)動(dòng)好了
$ 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 |
+-----------------------------------------------------------------------------+
為了避免出現(xiàn)此問(wèn)題炭臭,可關(guān)閉linux自動(dòng)更新:
編輯文件/etc/apt/apt.conf.d/10periodic
, 將其中的所有值改為0