
HF
Hugging Face Blog
· 1 min read
AI LabsHugging Face Blog
From PyTorch DDP to Accelerate to Trainer, mastery of distributed training with ease
General Overview
This tutorial assumes you have a basic understanding of PyTorch and how to train a simple model. It will showcase training on multiple GPUs through a process called Distributed Data Parallelism (DDP) through three different levels of increasing abstraction:
- Native PyTorch DDP through the
pytorch.distributedmodule - Utilizing 🤗 Accelerate's light wrapper around
pytorch.distributedthat also helps ensure the code can be run on a single GPU and TPUs with zero code changes and miminimal code changes to the original code - Utilizing 🤗 Transformer's high-level Trainer API which abstracts all the boilerplate code and supports various devices and distributed scenarios
What is "Distributed" training and why does it matter?
Take some very basic PyTorch training code below, which sets up and trains a model on MNIST based on the official MNIST example
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
class BasicNet(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1, 32, 3, 1)
self.conv2 = nn.Conv2d(32, 64, 3, 1)
self.dropout1 = nn.Dropout(0.25)
self.dropout2 = nn.Dropout(0.5)
self.fc1 = nn.Linear(9216, 128)
self.fc2 = nn.Linear(128, 10)
self.act = F.relu
def forward(self, x):
x = self.act(self.conv1(x))
x = self.act(self.conv2(x))
x = F.max_pool2d(x, 2)
x = self.dropout1(x)
x = torch.flatten(x, 1)
x = self.act(self.fc1(x))
x = self.dropout2(x)
x = self.fc2(x)
output = F.log_softmax(x, dim=1)
return output
We define the training device (cuda):
device = "cuda"
Build some PyTorch DataLoaders:
Move the model to the CUDA device:
model = BasicNet().to(device)
Build a PyTorch optimizer:
optimizer = optim.AdamW(model.parameters(), lr=1e-3)
PyTorch Distributed Data Parallelism
🤗 Accelerate
Using the notebook_launcher
Or:
Original source
This story was published by Hugging Face Blog. SyncAI.news shows a preview; the complete article is on the publisher's site.
Read the full story on huggingface.co


