This tutorial shows you how to run supervised fine-tuning (SFT) on a 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.
- Install MaxText 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 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 Editor (
roles/compute.editor)
- TPU Admin (
-
To prepare your MaxText container image:
- Cloud Build Editor (
roles/cloudbuild.builds.editor) - Artifact Registry Administrator (
roles/artifactregistry.admin) - Storage Administrator (
roles/storage.admin) - Service Usage Administrator (
roles/serviceusage.serviceUsageAdmin)
- Cloud Build Editor (
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_REPOSITORY_NAME: the name of the Artifact Registry repository for your MaxText images.
- 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.
Prepare your MaxText container image
To prepare your MaxText container image, including installing required dependencies, complete the following steps:
Create a Cloud Storage bucket:
Create an Artifact Registry repository:
Create a file in the root directory of your repository with the filename
cloudbuild.yamland the following contents:Use Cloud Build to build your MaxText Docker image:
Create your Cluster Toolkit cluster
To create and deploy a Cluster Toolkit cluster with 32 v6e TPU chips,
complete the following steps:
Create a custom Identity and Access Management (IAM) role, named
gke.gcsfuse.profileUser:Create a Cloud Storage bucket:
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 filegke-tpu-v6e-advanced.yamlby updating thenode_pool_service_accountmodule:Deploy your Cluster Toolkit cluster by using the blueprint
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.
After you've finished creating your Cluster Toolkit cluster, configure Docker:
To simplify subsequent commands, 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:
To check the status of the conversion job, run the following 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, 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.