← Back to AI Infrastructure Mind Map
🖥️ AI InfrastructureID: megatron-3d-bubble

Megatron 3D & Pipeline Bubble

Megatron TP/PP 与气泡率
🎯Core Definition
Megatron-LM's 3D parallelism (TP × PP × DP) implementation. TP column/row parallel with dual AllReduce: inside each transformer layer, the attention QKV projection is column-parallel (output split along hidden), the output projection row-parallel (input split along hidden), and the MLP the same — a column-parallel branch's output is a per-rank partial sum that must be AllReduced to recover the full result; the forward has 2 AllReduces per layer (attention output projection + MLP's second linear), the backward has 2 more (column-parallel weight gradients), 4 per layer per iteration, each message about b×s×hb \times s \times h. PP 1F1B schedule: the naive greedy schedule (all microbatches' forwards, then all backwards) keeps m microbatches' activations alive at once, peaking memory and starting backprop late; 1F1B (one-forward-one-backward) alternates one forward and one backward per stage, capping in-flight microbatches at p and lowering peak memory. Bubble formula: with p stages and m microbatches per batch, the steady-state bubble fraction is bubble=p−1m+p−1\text{bubble} = \frac{p-1}{m+p-1} — the larger m, the smaller the bubble for fixed p; numeric examples: p=4, m=4 → 37≈43%\frac{3}{7} \approx 43\%; p=8, m=32 → 739≈18%\frac{7}{39} \approx 18\%; p=8, m=64 → 771≈10%\frac{7}{71} \approx 10\%; approaching 0 as m grows. Interleaved (V-shaped/chunked) scheduling splits each stage's layers into c chunks, cutting the bubble by roughly c× at extra communication and memory cost. Ring-Attention sequence parallelism: the sequence is split into N chunks across N ranks; each rank first computes partial attention on its local chunk, then KV blocks circulate around the ring, and after N−1 rounds every rank has seen all KV — per-rank KV memory is O(1) (independent of seq length), per-layer communication ≈ 2× the full KV size (each rank reads all K and V once), bandwidth is constant while latency grows with N−1 serial hops; it shares the online-softmax technique with FlashAttention.
💡Use Cases
the hot zone of training-infra interviews: derive the bubble formula, why TP runs 2 AllReduces per layer, how 1F1B beats greedy, how Ring-Attention's memory and communication work out, and how a 175B model is configured in 3D.
⚡Key Problems Solved
TP spreads per-layer compute across GPUs at the cost of frequent small messages (only intra-node NVLink can carry them); PP splits layers so memory is amortized, but idle bubbles eat utilization — 1F1B squeezes memory, large m squeezes the bubble, interleaved trades further; Ring-Attention parallelizes the longest dimension (the sequence) so ultra-long-context training no longer hits the per-GPU KV memory ceiling.
🎯Core Interview Exam Points
Fundamentals · Production Scenarios · Deep Dive
Core Def
Explain the core definition, underlying mechanism/math, and key principles of Megatron 3D & Pipeline Bubble.
Use Cases
In production engineering, what are the primary use cases for Megatron 3D & Pipeline Bubble and what core pain points does it solve?
🔥 High-Frequency Deep-Dive Questions
1
Hand-derive the bubble: p stages, m microbatches → bubble=p−1m+p−1\text{bubble}=\frac{p-1}{m+p-1}; give numeric examples (p=4,m=4 → 3/7≈43%; p=8,m=32 → 7/39≈18%) and how to reduce it (larger m, interleaved chunks).
2
Describe TP column/row parallel with dual AllReduce: why column-parallel outputs need AllReduce summation and why row-parallel backward does too; how many per layer per iteration (2 forward + 2 backward = 4).
3
Compare greedy vs 1F1B scheduling: why 1F1B has lower peak memory (in-flight microbatches ≤ p) and earlier backprop start; why the bubble is not eliminated (inter-stage pipeline stalls).
4
Derive Ring-Attention: why per-rank KV memory is O(1) after splitting the sequence into N chunks; per-layer communication ≈ 2× the full KV; bandwidth/latency profile (constant bandwidth, N−1 serial hops) and its relation to FlashAttention.
5
3D integration: explain the DP/TP/PP division by communication level (TP intra-node frequent small messages, PP inter-node infrequent large messages, DP gradient AllReduce), and why 3D parallelism grows parallelism while cutting per-GPU memory and communication cost.
Updated 2026-08-12
🎯
Verify Mastery: Practice Exam Questions for "Megatron 3D & Pipeline Bubble"
Take pitfall multiple-choice questions with derivation options; mistakes are automatically logged.
🚀 Start Card Practice ➔
← Previous Card4D Parallelism DP/TP/PP/SPNext Card →ZeRO-1/2/3 & Offload

