This tutorial shows you how to fine-tune the Gemma 3 large language model (LLM) on a multi-node Slurm cluster that uses two A4 virtual machine (VM) instances. As part of this tutorial, you do the following:
Create a custom image.
Configure an RDMA network.
Run a distributed fine-tuning job. For efficient multi-node training, you use the Hugging Face Accelerate library with Fully Sharded Data Parallel (FSDP).
This tutorial is intended for machine learning (ML) engineers, platform administrators and operators, and for data and AI specialists who are interested in using Slurm job scheduling capabilities to handle fine-tuning workloads.
Objectives
Access Gemma 3 by using Hugging Face.
Prepare your environment.
Create an A4 Slurm cluster.
Prepare your workload.
Run a fine-tuning job.
Monitor your job.
Clean up.
Costs
In this document, you use the following billable components of Google Cloud:
- Compute Engine
- Google Kubernetes Engine (GKE) Enterprise edition
- Filestore
- Cloud Storage
- Cloud Logging
To generate a cost estimate based on your projected usage,
use the pricing calculator.
Before you begin
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
Enable the required API:
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.gcloud services enable compute.googleapis.com file.googleapis.com logging.googleapis.com cloudresourcemanager.googleapis.com servicenetworking.googleapis.com
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init -
Create or select a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_IDwith a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_IDwith your Google Cloud project name.
-
Verify that billing is enabled for your Google Cloud project.
Enable the required API:
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.gcloud services enable compute.googleapis.com file.googleapis.com logging.googleapis.com cloudresourcemanager.googleapis.com servicenetworking.googleapis.com
-
Grant roles to your user account. Run the following command once for each of the following IAM roles:
roles/compute.admin, roles/iam.serviceAccountUser, roles/file.editor, roles/storage.admin, roles/serviceusage.serviceUsageAdmingcloud projects add-iam-policy-binding PROJECT_ID --member="user:USER_IDENTIFIER" --role=ROLE
Replace the following:
PROJECT_ID: Your project ID.USER_IDENTIFIER: The identifier for your user account. For example,myemail@example.com.ROLE: The IAM role that you grant to your user account.
- Enable the default service account for your Google Cloud project:
gcloud iam service-accounts enable PROJECT_NUMBER-compute@developer.gserviceaccount.com \ --project=PROJECT_ID
Replace PROJECT_NUMBER with your project number. To review your project number, see Get an existing project.
- Grant the Editor role (
roles/editor) to the default service account:gcloud projects add-iam-policy-binding PROJECT_ID \ --member="serviceAccount:PROJECT_NUMBER-compute@developer.gserviceaccount.com" \ --role=roles/editor
- Create local authentication credentials for your user account:
gcloud auth application-default login
- Enable OS Login for your project:
gcloud compute project-info add-metadata --metadata=enable-oslogin=TRUE
- Sign in to or create a Hugging Face account.
Access Gemma 3 by using Hugging Face
To use Hugging Face to access Gemma 3, follow these steps:
Create a Hugging Face
readaccess token. Click Your Profile > Settings > Access tokens > +Create new tokenCopy and save the
read accesstoken value. You use it later in this tutorial.
Install Cluster Toolkit
For more information about using gcluster and managing clusters, see the Cluster Toolkit overview.
Prepare the Cluster Toolkit version:
Download the release:
Define the
gclusterpath:
Prepare your environment
To prepare your environment, follow these steps:
Set the default environment variables:
Replace the following:
YOUR_PROJECT_ID: the ID of the Google Cloud project where you want to create your Cloud Storage bucket.YOUR_ZONE: the zone where your reservation exists.YOUR_REGION: the region where your reservation exists.RESERVATION_NAME: the URL or the name of the reservation that you want to use to create your Slurm cluster.YOUR_CLUSTER_NAME: the name of the Slurm cluster that you want to create.YOUR_BUCKET_NAME: a name for your Cloud Storage bucket that follows bucket naming requirements.HUGGING_FACE_TOKEN: the Hugging Face access token that you created in the previous section.
Create a Cloud Storage bucket:
Create an A4 Slurm cluster
To create an A4 Slurm cluster, follow these steps:
Create the
a4high-slurm-deployment.yamlfile:Prepare the manifests:
Create the Terraform manifests:
Patch the manifests:
Deploy the cluster:
The
gcluster deploycommand is a two-phase process, which is as follows:The first phase builds a custom image with all software pre-installed, which can take up to 45 minutes to complete.
The second phase deploys the cluster by using that custom image. This process generally takes less time to complete than the first phase.
If the first phase succeeds but the second phase fails, then you can try to deploy the Slurm cluster again by skipping the first phase:
Prepare your workload
To prepare your workload, follow these steps:
Create workload scripts
To create the scripts that your fine-tuning workload will use, follow these steps:
To set up the Python virtual environment, create the
install_environment.shfile with the following content:To specify the configurations for your fine-tuning job, create the
accelerate_config.yamlfile with the following content:To specify the tasks for the jobs to run on your Slurm cluster, create the
submit.slurmfile with the following content:To specify the dependencies for your fine-tuning job, create the
requirements.txtfile with the following content:To specify the instructions for your job, create the
train.pyfile with the following content:
Upload scripts to the Slurm cluster
To upload the scripts that you created in the previous section to the Slurm cluster, follow these steps:
Set the
LOGIN_NODEvariable by retrieving the name of the login node for your cluster:The
LOGIN_NODEvariable stores a value similar to${CLUSTER_NAME}-login-001.Upload your scripts to the login node's home directory:
Connect to the Slurm cluster
Connect to the login node, and propagate your Hugging Face token to the new session:
Install frameworks and tools
After connecting to the login node, set up a Python virtual environment with the required dependencies:
Start your fine-tuning workload
To start your fine-tuning workload, follow these steps:
Submit the job to the Slurm scheduler and collect the Job ID:
On the login node in your Slurm cluster, you can monitor the job's progress by checking the output files created in your
homedirectory:If your job successfully starts, then the
.errfile shows a progress bar that updates as your job progresses.
Monitor your workload
You can monitor the use of the GPUs in your Slurm cluster to verify that your fine-tuning job is efficiently running. To do so, open the following link in your browser:
https://console.cloud.google.com/monitoring/metrics-explorer?project=PROJECT_ID&pageState=%7B%22xyChart%22%3A%7B%22dataSets%22%3A%5B%7B%22timeSeriesFilter%22%3A%7B%22filter%22%3A%22metric.type%3D%5C%22agent.googleapis.com%2Fgpu%2Futilization%5C%22%20resource.type%3D%5C%22gce_instance%5C%22%22%2C%22perSeriesAligner%22%3A%22ALIGN_MEAN%22%7D%2C%22plotType%22%3A%22LINE%22%7D%5D%7D%7D
When you monitor your workload, you can see the following:
GPUs usage: for a healthy fine-tuning job, you can expect to see the usage of all your 16 GPUs (eight GPUs for each VM in the cluster) rise and stabilize to a specific level throughout your training.
Job duration: the job should take approximately one hour to complete.
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources.
Delete your Slurm cluster
To delete your Slurm cluster:
If you need to delete all VPC networks, firewall rules, routers, IPs, and subnets associated with the project, do the following:
Delete your project
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID