東京電機大学人工知能同好会HP

東京電機大学人工知能同好会のHPです。

TensorFlow-GPUをCUDAで使えるようにするときのメモ

1.1 導入

普段グラボと言われると我々の多くはゲームを連想するだろう。

しかし今回はそれを一般的な用途(General Purpose)で使うのである。

(まぁGPGPUのGPってこれのことだし)

1.2 今回この記事の内容を行った環境

CPU:Ryzen5 1400

GPU:GTX1060

RAM:16GB

OS:Ubuntu16.04

2.1 手順1:CUDAを入れる

とりあえず動かないのは承知で入れてみる.

ターミナルにて

pip3 install tensorflow-gpu

その後

python3

を起動し

>> import tensorflow as tf

と入力。 すると

ImportError: libcublas.so.10.0: cannot open shared object file:

という結果またはそれに類するものが帰ってくる。

このlibcublas.so.〜のあとに続く数字の部分(今回はlibcublas.so.10.0なので10.0)

が今回インストールするのに必要なCUDAのバージョンである。

 

 次にここ

developer.nvidia.com

から必要なバージョンのCUDAをダウンロードする。

基本的にTensorFlowが出たばかりの最新版のCUDAを使用することは少ないため

右側にあるLegacy Releasesの部分から該当のバージョンを探しだしてダウンロードする。

CUDA Toolkit 10.0 Archive を選択し

Operating System > Linux

Architecture > x86_64

Distribution > Ubuntu

Version > 16.04

Installer Type > deb[local]

をそれぞれ選択してダウンロード あとは

 

Installation Instructions:
  1. `sudo dpkg -i cuda-repo-ubuntu1604-10-0-local-10.0.130-410.48_1.0-1_amd64.deb`
  2. `sudo apt-key add /var/cuda-repo-<version>/7fa2af80.pub`
  3. `sudo apt-get update`
  4. `sudo apt-get install cuda`

 

に従えばOK 多分うまく行く

 

2.2 手順2:CuDnnを入れる

 まだこのままじゃ動かない

次に

https://developer.nvidia.com/rdp/cudnn-download

からcuDNNをダウンロードする。

これにはアカウントが必要なので各自作ってやってほしい。

CUDAのバージョンにあった部分をクリック(今回はDownload cuDNN v7.5.0 (Feb 21, 2019), for CUDA 10.0)して

 

cuDNN Runtime Library for Ubuntu16.04 (Deb)

cuDNN Developer Library for Ubuntu16.04 (Deb)

cuDNN Code Samples and User Guide for Ubuntu16.04 (Deb)

 

の3つをダウンロードしてインストールする。

インストールする順番はRuntime→Developer→CodeSamplesでないとエラーが出る。

 

2.3 手順3:仕上げ

homeディレクトリの隠しファイルに.bashrc と.profile という設定ファイルがある。

これらを開いて中身の最後に

export PATH="/usr/local/cuda/bin:$PATH" export LD_LIBRARY_PATH="/usr/local/cuda/lib64:$LD_LIBRARY_PATH"

をそれぞれ書き込む。

 

3.1  結果

ターミナルで

python3

と入力、その後

>>import tensorflow

でエラーが出なければ

>>tensorflow.Session()

 と入力、すると

2019-04-04 00:58:16.697057: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA 2019-04-04 00:58:16.856579: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2019-04-04 00:58:16.857726: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x56fbb20 executing computations on platform CUDA. Devices: 2019-04-04 00:58:16.857749: I tensorflow/compiler/xla/service/service.cc:158] StreamExecutor device (0): GeForce GTX 1060 6GB, Compute Capability 6.1 2019-04-04 00:58:16.892815: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3193620000 Hz 2019-04-04 00:58:16.893704: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x5765840 executing computations on platform Host. Devices: 2019-04-04 00:58:16.893781: I tensorflow/compiler/xla/service/service.cc:158] StreamExecutor device (0): , 2019-04-04 00:58:16.894359: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1433] Found device 0 with properties: name: GeForce GTX 1060 6GB major: 6 minor: 1 memoryClockRate(GHz): 1.7335 pciBusID: 0000:08:00.0 totalMemory: 5.93GiB freeMemory: 5.46GiB 2019-04-04 00:58:16.894395: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1512] Adding visible gpu devices: 0 2019-04-04 00:58:16.896221: I tensorflow/core/common_runtime/gpu/gpu_device.cc:984] Device interconnect StreamExecutor with strength 1 edge matrix: 2019-04-04 00:58:16.896250: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990] 0 2019-04-04 00:58:16.896262: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1003] 0: N 2019-04-04 00:58:16.896563: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 5289 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1060 6GB, pci bus id: 0000:08:00.0, compute capability: 6.1)

 

 という出力が得られる。

 

 

 

 

もし変なエラーや抜けてる部分や要望があったらツイッター@TDU_ML_AI)でリプなりDMなりで指摘していただければありがたい。