# Accelerate

> Run the same PyTorch training code on CPU, multi-GPU, and TPU

- Category: Training & Fine-Tuning / Efficient Training
- Language: Python
- Licence: Apache-2.0
- Repository: https://github.com/huggingface/accelerate
- Website: https://huggingface.co/docs/accelerate
- Canonical page: https://ai-tldr.dev/tools/accelerate/

## In short

Accelerate is a Hugging Face library that lets you run a standard PyTorch training loop on any hardware setup without rewriting it. You keep your own training code and add only a few lines, and the library handles device placement, distributed processes, and mixed precision behind the scenes. It sits in the Efficient Training category of the Training & Fine-Tuning tools and is open-source, written in Python and licensed Apache-2.0.

## Overview

Accelerate is a Hugging Face library that lets you run a standard PyTorch training loop on any hardware setup without rewriting it. You keep your own training code and add only a few lines, and the library handles device placement, distributed processes, and mixed precision behind the scenes.

It is aimed at PyTorch users who want to write their own training loops but do not want to maintain the boilerplate needed for multi-GPU, TPU, or fp16/bf16/fp8 runs. The same script can run on a single CPU for debugging and then on a multi-GPU cluster without changes.

Within efficient training, Accelerate sits between plain PyTorch and full training frameworks. It wraps your model, optimizer, and dataloader through a single Accelerator object, and integrates backends like FSDP and DeepSpeed that you configure rather than hand-code.

## What it does

- Wraps your existing PyTorch model, optimizer, and dataloader with a single Accelerator.prepare() call
- Runs the same script on single CPU, single GPU, multi-GPU, and TPU without code changes
- Supports mixed precision training in fp8, fp16, and bf16
- Optional accelerate config and accelerate launch CLI to set up and start distributed runs
- Integrates DeepSpeed, including a DeepSpeedPlugin to tweak settings from Python
- Supports multi-CPU runs via MPI (Open MPI, Intel MPI, or MVAPICH)

## Getting started

Install the package, add a few lines to your existing PyTorch loop, then optionally configure and launch a distributed run.

1. Install Accelerate

   Install from PyPI. It is tested on Python 3.8+ and PyTorch 1.10.0+.

```bash
pip install accelerate
```

2. Adapt your training loop

   Create an Accelerator, use its device, prepare your model, optimizer, and dataloader, and call accelerator.backward(loss) instead of loss.backward().

```python
import torch
import torch.nn.functional as F
from datasets import load_dataset
from accelerate import Accelerator

accelerator = Accelerator()
device = accelerator.device

model = torch.nn.Transformer().to(device)
optimizer = torch.optim.Adam(model.parameters())

dataset = load_dataset('my_dataset')
data = torch.utils.data.DataLoader(dataset, shuffle=True)

model, optimizer, data = accelerator.prepare(model, optimizer, data)

model.train()
for epoch in range(10):
    for source, targets in data:
        source = source.to(device)
        targets = targets.to(device)
        optimizer.zero_grad()
        output = model(source)
        loss = F.cross_entropy(output, targets)
        accelerator.backward(loss)
        optimizer.step()
```

3. Configure your environment

   Run the optional CLI tool and answer the questions to generate a config file with your default options.

```bash
accelerate config
```

4. Launch the script

   Start your training script with accelerate launch, which applies your config automatically. This step is optional; plain python my_script.py also works.

```bash
accelerate launch my_script.py --args_to_my_script
```

## When to use it

- Take a single-GPU PyTorch training script to multi-GPU without rewriting the training loop
- Debug a model on a local CPU and then run the same code on a TPU or GPU cluster
- Enable mixed precision (fp16/bf16/fp8) training to cut memory use and speed up runs
- Train large models with DeepSpeed or FSDP by configuring backends instead of hand-coding them
