macOS上源码安装tensorflow并且添加SSE4.2、AVX、AVX2、FMA支持

首先从git拉取源码:

git clone --recurse-submodules https://github.com/tensorflow/tensorflow

cd tensorflow

安装依赖:

sudo pip install six numpy wheel

如果之前安装官方安装教程在线安装过,依赖程序就已经安装了,这里就不需要安装了
安装编译程序bazel:

brew install bazel

然后运行配置程序

./configure

此处在我电脑上报错了:

Problem with java installation: couldn't find/access rt.jar in /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home
原因是我安装了java9,java9中已经没有rt.jar这个文件了,解决办法是再安装一个java1.8
然后执行:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8.0_144`

-v后面是安装的jdk的版本

完成之后再执行./configure
 
然后执行编译命令:

bazel build -c opt --copt=-msse4.2 --copt=-mavx --copt=-mavx2 --copt=-mfma //tensorflow/tools/pip_package:build_pip_package

其中参数--copt配置的是相关指令集的支持,依次是SSE4.2、AVX、AVX2、FMA

应该添加哪些指令集?
安装官方文档安装之后执行时,回报一些错误提示:

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.

这里就已经告诉你可以支持哪些指令集了,全部添加就是了

编译完成之后,生成安装包:

bazel-bin/tensorflow/tools/pip_package/build_pip_package ./tensorflow_pkg

#卸载已经安装的

sudo pip uninstall tensorflow

#安装

sudo pip install tensorflow_pkg/tensorflow-1.3.0-cp36-cp36m-macosx_10_13_x86_64.whl