---
title: "From PyTorch DDP to Accelerate to Trainer, mastery of distributed training with ease"
description: "Converting a basic PyTorch MNIST training script to multi-GPU Distributed Data Parallel training is shown at three levels of abstraction."
url: "https://swe.quest/articles/from-pytorch-ddp-to-accelerate-to-trainer-mastery-of-distributed-training-with-8ad821e28b1383c4"
canonical: "https://huggingface.co/blog/pytorch-ddp-accelerate-transformers"
source: "Hugging Face"
published: "2022-10-21"
difficulty: "medium"
kind: "How-to"
reading_minutes: 9
tags: ["model-training","python","machine-learning"]
---

# From PyTorch DDP to Accelerate to Trainer, mastery of distributed training with ease

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2022-10-21, 9 min read, How-to. Original: [https://huggingface.co/blog/pytorch-ddp-accelerate-transformers](https://huggingface.co/blog/pytorch-ddp-accelerate-transformers)

## Summary

Converting a basic PyTorch MNIST training script to multi-GPU Distributed Data Parallel training is shown at three levels of abstraction. The first is native torch.distributed with process-group setup, a DDP wrapper and the torchrun launcher. The second is Hugging Face Accelerate's Accelerator.prepare, which keeps the loop nearly unchanged, runs on single GPU, multi-GPU or TPU, and shards batches through custom samplers so only one copy of the dataset sits in memory. The third is the Transformers Trainer, which needs only a custom collate function and compute\_loss override, and any of these can be launched from Jupyter via notebook\_launcher.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Model training](https://swe.quest/tags/model-training.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)
- [Machine learning](https://swe.quest/tags/machine-learning.md) (AI & ML)

## Prerequisites

- PyTorch training loops ([easy posts on Model training](https://swe.quest/tags/model-training.md?difficulty=easy))
- GPU training basics ([easy posts on Hardware](https://swe.quest/tags/hardware.md?difficulty=easy))

## Keywords

PyTorch, DistributedDataParallel, Hugging Face Accelerate, Transformers Trainer, torchrun, notebook\_launcher

## Related posts

- [Introducing 🤗 Accelerate](https://swe.quest/articles/introducing-accelerate-aaf88192c9f162b5.md): Hugging Face · 2021-04-16 · Medium · 7 min read · Model training, Python, Machine learning
  🤗 Accelerate lets PyTorch users keep their own training loops while running the same script on CPU, single or multi-GPU, multi-node, TPU, and with mixed precision, by adding about five lines: an Accelerator object, accelerator.prepare() for model/optimizer/dataloader, and accelerator.backward(). prepare() wraps the model in the right container such as DistributedDataParallel, wraps the optimizer for mixed precision, and shards any sampler's indices per process using synchronized RNGs instead of requiring DistributedSampler.
- [Accelerate 1.0.0](https://swe.quest/articles/accelerate-1-0-0-3876b57c9168d12c.md): Hugging Face · 2024-09-13 · Medium · 5 min read · Model training, Python, Machine learning
  Hugging Face has published the first release candidates for Accelerate 1.0.0.
- [Accelerate Large Model Training using PyTorch Fully Sharded Data Parallel](https://swe.quest/articles/accelerate-large-model-training-using-pytorch-fully-sharded-data-parallel-513a65557fbde722.md): Hugging Face · 2022-05-02 · Hard · 12 min read · Model training, Machine learning, Python, Performance engineering
  Hugging Face Accelerate exposes PyTorch FullyShardedDataParallel (FSDP), which shards parameters, gradients and optimizer states across GPUs (ZeRO stage 2/3) with optional CPU offload, and does so without changes to training code.
- [Accelerating PyTorch distributed fine-tuning with Intel technologies](https://swe.quest/articles/accelerating-pytorch-distributed-fine-tuning-with-intel-technologies-6164e2f4281509af.md): Hugging Face · 2021-11-19 · Medium · 9 min read · Model training, Hardware, Performance engineering, Python, Cloud platforms
  Fine-tuning transformer models can be distributed across a cluster of Intel Ice Lake Xeon CPU servers using the Intel Extension for PyTorch and the oneCCL communication backend, making CPU clusters a cost-effective option for shorter transfer-learning jobs.
- [Accelerate Large Model Training using DeepSpeed](https://swe.quest/articles/accelerate-large-model-training-using-deepspeed-d18dbe7623c821c0.md): Hugging Face · 2022-06-28 · Medium · 8 min read · Model training, Python, Performance engineering
  Hugging Face Accelerate can enable DeepSpeed ZeRO data parallelism, which shards optimizer states, gradients and parameters across GPUs and optionally offloads them to CPU, with no or minimal code changes.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
