TensorFlow源代碼構(gòu)建流程記錄解析
為什么從源代碼構(gòu)建
通常情況下,直接安裝構(gòu)建好的.whl即可。不過,當(dāng)需要一些特殊配置(或者閑來無事想體會(huì) TensorFlow 構(gòu)建過程到底有多麻煩)的時(shí)候,則需要選擇從源代碼構(gòu)建TensorFlow。萬幸文檔混亂的 TensorFlow 還是好心地為我們提供了一整頁的文檔供參考 www.tensorflow.org/install/sou… ,個(gè)人認(rèn)為其中最需要關(guān)注的部分莫過于經(jīng)過測試供參考的源配置(列于文末)。TF使用 Google 的開源構(gòu)建工具 bazel 構(gòu)建,并且源碼的版本與 bazel 的版本高度相關(guān),所以盡量匹配版本進(jìn)行構(gòu)建。
流程記錄 TF v1.14.0 CPU on Ubuntu 18.04
安裝對(duì)應(yīng)版本的 bazel
根據(jù)計(jì)劃構(gòu)建的版本,查閱文末的對(duì)應(yīng)配置,參考官方文檔: bazel.build/install/ubu… 安裝相應(yīng)版本的 bazel,如本次計(jì)劃構(gòu)建的版本是 v1.14.0,對(duì)應(yīng)的 bazel 版本是 0.24.1(此次使用0.26.1也是可以的)。
為方便,這里直接貼出對(duì)應(yīng) 0.26.1 release 的頁面: github.com/bazelbuild/… ,點(diǎn)擊assets找到對(duì)應(yīng)的文件下載即可。
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到相應(yīng) tag,比如這次是構(gòu)建 v1.14.0 版本:
git checkout v1.14.0
* 一些小調(diào)整,通??梢月赃^
Build with C++17
因?yàn)橹笮枰獙懙?Custom OP 依賴的另一個(gè)庫是 C++17,而除了剛剛才發(fā)布的 v2.10 版,以前的 TF默認(rèn)是使用 C++11,實(shí)際構(gòu)建的時(shí)候,代碼有一些 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 時(shí)會(huì)報(bào)錯(cuò)找不到 absl::make_unique)(這里 make_unique 是 C++17 標(biāo)準(zhǔn)庫里的用法,Google的abseil的make_unique方法則方便C++11的代碼也可以使用它;最新的v2.10版由于默認(rèn)使用C++17,已經(jīng)改為std::make_unique)
.bazelrc 文件里記錄了構(gòu)建時(shí)各種配置選項(xiàng) ([--config=option])的映射規(guī)則,如有需要可以進(jìn)行修改。由于 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)拉取遠(yuǎn)程倉庫的許多依賴。由于 TF 的構(gòu)建過程消耗內(nèi)存很嚴(yán)重,選擇在服務(wù)器上進(jìn)行構(gòu)建,而服務(wù)器遠(yuǎn)程拉取 github 上倉庫經(jīng)常失敗。所以需要手動(dòng)在網(wǎng)絡(luò)良好的機(jī)器上下載相應(yīng)的庫的 release (對(duì)應(yīng)的版本在 WORKSPACE 文件中可以找到一行注釋),存放在服務(wù)器本地,并在 WORKSPACE 文件中對(duì)應(yīng)的 http_archive 部分添加一行本地地址。若需要換版本,也可以在相應(yīng)github庫的releases下面找到對(duì)應(yīng)的 URL 及 sha256(實(shí)在是找不到對(duì)應(yīng)的也可以手動(dòng)下載壓縮包后通過 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
運(yùn)行源碼根目錄下的 ./configure 進(jìn)行配置。
./configure
此次編譯一個(gè)盡量簡略的 CPU 版本,會(huì)話如下:
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 對(duì)應(yīng)剛剛修改的 .bazelrc 文件中相應(yīng)的部分
注意:bazel build 的過程時(shí)間會(huì)比較長,對(duì)內(nèi)存的消耗較大,jobs 數(shù)謹(jǐn)慎開大。
bazel build 結(jié)束后,一個(gè)名為 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 的選項(xiàng)。
./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是對(duì)應(yīng)的版本,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)建流程記錄解析的詳細(xì)內(nèi)容,更多關(guān)于TensorFlow源代碼構(gòu)建流程的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- 深度學(xué)習(xí)Tensorflow2.8實(shí)現(xiàn)GRU文本生成任務(wù)詳解
- 深度學(xué)習(xí)TextRNN的tensorflow1.14實(shí)現(xiàn)示例
- 深度學(xué)習(xí)TextLSTM的tensorflow1.14實(shí)現(xiàn)示例
- Tensorflow2.10實(shí)現(xiàn)圖像分割任務(wù)示例詳解
- 簡單利用conda安裝tensorflow-gpu=2.2.0的過程及問題解決
- 教你如何使用Conda命令?+?安裝tensorflow
- tensorflow2.0如何實(shí)現(xiàn)cnn的圖像識(shí)別
- 深度學(xué)習(xí)Tensorflow2.8?使用?BERT?進(jìn)行文本分類
相關(guān)文章
形參出現(xiàn)在函數(shù)定義中,在整個(gè)函數(shù)體內(nèi)都可以使用, 離開該函數(shù)則不能使用。實(shí)參出現(xiàn)在主調(diào)函數(shù)中,進(jìn)入被調(diào)函數(shù)后,實(shí)參變量也不能使用,形參和實(shí)參的功能是作數(shù)據(jù)傳送。發(fā)生函數(shù)調(diào)用時(shí), 主調(diào)函數(shù)把實(shí)參的值傳送給被調(diào)函數(shù)的形參從而實(shí)現(xiàn)主調(diào)函數(shù)向被調(diào)函數(shù)的數(shù)據(jù)傳送2023-02-02
c++語言中虛函數(shù)實(shí)現(xiàn)多態(tài)的原理詳解
這篇文章主要給大家介紹了關(guān)于c++語言中虛函數(shù)實(shí)現(xiàn)多態(tài)的原理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用c++語言具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
C++實(shí)現(xiàn)LeetCode(347.前K個(gè)高頻元素)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(347.前K個(gè)高頻元素),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
C語言實(shí)現(xiàn)自動(dòng)分配地址的示例
本文介紹了兩種自動(dòng)分配地址的方法,包括通過宏定義實(shí)現(xiàn)地址分配和將EE地址作為一個(gè)結(jié)構(gòu)體,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11
淺談int8_t int64_t size_t ssize_t的相關(guān)問題(詳解)
下面小編就為大家?guī)硪黄獪\談int8_t int64_t size_t ssize_t的相關(guān)問題(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03

