Press "Enter" to skip to content

TensorFlow Build from Source for macOS

按照官网预编译的最新版 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,进行优化。

 

环境

 

 

RequireTFHWOSGCCPythonSupports
Version1.13.1CPUMacOS Mojave 10.14.4 (18E226)clang-1001.0.46.43.6.5FMA, 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

 

官网: docs.bazel.build/versions/ma…

 

# 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

 

等待漫长的构建过程(三个小时左右),最终构建成功会有以下提示:

 

Target //tensorflow/tools/pip_package:build_pip_package up-to-date:
  bazel-bin/tensorflow/tools/pip_package/build_pip_package
INFO: Elapsed time: 10260.288s, Critical Path: 398.73s
INFO: 9875 processes: 9875 local.
INFO: Build completed successfully, 10432 total actions

 

编译软件包

 

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!'

Be First to Comment

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注