從原始碼建構 TensorFlow pip 套件,並在 Ubuntu Linux 和 macOS 上安裝。雖然這些說明可能適用於其他系統,但僅在 Ubuntu 和 macOS 上經過測試和支援。
Linux 和 macOS 的設定
安裝下列建構工具以設定您的開發環境。
安裝 Python 和 TensorFlow 套件相依性
Ubuntu
sudo apt install python3-dev python3-pip
macOS
需要 Xcode 9.2 或更高版本。
使用 Homebrew 套件管理器安裝
brew install python
安裝 TensorFlow pip 套件相依性(如果使用虛擬環境,請省略 --user
引數)
pip install -U --user pip
安裝 Bazel
若要建構 TensorFlow,您需要安裝 Bazel。Bazelisk 是安裝 Bazel 的簡單方法,並會自動下載適用於 TensorFlow 的正確 Bazel 版本。為了方便使用,請將 Bazelisk 新增為 PATH
中的 bazel
可執行檔。
如果 Bazelisk 無法使用,您可以手動 安裝 Bazel。請務必從 TensorFlow 的 .bazelversion 檔案安裝正確的 Bazel 版本。
安裝 Clang(建議,僅限 Linux)
Clang 是以 C++ 編譯的 C/C++/Objective-C 編譯器,基於 LLVM。自 TensorFlow 2.13 起,它是建構 TensorFlow 的預設編譯器。目前支援的版本是 LLVM/Clang 17。
LLVM Debian/Ubuntu nightly packages 提供自動安裝指令碼和套件,以便在 Linux 上手動安裝。如果您手動將 llvm apt 儲存庫新增至套件來源,請務必執行下列命令
sudo apt-get update && sudo apt-get install -y llvm-17 clang-17
現在 /usr/lib/llvm-17/bin/clang
是此案例中 clang 的實際路徑。
或者,您可以下載並解壓縮預先建構的 Clang + LLVM 17。
以下範例說明您可以在 Debian/Ubuntu 作業系統上設定下載的 Clang + LLVM 17 二進位檔的步驟
變更為所需的目的目錄:
cd <desired directory>
載入並解壓縮封存檔...(適合您的架構)
wget https://github.com/llvm/llvm-project/releases/download/llvmorg-17.0.2/clang+llvm-17.0.2-x86_64-linux-gnu-ubuntu-22.04.tar.xz
tar -xvf clang+llvm-17.0.2-x86_64-linux-gnu-ubuntu-22.04.tar.xz
將解壓縮的內容(目錄和檔案)複製到
/usr
(您可能需要 sudo 權限,且正確的目錄可能因發行版本而異)。這會有效地安裝 Clang 和 LLVM,並將其新增至路徑。您不應該需要取代任何項目,除非您先前已安裝,在這種情況下,您應該取代檔案cp -r clang+llvm-17.0.2-x86_64-linux-gnu-ubuntu-22.04/* /usr
檢查取得的 Clang + LLVM 17 二進位檔版本
clang --version
現在
/usr/bin/clang
是新 clang 的實際路徑。您可以執行./configure
指令碼,或手動將環境變數CC
和BAZEL_COMPILER
設定為此路徑。
安裝 GPU 支援(選用,僅限 Linux)
macOS 不支援 GPU。
閱讀 GPU 支援 指南以安裝在 GPU 上執行 TensorFlow 所需的驅動程式和其他軟體。
下載 TensorFlow 原始碼
使用 Git 複製 TensorFlow 儲存庫
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
儲存庫預設為 master
開發分支。您也可以查看發行分支以進行建構
git checkout branch_name # r2.2, r2.3, etc.
設定建構
TensorFlow 建構是由儲存庫根目錄中的 .bazelrc
檔案設定的。./configure
或 ./configure.py
指令碼可用於調整常見設定。
請從儲存庫的根目錄執行 ./configure
指令碼。此指令碼會提示您輸入 TensorFlow 相依性的位置,並詢問其他建構設定選項(例如,編譯器旗標)。如需詳細資訊,請參閱「範例工作階段」章節。
./configure
此指令碼也有 Python 版本,即 ./configure.py
。如果使用虛擬環境,python configure.py
會優先處理環境內的路徑,而 ./configure
則優先處理環境外的路徑。在這兩種情況下,您都可以變更預設值。
範例工作階段
以下顯示 ./configure
指令碼的範例執行(您的工作階段可能不同)
設定選項
GPU 支援
為了獲得 GPU 支援,請在設定期間設定 cuda=Y
,並指定 CUDA 和 cuDNN 的版本。如果您的系統安裝了多個 CUDA 或 cuDNN 版本,請明確設定版本,而不是依賴預設值。./configure
會建立系統 CUDA 程式庫的符號連結—因此,如果您更新 CUDA 程式庫路徑,則必須在建構之前再次執行此設定步驟。
最佳化
針對編譯最佳化旗標,預設值 (-march=native
) 會針對您機器的 CPU 類型最佳化產生的程式碼。但是,如果為不同的 CPU 類型建構 TensorFlow,請考慮更具體的最佳化旗標。請查看 GCC 手冊以取得範例。
預先設定的組態
有一些預先設定的建構組態可供使用,例如可以新增至 bazel build
命令
--config=dbg
—使用偵錯資訊建構。如需詳細資訊,請參閱 CONTRIBUTING.md。--config=mkl
—支援 Intel® MKL-DNN。--config=monolithic
—適用於主要靜態單體式建構的組態。
建構並安裝 pip 套件
Bazel 建構選項
從原始碼建構 TensorFlow 可能會使用大量 RAM。如果您的系統記憶體受限,請使用以下命令限制 Bazel 的 RAM 使用量:--local_ram_resources=2048
。
官方 TensorFlow 套件是使用符合 manylinux2014 套件標準的 Clang 工具鏈建構的。
建構套件
若要建構 pip 套件,您需要指定 --repo_env=WHEEL_NAME
旗標。根據提供的名稱,將會建立套件,例如
建構 tensorflow CPU 套件
bazel build //tensorflow/tools/pip_package:wheel --repo_env=WHEEL_NAME=tensorflow_cpu
建構 tensorflow GPU 套件
bazel build //tensorflow/tools/pip_package:wheel --repo_env=WHEEL_NAME=tensorflow --config=cuda
建構 tensorflow TPU 套件
bazel build //tensorflow/tools/pip_package:wheel --repo_env=WHEEL_NAME=tensorflow_tpu --config=tpu
若要建構 nightly 套件,請設定 tf_nightly
而不是 tensorflow
,例如,建構 CPU nightly 套件
bazel build //tensorflow/tools/pip_package:wheel --repo_env=WHEEL_NAME=tf_nightly_cpu
因此,產生的 wheel 將位於
bazel-bin/tensorflow/tools/pip_package/wheel_house/
安裝套件
產生的 .whl
檔案名稱取決於 TensorFlow 版本和您的平台。使用 pip install
安裝套件,例如
pip install bazel-bin/tensorflow/tools/pip_package/wheel_house/tensorflow-version-tags.whl
Docker Linux 建構
TensorFlow 的 Docker 開發映像檔是設定環境以從原始碼建構 Linux 套件的簡單方法。這些映像檔已包含建構 TensorFlow 所需的原始碼和相依性。如需安裝指示和可用映像檔標籤的清單,請前往 TensorFlow Docker 指南。
僅限 CPU
以下範例使用 :devel
映像檔從最新的 TensorFlow 原始碼建構僅限 CPU 的套件。請查看 Docker 指南以取得可用的 TensorFlow -devel
標籤。
下載最新的開發映像檔,並啟動您將用來建構 pip 套件的 Docker 容器
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 套件。
- 調整容器外部檔案的擁有權權限。
./configure # if necessary
bazel build //tensorflow/tools/pip_package:wheel --repo_env=WHEEL_NAME=tensorflow_cpu --config=opt
`chown $HOST_PERMS bazel-bin/tensorflow/tools/pip_package/wheel_house/tensorflow-version-tags.whl
在容器內安裝並驗證套件
pip uninstall tensorflow # remove current version
pip install bazel-bin/tensorflow/tools/pip_package/wheel_house/tensorflow-version-tags.whl
cd /tmp # don't import from source directory
python -c "import tensorflow as tf; print(tf.__version__)"
在您的主機上,TensorFlow pip 套件位於目前目錄中(具有主機使用者權限):./tensorflow-version-tags.whl
GPU 支援
Docker 是為 TensorFlow 建構 GPU 支援的最簡單方法,因為主機僅需要 NVIDIA® 驅動程式(不必安裝 NVIDIA® CUDA® Toolkit)。如需設定 nvidia-docker(僅限 Linux),請參閱 GPU 支援指南和 TensorFlow Docker 指南。
以下範例下載 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 # if necessary
bazel build //tensorflow/tools/pip_package:wheel --repo_env=WHEEL_NAME=tensorflow --config=cuda --config=opt
chown $HOST_PERMS bazel-bin/tensorflow/tools/pip_package/wheel_house/tensorflow-version-tags.whl
在容器內安裝並驗證套件,並檢查 GPU
pip uninstall tensorflow # remove current version
pip install bazel-bin/tensorflow/tools/pip_package/wheel_house/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')))"
經過測試的建構組態
Linux
CPU
版本 | Python 版本 | 編譯器 | 建構工具 |
---|---|---|---|
tensorflow-2.16.1 | 3.9-3.12 | Clang 17.0.6 | Bazel 6.5.0 |
tensorflow-2.15.0 | 3.9-3.11 | Clang 16.0.0 | Bazel 6.1.0 |
tensorflow-2.14.0 | 3.9-3.11 | Clang 16.0.0 | Bazel 6.1.0 |
tensorflow-2.13.0 | 3.8-3.11 | Clang 16.0.0 | Bazel 5.3.0 |
tensorflow-2.12.0 | 3.8-3.11 | GCC 9.3.1 | Bazel 5.3.0 |
tensorflow-2.11.0 | 3.7-3.10 | GCC 9.3.1 | Bazel 5.3.0 |
tensorflow-2.10.0 | 3.7-3.10 | GCC 9.3.1 | Bazel 5.1.1 |
tensorflow-2.9.0 | 3.7-3.10 | GCC 9.3.1 | Bazel 5.0.0 |
tensorflow-2.8.0 | 3.7-3.10 | GCC 7.3.1 | Bazel 4.2.1 |
tensorflow-2.7.0 | 3.7-3.9 | GCC 7.3.1 | Bazel 3.7.2 |
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.16.1 | 3.9-3.12 | Clang 17.0.6 | Bazel 6.5.0 | 8.9 | 12.3 |
tensorflow-2.15.0 | 3.9-3.11 | Clang 16.0.0 | Bazel 6.1.0 | 8.9 | 12.2 |
tensorflow-2.14.0 | 3.9-3.11 | Clang 16.0.0 | Bazel 6.1.0 | 8.7 | 11.8 |
tensorflow-2.13.0 | 3.8-3.11 | Clang 16.0.0 | Bazel 5.3.0 | 8.6 | 11.8 |
tensorflow-2.12.0 | 3.8-3.11 | GCC 9.3.1 | Bazel 5.3.0 | 8.6 | 11.8 |
tensorflow-2.11.0 | 3.7-3.10 | GCC 9.3.1 | Bazel 5.3.0 | 8.1 | 11.2 |
tensorflow-2.10.0 | 3.7-3.10 | GCC 9.3.1 | Bazel 5.1.1 | 8.1 | 11.2 |
tensorflow-2.9.0 | 3.7-3.10 | GCC 9.3.1 | Bazel 5.0.0 | 8.1 | 11.2 |
tensorflow-2.8.0 | 3.7-3.10 | GCC 7.3.1 | Bazel 4.2.1 | 8.1 | 11.2 |
tensorflow-2.7.0 | 3.7-3.9 | GCC 7.3.1 | Bazel 3.7.2 | 8.1 | 11.2 |
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.16.1 | 3.9-3.12 | Clang from xcode 13.6 | Bazel 6.5.0 |
tensorflow-2.15.0 | 3.9-3.11 | Clang from xcode 10.15 | Bazel 6.1.0 |
tensorflow-2.14.0 | 3.9-3.11 | Clang from xcode 10.15 | Bazel 6.1.0 |
tensorflow-2.13.0 | 3.8-3.11 | Clang from xcode 10.15 | Bazel 5.3.0 |
tensorflow-2.12.0 | 3.8-3.11 | Clang from xcode 10.15 | Bazel 5.3.0 |
tensorflow-2.11.0 | 3.7-3.10 | Clang from xcode 10.14 | Bazel 5.3.0 |
tensorflow-2.10.0 | 3.7-3.10 | Clang from xcode 10.14 | Bazel 5.1.1 |
tensorflow-2.9.0 | 3.7-3.10 | Clang from xcode 10.14 | Bazel 5.0.0 |
tensorflow-2.8.0 | 3.7-3.10 | Clang from xcode 10.14 | Bazel 4.2.1 |
tensorflow-2.7.0 | 3.7-3.9 | Clang from xcode 10.11 | Bazel 3.7.2 |
tensorflow-2.6.0 | 3.6-3.9 | Clang from xcode 10.11 | Bazel 3.7.2 |
tensorflow-2.5.0 | 3.6-3.9 | Clang from xcode 10.11 | Bazel 3.7.2 |
tensorflow-2.4.0 | 3.6-3.8 | Clang from xcode 10.3 | Bazel 3.1.0 |
tensorflow-2.3.0 | 3.5-3.8 | Clang from xcode 10.1 | Bazel 3.1.0 |
tensorflow-2.2.0 | 3.5-3.8 | Clang from xcode 10.1 | Bazel 2.0.0 |
tensorflow-2.1.0 | 2.7, 3.5-3.7 | Clang from xcode 10.1 | Bazel 0.27.1 |
tensorflow-2.0.0 | 2.7, 3.5-3.7 | Clang from xcode 10.1 | Bazel 0.27.1 |
tensorflow-2.0.0 | 2.7, 3.3-3.7 | Clang from xcode 10.1 | Bazel 0.26.1 |
tensorflow-1.15.0 | 2.7, 3.3-3.7 | Clang from xcode 10.1 | Bazel 0.26.1 |
tensorflow-1.14.0 | 2.7, 3.3-3.7 | Clang from xcode | Bazel 0.24.1 |
tensorflow-1.13.1 | 2.7, 3.3-3.7 | Clang from xcode | Bazel 0.19.2 |
tensorflow-1.12.0 | 2.7, 3.3-3.6 | Clang from xcode | Bazel 0.15.0 |
tensorflow-1.11.0 | 2.7, 3.3-3.6 | Clang from xcode | Bazel 0.15.0 |
tensorflow-1.10.0 | 2.7, 3.3-3.6 | Clang from xcode | Bazel 0.15.0 |
tensorflow-1.9.0 | 2.7, 3.3-3.6 | Clang from xcode | Bazel 0.11.0 |
tensorflow-1.8.0 | 2.7, 3.3-3.6 | Clang from xcode | Bazel 0.10.1 |
tensorflow-1.7.0 | 2.7, 3.3-3.6 | Clang from xcode | Bazel 0.10.1 |
tensorflow-1.6.0 | 2.7, 3.3-3.6 | Clang from xcode | Bazel 0.8.1 |
tensorflow-1.5.0 | 2.7, 3.3-3.6 | Clang from xcode | Bazel 0.8.1 |
tensorflow-1.4.0 | 2.7, 3.3-3.6 | Clang from xcode | Bazel 0.5.4 |
tensorflow-1.3.0 | 2.7, 3.3-3.6 | Clang from xcode | Bazel 0.4.5 |
tensorflow-1.2.0 | 2.7, 3.3-3.6 | Clang from xcode | Bazel 0.4.5 |
tensorflow-1.1.0 | 2.7, 3.3-3.6 | Clang from xcode | Bazel 0.4.2 |
tensorflow-1.0.0 | 2.7, 3.3-3.6 | Clang from xcode | Bazel 0.4.2 |
GPU
版本 | Python 版本 | 編譯器 | 建構工具 | cuDNN | CUDA |
---|---|---|---|---|---|
tensorflow_gpu-1.1.0 | 2.7, 3.3-3.6 | Clang from xcode | Bazel 0.4.2 | 5.1 | 8 |
tensorflow_gpu-1.0.0 | 2.7, 3.3-3.6 | Clang from xcode | Bazel 0.4.2 | 5.1 | 8 |