🔗 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/megatron-3d-bubble
中文速览与面试要点

Megatron TP/PP 与气泡率 (Megatron 3D & Pipeline Bubble)

定义: Megatron-LM 的 3D 并行(TP × PP × DP)实现细节。TP 列/行并行与双 AllReduce: 每个 transformer 层内, attention 的 QKV 投影用列并行(输出沿 hidden 维切分)、输出投影用行并行(输入沿 hidden 维切分), MLP 同理——列并行分支的输出是各卡局部和, 必须 AllReduce 才能得到完整结果; 每层前向 2 次 AllReduce(attention 输出投影 + MLP 第二个线性层), 反向再 2 次(列并行权重梯度需要 AllReduce), 共 4 次/层/迭代, 每次消息量 ~b s h。PP 1F1B 调度: 朴素 greedy 调度(先跑完所有 microbatch 的前向再统一反向)会同时驻留 m 个 microbatch 的激活、显存尖峰且反向启动晚; 1F1B(one-forward-one-backward)每个 stage 交替执行一个前向与一个反向, 同时 in-flight 的 microbatch 数被限制在 p 以内, 峰值显存更低。气泡率公式: 设 p 个 stage、每个 batch 拆 m 个 microbatch, 稳态下气泡占比 bubble = p-1m+p-1——p 固定时 m 越大气泡越小; 数值例: p=4, m=4 → 37 43\%; p=8, m=32 → 739 18\%; p=8, m=64 → 771 10\%; m 足够大时趋近 0。interleaved(V 型/分块)调度把每个 stage 的层再分 c 块, 可把气泡再降约 c 倍, 但增加通信与显存开销。Ring-Attention 序列并行: 序列切成 N 段分到 N 卡, 每卡先算本地 chunk 的部分注意力, 再把 KV 块沿 ring 依次传递, N−1 轮后每卡收到全部 KV——单卡 KV 显存 O(1)(不随 seq 增长), 每层通信量 ≈ 2 × 全部 KV 大小(每卡完整读一遍 K 和 V), 带宽恒定、延迟随 N−1 次串行 hop 增长; 与 FlashAttention 的 online-softmax 技术同源。 使用场景: 训练 Infra 面试重灾区: “推导气泡率公式”“TP 为什么每层 2 次 AllReduce”“1F1B 比 greedy 好在哪里”“Ring-Attention 显存和通信怎么算”“175B 怎么配 3D”。 解决的问题: TP 让单层计算摊到多卡但带来高频小消息(只能靠节点内 NVLink 扛); PP 让不同 stage 各持部分层、显存随层数分摊, 但 idle 气泡把利用率吃掉——用 1F1B 压显存、用大 m 压气泡、用 interleaved 进一步折中; Ring-Attention 则把最长的维度(序列)也并行化, 让超长上下文训练不再受单卡 KV 显存限制。

核心要点

  • 手算气泡率: p 个 stage、m 个 microbatch → bubble= p-1m+p-1; 给出数值例(p=4,m=4 → 3/7≈43%; p=8,m=32 → 7/39≈18%)并说明如何降低气泡(增大 m、interleaved 分块)。
  • 描述 TP 列/行并行与双 AllReduce: 列并行输出为什么需要 AllReduce 求和、行并行反向为什么也需要; 每 transformer 层每迭代共几次(前向 2 + 反向 2 = 4)。
  • 对比 greedy 与 1F1B 调度: 为什么 1F1B 峰值显存更低(in-flight microbatch ≤ p)、反向启动更早; 气泡为何没有因此消除(stage 间的流水线等待)。
  • 推导 Ring-Attention: 序列切 N 段后每卡 KV 显存为何 O(1); 每层通信量 ≈ 2 × 全部 KV; 带宽/延迟特性(带宽恒定、N−1 次串行 hop)与 FlashAttention 的关系。
  • 3D 整合题: 按通信层次解释 DP/TP/PP 分工(TP 节点内高频小消息、PP 节点间低频大消息、DP 梯度 AllReduce), 为什么 3D 并行能同时扩大并行度、降低单卡显存与通信开销。