← Back to AI Infrastructure Mind Map
🖥️ AI InfrastructureID: zero-memory-offload

ZeRO-1/2/3 & Offload

ZeRO-1/2/3 显存切分
🎯Core Definition
ZeRO (Zero Redundancy Optimizer, DeepSpeed) fits large-model training into limited GPU memory by eliminating redundant copies of state. With Ψ\Psi total parameters (FP16, 2 bytes/element) trained in mixed precision with Adam, training must simultaneously hold the FP16 params 2Ψ2\Psi, the FP16 gradients 2Ψ2\Psi produced by backprop, and the FP32 optimizer states — a master weight copy 4Ψ4\Psi + first moment 4Ψ4\Psi + second moment 4Ψ4\Psi = 12Ψ12\Psi — giving a static total of 16Ψ=2Ψ+2Ψ+12Ψ16\Psi = 2\Psi + 2\Psi + 12\Psi. For a 70B model: 16×7016 \times 70 GB =1120= 1120 GB, which one H100 (80 GB) cannot hold, and naive DDP replicating the full state NdN_d times across NdN_d ranks makes it worse. ZeRO shards in three stages (per-rank memory with NdN_d ranks): ① ZeRO-1 shards only optimizer states: 2Ψ+2Ψ+12ΨNd2\Psi + 2\Psi + \tfrac{12\Psi}{N_d}; ② ZeRO-2 also shards gradients: 2Ψ+2Ψ+12ΨNd2\Psi + \tfrac{2\Psi + 12\Psi}{N_d}; ③ ZeRO-3 shards parameters as well: 16ΨNd\tfrac{16\Psi}{N_d} — 70B needs Nd=16N_d = 16 H100s (1120/16=701120/16 = 70 GB each). Still not enough, offload: move optimizer states and gradients to CPU RAM while the GPU keeps only the local FP16 param shard 2Ψ/Nd=140/8=17.52\Psi/N_d = 140/8 = 17.5 GB (8 GPUs), paging to NVMe on demand (ZeRO-Infinity, scaling to trillion-parameter models).
💡Use Cases
the canonical memory-budget calculation for pre-training and fine-tuning (7B–175B); favorite interview follow-ups are computing 70B training memory by hand, what ZeRO-2 saves over ZeRO-1, and what offload costs.
⚡Key Problems Solved
naive DDP stores NdN_d full copies of state so per-rank memory never shrinks as GPUs scale; ZeRO cuts the redundancy from O(Nd)O(N_d) to O(1)O(1) — per-rank memory drops by an order of magnitude (70B: 1120 GB → 70 GB/rank) while communication stays close to DDP (~1.5× for ZeRO-3: ~3Φ vs DDP's 2Φ); combined with CPU/NVMe offload, even 175B-class models train on a single node.
🎯Core Interview Exam Points
Fundamentals · Production Scenarios · Deep Dive
Core Def
Explain the core definition, underlying mechanism/math, and key principles of ZeRO-1/2/3 & Offload.
Use Cases
In production engineering, what are the primary use cases for ZeRO-1/2/3 & Offload and what core pain points does it solve?
🔥 High-Frequency Deep-Dive Questions
1
By hand, compute the per-rank static memory for training a 70B model (Adam + FP16/FP32 mixed precision): write 16Ψ=2Ψ+2Ψ+12Ψ16\Psi = 2\Psi + 2\Psi + 12\Psi, name each term (FP16 params / FP16 grads / FP32 master weights + first + second moments), arrive at 1120 GB, and explain why naive DDP still costs 1120 GB per rank (N redundant copies).
2
Write the per-rank memory formulas for ZeRO-1/2/3: 2Ψ+2Ψ+12ΨNd2\Psi + 2\Psi + \tfrac{12\Psi}{N_d}, 2Ψ+2Ψ+12ΨNd2\Psi + \tfrac{2\Psi + 12\Psi}{N_d}, 16ΨNd\tfrac{16\Psi}{N_d}; state what each stage shards and what remains replicated.
3
What does ZeRO-2 save over ZeRO-1, and by how much (gradients drop from 2Ψ2\Psi to 2Ψ/Nd2\Psi/N_d per rank)? Why can gradients be sharded — Reduce-Scatter right after backprop instead of a full All-Reduce.
4
Why is ZeRO-3's communication ~1.5× DDP (forward All-Gather Φ + backward Reduce-Scatter Φ + a second All-Gather for recompute Φ ≈ 3Φ, vs DDP's 2Φ), and is the memory win 16Ψ→16Ψ/Nd16\Psi \to 16\Psi/N_d worth it?
5
What does ZeRO-Offload move to the CPU and at what cost: optimizer states and the Adam step run on the CPU while the GPU keeps only the layer's param shard 2Ψ/Nd2\Psi/N_d; memory drops dramatically at the price of PCIe-bound, slower steps; how does ZeRO-Infinity extend to NVMe?
Updated 2026-08-12
🎯
Verify Mastery: Practice Exam Questions for "ZeRO-1/2/3 & Offload"
Take pitfall multiple-choice questions with derivation options; mistakes are automatically logged.
🚀 Start Card Practice ➔
← Previous CardMegatron 3D & Pipeline BubbleNext Card →Activation Memory Estimation

