TensorFlow Build from Source for macOS

栏目: 数据库 · 发布时间: 5年前

内容简介:按照官网预编译的最新版 TensorFlow for macOS,运行测试程序:输出结果有个警告信息:意思是警告本机 CPU 支持 AVX2 和 FMA 指令集,但安装的预编译 TensorFlow 版本不支持。 于是从源码编译安装 TensorFlow,进行优化。

按照官网预编译的最新版 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!'
复制代码

以上所述就是小编给大家介绍的《TensorFlow Build from Source for macOS》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

信息论基础

信息论基础

Thomas M.Cover、Joy A.Thomas / 清华大学出版社 / 2003-11-1 / 65.00元

《国际知名大学原版教材•信息论基础》系统介绍了信息论基本原理及其在通信理论、统计学、计算机科学、概率论以及投资理论等领域的应用。作者以循序渐进的方式,介绍了信息量的基本定义、相对熵、互信息以及他们如何自然地用来解决数据压缩、信道容量、信息率失真、统计假设、网络信息流等问题。一起来看看 《信息论基础》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具