How to beginner to intermediate · 3 min read

How to use Accelerate library for training

Quick answer
Use the Accelerate library from Hugging Face to simplify distributed and mixed precision training by wrapping your PyTorch training loop with Accelerator. Initialize Accelerator, prepare your model, optimizer, and dataloaders with accelerator.prepare(), then run your training loop as usual.

PREREQUISITES

  • Python 3.8+
  • pip install accelerate torch transformers
  • Basic knowledge of PyTorch training loops

Setup

Install the accelerate library along with torch and optionally transformers for model loading. Set up your environment variables if you want to configure distributed training or mixed precision.

bash
pip install accelerate torch transformers

Step by step

This example shows a minimal training loop using Accelerate to train a simple model on dummy data with mixed precision and device placement handled automatically.

python
import torch
from torch import nn, optim
from torch.utils.data import DataLoader, TensorDataset
from accelerate import Accelerator

# Initialize accelerator
accelerator = Accelerator()

# Dummy dataset
x = torch.randn(1000, 10)
y = torch.randint(0, 2, (1000,))
dataset = TensorDataset(x, y)
dataloader = DataLoader(dataset, batch_size=32, shuffle=True)

# Simple model
model = nn.Sequential(nn.Linear(10, 50), nn.ReLU(), nn.Linear(50, 2))
optimizer = optim.Adam(model.parameters(), lr=1e-3)
criterion = nn.CrossEntropyLoss()

# Prepare everything with accelerator
model, optimizer, dataloader = accelerator.prepare(model, optimizer, dataloader)

# Training loop
model.train()
for epoch in range(3):
    total_loss = 0
    for batch in dataloader:
        inputs, labels = batch
        optimizer.zero_grad()
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        accelerator.backward(loss)
        optimizer.step()
        total_loss += loss.item()
    print(f"Epoch {epoch+1}, Loss: {total_loss/len(dataloader):.4f}")
output
Epoch 1, Loss: 0.6932
Epoch 2, Loss: 0.6905
Epoch 3, Loss: 0.6878

Common variations

You can use Accelerate with different models from transformers, enable mixed precision by passing fp16=True to Accelerator(), or run distributed training on multiple GPUs or machines by configuring accelerate config.

python
from accelerate import Accelerator

# Enable mixed precision training
accelerator = Accelerator(fp16=True)

# For distributed training, run in terminal:
# accelerate config
# Then launch your script with:
# accelerate launch train.py

Troubleshooting

If you see errors related to device placement or multiple GPUs, ensure you run your script with accelerate launch after configuring with accelerate config. For memory issues, try enabling mixed precision or reducing batch size.

Key Takeaways

  • Use Accelerator to handle device placement, mixed precision, and distributed training seamlessly.
  • Always prepare your model, optimizer, and dataloaders with accelerator.prepare() before training.
  • Configure distributed settings with accelerate config and launch scripts with accelerate launch.
  • Enable mixed precision by passing fp16=True to Accelerator() for faster training and lower memory usage.
Verified 2026-04
Verify ↗