源码安装可以根据机器配置安装最优版,最大化利用硬件计算能力。
接上文tensorflow安装,若从未安装过,建议先使用安装包安装,后续扩展。
本文以CPU版本为主,解决AVX2等指令集支持问题。
上文环境:centos7.5、anaconda3、python3.6
date:2018/12/6
一、bazel安装
1.jdk8安装(bazel依赖jdk8)
1 | tar -zxvf jdk-8u181-linux-x64.tar.gz |
1 | vim /etc/profile |
1 | #jdk1.8 |
2.bazel安装
bazel官网:
https://github.com/bazelbuild/bazel/releases
https://docs.bazel.build/versions/master/install.html
1>yum引导
1 | yum install bazel |
2>下载安装
1 | sh bazel-0.20.0-installer-linux-x86_64.sh |
3>查看
1 | bazel version |
二、tensorflow编译
1.安装编译依赖库
numpy、pip、wheel
后两项若使用Anaconda安装python方式,默认已安装。
查看当前已安装库
1 | #进入python3.6环境 |
安装numpy
1 | conda install numpy |
3.编译并生成安装文件
1>下载最新tensorflow源码
https://github.com/tensorflow/tensorflow
2>配置编译选项
1 | cd tensorflow-master/ |
1 | WARNING: The following rc files are no longer being read, please transfer their contents or import their path into one of the standard rc files: |
3>编译源码
默认cpu编译方式:
1 | bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package |
查看cpu支持的指令集
1 | #执行 |
添加cpu支持指令集编译
1 | bazel build -c opt --copt=-msse3 --copt=-msse4.1 --copt=-msse4.2 --copt=-mavx --copt=-mavx2 --copt=-mfma //tensorflow/tools/pip_package:build_pip_package |
我安装事实证明,默认编译方式,也会根据当前CPU的指令集,默认添加支持。之前使用安装包安装的方式,运行tensorflow会有如下警告,使用默认方式安装完后也不再由此警告。故CPU安装推荐先使用默认方式。
1 | Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA |
漫长的等待,成功。
1 | INFO: Elapsed time: 4143.941s, Critical Path: 236.75s |
4>生成安装文件
生成whl文件,只有后面路径分隔有个空格。
1 | bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg |
出现如下,成功。
1 | Thu Dec 6 17:48:03 CST 2018 : === Output wheel file is in: /tmp/tensorflow_pkg |
轮子好了,可以安装了。
1 | cd /tmp/tensorflow_pkg/ && ls |