
通常情況下,直接安裝構(gòu)建好的.whl即可。不過,當(dāng)需要一些特殊配置(或者閑來無事想體會(huì) TensorFlow 構(gòu)建過程到底有多麻煩)的時(shí)候,則需要選擇從源代碼構(gòu)建TensorFlow。萬幸文檔混亂的 TensorFlow 還是好心地為我們提供了一整頁的文檔供參考 ?https://www.tensorflow.org/install/source?hl=zh-cn??> ,個(gè)人認(rèn)為其中最需要關(guān)注的部分莫過于經(jīng)過測試供參考的源配置(列于文末)。TF使用 Google 的開源構(gòu)建工具 bazel 構(gòu)建,并且源碼的版本與 bazel 的版本高度相關(guān),所以盡量匹配版本進(jìn)行構(gòu)建。
【資料圖】
根據(jù)計(jì)劃構(gòu)建的版本,查閱文末的對應(yīng)配置,參考官方文檔: ?https://bazel.build/install/ubuntu??> 安裝相應(yīng)版本的 bazel,如本次計(jì)劃構(gòu)建的版本是 v1.14.0,對應(yīng)的 bazel 版本是 0.24.1(此次使用0.26.1也是可以的)。
為方便,這里直接貼出對應(yīng) 0.26.1 release 的頁面: ?https://github.com/bazelbuild/bazel/releases?expanded=true&page=2&q=0.26.1??> ,點(diǎn)擊assets找到對應(yīng)的文件下載即可。
wget https://github.com/bazelbuild/bazel/releases/download/0.26.1/bazel-0.26.1-installer-linux-x86_64.shchmod +x bazel-version-installer-linux-x86_64.sh./bazel-version-installer-linux-x86_64.sh --user
從 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
因?yàn)橹笮枰獙懙?Custom OP 依賴的另一個(gè)庫是 C++17,而除了剛剛才發(fā)布的 v2.10 版,以前的 TF默認(rèn)是使用 C++11,實(shí)際構(gòu)建的時(shí)候,代碼有一些 minor fix。此處參考 ?https://github.com/tensorflow/tensorflow/pull/30836/files???> 修改 ??.bazelrc ?
??里 ??build:c++17?
?? 的配置,在 ??tensorflow/core/lib/gif/gif_io.cc?
?? 中添加 ??#include
??, 并在 ??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
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 (對應(yīng)的版本在 ??WORKSPACE?
?? 文件中可以找到一行注釋),存放在服務(wù)器本地,并在 WORKSPACE 文件中對應(yīng)的 ??http_archive?
?? 部分添加一行本地地址。若需要換版本,也可以在相應(yīng)github庫的releases下面找到對應(yīng)的 URL 及 sha256(實(shí)在是找不到對應(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
運(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-packagesPlease 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]: nNo XLA JIT support will be enabled for TensorFlow. Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: nNo OpenCL SYCL support will be enabled for TensorFlow. Do you wish to build TensorFlow with ROCm support? [y/N]: nNo ROCm support will be enabled for TensorFlow. Do you wish to build TensorFlow with CUDA support? [y/N]: nNo CUDA support will be enabled for TensorFlow. Do you wish to download a fresh release of clang? (Experimental) [y/N]: nClang 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]: nNot 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
官方提供的命令:
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?
? 對應(yīng)剛剛修改的 .bazelrc 文件中相應(yīng)的部分
注意:??bazel build?
? 的過程時(shí)間會(huì)比較長,對內(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?
??是對應(yīng)的版本,??tags?
?與系統(tǒng)有關(guān)。
版本 | 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 |
版本 | 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 |
版本 | 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 |
版本 | 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 |