Inferensys

Glossary

ZeRO Optimization

ZeRO (Zero Redundancy Optimizer) is a memory optimization technique that partitions model states across data parallel processes to eliminate memory redundancy, enabling the training of extremely large models.
Data scientist building training data pipeline on laptop, data preprocessing visible, technical workspace.
MEMORY OPTIMIZATION

What is ZeRO Optimization?

ZeRO (Zero Redundancy Optimizer) is a memory optimization technique that partitions model states across data parallel processes to eliminate memory redundancy, enabling the training of extremely large models.

ZeRO Optimization is a family of memory-efficient strategies for distributed data parallel training of large neural networks. It systematically partitions the three primary model states—optimizer states, gradients, and model parameters—across all available GPUs instead of replicating them. This eliminates memory redundancy, allowing the aggregate GPU memory of a cluster to be used as a single, larger pool. The technique is implemented in libraries like Microsoft DeepSpeed and is foundational for training models with hundreds of billions of parameters.

The strategy is implemented in progressive stages: ZeRO-1 partitions optimizer states, ZeRO-2 adds gradient partitioning, and ZeRO-3 partitions all model parameters. During forward and backward passes, parameters are gathered on-demand for each layer and then redistributed, trading a modest communication overhead for massive memory savings. This approach is distinct from model parallelism, as it operates within a data-parallel framework, making it compatible with other techniques like gradient checkpointing and mixed precision training to push the boundaries of feasible model size.

MEMORY OPTIMIZATION

Key Stages of ZeRO (ZeRO-1, ZeRO-2, ZeRO-3)

ZeRO (Zero Redundancy Optimizer) is a family of memory optimization stages developed by Microsoft for training massive models. Each stage progressively partitions more of the model's state across data-parallel processes, eliminating redundancies to enable unprecedented model scale.

01

ZeRO-1: Optimizer State Partitioning

ZeRO-1 eliminates memory redundancy for the optimizer states. In standard data parallelism, each GPU holds a full copy of the optimizer states (e.g., momentum and variance for Adam), which can consume ~12 bytes per parameter.

  • Mechanism: Partitions the optimizer states across the data-parallel processes. Each process is responsible for updating only its assigned partition.
  • Memory Reduction: Reduces optimizer state memory by a factor equal to the data-parallel degree (e.g., 8x reduction with 8 GPUs).
  • Communication: Requires a single all-gather operation before the weight update to collect the relevant optimizer partitions, but no extra communication during the backward pass.
  • Use Case: The first step, enabling larger models than standard data parallelism but not the full memory savings of later stages.
02

ZeRO-2: Gradient Partitioning

ZeRO-2 builds on ZeRO-1 by also partitioning the gradients. In standard training, each GPU stores the full gradients for all parameters after the backward pass.

  • Mechanism: Gradients are partitioned across processes immediately after they are computed during the backward pass. Each process only stores and updates the gradients for its assigned parameter partition.
  • Memory Reduction: Reduces gradient memory by a factor equal to the data-parallel degree, on top of the optimizer state savings from ZeRO-1.
  • Communication: Uses a reduce-scatter operation during the backward pass to aggregate gradients across partitions, ensuring correct averaging. This replaces the standard all-reduce used in data parallelism.
  • Impact: Often provides the most significant memory savings relative to its communication overhead, making it a highly efficient stage for scaling.
03

ZeRO-3: Parameter Partitioning

ZeRO-3 is the most comprehensive stage, partitioning the model parameters themselves across GPUs. This eliminates all major memory redundancies.

  • Mechanism: The full model parameters are partitioned across the data-parallel processes. Each GPU only stores the parameters for its assigned partition.
  • Memory Reduction: Reduces model parameter memory by a factor equal to the data-parallel degree. Combined with ZeRO-1 and ZeRO-2, it enables linear memory scaling with the number of GPUs.
  • Communication Overhead: Requires sophisticated communication scheduling:
    • Forward Pass: Parameters needed for a given layer are all-gathered just before computation and discarded afterward.
    • Backward Pass: Parameters are again gathered for gradient calculation, followed by a reduce-scatter for the gradients.
  • Use Case: Essential for training models with trillions of parameters, as it allows the aggregate GPU memory of a cluster to define the trainable model size.
05

Communication Patterns & Overhead

Each ZeRO stage introduces distinct communication operations that replace those of standard data parallelism.

  • Baseline (DP): Uses a single all-reduce operation after backward pass to average gradients.
  • ZeRO-1: Adds an all-gather before the optimizer step (for optimizer states).
  • ZeRO-2: Replaces the all-reduce with a reduce-scatter during backward pass (for gradients).
  • ZeRO-3: Adds multiple all-gather operations (before each layer in forward/backward) and a reduce-scatter (backward).
  • Overhead Management: The communication volume remains similar to baseline data parallelism, but the number of operations increases. This overhead is managed in DeepSpeed via optimized kernels and overlapping communication with computation.
