tensorflow from source
环境:Mac OS EI Captain.
显卡:Intel Iris Graphics 6100 1536 MB (是的,就是不能GPU加速了:('' )
我开始为了方便直接使用了sudo pip install tensorflow
来安装tensorflow,使用起来没有问题,但是每次import tensorflow的时候都会出现下面的提醒,一来不胜其烦,二来既然说可以提高性能,那我就看看怎么解决吧。社区果然也有很多人问这个问题,来看解答,似乎是从source安装tensorflow会解决这个问题。所以开始重新从source配置tensorflow,按照官网一步一步来。
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
[[0 2]
安装bazel
我使用了homebrew来安装,安装步骤见bazel文档。
- 安装JDK 8
- Oracle JDK官网, 找到对应版本,我的是"Mac OS X x64"。下载后按提示安装
- 安装Homebrew (如果你没有装过)
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
- 安装Bazel
$ brew install bazel
装好后务必检查bazel的版本$ bazel version
。我安装好后没有检查直接下一步,然后报错,提示bazel需要0.4.4以上然而我的bazel是0.2.2。老老实实$ brew upgrade bazel
,然后再次报错无法更新。google一通,原来homebrew很久没有更新了。$ brew update; brew upgrade bazel
。终于bazel版本问题解决。
安装python dependencies
$ sudo pip install six numpy wheel
通常应该都已经安装过了。
由于我系统无法GPU加速,就跳过GPU配置了,有兴趣的可以自己搞,按照官网文档来,应该不难。
配置
在进行下一步之前务必先运行下面的命令
$ cd tensorflow # cd to the top-level directory created
$ ./configure
运行命令后出现下面的输出,按需选择即可。我选择了全部默认配置,每步回车即可。需要配置GPU的在这里选好Cuda或者cuDNN的版本。
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python2.7
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]:
Do you wish to use jemalloc as the malloc implementation? [Y/n]
jemalloc enabled
Do you wish to build TensorFlow with Google Cloud Platform support? [y/N]
No Google Cloud Platform support will be enabled for TensorFlow
Do you wish to build TensorFlow with Hadoop File System support? [y/N]
No Hadoop File System support will be enabled for TensorFlow
Do you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N]
No XLA JIT 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]
Using python library path: /usr/local/lib/python2.7/dist-packages
Do you wish to build TensorFlow with OpenCL support? [y/N] N
No OpenCL 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 which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]:
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]:
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]:
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"]: 3.0
Setting up Cuda include
Setting up Cuda lib
Setting up Cuda bin
Setting up Cuda nvvm
Setting up CUPTI include
Setting up CUPTI lib64
Configuration finished
Build pip package
Build支持CPU的tensorflow的pip package:
$ bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
Build支持GPU的tensorflow的pip package:
$ bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
这一步巨慢,对RAM的要求比较高。如果你的电脑RAM是个问题,可以控制--local_resources 2048,.5,1.0
,但目测应该会更慢(我的RAM没问题都花了差不多一个小时)。Building过程会有一堆(超大一堆)的warning, 反正我没有理会。
上面的bazel build
命令build了一个名字叫build_pip_package
的脚本,运行脚本,build一个.whl
文件,放在了/tmp/tensorflow_pkg路径。
$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
安装 pip package
安装上面build好的pip package。.whl
文件的名字根据不同平台会不同,自己到/tmp文件夹下查询。我的文件名如下:
$ sudo pip install /tmp/tensorflow_pkg/tensorflow-1.0.1-cp27-cp27m-macosx_10_10_x86_64.whl
安装至此大功告成。
验证安装
安装完毕,需要检查一下安装是否OK。
- 启动一个terminal
- cd到除了
configure
命令所在的tensorflow
的路径 - 启动python
$python
在interactive shell里输入
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
输出应该为
Hello, TensorFlow!
如果这里发现文章开头的那些warning还是出现了,不要气馁。原因是你依然在调用原来的tensorflow (直接pip来的那款哦)。只需要先将它卸载,然后运行安装pip package步骤就可以了。
使用source安装的tensorflow相比较于pip安装的tensorflow肯定是做了一些优化,在CPU上跑,有人说速度可以提升3x, 有人说4-6x,也有人说没有区别,我还没有做过比较。集合大家总的说法来看,在小matrix运算上并不会有明显的优势,但是当matrix变大的时候,速度提升还是明显的。有兴趣的同学可以自我验证一下。