Run TorchTPU on GKE¶
Use this guide when your TPUs are GKE node pools: always for TPU v7x (Ironwood), and whenever you already run Kubernetes or need a multi-host job scheduled as a unit. You submit TorchTPU's reference Job and JobSet manifests, read their output, and then swap in your own code. The reasons for each field (node selectors, chip requests, TORCH_TPU_TOPOLOGY, why the three must agree) are in chapter 8, section "Running on GKE".
v7x provisioning in the public documentation
The public documents differ on how TPU v7x is provisioned. The vLLM-TorchTPU Cloud TPU Setup page generates queued-resource commands for v7x. Google Cloud's TPU queued resources documentation says the Cloud TPU API does not support Ironwood (TPU7x) or later generations, and TorchTPU's VM guide says GKE "is necessary to access recent TPU hardware". Plan on GKE for v7x; chapter 8 explains the choice.
Prerequisites¶
- A GKE Standard cluster with a TPU v7x (
tpu7x) node pool of four chips per node, topology2x2x1, for steps 4 to 6. Step 7 needs a 64-chip4x4x4pool. Google's "Deploy TPU workloads in GKE Standard" page creates them. - Quota: on-demand and spot TPU node pools consume Compute Engine quota, not Cloud TPU quota; a pool that consumes a reservation needs none. A new project may also need
GPUS_ALL_REGIONSabove 0 (see Install TorchTPU on a TPU VM, step 1). - A checkout of the TorchTPU repository (https://github.com/google-pytorch/torch_tpu): the manifests live in its
examples/directory. Run the commands below from its root. - For step 7, the JobSet controller installed in the cluster (https://jobset.sigs.k8s.io/).
Steps¶
1. Create a TPU node pool, if you do not have one¶
TPU node pools are created with gcloud container node-pools create. The general form, from Google's GKE TPU documentation:
gcloud container node-pools create NODE_POOL_NAME \
--cluster=CLUSTER_NAME --location=CONTROL_PLANE_LOCATION \
--node-locations=NODE_ZONE --machine-type=MACHINE_TYPE \
--tpu-topology=TPU_TOPOLOGY --placement-type=COMPACT
For v7x, the topology goes into a workload policy (gcloud compute resource-policies create workload-policy --type=HIGH_THROUGHPUT --accelerator-topology=...) that the pool references with --placement-policy, instead of --tpu-topology. Follow Google's current GKE TPU page for the exact v7x flags. A multi-host pool is one slice: it scales atomically, and an upgrade, repair or failure of one node recreates the whole slice. With --enable-autoscaling, set --num-nodes=0 so nodes appear on demand.
Check: step 4's pod gets scheduled. A pod that stays Pending with no clear error means the accelerator, topology and chip count in the manifest disagree with the pool's machine type.
2. Point kubectl at the cluster¶
export PROJECT=<YOUR_PROJECT_ID>
export CLUSTER=<YOUR_CLUSTER_NAME>
export LOCATION=<YOUR_CLUSTER_ZONE_OR_REGION>
export NODEPOOL_NAME=<YOUR_4CHIP_NODEPOOL_NAME>
export DOCKER_IMAGE=us-docker.pkg.dev/ml-oss-artifacts-published/torch-tpu-public-release-container/torch-tpu:latest
gcloud auth login
gcloud auth application-default login
gcloud components install kubectl gke-gcloud-auth-plugin
gcloud container clusters get-credentials ${CLUSTER} --location=${LOCATION} --project=${PROJECT}
The public image already contains TorchTPU and the example scripts under /workspace/examples/.
Check: get-credentials completes without an error; step 4 confirms the cluster answers.
3. Know the three fields that must agree¶
Every manifest below ties the pod to TPU hardware with three settings: the node selectors (cloud.google.com/gke-nodepool, cloud.google.com/gke-tpu-accelerator: tpu7x, cloud.google.com/gke-tpu-topology), the chip request (google.com/tpu: 4, request equal to limit, all four chips of the node), and TORCH_TPU_TOPOLOGY, which tells TorchTPU how much of that hardware the job uses. A v7x chip has two TensorCores, so a four-chip host has 8 devices.
| Workload | Manifest | gke-tpu-topology |
TORCH_TPU_TOPOLOGY |
Processes |
|---|---|---|---|---|
| Single device | examples/simple_pytorch/torchtpu_singledevice_slice_job.yaml |
2x2x1 |
"1,1,1,1" |
1 |
| Single host, all devices | examples/tensor_parallel/torchtpu_singlehost_slice_job.yaml |
2x2x1 |
"2,2,1,2" |
8, via torchrun |
| Multi-host | examples/tensor_parallel/torchtpu_multihost_slice_jobset.yaml |
4x4x4 |
"4,4,4,2" |
16 pods x 8 |
4. Run one device¶
sed -i "s|<DOCKER_IMAGE>|${DOCKER_IMAGE}|g" examples/simple_pytorch/torchtpu_singledevice_slice_job.yaml
sed -i "s|<NODEPOOL_NAME>|${NODEPOOL_NAME}|g" examples/simple_pytorch/torchtpu_singledevice_slice_job.yaml
kubectl apply -f examples/simple_pytorch/torchtpu_singledevice_slice_job.yaml
kubectl get job,pods -n default
kubectl logs -n default -l job-name=torchtpu-singledevice-1chip-job -f
Check: the job completes, and between the manifest's === Starting Single-Device 1-Chip, 1-Device TPU v7x Workload === and === Single-Host single-device Workload Completed Successfully === banners the log shows:
TPU Initialization complete. Primary device: tpu:0
cpu_tensor is on: cpu
tpu_tensor is on: tpu:0
direct_tpu_tensor is on: tpu:0
5. Run all eight devices of one host¶
sed -i "s|<DOCKER_IMAGE>|${DOCKER_IMAGE}|g" examples/tensor_parallel/torchtpu_singlehost_slice_job.yaml
sed -i "s|<NODEPOOL_NAME>|${NODEPOOL_NAME}|g" examples/tensor_parallel/torchtpu_singlehost_slice_job.yaml
kubectl apply -f examples/tensor_parallel/torchtpu_singlehost_slice_job.yaml
kubectl get job,pods -n default
The container runs launch_singlehost.sh, which detects the topology and starts one process per device:
eval "$(python3 -m torch.tpu.distributed)"
export TORCH_TPU_TOPOLOGY TORCH_TPU_SLICEBUILDER_ADDRESSES
python3 -m torch.distributed.run --nnodes=1 --node_rank=0 \
--master_addr=localhost --master_port=29500 \
--nproc_per_node=8 ./tp_worker.py
Check: the job reaches completion. tp_worker.py checks its tensor-parallel result against an unsharded CPU run with torch.testing.assert_close, so a completed job is a correctness check as well as a launch check.
6. Run your own code on one host¶
Replace the single-device manifest's command: block. The GKE guide's example runs the upstream MNIST script unchanged except for its DataLoader workers:
command:
- /bin/bash
- -c
- |
set -ex
export PYTHONUNBUFFERED=1
apt-get update && apt-get install -y git
git clone --depth 1 https://github.com/pytorch/examples.git /tmp/pytorch_examples
cd /tmp/pytorch_examples/mnist
sed -i "/'num_workers': 1,/d; /'persistent_workers': True,/d" main.py
python3 main.py --epochs=1 --dry-run
Job specs are immutable, so delete the previous run first:
kubectl delete -f examples/simple_pytorch/torchtpu_singledevice_slice_job.yaml --ignore-not-found
kubectl apply -f examples/simple_pytorch/torchtpu_singledevice_slice_job.yaml
kubectl logs -n default -l job-name=torchtpu-singledevice-1chip-job -f
Keep num_workers at 0: a forked DataLoader worker would try to reacquire the libtpu device lockfile. Non-distributed code that uses torch.accelerator or torch.device needs no other change; distributed code tuned for GPUs may need small ones (Port a custom model).
Check: the job runs to completion; a DataLoader error about the device points at num_workers.
7. Run a multi-host JobSet¶
With the JobSet controller installed and NODEPOOL_NAME set to your 64-chip pool:
sed -i "s|<DOCKER_IMAGE>|${DOCKER_IMAGE}|g" examples/tensor_parallel/torchtpu_multihost_slice_jobset.yaml
sed -i "s|<NODEPOOL_NAME>|${NODEPOOL_NAME}|g" examples/tensor_parallel/torchtpu_multihost_slice_jobset.yaml
kubectl apply -f examples/tensor_parallel/torchtpu_multihost_slice_jobset.yaml
kubectl get jobset,pods -n default
Each of the 16 pods runs launch_multihost.sh, which calls python3 -m torch.tpu.distributed to derive NNODES, NODE_RANK, MASTER_ADDR and MASTER_PORT for its host, then starts torchrun with --nproc_per_node=8.
Check: 16 pods are created, one per node, and the JobSet completes.
8. Retarget a manifest¶
To use another topology, change cloud.google.com/gke-tpu-slice-topology, cloud.google.com/gke-tpu-topology and TORCH_TPU_TOPOLOGY together, and keep the chip request equal to the chips per node. set_tpu_launch_env() never overwrites a variable that is already set, so a stale TORCH_TPU_TOPOLOGY in the manifest wins over the detected one; either set it correctly or delete it and let python3 -m torch.tpu.distributed detect it. Chapter 8's worked problem 4 retargets the single-host job to a ct6e-standard-8t v6e pool.
Troubleshooting¶
| Symptom | Cause | Fix |
|---|---|---|
Pod stays Pending with no clear error |
Accelerator, topology and chip count disagree with the node's machine type | Make all three match the node pool |
| Re-applying a Job does nothing new | Job specs are immutable | kubectl delete -f ... --ignore-not-found, then apply |
| A retargeted job still uses the old topology | set_tpu_launch_env() never overwrites a set variable |
Change TORCH_TPU_TOPOLOGY with both topology labels |
| DataLoader workers fail to open the TPU | Forked workers try to reacquire the libtpu lockfile |
num_workers=0 |
kubectl apply rejects the JobSet |
The JobSet controller is not installed | Install a current release from the JobSet project |
| Long-running service preempted after a week | Flex-start nodes run for at most seven days | A reservation first, spot and flex-start as fallbacks in a custom ComputeClass |
| Every pod of a multi-host job evicted after one node fails or upgrades | A multi-host pool is one slice and is repaired atomically | Expected for multi-host pools; checkpoint so a restart loses little |
References¶
- torch_tpu repository: Simple PyTorch on TorchTPU
- torch_tpu source: _internal/distributed/distributed.py
- llm-d: Google Cloud TPU
- TPU Builders Program Getting Started Guide
- TorchTPU 0.2.0 docs: Get started with TorchTPU on GKE
- Google Cloud: Deploy TPU workloads in GKE Standard
- JobSet
- Chapter 8: Getting capacity and installing: VM and GKE