TensorFlow Build from Source for macOS

背景

按照官網預編譯的最新版 TensorFlow for macOS,運行測試程序:html

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

輸出結果有個警告信息:python

(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,進行優化。git

環境

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.whlgithub

步驟

安裝 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…shell

# 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。macos

(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
複製代碼

等待漫長的構建過程(三個小時左右),最終構建成功會有如下提示:ubuntu

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 軟件包。windows

(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.whlruby

# 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 現已安裝完畢。bash

驗證安裝結果

# 驗證安裝效果,輸出結果再也不有不支持 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,您能夠經過如下平臺找到我:

相關文章
相關標籤/搜索