本指南介绍了如何开始开发 XLA 项目。
在开始之前,请完成以下前提条件:
然后,按照以下步骤获取源代码、设置环境、构建代码库并创建拉取请求。
获取代码
- 创建 XLA 代码库的分支。
克隆您的代码库分支,将
{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。
如需构建 XLA,您必须安装 Bazel。建议使用 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,请添加
--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 diff 运行检查。
# Make sure the main is updated.
git fetch origin main
bazel run //build_tools/ci:run_clang_tidy