从源代码构建TensorFlow流程记录
京东科技隐私计算产品部 曹雨晨
为什么从源代码构建
通常情况下,直接安装构建好的.whl即可。不过,当需要一些特殊配置(或者闲来无事想体会 TensorFlow 构建过程到底有多麻烦)的时候,则需要选择从源代码构建TensorFlow。万幸文档混乱的 TensorFlow 还是好心地为我们提供了一整页的文档供参考 https://www.tensorflow.org/install/source?hl=zh-cn ,个人认为其中最需要关注的部分莫过于经过测试供参考的源配置(列于文末)。TF使用 Google 的开源构建工具 bazel 构建,并且源码的版本与 bazel 的版本高度相关,所以尽量匹配版本进行构建。
流程记录 TF v1.14.0 CPU on Ubuntu 18.04
安装对应版本的 bazel
根据计划构建的版本,查阅文末的对应配置,参考官方文档: https://bazel.build/install/ubuntu 安装相应版本的 bazel,如本次计划构建的版本是 v1.14.0,对应的 bazel 版本是 0.24.1(此次使用0.26.1也是可以的)。
为方便,这里直接贴出对应 0.26.1 release 的页面: https://github.com/bazelbuild/bazel/releases?expanded=true&page=2&q=0.26.1 ,点击assets找到对应的文件下载即可。
wget https://github.com/bazelbuild/bazel/releases/download/0.26.1/bazel-0.26.1-installer-linux-x86_64.sh
chmod +x bazel-version-installer-linux-x86_64.sh
./bazel-version-installer-linux-x86_64.sh --user
克隆 TensorFlow 仓库
从 Github 上 clone 源码仓库
git clone https://github.com/tensorflow/tensorflow
cd 到仓库目录并 git checkout到相应 tag,比如这次是构建 v1.14.0 版本:
git checkout v1.14.0
* 一些小调整,通常可以略过
Build with C++17
因为之后需要写的 Custom OP 依赖的另一个库是 C++17,而除了刚刚才发布的 v2.10 版,以前的 TF默认是使用 C++11,实际构建的时候,代码有一些 minor fix。此处参考 https://github.com/tensorflow/tensorflow/pull/30836/files 修改 .bazelrc 里 build:c++17 的配置,在 tensorflow/core/lib/gif/gif_io.cc 中添加 #include<cstring>, 并在 tensorflow/stream_executor/stream_executor_pimpl.h 中添加 #include "absl/memory/memory.h"(否则 compile 时会报错找不到 absl::make_unique)(这里 make_unique 是 C++17 标准库里的用法,Google的abseil的make_unique方法则方便C++11的代码也可以使用它;最新的v2.10版由于默认使用C++17,已经改为std::make_unique)
.bazelrc 文件里记录了构建时各种配置选项 ([--config=option])的映射规则,如有需要可以进行修改。由于 GCC 不支持--stdlib命令,此次修改如下:
# Build TF with C++ 17 features.
- build:c++17 --cxxopt=-std=c++1z
- build:c++17 --cxxopt=-stdlib=libc++
+ build:c++17 --cxxopt=-std=c++17
网络不通
Bazel 在构建过程中,需要现拉取远程仓库的许多依赖。由于 TF 的构建过程消耗内存很严重,选择在服务器上进行构建,而服务器远程拉取 github 上仓库经常失败。所以需要手动在网络良好的机器上下载相应的库的 release (对应的版本在 WORKSPACE 文件中可以找到一行注释),存放在服务器本地,并在 WORKSPACE 文件中对应的 http_archive 部分添加一行本地地址。若需要换版本,也可以在相应github库的releases下面找到对应的 URL 及 sha256(实在是找不到对应的也可以手动下载压缩包后通过 shasum256 命令获取)
例如:
http_archive(
name = "build_bazel_rules_apple",
sha256 = "a045a436b642c70fb0c10ca84ff0fd2dcbd59cc89100d597a61e8374afafb366",
urls = ["https://github.com/bazelbuild/rules_apple/releases/download/0.18.0/rules_apple.0.18.0.tar.gz",
"file:///opt/tensorflow_build_deps/rules_apple.0.18.0.tar.gz"],
) # https://github.com/bazelbuild/rules_apple/releases
配置 build
运行源码根目录下的 ./configure 进行配置。
./configure
此次编译一个尽量简略的 CPU 版本,会话如下:
WARNING: --batch mode is deprecated. Please instead explicitly shut down your Bazel server using the command "bazel shutdown".
You have bazel 0.26.1 installed.
Please specify the location of python. [Default is /usr/local/bin/python]:
Found possible Python library paths:
/usr/local/lib/python3.6/dist-packages
/usr/lib/python3/dist-packages
Please input the desired Python library path to use. Default is [/usr/local/lib/python3.6/dist-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.
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=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
构建 pip 软件包并安装
官方提供的命令:
bazel build [--config=option] //tensorflow/tools/pip_package:build_pip_package
本次使用:
bazel build --config=c++17 --config=c++1z --jobs=6 //tensorflow/tools/pip_package:build_pip_package
其中使用的 --config=c++17 --config=c++1z 对应刚刚修改的 .bazelrc 文件中相应的部分
注意:bazel build 的过程时间会比较长,对内存的消耗较大,jobs 数谨慎开大。
bazel build 结束后,一个名为 build_pip_package 的可执行文件就创建好了,接下来可以执行:
./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
如果希望构建的whl名为 tf-nightly 版本,则可以加上 --nightly_flag 的选项。
./bazel-bin/tensorflow/tools/pip_package/build_pip_package --nightly_flag /tmp/tensorflow_pkg
此后便获得了 .whl 文件,通过 pip 安装即可:
pip install /tmp/tensorflow_pkg/tensorflow-[version]-[tags].whl
其中,version是对应的版本,tags与系统有关。
经过测试的源配置
Linux
CPU
| 版本 | Python 版本 | 编译器 | 构建工具 |
|---|---|---|---|
| tensorflow-2.6.0 | 3.6-3.9 | GCC 7.3.1 | Bazel 3.7.2 |
| tensorflow-2.5.0 | 3.6-3.9 | GCC 7.3.1 | Bazel 3.7.2 |
| tensorflow-2.4.0 | 3.6-3.8 | GCC 7.3.1 | Bazel 3.1.0 |
| tensorflow-2.3.0 | 3.5-3.8 | GCC 7.3.1 | Bazel 3.1.0 |
| tensorflow-2.2.0 | 3.5-3.8 | GCC 7.3.1 | Bazel 2.0.0 |
| tensorflow-2.1.0 | 2.7、3.5-3.7 | GCC 7.3.1 | Bazel 0.27.1 |
| tensorflow-2.0.0 | 2.7、3.3-3.7 | GCC 7.3.1 | Bazel 0.26.1 |
| tensorflow-1.15.0 | 2.7、3.3-3.7 | GCC 7.3.1 | Bazel 0.26.1 |
| tensorflow-1.14.0 | 2.7、3.3-3.7 | GCC 4.8 | Bazel 0.24.1 |
| tensorflow-1.13.1 | 2.7、3.3-3.7 | GCC 4.8 | Bazel 0.19.2 |
| 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 |
GPU
| 版本 | Python 版本 | 编译器 | 构建工具 | cuDNN | CUDA |
|---|---|---|---|---|---|
| tensorflow-2.6.0 | 3.6-3.9 | GCC 7.3.1 | Bazel 3.7.2 | 8.1 | 11.2 |
| tensorflow-2.5.0 | 3.6-3.9 | GCC 7.3.1 | Bazel 3.7.2 | 8.1 | 11.2 |
| tensorflow-2.4.0 | 3.6-3.8 | GCC 7.3.1 | Bazel 3.1.0 | 8.0 | 11.0 |
| tensorflow-2.3.0 | 3.5-3.8 | GCC 7.3.1 | Bazel 3.1.0 | 7.6 | 10.1 |
| tensorflow-2.2.0 | 3.5-3.8 | GCC 7.3.1 | Bazel 2.0.0 | 7.6 | 10.1 |
| tensorflow-2.1.0 | 2.7、3.5-3.7 | GCC 7.3.1 | Bazel 0.27.1 | 7.6 | 10.1 |
| tensorflow-2.0.0 | 2.7、3.3-3.7 | GCC 7.3.1 | Bazel 0.26.1 | 7.4 | 10.0 |
| tensorflow_gpu-1.15.0 | 2.7、3.3-3.7 | GCC 7.3.1 | Bazel 0.26.1 | 7.4 | 10.0 |
| tensorflow_gpu-1.14.0 | 2.7、3.3-3.7 | GCC 4.8 | Bazel 0.24.1 | 7.4 | 10.0 |
| tensorflow_gpu-1.13.1 | 2.7、3.3-3.7 | GCC 4.8 | Bazel 0.19.2 | 7.4 | 10.0 |
| 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-2.6.0 | 3.6-3.9 | Xcode 10.11 中的 Clang | Bazel 3.7.2 |
| tensorflow-2.5.0 | 3.6-3.9 | Xcode 10.11 中的 Clang | Bazel 3.7.2 |
| tensorflow-2.4.0 | 3.6-3.8 | Xcode 10.3 中的 Clang | Bazel 3.1.0 |
| tensorflow-2.3.0 | 3.5-3.8 | Xcode 10.1 中的 Clang | Bazel 3.1.0 |
| tensorflow-2.2.0 | 3.5-3.8 | Xcode 10.1 中的 Clang | Bazel 2.0.0 |
| tensorflow-2.1.0 | 2.7、3.5-3.7 | Xcode 10.1 中的 Clang | Bazel 0.27.1 |
| tensorflow-2.0.0 | 2.7、3.5-3.7 | Xcode 10.1 中的 Clang | Bazel 0.27.1 |
| tensorflow-2.0.0 | 2.7、3.3-3.7 | Xcode 10.1 中的 Clang | Bazel 0.26.1 |
| tensorflow-1.15.0 | 2.7、3.3-3.7 | Xcode 10.1 中的 Clang | Bazel 0.26.1 |
| tensorflow-1.14.0 | 2.7、3.3-3.7 | Xcode 中的 Clang | Bazel 0.24.1 |
| tensorflow-1.13.1 | 2.7、3.3-3.7 | Xcode 中的 Clang | Bazel 0.19.2 |
| 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 |
从源代码构建TensorFlow流程记录的更多相关文章
- 基于源代码为树莓派设备构建 TensorFlow
本指南为运行 Raspbian 9.0 操作系统的 Raspberry Pi 嵌入式设备构建 TensorFlow.虽然这些说明可能也适用于其他系列的 Raspberry Pi 设备,但它仅针对此文中 ...
- 3天时间从零到上架AppStore流程记录
3天时间从零到上架AppStore流程记录 清明假期刚过去一周,我如愿以偿把自己想要的一个App上架了AppStore 从有idea到技术选型,从设计稿到框架开发,从提审AppStore到上架一共经历 ...
- 从源代码构建 Go 开发环境
从源代码构建 Go 开发环境 Go 1.5 之前的版本 安装C 语言开发环境 在Go 1.5 之前的版本(比如 1.3.1.4),都会部分的依赖 C 语言的工具链,所以如果你有C 语言的开发环境,就可 ...
- vue框架构建项目流程
构建项目流程: 1.全局查询:node -v 2.全局初始化:npm install --global vue-cli 3.模块化工程:vue init webpack myapp--->y,n ...
- 利用cocoapods管理开源项目,支持 pod install安装整个流程记录(github公有库)
利用cocoapods管理开源项目,支持 pod install安装整个流程记录(github公有库),完成预期的任务,大致有下面几步: 1.代码提交到github平台 2.创建.podspec 3. ...
- CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-总目录
CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-总目录: 0.Windows 10本机下载Xshell,以方便往Linux主机上上传大文件 1.CentOS7+CDH5.14.0安 ...
- ActiveMQ从源代码构建
众多开源项目.我们一般都是直接拿过来用之而后快. 只是我们也应该知道这些项目是怎样从源代码构建而来的. 既然代码是写出来的,就不能避免有BUG存在,话说没有完美的软件,也没有无漏洞的程序. 事实上从源 ...
- SSL免费证书申请以及nginx配置https流程记录
设置https需要ssl 证书,可以通过FreeSSL[https://freessl.org/]申请. 流程记录: 输入域名,如 http://www.youdias.xin 选择品牌,如Let's ...
- webpack4.0构建项目流程
webpack4.0构建项目流程,具体的就不一一唠叨了,这里给出构建流程步骤: 流程大图: 下载高清大图
- TensorFlow使用记录 (六): 优化器
0. tf.train.Optimizer tensorflow 里提供了丰富的优化器,这些优化器都继承与 Optimizer 这个类.class Optimizer 有一些方法,这里简单介绍下: 0 ...
随机推荐
- 释放千行百业数据价值,华为云DAYU有一套
摘要:结合数字化转型中行业面临的挑战及产品解决方案解读数据使能服务DAYU. 大禹(DAYU)治水是一个有美好寓意的故事,大禹汲取了父亲治水的经验教训,总结出一套行之有效的治水方法,对洪水进行治理疏导 ...
- TreeMap - 红黑树
TreeMap是Map家族中的一员,也是用来存放key-value键值对的.平时在工作中使用的可能并不多,它最大的特点是遍历时是有顺序的,根据key的排序规则来 TreeMap是一个双列集合,是Map ...
- ElasticSearch 实现分词全文检索 - filter查询
目录 ElasticSearch 实现分词全文检索 - 概述 ElasticSearch 实现分词全文检索 - ES.Kibana.IK安装 ElasticSearch 实现分词全文检索 - Rest ...
- 断点续传(上传)C#版
断点续传(上传)Java版 1. 客户每次上传前先获取一下当前文件已经被服务器接受了多少 2. 上传时设定偏移量 服务端代码如下: /// <summary> /// 断点续传,获取已上传 ...
- JVM学习-自动内存管理
文章原文:https://gaoyubo.cn/blogs/6997cf1f.html 一.运行时数据区 Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域.这些区域 ...
- OpenShift 与 OpenStack:让云变得更简单
OpenShift 与 OpenStack 都是在 2010.2011 年左右创建的,用于构建可扩展云平台的开源技术,两者都用于在混合云环境中构建可扩展系统.从历史来看,OpenStack 的存在时间 ...
- Leaflet 地图偏移 地图纠偏
(地图瓦片纠偏最好的方法在这:https://www.cnblogs.com/s0611163/p/15606460.html) 地图区域是一个市,偏移量可以近似认为是固定不变的,通过修改Leafle ...
- IDEA 2021.2 新建JavaWeb项目及Tomcat部署
前文:JSP 简单入门与 IDEA 开发环境配置 参考链接: https://zhuanlan.zhihu.com/p/68133583 https://www.cnblogs.com/javabg/ ...
- vue学习笔记 六、ref定义单个数据
系列导航 vue学习笔记 一.环境搭建 vue学习笔记 二.环境搭建+项目创建 vue学习笔记 三.文件和目录结构 vue学习笔记 四.定义组件(组件基本结构) vue学习笔记 五.创建子组件实例 v ...
- RocketMQ(2)---核心概念、特性、使用等
对于RocketMQ而言,感觉官方提供的东西还是可以的:https://github.com/apache/rocketmq/tree/master/docs/cn