Skip to main content

ZeRO: Deep Memory Optimization for Training Trillion-Parameter Models

In 2020, Microsoft researchers introduced ZeRO (Zero Redundancy Optimizer) via their paper "ZeRO: Memory Optimization Towards Training Trillion Parameter Models" (arXiv:1910.02054). ZeRO is a memory optimization technique that eliminates redundancy in distributed training, enabling efficient scaling to trillion-parameter models. This provides an in-depth technical breakdown of ZeRO's partitioning strategies, memory usage analysis, and integration with DeepSpeed.

1. What is ZeRO?

ZeRO eliminates redundant memory copies of model states across GPUs. Instead of replicating parameters, gradients, and optimizer states across each GPU, ZeRO partitions them across all devices. This results in near-linear memory savings as the number of GPUs increases.

2. Limitations of Traditional Data Parallelism

In standard data-parallel training, every GPU maintains:

  • Model Parameters $\theta$
  • Gradients $\nabla \theta$
  • Optimizer States $O(\theta)$
This causes memory usage to scale linearly with model size, regardless of GPU count.

3. ZeRO’s Three Optimization Stages

 Comparing the per-device memory consumption of model states, with three stages of ZeRO-DP optimizations. Ψ denotes model size (number of parameters), K denotes the memory multiplier of optimizer states, and Nd denotes DP degree. In the example, we assume a model size of Ψ = 7.5B and DP of Nd = 64 with K = 12 based on mixed-precision training with Adam optimizer.

3.1 ZeRO-1: Partitioning Optimizer States

Each GPU stores only a portion of the optimizer states (e.g., Adam's first and second moments), reducing memory from $2 \times |\theta|$ to:

$$ \text{Memory}_{optimizer} = \frac{2 \times |\theta|}{N} $$

3.2 ZeRO-2: Partitioning Gradients

Gradients are no longer reduced to all GPUs. Instead, each GPU owns and updates a subset of $\nabla \theta$, reducing redundant storage.

3.3 ZeRO-3: Partitioning Model Parameters

ZeRO-3 splits the model parameters themselves, allowing a model to scale linearly with the number of GPUs. Forward and backward passes are executed using temporary broadcasts or gathers as needed.

4. Memory Efficiency

The combined memory usage in ZeRO is:

$$ \text{Total Memory}_{ZeRO} = \frac{|\theta| + |\nabla \theta| + |O(\theta)|}{N} $$

Compared to the traditional:

$$ \text{Total Memory}_{DP} = |\theta| + |\nabla \theta| + |O(\theta)| $$

This achieves up to $N$-fold improvement in memory efficiency.

5. DeepSpeed Integration

ZeRO is fully integrated into the DeepSpeed library. DeepSpeed supports ZeRO stages, CPU/NVMe offloading, mixed precision, and efficient activation checkpointing, enabling the training of trillion-scale models with manageable hardware.

6. Experimental Results

ZeRO showed up to 10× larger model capacity and improved throughput on models like BERT-Large and GPT-2. The performance remained stable even as GPU count increased.

7. Limitations and Future Work

While ZeRO increases communication overhead, ZeRO-Infinity (2021) introduced CPU and NVMe offloading to alleviate these costs. Future directions include asynchronous data movement and hierarchical memory systems.

References

  • Rajbhandari et al., "ZeRO: Memory Optimization Towards Training Trillion Parameter Models", arXiv:1910.02054
  • https://www.deepspeed.ai/
  • ZeRO-Infinity: https://arxiv.org/abs/2104.07857

Comments

Popular

Building an MCP Agent with UV, Python & mcp-use

Model Context Protocol (MCP) is an open protocol designed to enable AI agents to interact with external tools and data in a standardized way. MCP is composed of three components: server , client , and host . MCP host The MCP host acts as the interface between the user and the agent   (such as Claude Desktop or IDE) and plays the role of connecting to external tools or data through MCP clients and servers. Previously, Anthropic’s Claude Desktop was introduced as a host, but it required a separate desktop app, license, and API key management, leading to dependency on the Claude ecosystem.   mcp-use is an open-source Python/Node package that connects LangChain LLMs (e.g., GPT-4, Claude, Groq) to MCP servers in just six lines of code, eliminating dependencies and supporting multi-server and multi-model setups. MCP Client The MCP client manages the MCP protocol within the host and is responsible for connecting to MCP servers that provide the necessary functions for the ...

How to Save and Retrieve a Vector Database using LangChain, FAISS, and Gemini Embeddings

How to Save and Retrieve a Vector Database using LangChain, FAISS, and Gemini Embeddings Efficient storage and retrieval of vector databases is foundational for building intelligent retrieval-augmented generation (RAG) systems using large language models (LLMs). In this guide, we’ll walk through a professional-grade Python implementation that utilizes LangChain with FAISS and Google Gemini Embeddings to store document embeddings and retrieve similar information. This setup is highly suitable for advanced machine learning (ML) and deep learning (DL) engineers who work with semantic search and retrieval pipelines. Why Vector Databases Matter in LLM Applications Traditional keyword-based search systems fall short when it comes to understanding semantic meaning. Vector databases store high-dimensional embeddings of text data, allowing for approximate nearest-neighbor (ANN) searches based on semantic similarity. These capabilities are critical in applications like: Question Ans...

Using Gemini API in LangChain: Step-by-Step Tutorial

What is LangChain and Why Use It? LangChain  is an open-source framework that simplifies the use of  Large Language Models (LLMs)  like OpenAI, Gemini (Google), and others by adding structure, tools, and memory to help build real-world applications such as chatbots, assistants, agents, or AI-enhanced software. Why Use LangChain for LLM Projects? Chainable Components : Easily build pipelines combining prompts, LLMs, tools, and memory. Multi-Model Support : Work with Gemini, OpenAI, Anthropic, Hugging Face, etc. Built-in Templates : Manage prompts more effectively. Supports Multi-Turn Chat : Manage complex interactions with memory and roles. Tool and API Integration : Let the model interact with external APIs or functions. Let's Walk Through the Code: Gemini + LangChain I will break the code into  4 main parts , each showcasing different features of LangChain and Gemini API. Part 1: Basic Gemini API Call Using LangChain import os from dotenv import load_dotenv load_dot...