This tutorial shows you how to run multi-host reinforcement learning (RL)
training on a Tensor Processing Unit (TPU) v6e-32 cluster by using MaxText and
Cluster Toolkit. You use
Cluster Toolkit to execute a
multi-host training workload and export the results back to Hugging Face format
for serving.
Workload summary
Running this RL post-training workload on Cloud TPU and GKE involves the following components:
- Model: Gemma 4 26B A4B Instruct (
google/gemma-4-26b-a4b-it), an advanced open-weights instruction-tuned model. In this tutorial, the model weights are downloaded from Hugging Face, converted into MaxText-compatible format, and deployed on a GKE cluster with Cloud TPU v6e infrastructure. - Reinforcement Learning (RL) algorithm: Group Relative Policy
Optimization (GRPO) using MaxText and Pathways. The workload executes a
scalable post-training pipeline using MaxText's built-in RL trainer
(
maxtext.trainers.post_train.rl.train_rl) paired with a vLLM sampler backend. GRPO optimizes the policy by generating a group of outputs per prompt and evaluating relative rewards without needing a separate critic model. - Dataset and template: OpenMathInstruct-2 (
openmathinstruct2_rl.json), a large-scale math instruction-tuning dataset. The workload uses structured chat templates during the RL rollout phase to enhance multi-step mathematical reasoning capabilities. - Infrastructure and framework: GKE, managed using
Cluster Toolkit, on Cloud TPU v6e (
ct6e-standard-4t,v6e-32slice with a4x8topology). The framework is MaxText (tpu_post_training:0.2.4) with Pathways and a JAX backend.
Objectives
- Install Cluster Toolkit and its dependencies.
- Deploy a Cluster Toolkit cluster.
- Convert a Hugging Face model to MaxText format.
- Run an RL training workload on the TPU v6e cluster.
- Convert the fine-tuned model back to Hugging Face format for serving.
Costs
In this document, you use the following billable components of Google Cloud:
To generate a cost estimate based on your projected usage,
use the pricing calculator.
When you finish the tasks that are described in this document, you can avoid continued billing by deleting the resources that you created. For more information, see Clean up.
Before you begin
You need a Hugging Face access token to use this tutorial. You can sign up for a free account at Hugging Face. After you have an account, generate an access token:
- On the Welcome to Hugging Face page, click your account avatar and select Access tokens.
- On the Access tokens page, click Create new token.
- Select the Read token type and enter a name for your token.
- Your access token is displayed. Save the token in a safe place.
- On the
google/gemma-4-26b-a4b-itmodel card on Hugging Face, accept the license agreement for the model.
To get the permissions that you need to complete this tutorial, ask your administrator to grant you the following IAM roles on your project:
- TPU Admin (
roles/tpu.admin) - Kubernetes Engine Admin (
roles/container.admin) - Compute Admin (
roles/compute.admin) - Storage Admin (
roles/storage.admin) - Service Account User (
roles/iam.serviceAccountUser) - Service Account Admin (
roles/iam.serviceAccountAdmin) - Project IAM Admin (
roles/resourcemanager.projectIamAdmin) - Service Usage Admin (
roles/serviceusage.serviceUsageAdmin)
For more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
Set up your environment variables
Set up your environment variables:
Replace the following:
YOUR_PROJECT_ID: the ID of your Google Cloud project.YOUR_REGION: the region where your TPU reservation is located and where you deploy your cluster.YOUR_ZONE: the zone where your TPU reservation is located and where you deploy your cluster.YOUR_CLUSTER_NAME: the name of your GKE cluster. The name must start with a lowercase letter, contain only lowercase letters, numbers, and hyphens, and be 20 characters or fewer.YOUR_BUCKET_NAME: a globally unique name for a Cloud Storage bucket.YOUR_RESERVATION_NAME: the name of your TPU v6e reservation.YOUR_HF_TOKEN: your Hugging Face access token.
Install Cluster Toolkit dependencies
To complete this tutorial from a Linux or macOS client or workstation, follow the relevant steps in Install dependencies in the Cluster Toolkit documentation.
If you're using Cloud Shell, then you can skip this section.
Install Cluster Toolkit
Download and extract the prebuilt bundle for the specific version of
Cluster Toolkit defined in CLUSTER_TOOLKIT_VERSION (v1.103.0) into
your current working directory:
Authenticate your environment
Before running the deployment scripts, you must authenticate the Google Cloud CLI and set your Application Default Credentials (ADC). This ensures that Cluster Toolkit can provision resources and push images to Artifact Registry on your behalf.
Create your Cluster Toolkit cluster
To create and deploy a Cluster Toolkit cluster with 32 v6e TPU chips,
complete the following steps:
Create a Cloud Storage bucket:
Use the
gcluster deploycommand to deploy your Cluster Toolkit cluster by using the blueprintgke-tpu-v6e-advanced.yamland passing the required variables by using the--varsflag:
Convert the model to MaxText format
To train the model in MaxText format, you must convert it from Hugging Face format to MaxText format.
To simplify subsequent commands, use the
gcluster job configcommand to configure your default project, cluster, and location:Use the
gcluster job submitcommand to convert the model from Hugging Face format to MaxText format and store it in your Cloud Storage bucket:The command includes the
--await-job-completionflag, which means that it waits in your current terminal until the conversion finishes.Optional: To stream the conversion logs while the job is running, open a separate terminal in the working directory where you extracted
gcluster, and run thegcluster job logscommand:In your original terminal, verify that the converted model files are available in your Cloud Storage bucket:
Start the training workload
After the conversion process has completed, start the RL training workload:
Check the status, wait for the coordinator Pod to become ready, and stream the logs of the training job:
Immediately after submission, the output from the
./gcluster job listcommand might briefly showSuspendedwhile GKE scales up the Pathways coordinator CPU node pool (cpu-np). When the job begins, it outputs an initial pre-training evaluation at step 0 (Pre RL Training: ... mean_reward=...), pauses log output for several minutes during initial XLA compilation, and then trains for 50 steps, printingReshard finishedand rollout generations at each step without step numbers.Before you proceed to the next step, wait until the logs output
Post RL Training: ... mean_reward=...and the output from the./gcluster job listcommand shows the status ofgemma4-trainingasSucceeded.Verify that the training checkpoints are generated in your Cloud Storage bucket:
Convert the trained model back into Hugging Face format
After the training workload has completed, convert the model back to Hugging Face format:
Optional: To monitor the live conversion logs while the preceding
--await-job-completioncommand waits in your first terminal, open a separate terminal in the working directory where you extractedgcluster, and use thegcluster job logscommand:In your original terminal, verify that the trained Hugging Face model weights and configuration files are present in your Cloud Storage bucket:
Clean up
To avoid incurring additional charges, use the gcluster destroy
command to delete the
resources created during this tutorial:
What's next
- For more information about Cloud TPU, see Introduction to Cloud TPU.
- For architecture and configuration details for the
v6e-32TPU, see TPU v6e. - For more information about Cluster Toolkit, see Cluster Toolkit overview.