This tutorial shows you how to run supervised fine-tuning (SFT) on a multi-host Tensor Processing Unit (TPU) v6e 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 SFT workload on Cloud TPU and GKE involves the following components:
- Model: Gemma 4 31B (
google/gemma-4-31b-it), an advanced open-weights 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 (v6e-32) infrastructure. - Training method: SFT using MaxText. The workload executes a scalable
fine-tuning pipeline on Cloud TPU v6e using JAX. It optimizes model
parameters over 1000 steps using MaxText's built-in SFT trainer
(
maxtext.trainers.post_train.sft.train_sft) and instruction-tuning datasets. - Dataset and template: UltraChat 200k (
HuggingFaceH4/ultrachat_200k), a large-scale, high-quality multi-turn dialogue dataset. The workload uses this dataset to fine-tune the model and improve its conversational and instruction-following capabilities. - Infrastructure and framework: GKE, managed using
Cluster Toolkit, on Cloud TPU v6e (
v6e-32, with a4x8topology). The framework is MaxText (tpu_post_training:0.2.4) with 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 SFT training workload on the TPU.
- 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-31b-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:
-
To complete this tutorial:
- TPU Admin (
roles/tpu.admin) - Service Account User (
roles/iam.serviceAccountUser) - Compute Admin (
roles/compute.admin) - Storage Admin (
roles/storage.admin) - Kubernetes Engine Admin (
roles/container.admin) - Service Account Admin (
roles/iam.serviceAccountAdmin) - Project IAM Admin (
roles/resourcemanager.projectIamAdmin) - Service Usage Admin (
roles/serviceusage.serviceUsageAdmin)
- TPU Admin (
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_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.
By default, the script sets MODEL_NAME to gemma4-31b (the MaxText model
name for google/gemma-4-31b-it) and CLUSTER_NAME to gke-tpu-v6e. If you
customize CLUSTER_NAME, the name must start with a lowercase letter, contain
only lowercase letters, numbers, and hyphens, and be 20 characters or fewer so
that generated service account IDs stay within the 30-character limit.
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
To ensure that Cluster Toolkit can provision resources and push images to Artifact Registry on your behalf, authenticate the Google Cloud CLI and set your Application Default Credentials:
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:
Stage the blueprint configuration and copy the example deployment blueprint to an isolated working directory:
Update
tmp/gke-tpu-v6e-advanced.yamlto grant your cluster node pool service account (gke-np-sa) the Storage Admin role (roles/storage.admin) for writing checkpoints to your Cloud Storage bucket, switch the system node pool machine type toe2-standard-8, and pin the system node pool to your target zone to avoid regional machine-type stockouts:Use the
gcluster deploycommand to deploy your Cluster Toolkit cluster by using the blueprinttmp/gke-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 SFT workload:
The command includes the
--await-job-completionflag, which means that it waits in your current terminal until the training job finishes.Optional: To stream the live training logs while the job is running, open a separate terminal in the working directory where you extracted
gcluster, and run thegcluster job logscommand:After initial XLA compilation completes, the coordinator logs output progress for each step across the 1000-step run in the format
completed step: <step>, seconds: ..., TFLOP/s/device: ..., loss: ..., with the training loss decreasing from approximately1.57at step 5 to approximately0.82at step 1000. Before you proceed to the next section, wait until step 1000 finishes and the output from the./gcluster job listcommand shows the status ofsftasSucceeded.In your original terminal, verify that the SFT 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:
The command includes the
--await-job-completionflag, which means that it waits in your current terminal until the conversion finishes.Optional: To stream the live 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 final Hugging Face Safetensors weights are generated 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.