1. 程式人生 > 其它 >多版本cuda環境管理

多版本cuda環境管理

多版本cuda環境管理

接前文,python-3.7已經部署好,現在在python-3.8部署一套相應版本的環境

1、安裝CUDA

#安裝常用命令
(python-3.8) root@fb67dea379e0:~# pip install -i https://pypi.tuna.tsinghua.edu.cn/simple ipython pandas pillow matplotlib setproctitle networkx scikit-learn scipy tqdm GPUtil jupyterlab notebook h5py statsmodels
#下載cuda
(python-3.8) root@fb67dea379e0:~# wget http://developer.download.nvidia.com/compute/cuda/11.0.2/local_installers/cuda_11.0.2_450.51.05_linux.run

#安裝cuda
(python-3.8) root@fb67dea379e0:~# sh cuda_11.0.2_450.51.05_linux.run --silent --toolkit --samples --librarypath=/usr/local/cuda-11.0
#檢查安裝的版本
(python-3.8) root@fb67dea379e0:~# ln -s /usr/local/cuda-11.0/bin/nvcc /usr/bin/nvcc-python-3.8 
(python-3.8) root@fb67dea379e0:~# which nvcc-python-3.8
/usr/bin/nvcc-python-3.8

(python-3.8) root@fb67dea379e0:~# nvcc-python-3.8 -V
nvcc-python-3: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2020 NVIDIA Corporation
Built on Thu_Jun_11_22:26:38_PDT_2020
Cuda compilation tools, release 11.0, V11.0.194
Build cuda_11.0_bu.TC445_37.28540450_0
#測試 CUDA Toolkit 以驗證是否安裝成功
#Result = PASS則安裝成功
(python-3.8) root@fb67dea379e0:~# cd /usr/local/cuda-11.0/extras/demo_suite/
(python-3.8) root@fb67dea379e0:/usr/local/cuda-11.0/extras/demo_suite# ./deviceQuery
......
deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 11.6, CUDA Runtime Version = 11.0, NumDevs = 4, Device0 = NVIDIA GeForce RTX 2080 Ti, Device1 = NVIDIA GeForce RTX 2080 Ti, Device2 = NVIDIA GeForce RTX 2080 Ti, Device3 = NVIDIA GeForce RTX 2080 Ti
Result = PASS

2、安裝CUDNN

#下載cudnn
(python-3.8) root@fb67dea379e0:~# ll -d cudnn-11.0-linux-x64-v8.0.1.13.tgz 
-rw-r--r-- 1 root root 1142456047 Mar 31 03:39 cudnn-11.0-linux-x64-v8.0.1.13.tgz

#解壓縮
(python-3.8) root@fb67dea379e0:~# tar xf cudnn-11.0-linux-x64-v8.0.1.13.tgz

#安裝cudnn,把相應的檔案,複製到指定目錄即可
(python-3.8) root@fb67dea379e0:~# cp cuda/include/cudnn* /usr/local/cuda-11.0/include/
(python-3.8) root@fb67dea379e0:~# cp cuda/lib64/libcudnn* /usr/local/cuda-11.0/lib64/

#新增許可權
(python-3.8) root@fb67dea379e0:~# chmod a+r /usr/local/cuda-11.0/include/cudnn* /usr/local/cuda-11.0/lib64/libcudnn*

3、安裝tensorflow

#安裝指定版本的tensorflow
(python-3.8) root@fb67dea379e0:~# pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow==2.4.0
#驗證tensorflow是否安裝成功
(python-3.8) root@fb67dea379e0:~# pip show tensorflow
Name: tensorflow
Version: 2.4.0
Summary: TensorFlow is an open source machine learning framework for everyone.
Home-page: https://www.tensorflow.org/
Author: Google Inc.
Author-email: [email protected]
License: Apache 2.0
Location: /usr/local/anaconda3/envs/python-3.8/lib/python3.8/site-packages
Requires: absl-py, astunparse, flatbuffers, gast, google-pasta, grpcio, h5py, keras-preprocessing, numpy, opt-einsum, protobuf, six, tensorboard, tensorflow-estimator, termcolor, typing-extensions, wheel, wrapt
Required-by: 

4、安裝pytorch

#下載對應cuda版本編譯的安裝包
(python-3.8) root@fb67dea379e0:~# wget -P ./ https://download.pytorch.org/whl/cu110/torch-1.7.1%2Bcu110-cp38-cp38-linux_x86_64.whl

