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.
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
Authenticate your user account:
gcloud auth login
Generate Application Default Credentials (ADC):
gcloud auth application-default login
If you're running in a headless or remote SSH session without a browser, append
--no-launch-browser to both commands.
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 Hugging Face website, accept the license
agreement for the model that you plan to train. This tutorial uses the
model
gemma4-31b.
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) - Storage Object Viewer (
roles/storage.objectViewer) - Logs Writer (
roles/logging.logWriter) - Artifact Registry Writer (
roles/artifactregistry.writer)
- 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 by running the following script:
Replace the following:
- YOUR_PROJECT_ID: the ID of your Google Cloud project.
- YOUR_REGION: the region where you want to deploy your cluster.
- YOUR_ZONE: the zone where you want to deploy your cluster.
- YOUR_RESERVATION_NAME: the name of your reservation.
- YOUR_HF_TOKEN: your Hugging Face access token.
- YOUR_BUCKET_NAME: a globally unique name for a Cloud Storage bucket.
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
Install the prebuilt bundle for Cluster Toolkit by following the instructions at Install Cluster Toolkit.
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:
By default, your cluster node pool service account doesn't have the required permissions to write to your Cloud Storage bucket. To allow the node pool service account to write to your Cloud Storage bucket, you must grant it the
Storage Adminrole. To grant this role, edit the filetmp/gke-tpu-v6e-advanced.yamlby updating thenode_pool_service_accountmodule: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:Fetch Google Kubernetes Engine cluster credentials for
kubectl:Configure container registry authentication:
Grant the Storage Admin role (
roles/storage.admin) to your Google Kubernetes Engine service accounts:
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:To convert the model from Hugging Face format to MaxText format and store it in your Cloud Storage bucket, run the following script, which uses the
gcluster job submitcommand:
To check the status of the conversion job, run the gcluster job logs
command:
Start the training workload
After the conversion process has completed, you can start the SFT workload by running the following command:
To check the status of the training job, run the following command:
Convert the trained model back into Hugging Face format
After the training workload has completed, convert the model back to Hugging Face format:
To check the status of the conversion job, run the following command:
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.