M3-079M3: Deep Learning FoundationsDistributed Training BasicsEasy
⚡Flashcard Drill➔Mastery:
ZeRO (Zero Redundancy Optimizer): The Three Sharding Stages Explained
📐 Mathematical Definition
⚡ One-Sentence Core Answer
ZeRO eliminates memory redundancies in distributed training by sharding optimizer states (ZeRO-1), gradients (ZeRO-2), and model parameters (ZeRO-3) across data-parallel ranks.
📌 Key Takeaways
- •ZeRO-1: shards 16-byte FP32 optimizer states (Adam and ); reduces memory by with zero extra communication
- •ZeRO-2: shards gradients; ranks reduce-scatter gradients and retain only their partition; reduces memory by
- •ZeRO-3 (FSDP): shards model parameters; gathers full weights on-the-fly via All-Gather before each layer forward/backward, and discards them immediately
📐 Mathematical Derivations
Memory Breakdown and Sharding Mechanics (Rajbhandari et al., DeepSpeed, SC 2020):
For a model with parameters trained in FP16 mixed-precision with Adam:
- Model Weights (FP16): bytes.
- Gradients (FP16): bytes.
- Optimizer States (FP32 master weights , momentum , variance ): bytes.
Total baseline memory per GPU: bytes (e.g., 256GB for a 16B parameter model).
ZeRO Stages across $N$ Data-Parallel Ranks:
① ZeRO-1 ($P_{\text{os}}$): Shards optimizer states across ranks. Memory becomes . Communication: exact same All-Reduce overhead as standard DDP.
② ZeRO-2 ($P_{\text{os+g}}$): Shards both optimizer states and gradients. Gradients are synchronized via Reduce-Scatter instead of All-Reduce. Each rank updates only its parameter partition. Memory: . Communication: identical volume to standard DDP ().
③ ZeRO-3 ($P_{\text{os+g+p}}$): Shards model parameters as well. Each rank stores only parameters. Memory drops to bytes. Prior to each layer's forward pass, ranks perform an All-Gather to reconstruct parameters, compute layer, and release weights. Backward pass executes a second All-Gather. Increases communication volume by .
For a model with parameters trained in FP16 mixed-precision with Adam:
- Model Weights (FP16): bytes.
- Gradients (FP16): bytes.
- Optimizer States (FP32 master weights , momentum , variance ): bytes.
Total baseline memory per GPU: bytes (e.g., 256GB for a 16B parameter model).
ZeRO Stages across $N$ Data-Parallel Ranks:
① ZeRO-1 ($P_{\text{os}}$): Shards optimizer states across ranks. Memory becomes . Communication: exact same All-Reduce overhead as standard DDP.
② ZeRO-2 ($P_{\text{os+g}}$): Shards both optimizer states and gradients. Gradients are synchronized via Reduce-Scatter instead of All-Reduce. Each rank updates only its parameter partition. Memory: . Communication: identical volume to standard DDP ().
③ ZeRO-3 ($P_{\text{os+g+p}}$): Shards model parameters as well. Each rank stores only parameters. Memory drops to bytes. Prior to each layer's forward pass, ranks perform an All-Gather to reconstruct parameters, compute layer, and release weights. Backward pass executes a second All-Gather. Increases communication volume by .
🏭 Production Trade-offs
System design trade-offs: ZeRO-1/2 provide substantial memory relief with zero additional network traffic. ZeRO-3 allows training multi-billion parameter models without Tensor Parallelism, but incurs 50% more communication, requiring high-bandwidth interconnects (InfiniBand/RoCE).
⚠️ Common Interview Pitfalls
- ✕Using ZeRO-3 on clusters with slow 10Gbps Ethernet, causing training to be 90% communication-bound
- ✕Assuming ZeRO-1 increases network communication volume; ZeRO-1 has mathematically identical communication to PyTorch DDP
🎯 Interviewer Follow-up Inquiries
- ?Why does ZeRO-3 require 50% more communication volume than standard Data Parallelism (DDP)?
- ?How does ZeRO-Offload leverage host CPU RAM and NVMe SSDs to train massive models on single GPUs?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
📚
Associated Knowledge Base Guides & Mindmaps
Explore the comprehensive technical article, exam cards, and global architecture tree.
中文速览与面试要点
解释 ZeRO 的三个阶段
ZeRO 分片优化器状态(阶段 1)、梯度(阶段 2)、参数(阶段 3),显存逐级下降、通信逐级增加。
核心要点
- 数学机理:以参数量 Ψ、混合精度(FP16 参数 + FP32 master + Adam 的 m/v)计,单卡显存约为:参数 2Ψ(FP16)+ master 4Ψ(FP32)+ m 4Ψ + v 4Ψ = 14Ψ 字节 (另有激活与临时缓冲)。可见 优化器状态(m+v+master)占 12Ψ、远大于参数的 2Ψ ——这是显存的主要瓶颈。 ZeRO 的分片思路 (Rajbhandari 等 2020): ZeRO-1 把 优化器状态 按 DP 度 N 分片,每卡只存 1/N 的 m/v/master → 优化器显存降 N 倍(12Ψ→12Ψ/N); ZeRO-2 再把 梯度 分片(每卡只存 1/N 梯度)→ 梯度显存降 N 倍; ZeRO-3 再把 参数 分片(每卡只存 1/N 参数)→ 参数显存也降 N 倍,总显存降到约 14Ψ/N。 代价 :ZeRO-1 通信量与原 DP 相同(一次 reduce-scatter + all-gather 替代 all-reduce);ZeRO-2 略增;ZeRO-3 因每层前向需 all-gather 参数 、反向后丢弃,通信量显著增加(约 1.5 倍)。因此 ZeRO-3 适合'显存极度紧张、带宽尚可'的场景,ZeRO-1/2 适合'显存够用、追吞吐'的场景。
- 深度剖析与工程权衡:① 与 FSDP 的关系 ——PyTorch 的 FSDP(Fully Sharded Data Parallel)是 ZeRO-3 的等价实现(参数、梯度、优化器状态全分片),两者思想一致、API 不同;DeepSpeed 的 ZeRO-3 与 FSDP 可视为同一技术的两个工程实现。② 通信与计算的重叠 ——ZeRO-3 的 all-gather 可与计算重叠(预取下一层参数);DeepSpeed 与 FSDP 都实现了 prefetch 以隐藏通信延迟。③ offload 的组合 ——ZeRO-3 可把参数/优化器状态 offload 到 CPU(ZeRO-Offload/Infinity),进一步降低 GPU 显存但受 PCIe 带宽限制。④ 与 TP/PP 的组合 ——ZeRO 是'DP 维度的分片',与 TP(层内切)、PP(层间切)正交;大模型训练常用 'ZeRO-1 + TP + PP' 或 'FSDP + TP' 的组合。⑤ 显存账本的实用价值 ——面试中能算出'14Ψ 字节'并指出'优化器状态是大头',是理解 ZeRO 动机的关键;这比记住三个阶段名字更重要。⑥ 面试要点 ——被问'ZeRO 是什么',应从' 显存账本 → 优化器状态占大头 → 逐级分片 → 通信代价递增 '这条逻辑链回答;并主动区分 ZeRO-1/2/3 的适用场景。
- 每设备显存:模型 16Ψ + 优化器 12Ψ(FP32)量级
- ZeRO-1 省 4 倍优化器状态;ZeRO-2 再加梯度;ZeRO-3 全分片
- ZeRO-3 需 all-gather 参数,通信量增加
常见陷阱
- 以为 ZeRO 免费(ZeRO-3 通信量显著增加)
- 不知道优化器状态才是显存大头