Tensorflow GPU 支持:Cuda 安装
2022年4月17日
(ˇˍˇ) 想试试台式机的GPU的性能怎么样。想使用Tensorflow来做训练,下面是ubuntu18 的安装方法。简单记录如下:
- Ubuntu:18.04.
- Cuda:11.6
- CUDNN: 8.0
- Python: 3.8
安装方法参考:tensorflow GPU的支持:GPU support | TensorFlow :
Ubuntu 18.04 (CUDA 11.0)
我自已已经安装了CUda11.6 ,因此没有再安装。
# Add NVIDIA package repositories
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin
sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600
sudo apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
sudo add-apt-repository "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/ /"
sudo apt-get update
wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/nvidia-machine-learning-repo-ubuntu1804_1.0.0-1_amd64.deb
sudo apt install ./nvidia-machine-learning-repo-ubuntu1804_1.0.0-1_amd64.deb
sudo apt-get update
wget https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64/libnvinfer7_7.1.3-1+cuda11.0_amd64.deb
sudo apt install ./libnvinfer7_7.1.3-1+cuda11.0_amd64.deb
sudo apt-get update
# Install development and runtime libraries (~4GB)
sudo apt-get install --no-install-recommends \
cuda-11-0 \
libcudnn8=8.0.4.30-1+cuda11.0 \
libcudnn8-dev=8.0.4.30-1+cuda11.0
# Reboot. Check that GPUs are visible using the command: nvidia-smi
# Install TensorRT. Requires that libcudnn8 is installed above.
sudo apt-get install -y --no-install-recommends libnvinfer7=7.1.3-1+cuda11.0 \
libnvinfer-dev=7.1.3-1+cuda11.0 \
libnvinfer-plugin7=7.1.3-1+cuda11.0
安装好后可以可以使用 nvidia-smi
检查GPU的情况
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 510.54 Driver Version: 510.54 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 Quadro K1200 Off | 00000000:05:00.0 On | N/A |
| 42% 52C P8 1W / 35W | 957MiB / 4096MiB | 2% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
| 0 N/A N/A 1507 G /usr/lib/xorg/Xorg 89MiB |
| 0 N/A N/A 1638 G /usr/bin/gnome-shell 98MiB |
| 0 N/A N/A 1980 G /usr/lib/xorg/Xorg 301MiB |
| 0 N/A N/A 2102 G /usr/bin/gnome-shell 56MiB |
| 0 N/A N/A 2511 G ...RendererForSitePerProcess 94MiB |
| 0 N/A N/A 5862 G ...626722160213258988,131072 102MiB |
| 0 N/A N/A 6065 G /usr/lib/firefox/firefox 54MiB |
| 0 N/A N/A 7640 C python3 146MiB |
+-----------------------------------------------------------------------------+
安装好后如何测试GPU的支持呢? 建议创建conda的环境并进行测试,conda安装参考网上,安装完成后执行如下命令初始化环境:
conda create -n tensorflow pip python=3.8
conda activate tensorflow
最后就是测试了,如下代码参考自网络,分别测试了在GPU和CPU的性能。
import tensorflow as tf
import timeit
print('version of TF:',tf.__version__)
print(tf.test.is_gpu_available())
gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
cpus = tf.config.experimental.list_physical_devices(device_type='CPU')
print('GPU devices:')
print(gpus)
print('CPU devices:')
print(cpus)
def cpu_run():
with tf.device('/cpu:0'):
cpu_a = tf.random.normal([1000,2000])
cpu_b = tf.random.normal([2000,1000])
c = tf.matmul(cpu_a, cpu_b)
return c
def gpu_run():
with tf.device('/gpu:0'):
gpu_a = tf.random.normal([1000,2000])
gpu_b = tf.random.normal([2000,1000])
c = tf.matmul(gpu_a, gpu_b)
return c
k1 = 10
cpu_time = timeit.timeit(cpu_run, number=k1)
gpu_time = timeit.timeit(gpu_run, number=k1)
print('cpu time of k=10 :',cpu_time, 'gpu time of k=10 :',gpu_time)
k2 = 100
cpu_time = timeit.timeit(cpu_run, number=k2)
gpu_time = timeit.timeit(gpu_run, number=k2)
print('cpu time of k=100 :',cpu_time, 'gpu time of k=100 :',gpu_time)
k3 = 1000
cpu_time = timeit.timeit(cpu_run, number=k3)
gpu_time = timeit.timeit(gpu_run, number=k3)
print('cpu time of k=1000:',cpu_time, 'gpu time of k=1000:',gpu_time)
log结果如下,GPU确实 比CPU的速度会快很多。
version of TF: 2.8.0
WARNING:tensorflow:From /home/fredy/startjacinto/scripts/ubuntu/check_tensorflow_gpu.py:6: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.config.list_physical_devices('GPU')` instead.
2022-04-17 11:49:00.243760: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /device:GPU:0 with 2277 MB memory: -> device: 0, name: Quadro K1200, pci bus id: 0000:05:00.0, compute capability: 5.0
True
GPU devices:
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
CPU devices:
[PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')]
2022-04-17 11:49:00.254285: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 2277 MB memory: -> device: 0, name: Quadro K1200, pci bus id: 0000:05:00.0, compute capability: 5.0
cpu time of k=10 : 0.36524599599999874 gpu time of k=10 : 0.7779474049999635
cpu time of k=100 : 2.564006860000063 gpu time of k=100 : 0.09132983999995758
cpu time of k=1000: 23.854419104000044 gpu time of k=1000: 5.588481434999949
Post Views: 763
相关
上一篇