Pipeline Parallel:bubble 怎么来、多大?
1F1B、interleaved 解决什么?peak memory 一样吗?

英文问法Where does the pipeline bubble come from and how big is it? · GPipe vs 1F1B? · How does the interleaved schedule shrink the bubble, and at what cost? · Is peak memory the same with and without PP?

考过NVIDIA DevTech AI 实习 2024 技术面("提到了训练过程中的pipeline parallelism",1p3a 1062465)· 腾讯混元("流水线 Bubble 气泡如何产生、怎么优化降低损耗?")· 牛客("说明一下1f1b,dualpipe")· 快手("使用流水线并行和不使用PP并行,显存峰值一样吗")· 拼多多二面("如何提高串行的几个显卡的负载")· 美团("zero-bubble调度原理")

先弄清楚:每个概念是什么

Pipeline parallel(PP)
把 \(L\) 层按顺序切成 \(p\) 段(stage),每张卡一段,相邻 stage 之间 p2p 传 activation(backward 传 gradient)。一个 batch 切成 \(m\) 个 micro-batch 依次送进去。例:L = 32、p = 4 → 每卡 8 层。
Pipeline bubble
开工时后面的 stage 等 activation,收工时前面的 stage 等 gradient,这段空转就是 bubble。为了 optimizer 语义严格同步,每个 batch 末尾都要 flush 一次,所以每个 batch 都有。
1F1B
预热几个 forward 后,做一个 forward 就做一个 backward,backward 完就释放那份 activation。

看图:GPipe 和 1F1B 的时间格

时间 →(1 格 = 一次 F 或 B)GPipe:先做完全部 forward,再做 backwardGPU0F1F2F3F4F5F6B1B2B3B4B5B6GPU1F1F2F3F4F5F6B1B2B3B4B5B6GPU2F1F2F3F4F5F6B1B2B3B4B5B6GPU3F1F2F3F4F5F6B1B2B3B4B5B6GPU0 存几份1234566666665432101F1B:预热后做一个 forward 就做一个 backwardGPU0F1F2F3F4B1F5B2F6B3B4B5B6GPU1F1F2F3B1F4B2F5B3F6B4B5B6GPU2F1F2B1F3B2F4B3F5B4F6B5B6GPU3F1B1F2B2F3B3F4B4F5B5F6B6GPU0 存几份123444434343322110
图 1:p = 4、m = 6,每格是一次 forward(F)或 backward(B),虚线空格是 bubble。两种 schedule 都要 18 格、每张卡空 6 格,区别在最下面一行 GPU0 还存着几份 activation。GPipe 攒到 6 份(= m),1F1B 最多 4 份(= p)。

30 秒回答

Pipeline parallelism splits the layers into p stages on p GPUs and the batch into m micro-batches. At the start later stages wait for activations, and at the end earlier stages wait for gradients; that idle time is the bubble, (p−1)/m of the ideal compute time for both GPipe and 1F1B, so we want m ≫ p. GPipe runs all forwards before any backward and stashes activations for all m micro-batches; 1F1B alternates forward and backward after a warm-up, so at most p are in flight. Interleaving gives each GPU v non-contiguous chunks, cutting the bubble to (p−1)/(vm) at v times the point-to-point communication.

推导 / 关键公式

bubble 多大

开头空等 \(p-1\) 个 forward,结尾空等 \(p-1\) 个 backward:

\[ t_{pb} = (p-1)(t_f+t_b),\qquad t_{id} = m\,(t_f+t_b) \] \[ \text{bubble} = \frac{t_{pb}}{t_{id}} = \frac{p-1}{m} \]

interleaved

每张卡拿 \(v\) 段不相邻的层。每段的 forward、backward 时间都变成 \(1/v\),bubble 跟着 \(\div v\),但 stage 之间的 p2p 通信 \(\times v\),还要求 \(m\) 是 \(p\) 的整数倍:

\[ \text{bubble}_{\text{int}} = \frac{1}{v}\cdot\frac{p-1}{m} \]

peak activation(第一个 stage 最大)

GPipe 存 \(m\) 个 micro-batch。1F1B 存 \(p\) 个,每个 \(L/p\) 层,合计 \(L\) 层的量,和不切 PP 一样,不随 \(p\) 变小。interleaved 再乘 \(1 + \frac{p-1}{pv}\)。

手算例子

\(p = 4\)、\(m = 6\),F、B 各算 1 格(.. 是 bubble):

1F1B
GPU0 F1 F2 F3 F4 .. .. .. B1 F5 B2 F6 B3 .. B4 .. B5 .. B6
GPU1 .. F1 F2 F3 .. .. B1 F4 B2 F5 B3 F6 B4 .. B5 .. B6 ..
GPU2 .. .. F1 F2 .. B1 F3 B2 F4 B3 F5 B4 F6 B5 .. B6 .. ..
GPU3 .. .. .. F1 B1 F2 B2 F3 B3 F4 B4 F5 B5 F6 B6 .. .. ..
GPipe:GPU0 先做完 F1–F6 再 B1–B6,同样 18 格

18 格里理想只要 12 格,bubble \(= 6/12 = 3/6 = (p-1)/m\)。换成真实配置 \(p = 8\)、\(m = 32\):

schedulebubbleGPU0 存几份 micro-batch 的 activation
GPipe7/32 = 21.9%32
1F1B21.9%8
interleaved v = 47/128 = 5.5%8 × 1.22 = 9.75

必答点

追问

Zero Bubble 怎么做到的?

把 backward 拆成 B(对输入的 gradient,上一个 stage 在等)和 W(对 weight 的 gradient,没人等),W 往后挪去填 bubble。再绕开 optimizer step 的全局同步,在同步训练语义下做到零 bubble。

DualPipe 是什么?

DeepSeek-V3 的 schedule。micro-batch 从流水线两端同时喂,一对 forward、backward chunk 拆成 attention、all-to-all dispatch、MLP、all-to-all combine 交错执行,让 MoE 的 all-to-all 和 PP 通信被计算盖住。代价是每卡存两份参数,activation peak 多约 \(1/p\)。

串行的几张卡负载不均怎么办?

先减 bubble。第一个 stage 有 embedding、最后一个有 LM head 和 loss,给它们少放几层(Megatron:--decoder-first-pipeline-num-layers、--decoder-last-pipeline-num-layers)。

来源Narayanan et al. 2021 §2.2(pp.2–4,bubble、1F1B、interleaved)、§3.4(p.6)· GPipe §2.3(p.4)· PipeDream §3.3(p.7,1F1B)· Korthikanti et al. 2022 §4.2.3(p.8,第一个 stage 存 \(L\) 层、interleaved 系数)· Zero Bubble §2(p.3)、abstract· DeepSeek-V3 §3.2.1、Table 2(pp.12–13)· Megatron-LM arguments.py