从源代码构建 | TensorFlow


本站和网页 https://tensorflow.google.cn/install/source#docker_linux_builds 的作者无关,不对其内容负责。快照谨为网络故障时之索引,不代表被搜索网站的即时页面。

从源代码构建 | TensorFlow
安装
学习
简介
TensorFlow 新手?
TensorFlow
核心的开源机器学习库
针对 JavaScript
使用 JavaScript 进行机器学习开发的 TensorFlow.js
针对移动设备和边缘设备
针对移动设备和边缘设备推出的 TensorFlow Lite
针对生产环境
针对端到端机器学习组件推出的 TensorFlow Extended
API
TensorFlow (v2.11.0)
Versions…
TensorFlow.js
TensorFlow Lite
TFX
资源
模型和数据集
由 Google 和社区构建的预训练模型和数据集
工具
由各种可助您使用 TensorFlow 的工具构成的生态系统
库和扩展程序
在 TensorFlow 基础上构建的库和扩展程序
TensorFlow 认证计划
拿下可证明您精通机器学习技术的证书,让自己脱颖而出
学习机器学习知识
帮助您掌握使用 TensorFlow 进行机器学习的基础知识教育资源
Responsible AI
帮助您在机器学习工作流中落实 Responsible AI 做法的资源和工具
社区
加入
探索参与其中的方式
博客
及时了解 TensorFlow 的所有资讯
论坛 ↗
TensorFlow 社区的讨论平台
群组
用户群组、兴趣小组和邮寄名单
贡献
关于贡献代码和文档的指南
选择 TensorFlow 的原因
简介
案例研究
English
Bahasa Indonesia
Español – América Latina
Français
Italiano
Polski
Português – Brasil
Tiếng Việt
Türkçe
Русский
עברית
العربيّة
فارسی
हिंदी
বাংলা
ภาษาไทย
中文 – 简体
中文 – 繁體
日本語
한국어
GitHub
安装
安装
学习
更多
API
更多
资源
更多
社区
更多
选择 TensorFlow 的原因
更多
GitHub
安装 TensorFlow
软件包
pip
Docker
其他设置
GPU 设备插件
问题
从源代码构建
Linux/macOS
Windows
SIG Build
语言绑定
Java
Java(旧版)
Go
简介
TensorFlow
针对 JavaScript
针对移动设备和边缘设备
针对生产环境
TensorFlow (v2.11.0)
Versions…
TensorFlow.js
TensorFlow Lite
TFX
模型和数据集
工具
库和扩展程序
TensorFlow 认证计划
学习机器学习知识
Responsible AI
加入
博客
论坛 ↗
群组
贡献
简介
案例研究
Check out sessions from the WiML Symposium covering diffusion models with KerasCV, on-device ML, and more. Watch on demand
TensorFlow
安装
从源代码构建
从源代码构建 TensorFlow pip 软件包并将其安装在 Ubuntu Linux 和 macOS 上。尽管这些说明可能适用于其他系统,但仅针对 Ubuntu 和 macOS 进行了测试并在这两种平台上受支持。
注意:我们已针对 Linux 和 macOS 系统提供了经过充分测试的预构建 TensorFlow 软件包。
Linux 和 macOS 设置
安装以下构建工具以配置开发环境。
安装 Python 和 TensorFlow 软件包依赖项
Ubuntu
sudo apt install python3-dev python3-pip
macOS
需要 Xcode 9.2 或更高版本。
使用 Homebrew 软件包管理器安装:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
export PATH="/usr/local/opt/python/libexec/bin:$PATH"
# if you are on macOS 10.12 (Sierra) use export PATH="/usr/local/bin:/usr/local/sbin:$PATH"
brew install python
安装 TensorFlow pip 软件包依赖项(如果使用虚拟环境,请省略 --user 参数):
pip install -U --user pip numpy wheel
pip install -U --user keras_preprocessing --no-deps
注意:必须使用 pip 19.0 以上的版本才能安装 TensorFlow 2 .whl 软件包。请参见 setup.py 文件中的 REQUIRED_PACKAGES 部分,了解其他必需的依赖项。
安装 Bazel
您需要安装 Bazel,才能构建 TensorFlow。您可以使用 Bazelisk 轻松安装 Bazel,并且 Bazelisk 可以自动为 TensorFlow 下载合适的 Bazel 版本。为便于使用,请在 PATH 中将 Bazelisk 添加为 bazel 可执行文件。
如果没有 Bazelisk,您可以手动安装 Bazel。请务必安装受支持的 Bazel 版本,可以是 tensorflow/configure.py 中指定的介于 _TF_MIN_BAZEL_VERSION 和 _TF_MAX_BAZEL_VERSION 之间的任意版本。
安装 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 # r2.2, r2.3, etc.
配置 build
通过运行 TensorFlow 源代码树根目录下的 ./configure 配置系统 build。此脚本会提示您指定 TensorFlow 依赖项的位置,并要求指定其他构建配置选项(例如,编译器标记)。
./configure
如果使用的是虚拟环境,python configure.py 会优先处理环境内的路径,而 ./configure 会优先处理环境外的路径。在这两种情况下,您都可以更改默认设置。
示例会话
以下代码展示了 ./configure 脚本的示例运行会话(您的会话可能会有所不同):
查看示例配置会话
./configure
You have bazel 3.0.0 installed.
Please specify the location of python. [Default is /usr/bin/python3]:
Found possible Python library paths:
/usr/lib/python3/dist-packages
/usr/local/lib/python3.6/dist-packages
Please input the desired Python library path to use. Default is [/usr/lib/python3/dist-packages]
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 ROCm support? [y/N]:
No ROCm 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.
Do you wish to build TensorFlow with TensorRT support? [y/N]:
No TensorRT support will be enabled for TensorFlow.
Found CUDA 10.1 in:
/usr/local/cuda-10.1/targets/x86_64-linux/lib
/usr/local/cuda-10.1/targets/x86_64-linux/include
Found cuDNN 7 in:
/usr/lib/x86_64-linux-gnu
/usr/include
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 Each capability can be specified as "x.y" or "compute_xy" to include both virtual and binary GPU code, or as "sm_xy" to only include the binary code.
Please note that each additional compute capability significantly increases your build time and binary size, and that TensorFlow only supports compute capabilities >= 3.5 [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]:
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]:
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=ngraph # Build with Intel nGraph support.
--config=numa # Build with NUMA support.
--config=dynamic_kernels # (Experimental) Build kernels into separate shared objects.
--config=v2 # Build TensorFlow 2.x instead of 1.x.
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=nonccl # Disable NVIDIA NCCL support.
Configuration finished
配置选项
GPU 支持
为了支持 GPU,请在配置过程中设置 cuda=Y 并指定 CUDA 和 cuDNN 的版本。如果您的系统安装了多个版本的 CUDA 或 cuDNN,请明确设置版本,而不是依赖于默认版本。./configure 会创建指向系统 CUDA 库的符号链接,因此,如果您更新 CUDA 库路径,就必须在构建之前再次运行此配置步骤。
优化
对于编译优化标记,默认值 (-march=native) 会优化针对计算机的 CPU 类型生成的代码。但是,如果要针对不同类型的 CPU 构建 TensorFlow,请考虑指定一个更加具体的优化标记。请参阅 GCC 手册查看示例。
预先配置好的配置
您可以将一些预先配置好的 build 配置添加到 bazel build 命令中,例如:
--config=dbg - 构建时提供调试信息。详情请参阅 CONTRIBUTING.md。
--config=mkl - 支持 Intel® MKL-DNN。
--config=monolithic:此配置适用于基本保持静态的单体 build。
--config=v1:用于构建 TensorFlow 1.x,而不是 2.x。
注意:从 TensorFlow 1.6 开始,二进制文件使用 AVX 指令,这些指令可能无法在旧版 CPU 上运行。
构建 pip 软件包
TensorFlow 2.x
请安装 Bazel 并使用 bazel build 创建仅支持 CPU 的 TensorFlow 2.x 软件包:
bazel build [--config=option] //tensorflow/tools/pip_package:build_pip_package
注意:若要支持 GPU,您可在 ./configure 阶段使用 cuda=Y。
GPU 支持
若要构建支持 GPU 的 TensorFlow 软件包编译器,请运行以下命令:
bazel build --config=cuda [--config=option] //tensorflow/tools/pip_package:build_pip_package
TensorFlow 1.x
若要构建旧版 TensorFlow 1.x 软件包,请使用 --config=v1 选项:
bazel build --config=v1 [--config=option] //tensorflow/tools/pip_package:build_pip_package
Bazel 构建选项
请参阅 Bazel 命令行参考文档,详细了解构建选选项。
从源代码构建 TensorFlow 会消耗大量 RAM。如果您的系统受内存限制,请将 Bazel 的 RAM 使用量限制为:--local_ram_resources=2048。
官方 TensorFlow 软件包是使用符合 manylinux2010 软件包标准的 GCC 7.3 工具链构建的。
对于 GCC 5 及更高版本,可以使用 --cxxopt="-D_GLIBCXX_USE_CXX11_ABI=0" 进行构建,以与旧版 ABI 兼容。兼容 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
如需从 master 分支构建,请使用 --nightly_flag 获取合适的依赖项:
./bazel-bin/tensorflow/tools/pip_package/build_pip_package --nightly_flag /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
以下示例使用 :devel 映像从最新的 TensorFlow 源代码构建仅支持 CPU 的软件包。如需了解可用的 TensorFlow -devel 标记,请参阅 Docker 指南。
下载最新的开发映像并启动 Docker 容器(用于构建 pip 软件包):
docker pull tensorflow/tensorflow:devel
docker run -it -w /tensorflow_src -v $PWD:/mnt -e HOST_PERMS="$(id -u):$(id -g)" \
tensorflow/tensorflow:devel bash
git pull # within the container, download the latest source code
上面的 docker run 命令会在 /tensorflow_src 目录(即源代码树的根目录)中启动 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:devel bash
设置好源代码树后,在该容器的虚拟环境中构建 TensorFlow 软件包:
配置构建 - 此时会提示用户回答构建配置问题。
构建用于创建 pip 软件包的工具。
运行该工具,以创建 pip 软件包。
调整文件在容器外部的所有权。
./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 :devel-gpu 映像并使用 nvidia-docker 运行支持 GPU 的容器。此开发映像配置为构建支持 GPU 的 pip 软件包:
docker pull tensorflow/tensorflow:devel-gpu
docker run --gpus all -it -w /tensorflow -v $PWD:/mnt -e HOST_PERMS="$(id -u):$(id -g)" \
tensorflow/tensorflow:devel-gpu bash
git pull # within the container, download the latest source code
然后,在该容器的虚拟环境中,构建支持 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(\"Num GPUs Available: \", len(tf.config.list_physical_devices('GPU')))"
成功:TensorFlow 现已安装完毕。
经过测试的构建配置
Linux
CPU
版本Python 版本编译器构建工具
tensorflow-2.6.03.6-3.9GCC 7.3.1Bazel 3.7.2
tensorflow-2.5.03.6-3.9GCC 7.3.1Bazel 3.7.2
tensorflow-2.4.03.6-3.8GCC 7.3.1Bazel 3.1.0
tensorflow-2.3.03.5-3.8GCC 7.3.1Bazel 3.1.0
tensorflow-2.2.03.5-3.8GCC 7.3.1Bazel 2.0.0
tensorflow-2.1.02.7、3.5-3.7GCC 7.3.1Bazel 0.27.1
tensorflow-2.0.02.7、3.3-3.7GCC 7.3.1Bazel 0.26.1
tensorflow-1.15.02.7、3.3-3.7GCC 7.3.1Bazel 0.26.1
tensorflow-1.14.02.7、3.3-3.7GCC 4.8Bazel 0.24.1
tensorflow-1.13.12.7、3.3-3.7GCC 4.8Bazel 0.19.2
tensorflow-1.12.02.7、3.3-3.6GCC 4.8Bazel 0.15.0
tensorflow-1.11.02.7、3.3-3.6GCC 4.8Bazel 0.15.0
tensorflow-1.10.02.7、3.3-3.6GCC 4.8Bazel 0.15.0
tensorflow-1.9.02.7、3.3-3.6GCC 4.8Bazel 0.11.0
tensorflow-1.8.02.7、3.3-3.6GCC 4.8Bazel 0.10.0
tensorflow-1.7.02.7、3.3-3.6GCC 4.8Bazel 0.10.0
tensorflow-1.6.02.7、3.3-3.6GCC 4.8Bazel 0.9.0
tensorflow-1.5.02.7、3.3-3.6GCC 4.8Bazel 0.8.0
tensorflow-1.4.02.7、3.3-3.6GCC 4.8Bazel 0.5.4
tensorflow-1.3.02.7、3.3-3.6GCC 4.8Bazel 0.4.5
tensorflow-1.2.02.7、3.3-3.6GCC 4.8Bazel 0.4.5
tensorflow-1.1.02.7、3.3-3.6GCC 4.8Bazel 0.4.2
tensorflow-1.0.02.7、3.3-3.6GCC 4.8Bazel 0.4.2
GPU
版本Python 版本编译器构建工具cuDNNCUDA
tensorflow-2.6.03.6-3.9GCC 7.3.1Bazel 3.7.28.111.2
tensorflow-2.5.03.6-3.9GCC 7.3.1Bazel 3.7.28.111.2
tensorflow-2.4.03.6-3.8GCC 7.3.1Bazel 3.1.08.011.0
tensorflow-2.3.03.5-3.8GCC 7.3.1Bazel 3.1.07.610.1
tensorflow-2.2.03.5-3.8GCC 7.3.1Bazel 2.0.07.610.1
tensorflow-2.1.02.7、3.5-3.7GCC 7.3.1Bazel 0.27.17.610.1
tensorflow-2.0.02.7、3.3-3.7GCC 7.3.1Bazel 0.26.17.410.0
tensorflow_gpu-1.15.02.7、3.3-3.7GCC 7.3.1Bazel 0.26.17.410.0
tensorflow_gpu-1.14.02.7、3.3-3.7GCC 4.8Bazel 0.24.17.410.0
tensorflow_gpu-1.13.12.7、3.3-3.7GCC 4.8Bazel 0.19.27.410.0
tensorflow_gpu-1.12.02.7、3.3-3.6GCC 4.8Bazel 0.15.079
tensorflow_gpu-1.11.02.7、3.3-3.6GCC 4.8Bazel 0.15.079
tensorflow_gpu-1.10.02.7、3.3-3.6GCC 4.8Bazel 0.15.079
tensorflow_gpu-1.9.02.7、3.3-3.6GCC 4.8Bazel 0.11.079
tensorflow_gpu-1.8.02.7、3.3-3.6GCC 4.8Bazel 0.10.079
tensorflow_gpu-1.7.02.7、3.3-3.6GCC 4.8Bazel 0.9.079
tensorflow_gpu-1.6.02.7、3.3-3.6GCC 4.8Bazel 0.9.079
tensorflow_gpu-1.5.02.7、3.3-3.6GCC 4.8Bazel 0.8.079
tensorflow_gpu-1.4.02.7、3.3-3.6GCC 4.8Bazel 0.5.468
tensorflow_gpu-1.3.02.7、3.3-3.6GCC 4.8Bazel 0.4.568
tensorflow_gpu-1.2.02.7、3.3-3.6GCC 4.8Bazel 0.4.55.18
tensorflow_gpu-1.1.02.7、3.3-3.6GCC 4.8Bazel 0.4.25.18
tensorflow_gpu-1.0.02.7、3.3-3.6GCC 4.8Bazel 0.4.25.18
macOS
CPU
版本Python 版本编译器构建工具
tensorflow-2.6.03.6-3.9Xcode 10.11 中的 ClangBazel 3.7.2
tensorflow-2.5.03.6-3.9Xcode 10.11 中的 ClangBazel 3.7.2
tensorflow-2.4.03.6-3.8Xcode 10.3 中的 ClangBazel 3.1.0
tensorflow-2.3.03.5-3.8Xcode 10.1 中的 ClangBazel 3.1.0
tensorflow-2.2.03.5-3.8Xcode 10.1 中的 ClangBazel 2.0.0
tensorflow-2.1.02.7、3.5-3.7Xcode 10.1 中的 ClangBazel 0.27.1
tensorflow-2.0.02.7、3.5-3.7Xcode 10.1 中的 ClangBazel 0.27.1
tensorflow-2.0.02.7、3.3-3.7Xcode 10.1 中的 ClangBazel 0.26.1
tensorflow-1.15.02.7、3.3-3.7Xcode 10.1 中的 ClangBazel 0.26.1
tensorflow-1.14.02.7、3.3-3.7Xcode 中的 ClangBazel 0.24.1
tensorflow-1.13.12.7、3.3-3.7Xcode 中的 ClangBazel 0.19.2
tensorflow-1.12.02.7、3.3-3.6Xcode 中的 ClangBazel 0.15.0
tensorflow-1.11.02.7、3.3-3.6Xcode 中的 ClangBazel 0.15.0
tensorflow-1.10.02.7、3.3-3.6Xcode 中的 ClangBazel 0.15.0
tensorflow-1.9.02.7、3.3-3.6Xcode 中的 ClangBazel 0.11.0
tensorflow-1.8.02.7、3.3-3.6Xcode 中的 ClangBazel 0.10.1
tensorflow-1.7.02.7、3.3-3.6Xcode 中的 ClangBazel 0.10.1
tensorflow-1.6.02.7、3.3-3.6Xcode 中的 ClangBazel 0.8.1
tensorflow-1.5.02.7、3.3-3.6Xcode 中的 ClangBazel 0.8.1
tensorflow-1.4.02.7、3.3-3.6Xcode 中的 ClangBazel 0.5.4
tensorflow-1.3.02.7、3.3-3.6Xcode 中的 ClangBazel 0.4.5
tensorflow-1.2.02.7、3.3-3.6Xcode 中的 ClangBazel 0.4.5
tensorflow-1.1.02.7、3.3-3.6Xcode 中的 ClangBazel 0.4.2
tensorflow-1.0.02.7、3.3-3.6Xcode 中的 ClangBazel 0.4.2
GPU
版本Python 版本编译器构建工具cuDNNCUDA
tensorflow_gpu-1.1.02.7、3.3-3.6Xcode 中的 ClangBazel 0.4.25.18
tensorflow_gpu-1.0.02.7、3.3-3.6Xcode 中的 ClangBazel 0.4.25.18
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.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.
Last updated 2021-09-21 UTC.
[]
[]
掌握动态
博客
GitHub
Twitter
哔哩哔哩
支持
问题跟踪器
版本说明
Stack Overflow
品牌指南
引用 TensorFlow
条款
隐私权政策
ICP证合字B2-20070004号
订阅 TensorFlow 简报
订阅
English
Bahasa Indonesia
Español – América Latina
Français
Italiano
Polski
Português – Brasil
Tiếng Việt
Türkçe
Русский
עברית
العربيّة
فارسی
हिंदी
বাংলা
ภาษาไทย
中文 – 简体
中文 – 繁體
日本語
한국어