TensorFlow源代碼構(gòu)建流程記錄解析
為什么從源代碼構(gòu)建
通常情況下,直接安裝構(gòu)建好的.whl即可。不過,當需要一些特殊配置(或者閑來無事想體會 TensorFlow 構(gòu)建過程到底有多麻煩)的時候,則需要選擇從源代碼構(gòu)建TensorFlow。萬幸文檔混亂的 TensorFlow 還是好心地為我們提供了一整頁的文檔供參考 www.tensorflow.org/install/sou… ,個人認為其中最需要關(guān)注的部分莫過于經(jīng)過測試供參考的源配置(列于文末)。TF使用 Google 的開源構(gòu)建工具 bazel 構(gòu)建,并且源碼的版本與 bazel 的版本高度相關(guān),所以盡量匹配版本進行構(gòu)建。
流程記錄 TF v1.14.0 CPU on Ubuntu 18.04
安裝對應版本的 bazel
根據(jù)計劃構(gòu)建的版本,查閱文末的對應配置,參考官方文檔: bazel.build/install/ubu… 安裝相應版本的 bazel,如本次計劃構(gòu)建的版本是 v1.14.0,對應的 bazel 版本是 0.24.1(此次使用0.26.1也是可以的)。
為方便,這里直接貼出對應 0.26.1 release 的頁面: github.com/bazelbuild/… ,點擊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,比如這次是構(gòu)建 v1.14.0 版本:
git checkout v1.14.0
* 一些小調(diào)整,通常可以略過
Build with C++17
因為之后需要寫的 Custom OP 依賴的另一個庫是 C++17,而除了剛剛才發(fā)布的 v2.10 版,以前的 TF默認是使用 C++11,實際構(gòu)建的時候,代碼有一些 minor fix。此處參考 github.com/tensorflow/… 修改 .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,已經(jīng)改為std::make_unique)
.bazelrc 文件里記錄了構(gòu)建時各種配置選項 ([--config=option])的映射規(guī)則,如有需要可以進行修改。由于 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
網(wǎng)絡(luò)不通
Bazel 在構(gòu)建過程中,需要現(xiàn)拉取遠程倉庫的許多依賴。由于 TF 的構(gòu)建過程消耗內(nèi)存很嚴重,選擇在服務器上進行構(gòu)建,而服務器遠程拉取 github 上倉庫經(jīng)常失敗。所以需要手動在網(wǎng)絡(luò)良好的機器上下載相應的庫的 release (對應的版本在 WORKSPACE 文件中可以找到一行注釋),存放在服務器本地,并在 WORKSPACE 文件中對應的 http_archive 部分添加一行本地地址。若需要換版本,也可以在相應github庫的releases下面找到對應的 URL 及 sha256(實在是找不到對應的也可以手動下載壓縮包后通過 shasum256 命令獲?。?/p>
例如:
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
構(gòu)建 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 的過程時間會比較長,對內(nèi)存的消耗較大,jobs 數(shù)謹慎開大。
bazel build 結(jié)束后,一個名為 build_pip_package 的可執(zhí)行文件就創(chuàng)建好了,接下來可以執(zhí)行:
./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
如果希望構(gòu)建的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與系統(tǒng)有關(guān)。
經(jīng)過測試的源配置
Linux
CPU
| 版本 | Python 版本 | 編譯器 | 構(gòu)建工具 |
|---|---|---|---|
| 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 版本 | 編譯器 | 構(gòu)建工具 | 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 版本 | 編譯器 | 構(gòu)建工具 |
|---|---|---|---|
| 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 版本 | 編譯器 | 構(gòu)建工具 | 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源代碼構(gòu)建流程記錄解析的詳細內(nèi)容,更多關(guān)于TensorFlow源代碼構(gòu)建流程的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
c++語言中虛函數(shù)實現(xiàn)多態(tài)的原理詳解
這篇文章主要給大家介紹了關(guān)于c++語言中虛函數(shù)實現(xiàn)多態(tài)的原理的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用c++語言具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-05-05
C++實現(xiàn)LeetCode(347.前K個高頻元素)
這篇文章主要介紹了C++實現(xiàn)LeetCode(347.前K個高頻元素),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08
淺談int8_t int64_t size_t ssize_t的相關(guān)問題(詳解)
下面小編就為大家?guī)硪黄獪\談int8_t int64_t size_t ssize_t的相關(guān)問題(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03

