TensorFlow ソースコードからのビルド方法

TensorFlow は Pip を使ってインストールするのがおそらく一般的。ですが、ソースコードからもビルドすることができます。

環境

  • Ubuntu 16.04.1
  • Geforce GTX 1060
  • NVIDIA Driver 367.44
  • ※ CUDA Toolkit 8.0
  • ※ cuDNN 5.1
  • ※ TensorFlow (Commit: 2a6d7511f13a0387857081f1cf64d282d2816a62)

※が今回のインストール対象

NVIDIA Driver インストール

リポジトリを追加しておくと、apt-getで取得できるようになります。

# apt-get が参照するリポジトリを追加
sudo add-apt-repository ppa:graphics-drivers/ppa

# ドライバをインストール
sudo apt-get update && sudo apt-get install nvidia-367

※ ドライバは最新バージョン使うと、新しすぎてうまくいかない場合があります。どのバージョンがいいかここで確認しましょう

GCC ビルド

CUDA Toolkit を入れるときに GCC 5.3 が必要なのでビルドしておきます。

# 依存パッケージの取得
./contrib/download_prerequisites

# ソースコード取得
wget http://ftp.tsukuba.wide.ad.jp/software/gcc/releases/gcc-5.3.0/gcc-5.3.0.tar.gz

# ビルド
cd gcc-5.3.0
./configure --prefix=/usr/local/gcc-5.3.0 --enable-languages=c,c++ --disable-multilib --disable-bootstrap
make
make install

CUDA Toolkit インストール

CUDA Toolkit とは、GPUを計算用途に使うためのライブラリです。

まずはブラウザでCUDA Toolkitのダウンロードページにアクセス。LinuxX86_64 →Ubuntu → 16.04 → runfile (local) の順に選択。Base Installer と Patch 1 をダウンロードします。

NVIDIA Developer Program のアカウントがなければ登録しておきましょう

インストールはCUDA Toolkit は root に切り替えて行うのが楽です。

# root に切り替え
su root

# GCC にパスを通す
export PATH=/usr/local/gcc-5.3.0/bin:$PATH

# ビルド
# 色々聞かれますが、ドライバはインストール済みなので不要です
# サンプルは入れても入れなくてもよいです。
sh ./cuda_8.0.27_linux.run
Do you accept the previously read EULA?
accept/decline/quit: accept

Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 361.77?
(y)es/(n)o/(q)uit: n

Install the CUDA 8.0 Toolkit?
(y)es/(n)o/(q)uit: y

Enter Toolkit Location
 [ default is /usr/local/cuda-8.0 ]: <Enter>

Do you want to install a symbolic link at /usr/local/cuda?
(y)es/(n)o/(q)uit: y

Install the CUDA 8.0 Samples?
(y)es/(n)o/(q)uit: n

Installing the CUDA Toolkit in /usr/local/cuda-8.0 ...

===========
= Summary =
===========

Driver: Not Selected
Toolkit: Installed in /usr/local/cuda-8.0
Samples: Not Selected

同様に、Patch 1 もインストールします

sh ./cuda_8.0.27.1_linux.run

cuDNN インストール

cuDNN は CUDAを Deep Leaning で使うためのライブラリです

cuDNNのダウンロードページから cuDNN v5.1 Library for Linux をダウンロードします

コンパイル済みのものが取得できるので、展開して/usr/local/cuda-8.0に配置するだけでOKです

tar zxvf cudnn-8.0-linux-x64-v5.1.tgz
cp ./cuda/include/cudnn.h /usr/local/cuda-8.0/include/
cp ./cuda/lib64/* /usr/local/cuda-8.0/lib64/

忘れずに~/.bashrc でライブラリパスを追加しておきます

# .bashrc に追記
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

Bazel インストール

長いけどあと少し。がんばりましょう。

Bazel とは Google製のビルドツールです。TensorFlow は Bazel を使ってビルドするように作られています

# Bazel のリポジトリを参照できるように設定
echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
curl https://storage.googleapis.com/bazel-apt/doc/apt-key.pub.gpg | sudo apt-key add

# 依存パッケージのインストール
sudo apt-get install openjdk-8-jdk

# Bazel のインストール
sudo apt-get update && sudo apt-get install bazel

TensorFlow ビルド

TensorFlow は GitHub から最新コードを取得してビルドします。

# 依存ツールをインストール
sudo apt-get install python-pip python-numpy swig python-dev python-wheel

# ソースコード取得
git clone https://github.com/tensorflow/tensorflow

# 例のごとく configure を実行
cd tensorflow
./configure

ここで先ほどビルドした GCC 5.3.0 を選択するとエラーが出てしまうので注意しましょう

Please specify the location of python. [Default is /usr/bin/python]: <Enter>
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N] n
No Google Cloud Platform support will be enabled for TensorFlow
Found possible Python library paths:
  /usr/local/lib/python2.7/dist-packages
  /usr/lib/python2.7/dist-packages
Please input the desired Python library path to use.  Default is [/usr/local/lib/python2.7/dist-packages]
<Enter>
/usr/local/lib/python2.7/dist-packages
Do you wish to build TensorFlow with GPU support? [y/N] y
GPU support will be enabled for TensorFlow
Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: <Enter>
Please specify the Cuda SDK version you want to use, e.g. 7.0. [Leave empty to use system default]: 8.0
Please specify the location where CUDA 8.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: <Enter>
Please specify the Cudnn version you want to use. [Leave empty to use system default]: 5
Please specify the location where cuDNN 5 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: <Enter>
Please specify a list of comma-separated Cuda compute capabilities you want to build with.
You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
Please note that each additional compute capability significantly increases your build time and binary size.
[Default is: "3.5,5.2"]: <Enter>
.
INFO: Starting clean (this may take a while). Consider using --expunge_async if the clean takes more than several minutes.
.
INFO: All external dependencies fetched successfully.
Configuration finished
# Bazel を使ってビルドします。時間がかかるのでコーヒーでも飲みながら待ちましょう
# ちなみに -j <スレッド数> というオプションをつけるとマルチスレッド処理できます
bazel build -c opt --config=cuda //tensorflow/tools/pip_package:build_pip_package

# Pip 用のパッケージを生成してインストール
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
sudo pip install /tmp/tensorflow_pkg/tensorflow-0.10.0rc0-py2-none-any.whl

今後インストール方法が変わるかもしれないので、上手くいかなければ TensorFlow の Download and Setup もチェックしてください。

参考にしたサイト