(python-3.8) root@fb67dea379e0:~# ll -d torch-1.7.1+cu110-cp38-cp38-linux_x86_64.whl 
-rw-r--r-- 1 root root 1156760198 Dec 10  2020 torch-1.7.1+cu110-cp38-cp38-linux_x86_64.whl
#安裝下載好的安裝包
(python-3.8) root@fb67dea379e0:~# pip install torch-1.7.1+cu110-cp38-cp38-linux_x86_64.whl 
Processing ./torch-1.7.1+cu110-cp38-cp38-linux_x86_64.whl
Requirement already satisfied: numpy in /usr/local/anaconda3/envs/python-3.8/lib/python3.8/site-packages (from torch==1.7.1+cu110) (1.19.5)
Requirement already satisfied: typing-extensions in /usr/local/anaconda3/envs/python-3.8/lib/python3.8/site-packages (from torch==1.7.1+cu110) (3.7.4.3)
Installing collected packages: torch
Successfully installed torch-1.7.1+cu110
#驗證pytorch是否安裝成功,這裡可以寫一個小指令碼驗證下,如沒有報錯就是安裝成功了
(python-3.8) root@fb67dea379e0:~# vim pytorch.py
(python-3.8) root@fb67dea379e0:~# cat pytorch.py 
from __future__ import print_function  
import torch
x = torch.rand(5, 3)
print(x)

(python-3.8) root@fb67dea379e0:~# python3.8 pytorch.py 
tensor([[0.6323, 0.7606, 0.1812],
        [0.2629, 0.5828, 0.2895],
        [0.6964, 0.3038, 0.3887],
        [0.0552, 0.3270, 0.5731],
        [0.2724, 0.4341, 0.6136]])

5、多版本cuda環境管理,哪個環境就用哪個cuda

1、python-3.8環境

#進入環境生效指令碼
(python-3.8) root@fb67dea379e0:~# mkdir -p ~/anaconda3/envs/python-3.8/etc/conda/activate.d

(python-3.8) root@fb67dea379e0:~# vim ~/anaconda3/envs/python-3.8/etc/conda/activate.d/activate.sh
(python-3.8) root@fb67dea379e0:~# cat ~/anaconda3/envs/python-3.8/etc/conda/activate.d/activate.sh
#!/bin/sh
ORIGINAL_LD_LIBRARY_PATH=$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/cuda-11.0/lib64:/usr/local/cuda-11.0/extras/CUPTI/lib64:/lib/nccl/cuda-11.0:$LD_LIBRARY_PATH

(python-3.8) root@fb67dea379e0:~# chmod +x ~/anaconda3/envs/python-3.8/etc/conda/activate.d/activate.sh
#退出環境生效指令碼
(python-3.8) root@fb67dea379e0:~# mkdir -p ~/anaconda3/envs/python-3.8/etc/conda/deactivate.d

(python-3.8) root@fb67dea379e0:~# vim ~/anaconda3/envs/python-3.8/etc/conda/deactivate.d/deactivate.sh
(python-3.8) root@fb67dea379e0:~# cat ~/anaconda3/envs/python-3.8/etc/conda/deactivate.d/deactivate.sh
#!/bin/sh
export LD_LIBRARY_PATH=$ORIGINAL_LD_LIBRARY_PATH
unset ORIGINAL_LD_LIBRARY_PATH

(python-3.8) root@fb67dea379e0:~# chmod +x ~/anaconda3/envs/python-3.8/etc/conda/deactivate.d/deactivate.sh

2、python-3.7環境

#進入環境生效指令碼
(python-3.7) root@fb67dea379e0:~# mkdir -p ~/anaconda3/envs/python-3.7/etc/conda/activate.d

(python-3.7) root@fb67dea379e0:~# vim ~/anaconda3/envs/python-3.7/etc/conda/activate.d/activate.sh
(python-3.7) root@fb67dea379e0:~# cat ~/anaconda3/envs/python-3.7/etc/conda/activate.d/activate.sh
#!/bin/sh
ORIGINAL_LD_LIBRARY_PATH=$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/cuda-10.2/lib64:/usr/local/cuda-10.2/extras/CUPTI/lib64:/lib/nccl/cuda-10.2:$LD_LIBRARY_PATH

(python-3.7) root@fb67dea379e0:~# chmod +x ~/anaconda3/envs/python-3.7/etc/conda/activate.d/activate.sh
#退出環境生效指令碼
(python-3.7) root@fb67dea379e0:~# mkdir -p ~/anaconda3/envs/python-3.7/etc/conda/deactivate.d

(python-3.7) root@fb67dea379e0:~# vim ~/anaconda3/envs/python-3.7/etc/conda/deactivate.d/deactivate.sh
(python-3.7) root@fb67dea379e0:~# cat ~/anaconda3/envs/python-3.7/etc/conda/deactivate.d/deactivate.sh
#!/bin/sh
export LD_LIBRARY_PATH=$ORIGINAL_LD_LIBRARY_PATH
unset ORIGINAL_LD_LIBRARY_PATH

(python-3.7) root@fb67dea379e0:~# chmod +x ~/anaconda3/envs/python-3.7/etc/conda/deactivate.d/deactivate.sh