This tutorial shows you how to train a large language model (LLM) on a multi-node, multi-GPU Slurm cluster on Google Cloud. The model that you use in this tutorial is based on a Qwen2 1.5 billion parameter model. The Slurm cluster uses two a4-highgpu-8g virtual machines (VMs), which each have 8 NVIDIA B200 GPUs.
The two main processes described in this tutorial are as follows:
- Deploy a production-grade, high-performance Slurm cluster using the Google Cloud Cluster Toolkit. As part of this deployment, you create a custom VM image with the necessary software pre-installed. You also set up a shared Filestore instance, and configure high-speed RDMA networking.
- After the cluster is deployed, you run a distributed pre-training job by using the set of scripts that accompany this tutorial. The job leverages the Hugging Face Accelerate library.
This tutorial is intended for machine learning (ML) engineers, researchers, platform administrators and operators, and for data and AI specialists who are interested in deploying high-performance Slurm clusters on Google Cloud to train LLMs.
Objectives
- Access the Qwen2 model by using Hugging Face.
- Prepare your environment.
- Create and deploy a production-grade A4 Slurm cluster.
- Train the Qwen2 model by using the Accelerate library .
- Monitor your job.
- Clean up.
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.
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.serviceUsageAdmin, roles/compute.osAdminLogin, roles/iap.tunnelResourceAccessorgcloud 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 Qwen2 by using Hugging Face
To use Hugging Face to access Qwen2, do the following:
Install Cluster Toolkit
Cluster Toolkit is an open-source tool that simplifies deploying high
performance computing (HPC), artificial intelligence (AI), and machine learning
(ML) workloads on Google Cloud. 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 name of the Google Cloud project where you want to create the GKE cluster.YOUR_CLUSTER_NAME: the name of the Slurm cluster that you want to create.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_BUCKET_NAME: the name of the bucket where you store the results from the training checkpoint. Before you create it, familiarize yourself with bucket naming requirements.HUGGING_FACE_TOKEN: the Hugging Face token that you created in an earlier step.
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: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 50 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 training 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 a
requirements.txtfile with the following content:
Note: This tutorial doesn't use the latest versions of NVIDIA and Pytorch dependencies. If you need newer dependencies, then see the NVIDIA documentation and the Pytorch documentation.To download, tokenize, and preprocess the dataset into a format that's ready for training, create a
preprocess_data.pyfile with the following content:To specify the instructions for your job, create a
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.Create a firewall rule:
Upload your scripts to the login node's home directory:
Connect to the Slurm cluster
Connect to the Slurm cluster by connecting to the login node through SSH:
Install frameworks and tools
After you connect to the login node, install frameworks and tools by doing the following:
Set up a Python virtual environment with all the required dependencies:
Start your pre-training your workload
To start your training your workload, do the following:
Submit the job to the Slurm scheduler:
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
Alternately, you can enter the command directly in the terminal:
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. TEST
Job duration: the job should take approximately one hour to complete.
Download your model
After you have successfully run your job, your trained model is saved in the
~/qwen2-from-scratch-on-smollm-fineweb/ directory on the login node. Because this
persistent shared directory is mounted across all of the nodes in your cluster,
your model checkpoints remain available even after the job completes or the
compute nodes are deallocated.
You can download the saved model from the login node to your local machine by
using the gcloud compute scp command, as shown in the following example:
After you download your model, you can do the following:
- Load the model for inference: Use the Hugging Face Transformers framework
to load the
qwen2-trained-model/directory and perform inference with your newly trained Qwen2 model. - Additional fine-tuning: Use the saved checkpoint as a starting point for additional fine-tuning on a more specific dataset.
- Push the model to the Hugging Face Hub: Share your trained model by pushing it to the Hugging Face Hub.
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 resources
To delete your Slurm cluster, follow these steps:
To delete your Cloud Storage bucket, follow these steps:
To delete a Packer image, open your web browser, navigate to the following page, search for your specific image, and click Delete.
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