【bazel系列】从源码安装tensorflow

为什么要通过源码安装tensorflow?

  1. 特定平台没有安装包,比如arm
  2. 对特定平台加速,(比如AVX指令集)
  3. 定制化 (OPENCL)
1
2
3
4
>>> import tensorflow as tf
>>> tf.Session()
2018-08-05 09:38:38.966050: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
# AVX是x86指令集的扩展,用于线性代数计算的加速(例如矩阵乘法、卷积等)
  1. 如果没有GPU,只在CPU上运行tensorflow的代码,那么最好从源码编译tensorflow,利用AVX、AVX2、FMA等进行优化。
  2. 有些tensorflow operation会在CPU上运行,不在GPU上运行。

    Placing input pipeline operations on the CPU can significantly improve performance. Utilizing the CPU for the input pipeline frees the GPU to focus on training.
    1.

下载tensorflow源码

1
2
3
4
# git clone http://10.31.39.11/xusong/tensorflow.git
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
git checkout r1.8

启动Docker环境

1
2
3
4
5
NAME=xusong_tf_bazel2              # 命名你的容器
nvidia-docker run -it \
--name $NAME \
-v `pwd`:/root/tensorflow1.8.0 \
tensorflow/tensorflow:1.8.0-devel-gpu /bin/bash

编译tensorflow

1
2
3
4
5
6
cd tensorflow1.8.0
./configure # 全部采用默认配置
# 把tensorflow源码编译成whl文件,编译后保存在/tmp/tensorflow_pkg目录下
bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
#
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

成功后,会编译成一个.whl文件。

安装tensorflow

1
pip install /tmp/tensorflow_pkg/tensorflow-1.9.0-py2-none-any.whl

验证

1
2
3
4
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

原理

核心主要在以下命令:

1
bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package

https://github.com/tensorflow/tensorflow/tree/master/tensorflow/tools/pip_package