# DeepSpeed

> Train very large models across many GPUs with ZeRO memory partitioning and offloading

- Category: Training & Fine-Tuning / Efficient Training
- Language: Python
- Licence: Apache-2.0
- Repository: https://github.com/deepspeedai/DeepSpeed
- Website: https://www.deepspeed.ai
- Canonical page: https://ai-tldr.dev/tools/deepspeed/

## In short

DeepSpeed is a deep learning optimization library for PyTorch. Its core idea, ZeRO (Zero Redundancy Optimizer), splits model states, gradients, and optimizer state across the GPUs in a job instead of copying them to every device. 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

DeepSpeed is a deep learning optimization library for PyTorch. Its core idea, ZeRO (Zero Redundancy Optimizer), splits model states, gradients, and optimizer state across the GPUs in a job instead of copying them to every device. With ZeRO-Infinity and offloading, it can also move data to CPU memory or NVMe, so you can train models that would not fit in GPU memory alone.

It is built for teams training large models that do not fit on a single GPU, or that train too slowly with plain data parallelism. DeepSpeed has been used to train large language models such as BLOOM (176B) and Megatron-Turing NLG (530B). You wrap a normal PyTorch model and run it through DeepSpeed's engine and launcher.

In the efficient-training space, DeepSpeed sits alongside the model code rather than replacing it. It works with plain PyTorch and integrates with higher-level tools like Hugging Face Accelerate and PyTorch Lightning, so you can adopt ZeRO and offloading without rewriting your training loop from scratch.

## What it does

- ZeRO partitions optimizer state, gradients, and parameters across GPUs to cut per-device memory use
- ZeRO-Infinity and offloading move data to CPU or NVMe so larger models fit in available memory
- 3D parallelism combines data, tensor, and pipeline parallelism for large-scale training
- A JSON config file (ds_config.json) controls optimizer, precision, and ZeRO settings without code changes
- The deepspeed launcher handles single-node and multi-node distributed runs via a hostfile
- Integrates with Hugging Face Accelerate and PyTorch Lightning

## Getting started

Install the package, wrap your PyTorch model with deepspeed.initialize, and launch training with the deepspeed CLI and a config file.

1. Install DeepSpeed

   Install from PyPI into an environment that already has a compatible PyTorch build.

```bash
pip install deepspeed
```

2. Initialize the engine

   Wrap your model and optimizer parameters with deepspeed.initialize to get a training engine.

```python
model_engine, optimizer, _, _ = deepspeed.initialize(args=cmd_args,
                                                     model=model,
                                                     model_parameters=params)
```

3. Run the training loop

   Use the returned engine for the forward pass, backward pass, and weight update.

```python
for step, batch in enumerate(data_loader):
    # forward() method
    loss = model_engine(batch)

    # runs backpropagation
    model_engine.backward(loss)

    # weight update
    model_engine.step()
```

4. Launch with the deepspeed CLI

   Start training with the launcher, passing your entry script and a DeepSpeed config JSON.

```bash
deepspeed --hostfile=myhostfile <client_entry.py> <client args> \
  --deepspeed --deepspeed_config ds_config.json
```

## When to use it

- Training a large language model that does not fit on a single GPU by partitioning its state across many GPUs with ZeRO
- Fitting a bigger model into limited GPU memory by offloading optimizer state and parameters to CPU or NVMe
- Scaling an existing PyTorch training job to multiple nodes using the deepspeed launcher and a hostfile
- Adding ZeRO and offloading to a Hugging Face Accelerate or PyTorch Lightning workflow without rewriting the training loop
