---
title: "How 🤗 Accelerate runs very large models thanks to PyTorch"
description: "Accelerate loads and runs models too large for a single GPU or for CPU RAM by changing PyTorch's usual load path."
url: "https://swe.quest/articles/how-accelerate-runs-very-large-models-thanks-to-pytorch-00771d3f91bebb0b"
canonical: "https://huggingface.co/blog/accelerate-large-models"
source: "Hugging Face"
published: "2022-09-27"
difficulty: "medium"
kind: "Deep dive"
reading_minutes: 11
tags: ["inference","memory-management","python","llms"]
---

# How 🤗 Accelerate runs very large models thanks to PyTorch

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2022-09-27, 11 min read, Deep dive. Original: [https://huggingface.co/blog/accelerate-large-models](https://huggingface.co/blog/accelerate-large-models)

## Summary

Accelerate loads and runs models too large for a single GPU or for CPU RAM by changing PyTorch's usual load path. It creates an empty model on the meta device, computes a device map that fills GPUs first, then CPU RAM, then disk, and loads sharded checkpoints one shard at a time so peak RAM is bounded by the largest shard (7.19GB for BLOOM instead of 352GB). Module hooks then move inputs and offloaded weights onto the right device at forward time. Practical details include no\_split\_module\_classes for residual blocks, device\_map strategies (auto, balanced\_low\_0, sequential), dtype effects on placement, and offload\_folder/offload\_state\_dict for fitting OPT-13B on Colab.

Written by swe.quest, not by the author.

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

## Topics

- [Inference & serving](https://swe.quest/tags/inference.md) (AI & ML)
- [Memory management](https://swe.quest/tags/memory-management.md) (Compilers & runtimes)
- [Python](https://swe.quest/tags/python.md) (Languages)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)

## Prerequisites

- PyTorch modules and state\_dict
- GPU memory basics ([easy posts on Hardware](https://swe.quest/tags/hardware.md?difficulty=easy))
- Transformer model architecture ([easy posts on System design](https://swe.quest/tags/system-design.md?difficulty=easy))

## Keywords

Accelerate, PyTorch, meta device, Transformers, BLOOM, OPT, sharded checkpoints, device\_map

## Related posts

- [Fine-tuning Llama 2 70B using PyTorch FSDP](https://swe.quest/articles/fine-tuning-llama-2-70b-using-pytorch-fsdp-0e8dde34b4ad3fd0.md): Hugging Face · 2023-09-13 · Hard · 10 min read · Model training, LLMs, Hardware, Python
  Full fine-tuning of Llama 2 70B across two nodes of 8×A100 80GB GPUs with PyTorch FSDP, Hugging Face Accelerate, Transformers and TRL, addressing three obstacles.
- [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.
- [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.
- [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.

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)
