This tutorial shows you how to fine-tune a mistralai/Mixtral-8x7B-v0.1
model on a multi-node, multi-GPU Slurm cluster on Google Cloud. The cluster uses two
a4-highgpu-8g virtual machine (VM) instances, 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 by 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 Lustre file system and configure high-speed networking.
- After the cluster is deployed, you run a distributed fine-tuning job by using the set of scripts that accompany this tutorial. The job leverages PyTorch Fully Sharded Data Parallel (FSDP), which you access through the Hugging Face Transformer Reinforcement Learning (TRL) library.
This tutorial is intended for machine learning (ML) engineers, researchers, platform administrators and operators, and data and AI specialists who are interested in distributing an AI workload across multiple nodes and GPUs.
Objectives
- Access Mixtral by using Hugging Face
- Install Cluster Toolkit
- Prepare your environment
- Create and deploy a production-grade A4 High-GPU Slurm cluster.
- Configure a multi-node environment for distributed training with FSDP.
- Fine-tune the Mixtral model by using the Hugging Face
trl.SFTTrainerclass. - Stage data to local SSDs.
- 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 lustre.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 lustre.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.
- Install the dependencies that you need to use the Cluster Toolkit.
Access Mixtral by using Hugging Face
To use Hugging Face to access Mixtral, do the following:
- Sign in to Hugging Face and explore the Mixtral model.
- Create a Hugging Face
readaccess token. - Copy and save the token value. You use it later in this tutorial.
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 your Slurm cluster.YOUR_ZONE: the zone where your reservation exists.YOUR_REGION: the region where your reservation exists.YOUR_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 your deployment. Use a short name containing only letters and numbers (for example,a4high). This name is also assigned to the Slurm cluster created by the deployment.YOUR_GCS_BUCKET: the name of the bucket where you store the results from the training checkpoint. Specify an existing bucket or create a new one. Before you create it, familiarize yourself with bucket naming requirements.YOUR_HF_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, do the following:
Overwrite the
a4high-slurm-deployment.yamlfile in theexamples/machine-learning/a4-highgpu-8gdirectory with your environment variables by running the following command:Open the
a4high-slurm-blueprint.yamlfile in theexamples/machine-learning/a4-highgpu-8gdirectory, and edit it to use Managed Lustre for the shared/homedirectory as follows:- Remove the default
homefsmodule block withsource: modules/file-system/filestore. - Enable the
lustrefs(homefsblock withremote_mount: lustrefs) andprivate-service-accessmodules. - In the
varsblock, configure the following:- Change the value for
install_managed_lustrein/var/tmp/slurm_vars.jsontotrue. - Set
per_unit_storage_throughputparameter to500. - Set
lustre_size_gibparameter to36000. - Uncomment
lustre_instance_id: lustre-instance. - Comment out or remove unused
filestore_ip_range.
- Change the value for
- Remove the default
Deploy the cluster:
The
./gcluster deploycommand starts 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 35 minutes to complete.
- The second phase deploys the cluster by using that custom image. This process should complete more quickly than 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 Python dependencies for the training script, create a
requirements-fsdp.txtfile with the following content:Specify
train-mixtral.pyas the main training script:To specify the tasks for the jobs to run on your Slurm cluster, create the
train-mixtral.shfile with the following content:
Upload the scripts to your 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${DEPLOYMENT_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 the required frameworks and tools by running the installation script on a compute node:
This command sets up the virtual environment, installs all dependencies, and
downloads the Mixtral model weights into ~/Mixtral-8x7B-v0.1. This process can
take over 30 minutes to complete.
Running this step via srun delegates the installation script from the login node to an allocated compute node, allowing the script to access GPU drivers during compilation.
Start your fine-tuning workload
To start your training workload, do the following:
Submit the job to the Slurm scheduler:
Monitor your workload
To monitor the progress of your fine-tuning job, use the following methods:
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.The job has two main phases:
- Copying the large base model to the local SSD of each compute node.
- The training job, which begins once the copying of the model is complete.
The whole job takes about 60 minutes to run.
You can also 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=YOUR_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%7DAlternately, you can enter the command directly in the terminal:
When you monitor your workload, you can see the following:
GPU 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 the resources
Run the following commands to delete the resources that you created in this tutorial:
To delete your Slurm cluster, go to the
cluster-toolkitdirectory and run the following command:Delete your bucket:
To delete a Packer image, open your web browser, navigate to the following page, search for your specific image, and click Delete.
Delete your project
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
What's next
- Redeploy a Slurm cluster
- Test network performance on a Slurm cluster
- Monitor VMs in a Slurm cluster
- Create a serving endpoint: Once you have your fine-tuned model, you can deploy it to a serving endpoint using Google Kubernetes Engine (GKE) or Vertex AI to make it accessible for inference.
- Learn about recommended storage services for AI and ML workloads