本指南說明如何開始開發 XLA 專案。
開始之前,請先完成下列必要條件:
然後按照下列步驟取得原始碼、設定環境、建構存放區,以及建立提取要求。
取得程式碼
- 建立 XLA 存放區的 Fork。
複製存放區的分支版本,並將
{USER}替換成 GitHub 使用者名稱:git clone <a href="https://github.com/">https://github.com/</a>{USER}/xla.git變更至
xla目錄:cd xla設定遠端上游存放區:
git remote add upstream <a href="https://github.com/openxla/xla.git">https://github.com/openxla/xla.git</a>
設定環境
安裝 Bazel。
您必須安裝 Bazel 才能建構 XLA。建議使用 Bazelisk 安裝 Bazel,因為這項工具會自動下載適用於 XLA 的正確 Bazel 版本。如果無法取得 Bazelisk,可以手動安裝 Bazel。
建立並執行 ml-build Docker 容器。
如要設定 Docker 容器,建構同時支援 CPU 和 GPU 的 XLA,請執行下列指令:
docker run -itd --rm \ --name xla \ -w /xla \ -v $PWD:/xla \ us-docker.pkg.dev/ml-oss-artifacts-published/ml-public-container/ml-build:latest \ bash如要建構支援 GPU/CUDA 的 TensorFlow,請加入
--gpus all,授予容器存取所有可用 GPU 的權限。這會啟用 CUDA 計算功能的自動偵測功能。
建構
設定 CPU:
docker exec xla ./configure.py --backend=CPU
設定 GPU:
docker exec xla ./configure.py --backend=CUDA
執行 nvidia-smi 時,系統會自動偵測 CUDA 運算功能。如果建構期間無法使用 GPU,您必須手動指定運算功能。例如:
# Automatically detects compute capabilities (requires GPUs)
./configure.py --backend=CUDA
# Manually specify compute capabilities (for builds without GPUs)
./configure.py --backend=CUDA --cuda_compute_capabilities="9.0"
版本:
docker exec xla bazel build \
--spawn_strategy=sandboxed \
--test_output=all \
//xla/...
- 啟動 Docker 容器時,請不要使用
--gpus all旗標。 - 在
./configure.py期間,使用--cuda_compute_capabilities旗標手動指定 CUDA 計算功能。
# Automatically detects compute capabilities (requires GPUs)
./configure.py --backend=CUDA
# Manually specify compute capabilities (for builds without GPUs)
./configure.py --backend=CUDA --cuda_compute_capabilities="9.0"
bazel build \
--spawn_strategy=sandboxed \
--test_output=all \
//xla/...
第一次建構時會耗費較長時間,因為系統必須建構整個堆疊,包括 XLA、MLIR 和 StableHLO。
如要進一步瞭解如何建構 XLA,請參閱「從來源建構」。
建立提取要求
準備好要將變更送審時,請建立提取要求。
如要瞭解 XLA 程式碼審查理念,請參閱「審查程序」。
靜態分析 (Clang-Tidy)
為維持程式碼品質,XLA 會使用 clang-tidy 進行靜態分析,並納入驗證。
如何執行
執行檢查的方式有兩種。如要針對特定目標執行,請使用下列指令:
bazel build --config=clang-tidy //path/to:target1 //path/to:target2
CI 工作流程也會使用輔助指令碼,針對功能分支與上游主分支的 Git 差異執行指令碼。
# Make sure the main is updated.
git fetch origin main
bazel run //build_tools/ci:run_clang_tidy