MEMORY OPTIMIZATION COMPARISON

ZeRO vs. Traditional Data Parallelism

A feature-by-feature comparison of ZeRO's memory optimization stages against the standard data parallelism approach used in distributed deep learning.

Feature / MetricTraditional Data Parallelism (Baseline)ZeRO Stage 1 (Optimizer State Partitioning)ZeRO Stage 2 (Gradient Partitioning)ZeRO Stage 3 (Parameter Partitioning)

Memory Redundancy

High

Moderate

Low

Minimal

Model State Memory per GPU

Full copy of all model states (parameters, gradients, optimizer states)

Full parameters & gradients, partitioned optimizer states

Full parameters, partitioned gradients & optimizer states

Partitioned parameters, gradients, and optimizer states

Communication Volume

All-reduce of gradients only

All-reduce of gradients only

All-reduce of gradients only

All-gather/broadcast for parameters as needed

Communication Overhead

Low

Low

Low

Moderate to High (dependent on model)

Maximum Model Size Support

Limited by single-GPU memory for all states

~2x larger than baseline

~4x larger than baseline

Proportional to total GPU count (can train models with trillions of parameters)

Implementation Complexity

Low (standard in most frameworks)

Moderate

Moderate

High

Primary Use Case

Training models that fit entirely on a single GPU with data parallelism

Moderate memory savings for large optimizer states (e.g., Adam)

Significant memory savings for very large models

Training extremely large models that exceed aggregate memory of all GPUs

Gradient Synchronization

Requires Model Parameter Offloading

MEMORY EFFICIENCY

Primary Use Cases for ZeRO Optimization

ZeRO (Zero Redundancy Optimizer) is a memory optimization technique that partitions model states across data parallel processes. Its primary use cases address the fundamental memory bottlenecks encountered when training and fine-tuning extremely large models.

02

Full Fine-Tuning of Large Pre-trained Models

When adapting a large pre-trained model (e.g., Llama 2 70B) to a new domain via full fine-tuning, all model parameters are updated. This process requires storing the model weights, gradients, optimizer states (like momentum and variance for Adam), and activations—a massive memory footprint. ZeRO stages 2 and 3 enable this by distributing these components. For example, ZeRO-2 partitions gradients and optimizer states, while ZeRO-3 also partitions the model parameters themselves. This makes full fine-tuning feasible on a modest cluster of GPUs, allowing for deeper specialization than parameter-efficient fine-tuning (PEFT) methods like LoRA.

03

Enabling Longer Sequence Lengths

Training on long sequences (e.g., 32k+ tokens) is critical for document understanding, code generation, and long-context reasoning. The memory for activations (intermediate layer outputs) scales linearly with sequence length and batch size, quickly becoming the dominant memory consumer. While ZeRO primarily targets optimizer and parameter memory, its memory savings free up GPU resources to store these larger activations. Furthermore, ZeRO can be combined with activation checkpointing (or gradient checkpointing), which recomputes activations during the backward pass instead of storing them, enabling training with sequences that would be otherwise impossible.

04

Facilitating Large Batch Sizes

Large batch sizes are often necessary for stable training, especially in distributed settings, and to maximize GPU utilization. However, per-GPU memory limits batch size. By drastically reducing the memory footprint of the model states, ZeRO allows for significantly larger effective batch sizes per GPU. Users can either increase the micro-batch size per GPU for better hardware efficiency or scale out to more GPUs with data parallelism while maintaining a manageable memory profile. This is crucial for data-intensive tasks like contrastive learning or when using massive datasets.

05

Supporting Multi-Task and Instruction Tuning

Instruction tuning on large, diverse datasets (e.g., mixtures of Alpaca, ShareGPT, and proprietary data) involves processing many distinct task formats. Efficiently handling this requires flexible data loading and often larger model capacities. ZeRO's memory efficiency allows practitioners to use larger base models for instruction tuning, leading to more capable and generalist models. It also supports the full fine-tuning phase often used after initial supervised fine-tuning (SFT) in alignment pipelines like RLHF, where both the policy model and the reward model can be large.

ZERO OPTIMIZATION

Frequently Asked Questions

ZeRO (Zero Redundancy Optimizer) is a suite of memory optimization techniques that partition model states across data parallel processes to eliminate memory redundancy, enabling the training of models with trillions of parameters.

ZeRO (Zero Redundancy Optimizer) is a memory optimization technique for distributed training that partitions the three primary model states—optimizer states, gradients, and model parameters—across data parallel processes to eliminate memory redundancy. It works by sharding these states so that each GPU in a data parallel group stores only a slice, using collective communication operations to gather the full states only when needed for computation. This partitioning enables the training of models significantly larger than the memory of any single GPU.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.