背景

按照官网预编译的最新版 TensorFlow for macOS,运行测试程序:

(venv) $  python -c "import tensorflow as tf; hello = tf.constant('Hello, TensorFlow!'); sess = tf.Session(); print(sess.run(hello))" # output: b'Hello, TensorFlow!'

输出结果有个警告信息:

(venv) $  2019-04-19 14:45:50.202157: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
(venv) $  b'Hello, TensorFlow!'

意思是警告本机 CPU 支持 AVX2 和 FMA 指令集,但安装的预编译 TensorFlow 版本不支持。
于是从源码编译安装 TensorFlow,进行优化。

环境

Require TF HW OS GCC Python Supports
Version 1.13.1 CPU MacOS Mojave 10.14.4 (18E226) clang-1001.0.46.4 3.6.5 FMA, AVX, AVX2, SSE4.1, SSE4.2

构建产物:tensorflow-1.13.1-cp36-cp36m-macosx_10_13_x86_64.whl

步骤

安装 Python 和 TensorFlow 软件包依赖项

# install Homebrew if not installed
$  /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$  export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
$  brew update

# install Python 3.6.5 if not installed
$  brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb
$  brew link --overwrite python
$  python3 --version

# 使用特定于 shell 的命令激活该虚拟环境:
$  source ./venv/bin/activate

# 安装 TensorFlow pip 软件包依赖项
(venv) $  pip install -U  pip six numpy wheel mock
(venv) $  pip install -U  keras_applications==1.0.6 --no-deps
(venv) $  pip install -U  keras_preprocessing==1.0.5 --no-deps

安装 Bazel

官网:https://docs.bazel.build/versions/master/install-os-x.html#install-on-mac-os-x-homebrew

# Please note that if your system has the Bazel package from homebrew core installed you first need to uninstall it by typing: `brew uninstall bazel`
# Bazel 0.20.0 because TensorFlow require version 0.21.0 or lower to build
$  brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/da863ab7d8122b8ad406eb5e8bb2253953e6bcc9/Formula/bazel.rb

# You can confirm Bazel is installed successfully by running the following command:
$  bazel version

# Once installed, you can upgrade to a newer version of Bazel using the following command:
$  brew upgrade bazelbuild/tap/bazel

下载 TensorFlow 源代码

$  source ./venv/bin/activate
(venv) $  git clone https://github.com/tensorflow/tensorflow.git
(venv) $  cd tensorflow
# 代码库默认为 master 开发分支。您也可以检出要编译的版本分支:
(venv) $  git checkout r1.13  # version 1.13.1 on 2019/04/19

配置编译系统

(venv) $   ./configure
WARNING: --batch mode is deprecated. Please instead explicitly shut down your Bazel server using the command "bazel shutdown".
INFO: Invocation ID: 824b97ef-4279-4576-8e4c-b9c405cb7a28
You have bazel 0.20.0-homebrew installed.
Please specify the location of python. [Default is /Users/xiaobailong24/venv/bin/python]:


Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: module 'site' has no attribute 'getsitepackages'
Found possible Python library paths:
  /Users/xiaobailong24/venv/lib/python3.6/site-packages
Please input the desired Python library path to use.  Default is [/Users/xiaobailong24/venv/lib/python3.6/site-packages]

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 OpenCL SYCL support? [y/N]: N
No OpenCL SYCL support will be enabled for TensorFlow.

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

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

Do you wish to download a fresh release of clang? (Experimental) [y/N]: N
Clang will not be downloaded.

Do you wish to build TensorFlow with MPI support? [y/N]: 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 -Wno-sign-compare]:


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

Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See .bazelrc for more details.
    --config=mkl         	# Build with MKL support.
    --config=monolithic  	# Config for mostly static monolithic build.
    --config=gdr         	# Build with GDR support.
    --config=verbs       	# Build with libverbs support.
    --config=ngraph      	# Build with Intel nGraph support.
    --config=dynamic_kernels	# (Experimental) Build kernels into separate shared objects.
Preconfigured Bazel build configs to DISABLE default on features:
    --config=noaws       	# Disable AWS S3 filesystem support.
    --config=nogcp       	# Disable GCP support.
    --config=nohdfs      	# Disable HDFS support.
    --config=noignite    	# Disable Apacha Ignite support.
    --config=nokafka     	# Disable Apache Kafka support.
    --config=nonccl      	# Disable NVIDIA NCCL support.
Configuration finished

编译 pip 软件包

Bazel 构建

从源代码编译 TensorFlow 可能会消耗大量内存。如果系统内存有限,请使用以下命令限制 Bazel 的内存消耗量:–local_resources 2048,.5,1.0。我这里使用了 3072。

(venv) $  bazel build -c opt --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-msse4.1 --copt=-msse4.2 -k //tensorflow/tools/pip_package:build_pip_package --local_resources 3072,.5,1.0

编译软件包

bazel build 命令会创建一个名为 build_pip_package 的可执行文件,此文件是用于编译 pip 软件包的程序。请如下所示地运行该可执行文件,以在 /tmp/tensorflow_pkg 目录中编译 .whl 软件包。

(venv) $  ./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

安装软件包

生成的 .whl 文件的文件名取决于 TensorFlow 版本和您的平台,这里生成的为:tensorflow-1.13.1-cp36-cp36m-macosx_10_13_x86_64.whl

# Please note that if your system has the tensorflow installed you first need to uninstall it by typing: `pip uninstall tensorflow`
(venv) $  pip install /tmp/tensorflow_pkg/tensorflow-1.13.1-cp36-cp36m-macosx_10_13_x86_64.whl

成功:TensorFlow 现已安装完毕。

验证安装结果

# 验证安装效果,输出结果不再有不支持 AVX2 的警告
(venv) $  python -c "import tensorflow as tf; hello = tf.constant('Hello, TensorFlow!'); sess = tf.Session(); print(sess.run(hello))" # output: b'Hello, TensorFlow!'

参考

  1. 【维基百科】AVX指令集
  2. 【lakshayg】https://github.com/lakshayg/tensorflow-build
  3. 【TensorFlow】从源代码编译
  4. 【幻悠尘的小窝】通过源码编译安装TensorFlow-CPU版本支持AVX等指令集
  5. 【Aleksandr Sokolovskii】[Update 2] How to build and install TensorFlow GPU/CPU for Windows from source code using bazel and Python 3.6

联系

我是 xiaobailong24,您可以通过以下平台找到我: