Tensorflow從源碼編譯

从源代码构建

从源代码构建 TensorFlow pip 软件包并将其安装在 Ubuntu Linux 和 macOS 上。尽管这些说明可能适用于其他系统,但仅针对 Ubuntu 和 macOS 进行了测试并在这两种平台上受支持。

注意:我们已经针对 Linux 和 macOS 系统提供了经过精密测试的预构建  TensorFlow 软件包

Linux 和 macOS 设置

安装以下构建工具以配置开发环境。

安装 Python 和 TensorFlow 软件包依赖项

Ubuntu

sudo apt install python-dev python-pip  # or python3-dev python3-pip

mac OS

需要 Xcode 8.3 或更高版本。

使用 Homebrew 软件包管理器安装:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
brew install python@2  # or python (Python 3)

安装 TensorFlow pip 软件包依赖项(如果使用虚拟环境,请省略 --user 参数):

pip install -U --user pip six numpy wheel mock
pip install -U --user keras_applications==1.0.5 --no-deps
pip install -U --user keras_preprocessing==1.0.3 --no-deps

这些依赖项列在 REQUIRED_PACKAGES 下的 setup.py 文件中。

安装 Bazel

安装 Bazel,它是用于编译 TensorFlow 的构建工具。

将 Bazel 可执行文件的位置添加到 PATH 环境变量中。

安装支持 GPU 的版本(可选,仅限 Linux)

没有针对 macOS 的 GPU 支持版本。

要安装在 GPU 上运行 TensorFlow 所需的驱动程序和其他软件,请参阅 GPU 支持指南。

注意:您可以轻松地设置 TensorFlow 的某个支持 GPU 的  Docker 映像

下载 TensorFlow 源代码

使用 Git 克隆 TensorFlow 代码库

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

代码库默认为 master 开发分支。您也可以检出要构建的版本分支

git checkout branch_name  # r1.9, r1.10, etc.

要测试源代码树的副本,请对 r1.12 及更早版本运行以下测试(这可能需要一段时间):

bazel test -c opt -- //tensorflow/... -//tensorflow/compiler/... -//tensorflow/contrib/lite/...

对于 r1.12 之后的版本(如 master),请运行以下命令:

bazel test -c opt -- //tensorflow/... -//tensorflow/compiler/... -//tensorflow/lite/...
要点:如果您在使用最新的开发分支时遇到构建问题,请尝试已知可用的版本分支。

配置构建

通过在 TensorFlow 源代码树的根目录下运行以下命令来配置系统构建:

./configure

此脚本会提示您指定 TensorFlow 依赖项的位置,并要求指定其他构建配置选项(例如,编译器标记)。以下代码展示了 ./configure 的示例运行会话(您的会话可能会有所不同):

查看示例配置会话

./configure
You have bazel 0.15.0 installed.
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python2.7

Found possible Python library paths:
  /usr/local/lib/python2.7/dist-packages
  /usr/lib/python2.7/dist-packages
Please input the desired Python library path to use.  Default is [/usr/lib/python2.7/dist-packages]

Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]:
jemalloc as malloc support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]:
Google Cloud Platform support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Hadoop File System support? [Y/n]:
Hadoop File System support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Amazon AWS Platform support? [Y/n]:
Amazon AWS Platform support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Apache Kafka Platform support? [Y/n]:
Apache Kafka Platform support will be enabled for TensorFlow.

Do you wish to build TensorFlow with XLA JIT support? [y/N]:
No XLA JIT support will be enabled for TensorFlow.

Do you wish to build TensorFlow with GDR support? [y/N]:
No GDR support will be enabled for TensorFlow.

Do you wish to build TensorFlow with VERBS support? [y/N]:
No VERBS support will be enabled for TensorFlow.

Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]:
No OpenCL SYCL support will be enabled for TensorFlow.

Do you wish to build TensorFlow with CUDA support? [y/N]: Y
CUDA support will be enabled for TensorFlow.

Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 9.0]: 9.0

Please specify the location where CUDA 9.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:

Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7.0]: 7.0

Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda]:

Do you wish to build TensorFlow with TensorRT support? [y/N]:
No TensorRT support will be enabled for TensorFlow.

Please specify the NCCL version you want to use. If NCLL 2.2 is not installed, then you can use version 1.3 that can be fetched automatically but it may have worse performance with multiple GPUs. [Default is 2.2]: 1.3

Please specify a list of comma-separated Cuda compute capabilities you want to build with.
You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
Please note that each additional compute capability significantly increases your
build time and binary size. [Default is: 3.5,7.0] 6.1

Do you want to use clang as CUDA compiler? [y/N]:
nvcc will be used as CUDA compiler.

Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]:

Do you wish to build TensorFlow with MPI support? [y/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]:

Would you like to interactively configure ./WORKSPACE for Android builds? [y/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

配置选项

对于 GPU 支持,请指定 CUDA 和 cuDNN 的版本。如果您的系统安装了多个 CUDA 或 cuDNN 版本,请明确设置版本而不是依赖于默认版本。./configure 会创建指向系统 CUDA 库的符号链接,因此,如果您更新 CUDA 库路径,则必须在构建之前再次运行此配置步骤。

对于编译优化标记,默认值 (-march=native) 会优化针对计算机的 CPU 类型生成的代码。但是,如果要针对不同类型的 CPU 构建 TensorFlow,请考虑指定一个更加具体的优化标记。要查看相关示例,请参阅 GCC 手册

您可以将一些预先配置好的构建配置添加到 bazel build 命令中,例如:

  • --config=mkl - 支持 Intel® MKL-DNN
  • --config=monolithic - 此配置适用于基本保持静态的单体构建。
注意:从 TensorFlow 1.6 开始,二进制文件使用 AVX 指令,这些指令可能无法在旧版 CPU 上运行。

构建 pip 软件包

Bazel 构建

仅支持 CPU

使用 bazel 构建仅支持 CPU 的 TensorFlow 软件包构建器:

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

GPU 支持

要构建支持 GPU 的 TensorFlow 软件包构建器,请运行以下命令:

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

Bazel 构建选项

从源代码构建 TensorFlow 可能会消耗大量内存。如果系统内存有限,请使用以下命令限制 Bazel 的内存消耗量:--local_resources 2048,.5,1.0

官方 TensorFlow 软件包是使用 GCC 4 构建的,并使用旧版 ABI。对于 GCC 5 及更高版本,为了使您的构建与旧版 ABI 兼容,请使用 --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0"。兼容 ABI 可确保针对官方 TensorFlow pip 软件包构建的自定义操作继续支持使用 GCC 5 构建的软件包。

构建软件包

bazel build 命令会创建一个名为 build_pip_package 的可执行文件,此文件是用于构建 pip 软件包的程序。例如,以下命令会在 /tmp/tensorflow_pkg 目录中构建 .whl 软件包:

./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

尽管可以在同一个源代码树下构建 CUDA 和非 CUDA 配置,但建议您在同一个源代码树中的这两种配置之间切换时运行 bazel clean

安装软件包

生成的 .whl 文件的文件名取决于 TensorFlow 版本和您的平台。例如,使用 pip install 安装软件包:

pip install /tmp/tensorflow_pkg/tensorflow-version-tags.whl
成功:TensorFlow 现已安装完毕。

Docker Linux 构建

借助 TensorFlow 的 Docker 开发映像,您可以轻松设置环境,以从源代码构建 Linux 软件包。这些映像已包含构建 TensorFlow 所需的源代码和依赖项。要了解安装说明和可用映像标记的列表,请参阅 TensorFlow Docker 指南

仅支持 CPU

以下示例使用 :nightly-devel 映像从最新的 TensorFlow 源代码构建仅支持 CPU 的 Python 2 软件包。要了解可用的 TensorFlow -devel 标记,请参阅 Docker 指南

下载最新的开发映像并启动 Docker 容器(用于构建 pip 软件包):

docker pull tensorflow/tensorflow:nightly-devel
docker run -it -w /tensorflow -v $PWD:/mnt -e HOST_PERMS="$(id -u):$(id -g)" \
    tensorflow/tensorflow:nightly-devel bash

git pull  # within the container, download the latest source code

上面的 docker run 命令会在 /tensorflow 目录(源代码树的根目录)中启动 shell。它会在该容器的 /mnt 目录中装载主机的当前目录,并通过一个环境变量将主机用户的信息传递给该容器(用于设置权限,Docker 会让此过程变得很复杂)。

或者,要在容器中构建 TensorFlow 的主机副本,请在该容器的 /tensorflow 目录中装载主机源代码树:

docker run -it -w /tensorflow -v /path/to/tensorflow:/tensorflow -v $PWD:/mnt \
    -e HOST_PERMS="$(id -u):$(id -g)" tensorflow/tensorflow:nightly-devel bash

设置好源代码树后,在该容器的虚拟环境中构建 TensorFlow 软件包:

  1. 配置构建 - 此时会提示用户回答构建配置问题。
  2. 构建用于创建 pip 软件包的工具。
  3. 运行该工具,以创建 pip 软件包。
  4. 调整文件在容器外部的所有权。
./configure  # answer prompts or use defaults

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

./bazel-bin/tensorflow/tools/pip_package/build_pip_package /mnt  # create package

chown $HOST_PERMS /mnt/tensorflow-version-tags.whl

在该容器中安装并验证软件包:

pip uninstall tensorflow  # remove current version

pip install /mnt/tensorflow-version-tags.whl
cd /tmp  # don't import from source directory
python -c "import tensorflow as tf; print(tf.__version__)"
成功:TensorFlow 现已安装完毕。

在主机上,TensorFlow pip 软件包位于当前目录(具有主机用户权限)中:./tensorflow-version-tags.whl

GPU 支持

Docker 是为 TensorFlow 构建 GPU 支持的最简单方法,因为主机只需安装 NVIDIA® 驱动程序(无需安装 NVIDIA® CUDA® 工具包)。要设置 nvidia-docker,请参阅 GPU 支持指南和 TensorFlow Docker 指南(仅限 Linux)。

以下示例会下载 TensorFlow :nightly-devel-gpu-py3 映像并使用 nvidia-docker 运行支持 GPU 的容器。此开发映像配置为构建支持 GPU 的 Python 3 pip 软件包:

docker pull tensorflow/tensorflow:nightly-devel-gpu-py3
docker run --runtime=nvidia -it -w /tensorflow -v $PWD:/mnt -e HOST_PERMS="$(id -u):$(id -g)" \
    tensorflow/tensorflow:nightly-devel-gpu-py3 bash

然后,在该容器的虚拟环境中,构建支持 GPU 的 TensorFlow 软件包:

./configure  # answer prompts or use defaults

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

./bazel-bin/tensorflow/tools/pip_package/build_pip_package /mnt  # create package

chown $HOST_PERMS /mnt/tensorflow-version-tags.whl

在该容器中安装和验证软件包并检查是否有 GPU:

pip uninstall tensorflow  # remove current version

pip install /mnt/tensorflow-version-tags.whl
cd /tmp  # don't import from source directory
python -c "import tensorflow as tf; print(tf.contrib.eager.num_gpus())"
成功:TensorFlow 现已安装完毕。

经过测试的构建配置

Linux

版本 Python 版本 编译器 构建工具
tensorflow-1.12.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.15.0
tensorflow-1.11.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.15.0
tensorflow-1.10.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.15.0
tensorflow-1.9.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.11.0
tensorflow-1.8.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.10.0
tensorflow-1.7.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.10.0
tensorflow-1.6.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.9.0
tensorflow-1.5.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.8.0
tensorflow-1.4.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.5.4
tensorflow-1.3.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.4.5
tensorflow-1.2.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.4.5
tensorflow-1.1.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.4.2
tensorflow-1.0.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.4.2
版本 Python 版本 编译器 构建工具 cuDNN CUDA
tensorflow_gpu-1.12.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.15.0 7 9
tensorflow_gpu-1.11.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.15.0 7 9
tensorflow_gpu-1.10.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.15.0 7 9
tensorflow_gpu-1.9.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.11.0 7 9
tensorflow_gpu-1.8.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.10.0 7 9
tensorflow_gpu-1.7.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.9.0 7 9
tensorflow_gpu-1.6.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.9.0 7 9
tensorflow_gpu-1.5.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.8.0 7 9
tensorflow_gpu-1.4.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.5.4 6 8
tensorflow_gpu-1.3.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.4.5 6 8
tensorflow_gpu-1.2.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.4.5 5.1 8
tensorflow_gpu-1.1.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.4.2 5.1 8
tensorflow_gpu-1.0.0 2.7、3.3-3.6 GCC 4.8 Bazel 0.4.2 5.1 8

macOS

CPU

版本 Python 版本 编译器 构建工具
tensorflow-1.12.0 2.7、3.3-3.6 XCode 中的 Clang Bazel 0.15.0
tensorflow-1.11.0 2.7、3.3-3.6 XCode 中的 Clang Bazel 0.15.0
tensorflow-1.10.0 2.7、3.3-3.6 XCode 中的 Clang Bazel 0.15.0
tensorflow-1.9.0 2.7、3.3-3.6 XCode 中的 Clang Bazel 0.11.0
tensorflow-1.8.0 2.7、3.3-3.6 XCode 中的 Clang Bazel 0.10.1
tensorflow-1.7.0 2.7、3.3-3.6 XCode 中的 Clang Bazel 0.10.1
tensorflow-1.6.0 2.7、3.3-3.6 XCode 中的 Clang Bazel 0.8.1
tensorflow-1.5.0 2.7、3.3-3.6 XCode 中的 Clang Bazel 0.8.1
tensorflow-1.4.0 2.7、3.3-3.6 XCode 中的 Clang Bazel 0.5.4
tensorflow-1.3.0 2.7、3.3-3.6 XCode 中的 Clang Bazel 0.4.5
tensorflow-1.2.0 2.7、3.3-3.6 XCode 中的 Clang Bazel 0.4.5
tensorflow-1.1.0 2.7、3.3-3.6 XCode 中的 Clang Bazel 0.4.2
tensorflow-1.0.0 2.7、3.3-3.6 XCode 中的 Clang Bazel 0.4.2

GPU

版本 Python 版本 编译器 构建工具 cuDNN CUDA
tensorflow_gpu-1.1.0 2.7、3.3-3.6 XCode 中的 Clang Bazel 0.4.2 5.1 8
tensorflow_gpu-1.0.0 2.7、3.3-3.6 XCode 中的 Clang Bazel 0.4.2 5.1 8

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 3.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

猜你喜欢

转载自www.cnblogs.com/2008nmj/p/10355527.html