|
| 1 | +# Single-host Supervised Fine-Tuning (SFT) with TPUs on Google Kubernetes Engine (GKE) using MaxText |
| 2 | + |
| 3 | +This example implements Supervised Fine-Tuning (SFT) using MaxText and Tunix on |
| 4 | +Cloud TPUs on Google Kubernetes Engine (GKE). |
| 5 | + |
| 6 | +It leverages **MaxText**'s scalable FSDP training loops and **Tunix** |
| 7 | +post-training libraries on a single TPU v6e-8 slice (`v6e-2x4`) to fine-tune |
| 8 | +Llama-3.1-8B-Instruct on instruction-following datasets. |
| 9 | + |
| 10 | +This use-case is built on top of the |
| 11 | +[GKE Training Reference Architecture](/platforms/gke/base/use-cases/training-ref-arch/terraform/README.md). |
| 12 | + |
| 13 | +## Before you begin |
| 14 | + |
| 15 | +- The |
| 16 | + [GKE Training Reference Architecture](/platforms/gke/base/use-cases/training-ref-arch/terraform/README.md) |
| 17 | + is deployed and configured. |
| 18 | + |
| 19 | +- Get access to the model. |
| 20 | + |
| 21 | + - For Llama-3.1: |
| 22 | + - Accept the terms of the license on the Hugging Face model page: |
| 23 | + - [**meta-llama/Llama-3.1-8B-Instruct**](https://huggingface.co/meta-llama/Llama-3.1-8B-Instruct) |
| 24 | + |
| 25 | +- Ensure your |
| 26 | + [Hugging Face Hub **Read** access token](/platforms/gke/base/core/huggingface/initialize/README.md) |
| 27 | + has been added to Secret Manager. |
| 28 | + |
| 29 | +- Hardware & Storage Prerequisites: |
| 30 | + - **Hardware**: This configuration is tuned for a **TPU v6e-8** (`v6e-2x4`) |
| 31 | + slice topology. |
| 32 | + - **Storage**: GCS bucket configured for storing Hugging Face converted |
| 33 | + checkpoints and SFT checkpoint weights. |
| 34 | + |
| 35 | +## Create and configure the Google Cloud resources |
| 36 | + |
| 37 | +- Deploy the SFT cloud infrastructure resources (SFT dataset bucket, Kubernetes |
| 38 | + service accounts, IAM bindings, and namespaces). |
| 39 | + |
| 40 | + ```shell |
| 41 | + export TF_PLUGIN_CACHE_DIR="${ACP_REPO_DIR}/.terraform.d/plugin-cache" |
| 42 | + cd ${ACP_REPO_DIR}/platforms/gke/base/use-cases/training-ref-arch/terraform/sft-tpu-maxtext-single-host && \ |
| 43 | + rm -rf .terraform/ terraform.tfstate* && \ |
| 44 | + terraform init && \ |
| 45 | + terraform plan -input=false -out=tfplan && \ |
| 46 | + terraform apply -input=false tfplan && \ |
| 47 | + rm tfplan |
| 48 | + ``` |
| 49 | + |
| 50 | +## Convert the Hugging Face weights to MaxText format |
| 51 | + |
| 52 | +Before starting Supervised Fine-Tuning (SFT) training, you can run a one-time |
| 53 | +CPU-based checkpoint conversion job to convert the base Hugging Face weights |
| 54 | +into MaxText format. Running this on CPU nodes preserves valuable TPU resources. |
| 55 | + |
| 56 | +- Choose the model to convert. |
| 57 | + |
| 58 | + - **Llama 3.1 8B Instruction-Tuned**: |
| 59 | + |
| 60 | + ```shell |
| 61 | + export HF_MODEL_ID="llama3.1-8b-Instruct" |
| 62 | + ``` |
| 63 | + |
| 64 | + - **Gemma 3 4B Instruction-Tuned**: |
| 65 | + |
| 66 | + ```shell |
| 67 | + export HF_MODEL_ID="google/gemma-3-4b-it" |
| 68 | + ``` |
| 69 | + |
| 70 | + - **Gemma 4 26B Instruction-Tuned**: |
| 71 | + |
| 72 | + ```shell |
| 73 | + export HF_MODEL_ID="gemma4-26b" |
| 74 | + ``` |
| 75 | + |
| 76 | +- Source the environment configuration: |
| 77 | + |
| 78 | + ```shell |
| 79 | + source "${ACP_REPO_DIR}/platforms/gke/base/use-cases/training-ref-arch/terraform/_shared_config/scripts/set_environment_variables.sh" |
| 80 | + ``` |
| 81 | + |
| 82 | +- Configure the checkpoint converter deployment: |
| 83 | + |
| 84 | + ```shell |
| 85 | + "${ACP_REPO_DIR}/platforms/gke/base/use-cases/training-ref-arch/kubernetes-manifests/maxtext-checkpoint-converter/configure_checkpoint_converter.sh" |
| 86 | + ``` |
| 87 | + |
| 88 | +- Deploy the checkpoint converter job: |
| 89 | + |
| 90 | + ```shell |
| 91 | + kubectl apply --kustomize "${ACP_REPO_DIR}/platforms/gke/base/use-cases/training-ref-arch/kubernetes-manifests/maxtext-checkpoint-converter/checkpoint-converter" |
| 92 | + ``` |
| 93 | + |
| 94 | +- Watch the checkpoint converter job until it is complete: |
| 95 | + |
| 96 | + ```shell |
| 97 | + watch --color --interval 5 --no-title \ |
| 98 | + "kubectl --namespace=${sft_cpu_maxtext_checkpoint_converter_kubernetes_namespace_name} get job/${HF_MODEL_ID_HASH}-maxtext-checkpoint-converter | GREP_COLORS='mt=01;92' egrep --color=always -e '^' -e 'Complete' |
| 99 | + echo '\nLogs(last 10 lines):' |
| 100 | + kubectl --namespace=${sft_cpu_maxtext_checkpoint_converter_kubernetes_namespace_name} logs job/${HF_MODEL_ID_HASH}-maxtext-checkpoint-converter --all-containers --tail 10" |
| 101 | + ``` |
| 102 | + |
| 103 | + Once complete, your model checkpoints will be stored under |
| 104 | + `gs://${huggingface_hub_models_bucket_name}/maxtext-checkpoint-converter-output/`. |
| 105 | + |
| 106 | +- Clean up the CPU conversion job: |
| 107 | + |
| 108 | + ```shell |
| 109 | + kubectl delete --ignore-not-found --kustomize "${ACP_REPO_DIR}/platforms/gke/base/use-cases/training-ref-arch/kubernetes-manifests/maxtext-checkpoint-converter/checkpoint-converter" |
| 110 | + ``` |
| 111 | + |
| 112 | +## Deploy the SFT workload |
| 113 | + |
| 114 | +- Source the environment configuration. |
| 115 | + |
| 116 | + ```shell |
| 117 | + source "${ACP_REPO_DIR}/platforms/gke/base/use-cases/training-ref-arch/terraform/_shared_config/scripts/set_environment_variables.sh" |
| 118 | + ``` |
| 119 | + |
| 120 | +- Configure the SFT deployment manifests. |
| 121 | + |
| 122 | + ```shell |
| 123 | + "${ACP_REPO_DIR}/platforms/gke/base/use-cases/training-ref-arch/kubernetes-manifests/sft-tpu-maxtext-single-host/configure_job.sh" |
| 124 | + ``` |
| 125 | + |
| 126 | +- Deploy the SFT workload. |
| 127 | + |
| 128 | + For TPU v6e (Llama 3.1 8B): |
| 129 | + |
| 130 | + ```shell |
| 131 | + kubectl apply --kustomize "${ACP_REPO_DIR}/platforms/gke/base/use-cases/training-ref-arch/kubernetes-manifests/sft-tpu-maxtext-single-host/v6e-2x4-llama-3-1-8b-instruct" |
| 132 | + ``` |
| 133 | + |
| 134 | + For TPU v6e (Gemma 3 4B): |
| 135 | + |
| 136 | + ```shell |
| 137 | + kubectl apply --kustomize "${ACP_REPO_DIR}/platforms/gke/base/use-cases/training-ref-arch/kubernetes-manifests/sft-tpu-maxtext-single-host/v6e-2x4-gemma-3-4b-instruct" |
| 138 | + ``` |
| 139 | + |
| 140 | +- Watch the SFT training job until it is complete: |
| 141 | + |
| 142 | + ```shell |
| 143 | + watch --color --interval 5 --no-title \ |
| 144 | + "kubectl --namespace=${sft_tpu_maxtext_single_host_kubernetes_namespace_name} get job/sft-tpu-maxtext-single-host-v6e-2x4-llama-3-1-8b-instruct | GREP_COLORS='mt=01;92' egrep --color=always -e '^' -e 'Complete' |
| 145 | + echo '\nLogs(last 10 lines):' |
| 146 | + kubectl --namespace=${sft_tpu_maxtext_single_host_kubernetes_namespace_name} logs job/sft-tpu-maxtext-single-host-v6e-2x4-llama-3-1-8b-instruct --all-containers --tail 10" |
| 147 | + ``` |
| 148 | + |
| 149 | + When the job is complete, you will see the following status: |
| 150 | + |
| 151 | + ```text |
| 152 | + NAME STATUS COMPLETIONS DURATION AGE |
| 153 | + sft-tpu-maxtext-single-host-v6e-2x4-llama-3-1-8b-instruct Complete 1/1 ### ### |
| 154 | + ``` |
| 155 | + |
| 156 | + You can press `CTRL`+`c` to terminate the watch. |
| 157 | + |
| 158 | +## Viewing Metrics (MLflow & TensorBoard) |
| 159 | + |
| 160 | +MaxText logs step metrics directly to TensorBoard format in Cloud Storage during |
| 161 | +execution. |
| 162 | + |
| 163 | +### Accessing the MLflow UI |
| 164 | + |
| 165 | +Because MLflow runs inside the cluster, you can port-forward the service to view |
| 166 | +the dashboard locally: |
| 167 | + |
| 168 | +1. **Port-forward the MLflow Service:** |
| 169 | + |
| 170 | + ```shell |
| 171 | + kubectl port-forward --namespace=${sft_cpu_mlflow_kubernetes_namespace_name} svc/mlflow-service-svc 5000:5000 |
| 172 | + ``` |
| 173 | + |
| 174 | +2. **Open your Browser:** Navigate to `http://localhost:5000` |
| 175 | + |
| 176 | +3. **View SFT Experiment Runs:** |
| 177 | + - Select the `sft-tpu-maxtext-single-host` experiment. |
| 178 | + - Click on your active run (e.g., `Llama3.1-8B-SFT-...`). |
| 179 | + - Inspect logged loss curves, learning rates, and gradient norms. |
| 180 | + |
| 181 | +## Critical Design Features |
| 182 | + |
| 183 | +1. **Decoupled Conversion**: Checkpoint conversion from Hugging Face format to |
| 184 | + MaxText is decoupled into a dedicated CPU-based conversion job, preserving |
| 185 | + valuable TPU resources for training. |
| 186 | +2. **Official Post-Training Container Image**: Uses the official Google Cloud |
| 187 | + TPU MaxText post-training image (`tpu_post_training:0.2.4`) with execution |
| 188 | + scripts mounted declaratively via Kubernetes ConfigMaps, eliminating the need |
| 189 | + to build and maintain custom container images. |
| 190 | +3. **Optimized Resource Layout**: Configured with a dedicated GCS bucket and GKE |
| 191 | + Workload Identity bindings for clean security isolation. |
0 commit comments