覚え書きブログ

tensorflowのソースからのビルド

以下のnn_ops.pyのconv1_transpose(1D deconv)を使いたく、最新版のtensorflowをソースからビルドしてみた。なお、現時点(2017/12/30)でpipでインストール可能なtensorflow v1.4.1は、まだconv1d_transposeは対応していない。
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/nn_ops.py

基本的には、以下のサイトを参考にして進めた。
https://www.tensorflow.org/install/install_sources

1) 最新版のソースをpull

> git clone https://github.com/tensorflow/tensorflow 

2) GoogleのビルドツールBazelのインストール
以下のページに従い、Bazelをインストールする。
https://docs.bazel.build/versions/master/install-ubuntu.html
実際の手順:

> sudo apt-get install openjdk-8-jdk
> echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
> curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
> sudo apt-get update && sudo apt-get install bazel

3) Pythonのインストール
以下のページに従い、Anaconda3系をインストール
hirotaka-hachiya.hatenablog.com

4) configureの実行
以下のように、様々なオプションを付けるか否か聞かれるが、CUDAまわり以外はよくわからないので基本的には「No」で答えて実行した。
なお、CUDAとCUDNNは、以下のようにバージョンを8.0と6に指定した。

>  ./configure
You have bazel 0.9.0 installed.
Please specify the location of python. [Default is /usr/local/anaconda3/bin/python]:


Found possible Python library paths:
  /usr/local/anaconda3/lib/python3.6/site-packages
Please input the desired Python library path to use.  Default is [/usr/local/anaconda3/lib/python3.6/site-packages]

Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]: n
No jemalloc as malloc support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]: n
No Google Cloud Platform support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Hadoop File System support? [Y/n]: n
No Hadoop File System support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Amazon S3 File System support? [Y/n]: n
No Amazon S3 File System support will be enabled for TensorFlow.

Do you wish to build TensorFlow with XLA JIT support? [y/N]: n
No XLA JIT support will be enabled for TensorFlow.

Do you wish to build TensorFlow with GDR support? [y/N]: n
No GDR support will be enabled for TensorFlow.

Do you wish to build TensorFlow with VERBS support? [y/N]: n
No VERBS support will be enabled for TensorFlow.

Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: n
No OpenCL SYCL support will be enabled for TensorFlow.

Do you wish to build TensorFlow with CUDA support? [y/N]: y
CUDA support will be enabled for TensorFlow.

Please specify the CUDA SDK version you want to use, e.g. 7.0. [Leave empty to default to CUDA 9.0]: 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]: /usr/local/cuda-8.0


Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7.0]: 6


Please specify the location where cuDNN 6 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda-8.0]:


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: 6.1]


Do you want to use clang as CUDA compiler? [y/N]: n
nvcc will be used as CUDA compiler.

Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]:


Do you wish to build TensorFlow with MPI support? [y/N]:
No MPI support will be enabled for TensorFlow.

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]:


Add "--config=mkl" to your bazel command to build with MKL support.
Please note that MKL on MacOS or windows is still not supported.
If you would like to use a local MKL instead of downloading, please set the environment variable "TF_MKL_ROOT" every time before build.

Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]:
Not configuring the WORKSPACE for Android builds.

Configuration finished

5) GPUモードでビルド

> bazel build --config=opt --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" --config=cuda //tensorflow/tools/pip_package:build_pip_package
> bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

6) /tmp/tensorflow_pkgにできたwhlファイルを確認し、pipでインストール

> ls /tmp/tensorflow_pkg
tensorflow-1.4.0-cp36-cp36m-linux_x86_64.whl
> pip install /tmp/tensorflow_pkg/tensorflow-1.4.0-cp36-cp36m-linux_x86_64.whl

7) 動作確認
※tensorflowのソースディレクトリで、pythonインタラクティブモードで起動し、「import tensorflow」を実行すると、以下のようなエラーがでるので要注意。

ImportError: Could not import tensorflow. Do not import tensorflow from its source directory; change directory to outside the TensorFlow source tree, and relaunch your Python interpreter from there.

> python
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
2017-12-31 02:01:42.350524: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1202] Found device 0 with properties:
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.582
pciBusID: 0000:03:00.0
totalMemory: 10.90GiB freeMemory: 10.52GiB
2017-12-31 02:01:42.350572: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1296] Adding visible gpu device 0
2017-12-31 02:01:42.645996: I tensorflow/core/common_runtime/gpu/gpu_device.cc:983] Creating TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 10190 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:03:00.0, compute capability: 6.1)
>>> print(sess.run(hello))
b'Hello, TensorFlow!'

8) conv1d_transposeの動作確認
以下のページを参考に、動作確認をした。
https://stackoverflow.com/questions/47106331/how-to-use-the-conv1d-transpose-in-tensorflow

import numpy as np
import tensorflow as tf
from tensorflow.python.ops import nn_ops

x = tf.placeholder(shape=[None, 256, 16], dtype=tf.float32)
filter = tf.Variable(tf.random_normal([3, 8, 16]))    # [kernel_width, output_depth, input_depth]
out = nn_ops.conv1d_transpose(x, filter, output_shape=[100, 1024, 8], stride=4, padding="VALID")

with tf.Session() as sess:
   sess.run(tf.global_variables_initializer())
   result = sess.run(out, feed_dict={x: np.zeros([100, 256, 16])})
   print(result.shape)
   print(result[0])
> python conv1d_transpose_test.py
2017-12-31 02:22:16.602713: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1202] Found device 0 with properties:
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.582
pciBusID: 0000:03:00.0
totalMemory: 10.90GiB freeMemory: 10.52GiB
2017-12-31 02:22:16.602757: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1296] Adding visible gpu device 0
2017-12-31 02:22:16.835205: I tensorflow/core/common_runtime/gpu/gpu_device.cc:983] Creating TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 10190 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080 Ti, pci bus id: 0000:03:00.0, compute capability: 6.1)
(100, 1024, 8)
[[ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 ...,
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]
 [ 0.  0.  0. ...,  0.  0.  0.]]