创建多主机 TPU 切片
了解如何使用代管式实例组 (MIG) 创建多主机 TPU 切片、连接到切片以及运行计算。本快速入门使用按需使用选项。在本快速入门中,请在本地终端 或 Cloud Shell中运行命令。
准备工作
-
安装 Google Cloud CLI。
-
配置 gcloud CLI 以使用您的联合身份。
如需了解详情,请参阅使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init -
选择或创建项目所需角色
- 选择项目:选择项目不需要特定的 IAM 角色,您可以选择已获授角色的任何项目。
-
创建项目:如需创建项目,您需要 Project Creator 角色
(
roles/resourcemanager.projectCreator),该角色包含resourcemanager.projects.create权限。了解如何授予 角色。
-
创建 Cloud de Confiance 项目:
gcloud projects create PROJECT_ID
将
PROJECT_ID替换为您要创建的 Cloud de Confiance 项目名称。 -
选择您创建的 Cloud de Confiance 项目:
gcloud config set project PROJECT_ID
将
PROJECT_ID替换为您的 Cloud de Confiance 项目名称。
-
如果您要使用现有项目来完成本指南, 请验证您是否拥有完成本指南所需的权限。如果您创建了新项目, 则您已拥有所需的权限。
启用 Compute Engine API:
启用 API 所需的角色
如需启用 API,您需要拥有
serviceusage.services.enable权限。如果您 创建了项目,则您可能已通过 Owner 角色 (roles/owner) 拥有此权限。否则,您可以通过 Service Usage Admin 角色 (roles/serviceusage.serviceUsageAdmin) 获得此权限。 了解如何授予角色。gcloud services enable compute.googleapis.com
所需的角色
如需获得创建构成多主机 TPU 切片的 MIG、使用 SSH 连接到 MIG 中的每个虚拟机以及运行命令所需的权限,请让您的管理员为您授予项目的以下 IAM 角色:
- Compute Instance Admin (v1) (
roles/compute.instanceAdmin.v1) - Service Account User (
roles/iam.serviceAccountUser) - Service Usage Admin (
roles/serviceusage.serviceUsageAdmin)
如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限。
创建实例模板
如需为 TPU v6e 虚拟机创建实例模板,请使用 gcloud compute
instance-templates create
命令:
gcloud compute instance-templates create quickstart-tpu-instance-template \
--machine-type=ct6e-standard-4t \
--maintenance-policy=TERMINATE \
--image-family=ubuntu-accel-2204-amd64-tpu-v5e-v5p-v6e \
--image-project=ubuntu-os-accelerator-images \
--region=us-east5
创建工作负载政策
工作负载政策定义了计算实例的物理属性。在 TPU 切片中,加速器拓扑定义了 TPU 芯片的物理排列方式。 对于互连的多主机 TPU 切片,必须指定加速器拓扑。
如需为多主机 TPU 切片创建工作负载政策,请使用带有 --accelerator-topology 标志的 gcloud compute
resource-policies create workload-policy
command
命令。以下命令会创建一个具有 2x4 拓扑的工作负载政策:
gcloud compute resource-policies create workload-policy quickstart-tpu-workload-policy \
--type=high-throughput \
--accelerator-topology=2x4 \
--region=us-east5
创建 MIG
运行以下命令以创建构成多主机 TPU 切片的 MIG。
如需创建构成多主机 TPU 切片的 MIG,请使用
gcloud compute instance-groups managed create命令:gcloud compute instance-groups managed create quickstart-tpu-mig \ --size=2 \ --target-size-policy-mode=bulk \ --template=quickstart-tpu-instance-template \ --region=us-east5 \ --target-distribution-shape=any-single-zone \ --instance-redistribution-type=none \ --default-action-on-vm-failure=do-nothing \ --workload-policy=projects/PROJECT_ID/regions/us-east5/resourcePolicies/quickstart-tpu-workload-policy将
PROJECT_ID替换为您的 Cloud de Confiance by S3NS 项目 ID。(可选)使用以下命令验证代管式实例是否正在运行:
如需查看 MIG 的整体状态,请使用
gcloud compute instance-groups managed describecommand:gcloud compute instance-groups managed describe quickstart-tpu-mig \ --region=us-east5如需查看每个实例的状态,请使用
gcloud compute instance-groups managed list-instancescommand:gcloud compute instance-groups managed list-instances quickstart-tpu-mig \ --region=us-east5
安装 JAX
在 MIG 中所有 TPU 虚拟机实例的虚拟环境中安装依赖项和 JAX 框架。如果您的 TPU 虚拟机安装的 Python 版本低于 3.11,则必须安装 Python 3.11 才能运行最新版本的 JAX。
检查 TPU 虚拟机上运行的 Python 版本:
gcloud compute instance-groups managed list-instances quickstart-tpu-mig \ --region=us-east5 \ --uri \ | xargs -I {} -P 0 gcloud compute ssh {} \ --command='python3 --version'如果版本低于 Python 3.11,请安装 Python 3.11:
gcloud compute instance-groups managed list-instances quickstart-tpu-mig \ --region=us-east5 \ --uri \ | xargs -I {} -P 0 gcloud compute ssh {} \ --command='sudo apt update && \ sudo apt install -y software-properties-common && \ sudo add-apt-repository -y ppa:deadsnakes/ppa && \ sudo apt update && \ sudo apt install -y python3.11 python3.11-dev'创建虚拟环境:
gcloud compute instance-groups managed list-instances quickstart-tpu-mig \ --region=us-east5 \ --uri \ | xargs -I {} -P 0 gcloud compute ssh {} \ --command='sudo apt install -y python3.11-venv && \ python3.11 -m venv ~/jax_venv'在虚拟环境中安装 JAX:
gcloud compute instance-groups managed list-instances quickstart-tpu-mig \ --region=us-east5 \ --uri \ | xargs -I {} -P 0 gcloud compute ssh {} \ --command='source ~/jax_venv/bin/activate && \ pip install --upgrade pip -q && \ pip install jax[tpu] -f https://storage.googleapis.com/jax-releases/libtpu_releases.html -q'
在切片上运行 JAX 代码
如需在 TPU 切片上运行 JAX 代码,您必须在 TPU 切片中的每个主机上运行该代码。在切片中的每个主机上调用 jax.device_count() 函数调用之前,该函数调用都会停止响应。以下示例展示了如何在 TPU 切片上运行 JAX 计算。
准备代码
在每个实例上创建一个名为 example.py 的文件:
gcloud compute instance-groups managed list-instances quickstart-tpu-mig \
--region=us-east5 \
--uri \
| xargs -I {} -P 0 gcloud compute ssh {} \
--command="cat << 'EOF' > ~/example.py
import jax
# Initialize the slice
jax.distributed.initialize()
# The total number of TPU cores in the slice
device_count = jax.device_count()
# The number of TPU cores attached to this host
local_device_count = jax.local_device_count()
# The psum is performed over all mapped devices across the slice
xs = jax.numpy.ones(jax.local_device_count())
r = jax.pmap(lambda x: jax.lax.psum(x, 'i'), axis_name='i')(xs)
# Print from a single host to avoid duplicated output
if jax.process_index() == 0:
print('global device count:', jax.device_count())
print('local device count:', jax.local_device_count())
print('pmap result:', r)
EOF"
在切片上运行代码
在切片中的每个 TPU 虚拟机上运行 example.py 程序:
gcloud compute instance-groups managed list-instances quickstart-tpu-mig \
--region=us-east5 \
--uri \
| xargs -I {} -P 0 gcloud compute ssh {} \
--command='source ~/jax_venv/bin/activate && python3 ~/example.py'
输出应类似如下所示:
global device count: 8
local device count: 4
pmap result: [8. 8. 8. 8.]
清理
为避免因本页面中使用的资源导致您的 Cloud de Confiance 账号产生费用,请删除包含这些资源的 Cloud de Confiance 项目。
或者,如果您想保留项目,则可以使用 gcloud compute instance-groups managed
delete 命令仅删除 MIG 和
组中的所有虚拟机:
gcloud compute instance-groups managed delete quickstart-tpu-mig --region=us-east5