TensorFlow Build from Source for macOS

xiaobailong24發表於2019-04-19

背景

按照官網預編譯的最新版 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

官網: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!'
複製程式碼

參考

  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,您可以通過以下平臺找到我:

相關文章