🔗 More AI Infrastructure Knowledge Cards

引用出处: TalentMe 450+ 工业级 AI 架构考点知识库 (Tech Vault) · 遵循 CC-BY-4.0 知识共享协议
URL: https://talentme.airsota.com/resources/tech/roadmap/mindmap/AI_Infra/card/zero-memory-offload
中文速览与面试要点

ZeRO-1/2/3 显存切分 (ZeRO-1/2/3 & Offload)

定义: ZeRO(Zero Redundancy Optimizer, DeepSpeed)通过切掉“冗余保存”把大模型训练塞进有限的 GPU 显存。设参数总数为 (FP16 每元素 2 字节),用 Adam 混合精度训练需同时保存 FP16 参数 2 、反向产生的 FP16 梯度 2 ,以及 FP32 优化器状态——主权重副本 4 + 一阶动量 4 + 二阶动量 4 共 12 ,静态显存合计 16 = 2 + 2 + 12 。70B 模型: 16 70 GB = 1120 GB,单张 H100(80 GB)装不下,朴素 DDP 在 N_d 卡上复制 N_d 份完整状态、更装不下。ZeRO 分三档切分(设 N_d 卡,每卡显存): ① ZeRO-1 只切优化器状态: 2 + 2 + 12 N_d;② ZeRO-2 再切梯度: 2 + 2 + 12 N_d;③ ZeRO-3 连参数也切: 16 N_d——70B 需 N_d = 16 张 H100(每卡 1120/16 = 70 GB)才放得下。还不够时 Offload: 优化器状态与梯度移到 CPU 内存,GPU 只保留本层 FP16 参数分片 2 /N_d = 140/8 = 17.5 GB(8 卡),必要时按页换到 NVMe(ZeRO-Infinity,可扩展至万亿参数)。 使用场景: 大模型预训练/微调(7B–175B)显存预算的面试计算题;高频追问“手算 70B 训练显存”“ZeRO-2 相比 ZeRO-1 省什么”“Offload 换来什么代价”。 解决的问题: 朴素 DDP 在 N_d 卡上冗余保存 N_d 份完整状态,显存不随卡数下降;ZeRO 把冗余从 O(N_d) 倍压到 O(1) 倍,每卡显存降一个数量级(70B: 1120 GB → 每卡 70 GB),通信量只比 DDP 多约 1.5 倍(ZeRO-3 约 3Φ vs DDP 的 2Φ),配合 CPU/NVMe Offload 甚至能在单节点训练 175B 级模型。

核心要点

  • 手算 70B 模型(Adam + FP16/FP32 混合精度)训练的单卡静态显存: 写出 16 = 2 + 2 + 12 ,逐项说明(FP16 参数 / FP16 梯度 / FP32 主权重 + 一阶 + 二阶动量),得出 1120 GB,并解释为何朴素 DDP 每卡仍是 1120 GB(复制 N 份冗余)。
  • 写出 ZeRO-1/2/3 三档的每卡显存公式 2 + 2 + 12 N_d、2 + 2 + 12 N_d、 16 N_d,并说明每一档切了什么、还剩什么未切。
  • ZeRO-2 相比 ZeRO-1 省了什么?省多少(梯度从每卡 2 降到 2 /N_d)?梯度为什么能被切——反向后立即 Reduce-Scatter 聚合分片,而不是全量 All-Reduce。
  • ZeRO-3 的通信量为什么约是 DDP 的 1.5 倍(前向 All-Gather 参数 Φ + 反向 Reduce-Scatter Φ + 反向重算时再 All-Gather 参数 Φ ≈ 3Φ,vs DDP 的 2Φ),显存收益 16 16 /N_d 是否划算。
  • ZeRO-Offload 把什么移到 CPU、代价是什么: 优化器状态与 Adam 计算下放 CPU,GPU 只留本层参数分片 2 /N_d;换来显存大降,代价是 PCIe 带宽受限、步时间变长;ZeRO-Infinity 如何再扩展到 NVMe。