This tutorial shows you how to train the ResNet-50 model on a Cloud TPU device with PyTorch. You can apply the same pattern to other TPU-optimized image classification models that use PyTorch and the ImageNet dataset.
The model in this tutorial is based on Deep Residual Learning for Image Recognition, which first introduces the residual network (ResNet) architecture. The tutorial uses the 50-layer variant, ResNet-50, and demonstrates training the model using PyTorch/XLA.
Objectives
- Prepare the dataset.
- Run the training job.
- Verify the output results.
Costs
In this document, you use the following billable components of Google Cloud:
- Compute Engine
- Cloud TPU
To generate a cost estimate based on your projected usage,
use the pricing calculator.
Before you begin
Before starting this tutorial, check that your Google Cloud project is correctly set up.
- 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.
-
In the Google Cloud console, on the project selector page, select or create 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.
-
Verify that billing is enabled for your Google Cloud project.
-
In the Google Cloud console, on the project selector page, select or create 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.
-
Verify that billing is enabled for your Google Cloud project.
This walkthrough uses billable components of Google Cloud. Check the Cloud TPU pricing page to estimate your costs. Be sure to clean up resources you created when you've finished with them to avoid unnecessary charges.
Create a TPU VM
Open a Cloud Shell or a terminal window.
Define environment variables for Google Cloud CLI command parameters:
export PROJECT_ID=your-project-id export TPU_NAME=your-tpu-name export ZONE=us-central1-b export ACCELERATOR_TYPE=v6e-8 export RUNTIME_VERSION=v2-alpha-tpuv6e
Environment variable descriptions
PROJECT_ID: Your Google Cloud project ID. Use an existing project or create a new one.TPU_NAME: The name of the TPU.ZONE: The zone in which to create the TPU VM. For more information about supported zones, see TPU regions and zones.ACCELERATOR_TYPE: The accelerator type specifies the version and size of the Cloud TPU you want to create. For more information about supported accelerator types for each TPU version, see TPU versions.RUNTIME_VERSION: The Cloud TPU software version.
Create a TPU VM
gcloud compute tpus tpu-vm create $TPU_NAME \ --accelerator-type=$ACCELERATOR_TYPE \ --version=$RUNTIME_VERSION \ --zone=$ZONE \ --project=$PROJECT_IDConnect to your TPU VM using SSH:
gcloud compute tpus tpu-vm ssh $TPU_NAME --zone=$ZONE
Install PyTorch/XLA on your TPU VM
Create a file named
requirements.txtand add the following content:--find-links https://storage.googleapis.com/libtpu-releases/index.html --find-links https://storage.googleapis.com/libtpu-wheels/index.html torch~=2.6.0 torch_xla[tpu]~=2.6.0 torchvision ray[default]==2.40.0Install the dependencies:
pip install -r requirements.txtClone the PyTorch/XLA GitHub repository
git clone --depth=1 https://github.com/pytorch/xla.gitRun the training script with fake data
PJRT_DEVICE=TPU python3 xla/test/test_train_mp_imagenet.py \ --fake_data \ --batch_size=256 \ --num_epochs=1
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.
Disconnect from the TPU VM:
exitDelete your TPU VM.
gcloud compute tpus tpu-vm delete $TPU_NAME \ --zone=$ZONE
What's next