原文博客:Doi技術團隊
連接地址:https://blog.doiduoyi.com/authors/1584446358138
初心:記錄優秀的Doi技術團隊學習經歷html
目錄
文章目錄
前言
最近在學習PaddlePaddle在各個顯卡驅動版本的安裝和使用,因此同時也學習如何在Ubuntu安裝和卸載CUDA和CUDNN,在學習過程當中,順便記錄學習過程。在供你們學習的同時,也在增強本身的記憶。本文章以卸載CUDA 8.0 和 CUDNN 7.05 爲例,以安裝CUDA 10.0 和 CUDNN 7.4.2 爲例。python
安裝顯卡驅動
禁用nouveau驅動
sudo vim /etc/modprobe.d/blacklist.conf
在文本最後添加:linux
blacklist nouveau options nouveau modeset=0
而後執行:web
sudo update-initramfs -u
重啓後,執行如下命令,若是沒有屏幕輸出,說明禁用nouveau成功:vim
lsmod | grep nouveau
下載驅動
官網下載地址:https://www.nvidia.cn/Download/index.aspx?lang=cn ,根據本身顯卡的狀況下載對應版本的顯卡驅動,好比筆者的顯卡是RTX2070:
bash
下載完成以後會獲得一個安裝包,不一樣版本文件名可能不同:app
NVIDIA-Linux-x86_64-410.93.run
卸載舊驅動
如下操做都須要在命令界面操做,執行如下快捷鍵進入命令界面,並登陸:ide
Ctrl-Alt+F1
執行如下命令禁用X-Window服務,不然沒法安裝顯卡驅動:svg
sudo service lightdm stop
執行如下三條命令卸載原有顯卡驅動:oop
sudo apt-get remove --purge nvidia* sudo chmod +x NVIDIA-Linux-x86_64-410.93.run sudo ./NVIDIA-Linux-x86_64-410.93.run --uninstall
安裝新驅動
直接執行驅動文件便可安裝新驅動,一直默認便可:
sudo ./NVIDIA-Linux-x86_64-410.93.run
執行如下命令啓動X-Window服務
sudo service lightdm start
最後執行重啓命令,重啓系統便可:
reboot
注意: 若是系統重啓以後出現重複登陸的狀況,多數狀況下都是安裝了錯誤版本的顯卡驅動。須要下載對應自己機器安裝的顯卡版本。
卸載CUDA
爲何一開始我就要卸載CUDA呢,這是由於筆者是換了顯卡RTX2070,本來就安裝了CUDA 8.0 和 CUDNN 7.0.5不可以正常使用,筆者須要安裝CUDA 10.0 和 CUDNN 7.4.2,因此要先卸載原來的CUDA。注意如下的命令都是在root用戶下操做的。
卸載CUDA很簡單,一條命令就能夠了,主要執行的是CUDA自帶的卸載腳本,讀者要根據本身的cuda版本找到卸載腳本:
sudo /usr/local/cuda-8.0/bin/uninstall_cuda_8.0.pl
卸載以後,還有一些殘留的文件夾,以前安裝的是CUDA 8.0。能夠一併刪除:
sudo rm -rf /usr/local/cuda-8.0/
這樣就算卸載完了CUDA。
安裝CUDA
安裝的CUDA和CUDNN版本:
- CUDA 10.0
- CUDNN 7.4.2
接下來的安裝步驟都是在root用戶下操做的。
下載和安裝CUDA
咱們能夠在官網:CUDA10下載頁面,
下載符合本身系統版本的CUDA。頁面以下:
下載完成以後,給文件賦予執行權限:
chmod +x cuda_10.0.130_410.48_linux.run
執行安裝包,開始安裝:
./cuda_10.0.130_410.48_linux.run
開始安裝以後,須要閱讀說明,可使用Ctrl + C
直接閱讀完成,或者使用空格鍵
慢慢閱讀。而後進行配置,我這裏說明一下:
(是否贊成條款,必須贊成才能繼續安裝) accept/decline/quit: accept (這裏不要安裝驅動,由於已經安裝最新的驅動了,不然可能會安裝舊版本的顯卡驅動,致使重複登陸的狀況) Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 410.48? (y)es/(n)o/(q)uit: n Install the CUDA 10.0 Toolkit?(是否安裝CUDA 10 ,這裏必需要安裝) (y)es/(n)o/(q)uit: y Enter Toolkit Location(安裝路徑,使用默認,直接回車就行) [ default is /usr/local/cuda-10.0 ]: Do you want to install a symbolic link at /usr/local/cuda?(贊成建立軟連接) (y)es/(n)o/(q)uit: y Install the CUDA 10.0 Samples?(不用安裝測試,自己就有了) (y)es/(n)o/(q)uit: n Installing the CUDA Toolkit in /usr/local/cuda-10.0 ...(開始安裝)
安裝完成以後,能夠配置他們的環境變量,在vim ~/.bashrc
的最後加上如下配置信息:
export CUDA_HOME=/usr/local/cuda-10.0 export LD_LIBRARY_PATH=${CUDA_HOME}/lib64 export PATH=${CUDA_HOME}/bin:${PATH}
最後使用命令source ~/.bashrc
使它生效。
可使用命令nvcc -V
查看安裝的版本信息:
test@test:~$ nvcc -V nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2018 NVIDIA Corporation Built on Sat_Aug_25_21:08:01_CDT_2018 Cuda compilation tools, release 10.0, V10.0.130
測試安裝是否成功
執行如下幾條命令:
cd /usr/local/cuda-10.0/samples/1_Utilities/deviceQuery make ./deviceQuery
正常狀況下輸出:
./deviceQuery Starting... CUDA Device Query (Runtime API) version (CUDART static linking) Detected 1 CUDA Capable device(s) Device 0: "GeForce RTX 2070" CUDA Driver Version / Runtime Version 10.0 / 10.0 CUDA Capability Major/Minor version number: 7.5 Total amount of global memory: 7950 MBytes (8335982592 bytes) (36) Multiprocessors, ( 64) CUDA Cores/MP: 2304 CUDA Cores GPU Max Clock rate: 1620 MHz (1.62 GHz) Memory Clock rate: 7001 Mhz Memory Bus Width: 256-bit L2 Cache Size: 4194304 bytes Maximum Texture Dimension Size (x,y,z) 1D=(131072), 2D=(131072, 65536), 3D=(16384, 16384, 16384) Maximum Layered 1D Texture Size, (num) layers 1D=(32768), 2048 layers Maximum Layered 2D Texture Size, (num) layers 2D=(32768, 32768), 2048 layers Total amount of constant memory: 65536 bytes Total amount of shared memory per block: 49152 bytes Total number of registers available per block: 65536 Warp size: 32 Maximum number of threads per multiprocessor: 1024 Maximum number of threads per block: 1024 Max dimension size of a thread block (x,y,z): (1024, 1024, 64) Max dimension size of a grid size (x,y,z): (2147483647, 65535, 65535) Maximum memory pitch: 2147483647 bytes Texture alignment: 512 bytes Concurrent copy and kernel execution: Yes with 3 copy engine(s) Run time limit on kernels: Yes Integrated GPU sharing Host Memory: No Support host page-locked memory mapping: Yes Alignment requirement for Surfaces: Yes Device has ECC support: Disabled Device supports Unified Addressing (UVA): Yes Device supports Compute Preemption: Yes Supports Cooperative Kernel Launch: Yes Supports MultiDevice Co-op Kernel Launch: Yes Device PCI Domain ID / Bus ID / location ID: 0 / 1 / 0 Compute Mode: < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) > deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 10.0, CUDA Runtime Version = 10.0, NumDevs = 1 Result = PASS
下載和安裝CUDNN
進入到CUDNN的下載官網:https://developer.nvidia.com/rdp/cudnn-download ,然點擊Download開始選擇下載版本,固然在下載以前還有登陸,選擇版本界面以下,咱們選擇cuDNN Library for Linux
:
下載以後是一個壓縮包,以下:
cudnn-10.0-linux-x64-v7.4.2.24.tgz
而後對它進行解壓,命令以下:
tar -zxvf cudnn-10.0-linux-x64-v7.4.2.24.tgz
解壓以後能夠獲得如下文件:
cuda/include/cudnn.h cuda/NVIDIA_SLA_cuDNN_Support.txt cuda/lib64/libcudnn.so cuda/lib64/libcudnn.so.7 cuda/lib64/libcudnn.so.7.4.2 cuda/lib64/libcudnn_static.a
使用如下兩條命令複製這些文件到CUDA目錄下:
cp cuda/lib64/* /usr/local/cuda-10.0/lib64/ cp cuda/include/* /usr/local/cuda-10.0/include/
拷貝完成以後,可使用如下命令查看CUDNN的版本信息:
cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2
測試安裝結果
到這裏就已經完成了CUDA 10 和 CUDNN 7.4.2 的安裝。能夠安裝對應的Pytorch的GPU版本測試是否能夠正常使用了。安裝以下:
pip3 install https://download.pytorch.org/whl/cu100/torch-1.0.0-cp35-cp35m-linux_x86_64.whl pip3 install torchvision
而後使用如下的程序測試安裝狀況:
import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim import torch.backends.cudnn as cudnn from torchvision import datasets, transforms class Net(nn.Module): def __init__(self): super(Net, self).__init__() self.conv1 = nn.Conv2d(1, 10, kernel_size=5) self.conv2 = nn.Conv2d(10, 20, kernel_size=5) self.conv2_drop = nn.Dropout2d() self.fc1 = nn.Linear(320, 50) self.fc2 = nn.Linear(50, 10) def forward(self, x): x = F.relu(F.max_pool2d(self.conv1(x), 2)) x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2)) x = x.view(-1, 320) x = F.relu(self.fc1(x)) x = F.dropout(x, training=self.training) x = self.fc2(x) return F.log_softmax(x, dim=1) def train(model, device, train_loader, optimizer, epoch): model.train() for batch_idx, (data, target) in enumerate(train_loader): data, target = data.to(device), target.to(device) optimizer.zero_grad() output = model(data) loss = F.nll_loss(output, target) loss.backward() optimizer.step() if batch_idx % 10 == 0: print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( epoch, batch_idx * len(data), len(train_loader.dataset), 100. * batch_idx / len(train_loader), loss.item())) def main(): cudnn.benchmark = True torch.manual_seed(1) device = torch.device("cuda") kwargs = { 'num_workers': 1, 'pin_memory': True} train_loader = torch.utils.data.DataLoader( datasets.MNIST('../data', train=True, download=True, transform=transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,)) ])), batch_size=64, shuffle=True, **kwargs) model = Net().to(device) optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5) for epoch in range(1, 11): train(model, device, train_loader, optimizer, epoch) if __name__ == '__main__': main()
若是正常輸出一下如下信息,證實已經安裝成了:
Train Epoch: 1 [0/60000 (0%)] Loss: 2.365850 Train Epoch: 1 [640/60000 (1%)] Loss: 2.305295 Train Epoch: 1 [1280/60000 (2%)] Loss: 2.301407 Train Epoch: 1 [1920/60000 (3%)] Loss: 2.316538 Train Epoch: 1 [2560/60000 (4%)] Loss: 2.255809 Train Epoch: 1 [3200/60000 (5%)] Loss: 2.224511 Train Epoch: 1 [3840/60000 (6%)] Loss: 2.216569 Train Epoch: 1 [4480/60000 (7%)] Loss: 2.181396
參考資料
- https://developer.nvidia.com
- https://www.cnblogs.com/luofeel/p/8654964.html
本文同步分享在 博客「夜雨飄零1」(CSDN)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。