GTX 1080 上的 TensorFlow


Ubuntu 16.03 安裝CUDA、NVIDIA驅動,CUDNN及GPU版TensorFlow。

GPU 支持的TensorFlow讓算力大幅提升,但是安裝好一切支持卻不那麼容易!其實主要是三個東西:

  1. Nvidia 驅動:顯卡驅動
  2. CUDA Toolkit CUDA工具箱
  3. CUDNN:CUDA Deep Neural Network library 神經網絡庫函數

    依賴

1
2
3
4
5
6
7
8
9
10
11
12
13
 $ sudo apt-get update
$ sudo apt-get install
freeglut3-dev
g++-4.9
gcc-4.9
libglu1-mesa-dev
libx11-dev
libxi-dev
libxmu-dev
nvidia-modprobe
python-dev
python-pip
python-virtualenv

安裝Nvidia驅動

1
2
3
4
 $ sudo apt-get purge nvidia-* 刪除nvidia 之前的
$ sudo add-apt-repository ppa:graphics-drivers/ppa
$ sudo apt-get update
$ sudo apt-get install nvidia-384

可在

接下來重啟
重啟後,檢測Nvidia驅動安裝情況,

 1
 $ cat /proc/driver/nvidia/version 
 1
2
 NVRM version: NVIDIA UNIX x86_64 Kernel Module 384.98 Thu Oct 26 15:16:01 PDT 2017
GCC version: gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)

顯示Nvidia's system management interface:

 1
 $ sudo nvidia-smi 
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 +-----------------------------------------------------------------------------+
| NVIDIA-SMI 384.98 Driver Version: 384.98 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce GTX 1080 Off | 00000000:01:00.0 On | N/A |
| 0% 47C P8 12W / 215W | 7992MiB / 8112MiB | 2% Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 994 G /usr/lib/xorg/Xorg 193MiB |
| 0 1889 G compiz 151MiB |
| 0 5068 C /home/frank/anaconda3/bin/python 7643MiB |
+-----------------------------------------------------------------------------+

設置GCC 4.9為默認

1
2
3
4
5
 $ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 10
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 20

$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 10
$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 20

安裝CUDA

當前雖然CUDA-9.0已經發布,但是TensorFlow默認編譯版本還是基於CUDA-8.0的,我們在這裡
Screen Shot 2017-11-13 at 18.35.28

使用如下安裝

1
 sudo cuda_8.0.61_375.26_linux.run --override

安裝時記得

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 Do you accept the previously
You are attempting to install on an unsupported configuration. Do you wish to
Install NVIDIA Accelerated Graphics Driver
Install the CUDA 8.0 Toolkit? ((y)es/(n)o/(q)uit): yes
Enter Toolkit Location [ default is /usr/
Do you want to install a symbolic link at /usr/
Install the CUDA 8.0 Samples? ((y)es/(n)o/(q)uit): no
Installing the CUDA Toolkit

===========
= Summary =8.0
===========

Driver: Not Selected
Toolkit: Installed
Samples: Not Selected

Please make sure that
- PATH includes /usr/
- LD_LIBRARY_PATH includes /usr/

To uninstall the CUDA Toolkit, run the uninstall script
To uninstall the NVIDIA Driver, run nvidia-uninstall

Please see CUDA_Installation_Guide_Linux.pdf

***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 352.00 is required
To install the driver using this installer, run the following
sudo <CudaInstaller>.run -silent -driver

Logfile is /tmp/cuda_install_14557.log

記得上面這裡也有個詢問你是否安裝Nvidia驅動的地方,因為我們前面已經安裝了最新的版本,這里當然選擇no。

添加環境變量

1
2
3
 $
$
$

查看CUDA compiler

 1
 $ nvcc -V 
 1
2
3
4
 nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Tue_Jan_10_13:22:03_CST_2017
Cuda compilation tools, release 8.0, V8.0.61

安裝CUDA Deep Neural Network library :CUDNN

在此處下載
選擇適配CUDA的版本,以及cuDNN v7.0 Library for Linux,這個就是個targz文件。
Screen Shot 2017-11-13 at 18.42.51

接下來操作就是把cudnn的幾個庫放到cuda裡面:

 1
2
3
4
 $ tar xvf cudnn-8.0-linux-x64-v7.tgz
$ sudo cp cuda/include/cudnn.h /usr/
$ sudo cp cuda/lib64/libcudnn* /usr/
$ sudo chmod a+r /usr/

TensorFlow安裝

pip install –upgrade tfBinaryURL

Screen Shot 2017-11-13 at 18.47.45

驗證TensorFlow安裝

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 In [1]: import tensorflow as tf

In [2]: sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
2017-11-13 18:54:59.081831: I tensorflow/core/platform/cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA
2017-11-13 18:54:59.186280: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:892] successful NUMA node
2017-11-13 18:54:59.186604: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Found device 0 with properties:
name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.86
pciBusID: 0000:01:00.0
totalMemory: 7.92GiB freeMemory: 7.46GiB
2017-11-13 18:54:59.186617: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1120] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1)
Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1
2017-11-13 18:54:59.216573: I tensorflow/core/common_runtime/direct_session.cc:299] Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce GTX 1080, pci bus id: 0000:01:00.0, compute capability: 6.1

如上,打印出這些信息就證明安裝成功啦!

CUDA ToolKit 8.0 升級到9.0指南

主要就是需要下載CUDA ToolKit 9.0 的安裝包,和8.0一樣安裝,注意下面的四步驟我們只需要第二步(ToolKit)和第四步(創建軟鏈接,原有的是指向8.0的)

因為CUDNN被放在CUDA ToolKit 8.0內,所有這裡我們需要重新下載CUDNN並解壓到CUDA ToolKit 9.0文件夾內,

再在

1
2
 pip install --ignore-installed --upgrade 
<url>

url

Leave a Comment

Your email address will not be published.