2018-05-22-源码安装TensorFlow

使用virtualenv安装了tensorflow,但运行的时候发现CPU可以支持AVX2和FMV,但需要从源码重新构建。好吧,那就开始吧~

参考:

从源代码安装 TensorFlow

1. 环境

系统:macOS High Sierra 10.13.4

python:2.7.15(系统自带)

2. 准备

2.1 避免冲突

我在前面已经用virtualenv创建了tensorflow的环境,此前创建的目录名称“tensorflow”,怕产生冲突,将虚拟环境的目录名称修改为“vir-tensorflow”

2.2 克隆代码库

打开终端,输入以下命令:

$ git clone https://github.com/tensorflow/tensorflow

此时,会在用户目录下创建一个“tensorflow”的目录(幸亏刚刚该虚拟环境目录的命名了~~)

下载的时间比较长,请耐心等待~~快受不了公司的网速了!!!!!

2.3 针对macOS准备环境

2.3.1 安装bazel

Installing Bazel on macOS

使用brew安装:

brew install bazel        #安装

bazel version              #查看是否安装成功

brew upgrade bazel   #升级版本

2.3.2 安装python依赖项

必须安装软件包:

six

numpy:数值处理

wheel:管理wheel格式的python压缩包

使用pip来安装:

$ sudo pip install six numpy wheel

当前版本:

six——1.11.0,numpy——1.14.3,wheel——0.31.0

2.4 安装配置

2.4.1 软件包配置

克隆源码库到本地后,在根目录中包含有一个名为“configure”的bash脚本。必须先运行该脚本,然后才能创建pip软件包并安装TensorFlow。

$ cd /xxxx/tensorflow

$ ./configure

进入tensorflow源码树的根目录,执行configure的bash脚本。

此时configure脚本会向您提出若干问题。我选择默认值,针对本机CPU类型优化所生成的代码,但是这样构建出来的TensorFlow是无法在其他类型的CPU上运行的。

以下是我运行configure脚本时的问题选择:

Extracting Bazel installation...

You have bazel 0.13.0-homebrew installed.

Please specify the location of python. [Default is /usr/local/opt/python@2/bin/python2.7]: /usr/bin/python2.7

Found possible Python library paths:

  /Library/Python/2.7/site-packages

Please input the desired Python library path to use.  Default is [/Library/Python/2.7/site-packages]

/Library/Python/2.7/site-packages

Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]: N

No Google Cloud Platform support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Hadoop File System support? [Y/n]: N

No Hadoop File System support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Amazon S3 File System support? [Y/n]: N

No Amazon S3 File System support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Apache Kafka Platform support? [Y/n]: N

No Apache Kafka Platform support will be enabled for TensorFlow.

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 GDR support? [y/N]: N

No GDR support will be enabled for TensorFlow.

Do you wish to build TensorFlow with VERBS support? [y/N]: N

No VERBS 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 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]: -march=native

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 tools/bazel.rc for more details.

--config=mkl # Build with MKL support.

--config=monolithic # Config for mostly static monolithic build.

Configuration finished

2.4.2 软件包构建

配置后,进行构建:

$ bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package

报错了!!!!!

Starting local Bazel server and connecting to it...

................

WARNING: /private/var/tmp/_bazel_yujianwen/7cea4d7c61741771d65fc25d431ce59d/external/protobuf_archive/WORKSPACE:1: Workspace name in /private/var/tmp/_bazel_yujianwen/7cea4d7c61741771d65fc25d431ce59d/external/protobuf_archive/WORKSPACE (@com_google_protobuf) does not match the name given in the repository's definition (@protobuf_archive); this will cause a build error in future versions

ERROR: /private/var/tmp/_bazel_yujianwen/7cea4d7c61741771d65fc25d431ce59d/external/local_config_cc/BUILD:50:5: in apple_cc_toolchain rule @local_config_cc//:cc-compiler-armeabi-v7a: Xcode version must be specified to use an Apple CROSSTOOL. If your Xcode version has changed recently, try: "bazel clean --expunge" to re-run Xcode configuration

ERROR: While resolving toolchains for target //tensorflow/tools/pip_package:simple_console: Analysis of target '@local_config_cc//:cc-compiler-armeabi-v7a' failed; build aborted

ERROR: Analysis of target '//tensorflow/tools/pip_package:build_pip_package' failed; build aborted: Analysis of target '@local_config_cc//:cc-compiler-armeabi-v7a' failed; build aborted

INFO: Elapsed time: 17.909s

INFO: 0 processes.

FAILED: Build did NOT complete successfully (64 packages loaded)

    currently loading: tensorflow/python ... (3 packages)

Error: Xcode version must be specified to use an Apple CROSSTOOL

处理:

应该是Xcode使用版本与系统版本不一致,需要修改Xcode版本

本机Xocde版本:9.3.1

$ sudo xcode-select -s /Applications/Xcode.app/Contents/Developer

$ bazel clean --expunge

执行以上指令后,重新构建,没有Error,可以往下执行~~~

过了一个中午,还是构建过程还是出现error,一共20个。停止了~~~

你可能感兴趣的:(2018-05-22-源码安装TensorFlow)