Pipeline Parallelism

Splitting model layers across GPUs: naive pipeline with bubble overhead, GPipe micro-batching, 1F1B schedule, interleaved stages, and the bubble fraction analysis for optimal pipeline configuration.

Advanced

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. Naive Pipeline (All-Forward Then All-Backward)
  5. GPipe: Micro-Batching
  6. 1F1B Schedule
  7. Interleaved Stages
  8. Bubble Fraction Analysis
  9. Combining PP with TP and DP
  10. Common Pitfalls
  11. Summary
  12. Exercises

Learning Objectives

  1. Derive the bubble fraction for naive pipeline parallelism.
  2. Explain how micro-batching reduces the bubble.
  3. Derive the 1F1B schedule and its memory advantage.
  4. Compute the optimal number of micro-batches for a given pipeline depth.
  5. Design a 3D parallelism strategy combining PP, TP, and DP.

Notation

  • PP — number of pipeline stages
  • MM — number of micro-batches
  • tf,tbt_f, t_b — time for one micro-batch forward/backward per stage
  • BbubbleB_{\text{bubble}} — bubble fraction (idle time / total time)

Core Intuition

If a model has 32 layers and you have 4 GPUs, assign 8 layers per GPU (stages). Data flows through stages sequentially: stage 1 → stage 2 → stage 3 → stage 4 (forward), then backward. The problem: while stage 4 computes, stages 1-3 are idle. Micro-batching splits the batch into smaller chunks that flow through the pipeline simultaneously, reducing idle time.

Pipeline Parallelism

Micro-batches flowing through 4 pipeline stagesGPU0GPU1GPU2GPU3Bubble: 43%Efficiency: 57%
MicroB
4
ActiveBubble (idle)
Explore: Pipeline parallelism splits the model across GPUs with micro-batches filling the pipeline. More micro-batches reduce bubble (idle) time at the cost of memory.

Naive Pipeline (All-Forward Then All-Backward)

Schedule: All micro-batches go forward through all stages, then all go backward.

Timeline for P=4P=4, M=1M=1:

  • Time 1: Stage 1 forward → Stages 2,3,4 idle.
  • Time 2: Stage 2 forward → Stages 1,3,4 idle.
  • Time 3: Stage 3 forward → Stages 1,2,4 idle.
  • Time 4: Stage 4 forward → Stages 1,2,3 idle.
  • Time 5-8: Backward (same pattern reversed).

Bubble time: (P1)(P-1) idle slots per phase (forward and backward).

Bubble fraction: (P1)/(P1+M)(P1)/P(P-1)/(P-1+M) \to (P-1)/P for M=1M=1. With P=4P=4: 75% idle!


GPipe: Micro-Batching

Idea: Split batch into MM micro-batches. Pipeline them through stages:

Forward: Micro-batch 1 enters stage 1, then stage 2, ... while micro-batch 2 enters stage 1, etc.

Bubble fraction:

Bbubble=(P1)M+P1tf+tbtf+tb.(1)B_{\text{bubble}} = \frac{(P-1)}{M + P - 1} \cdot \frac{t_f + t_b}{t_f + t_b}. \tag{1}

For large MM: Bbubble(P1)/MB_{\text{bubble}} \approx (P-1)/M. With P=4,M=32P=4, M=32: bubble = 9.4%9.4\%.

Drawback: Must store activations for all MM micro-batches simultaneously (for backward pass). Memory: O(M×activations per micro-batch)O(M \times \text{activations per micro-batch}).


1F1B Schedule

One Forward, One Backward: Alternate forward and backward micro-batches after the pipeline fills up.

Schedule (after warm-up):

  1. Warm-up: stages complete one forward pass each (pipeline filling).
  2. Steady state: each stage alternates 1 forward + 1 backward.
  3. Cool-down: complete remaining backward passes.

Memory advantage: At any time, each stage holds at most PP micro-batch activations (not MM). Memory: O(P)O(P) vs O(M)O(M) for GPipe.

Bubble fraction: Same as GPipe: (P1)/M(P-1)/M. But much less memory.


Interleaved Stages

Idea (Megatron-LM v3): Assign multiple non-consecutive stages to each GPU.

With P=4P=4 GPUs and 16 model chunks:

  • GPU 0: chunks 0, 4, 8, 12
  • GPU 1: chunks 1, 5, 9, 13
  • GPU 2: chunks 2, 6, 10, 14
  • GPU 3: chunks 3, 7, 11, 15

Benefit: Each GPU processes v=4v = 4 virtual stages. Bubble fraction becomes:

Bbubble=P1Mv.(2)B_{\text{bubble}} = \frac{P-1}{M \cdot v}. \tag{2}

With v=4v=4: 4×4\times less bubble for same MM.

Cost: More communication (activations must transfer between GPUs more frequently).


Bubble Fraction Analysis

Optimal micro-batches: To achieve <5%<5\% bubble with PP stages:

M>20(P1).(3)M > 20(P-1). \tag{3}

For P=8P=8: need M>140M > 140 micro-batches.

Global batch size constraint: Bglobal=M×Bmicro×DP degreeB_{\text{global}} = M \times B_{\text{micro}} \times \text{DP degree}. Large MM requires large global batch size.

Tradeoff: More micro-batches → less bubble, but larger batch sizes (potentially hurting convergence) and more pipeline flushes.


Combining PP with TP and DP

3D parallelism (Megatron-DeepSpeed):

  • TP (tensor): within a node, 4-8 GPUs.
  • PP (pipeline): across nodes, 4-16 stages.
  • DP (data): across PP replicas, 8-64x.

Total GPUs: Ntotal=TP×PP×DPN_{\text{total}} = \text{TP} \times \text{PP} \times \text{DP}.

Example: Training a 175B model on 512 GPUs:

  • TP = 8 (within node, 8 GPUs per node).
  • PP = 8 (8 pipeline stages across 8 nodes).
  • DP = 8 (8-way data parallel replicas).
  • Total: 8×8×8=5128 \times 8 \times 8 = 512 GPUs.

Common Pitfalls

Pitfall 1. Unbalanced stages. If layers have different compute costs (e.g., attention layers with different sequence lengths), stages must be balanced by compute, not just layer count.

Pitfall 2. Using too few micro-batches. With M<PM < P: bubble > 50%. Always ensure MPM \gg P.

Pitfall 3. Ignoring activation memory in 1F1B. While 1F1B saves memory vs GPipe, each stage still holds PP activations. For very deep pipelines (P=16P=16), this is significant.


Summary

  • Pipeline parallelism splits layers across GPUs; data flows sequentially.
  • Bubble overhead: (P1)/M(P-1)/M — reduced by more micro-batches.
  • 1F1B: Same efficiency as GPipe but O(P)O(P) memory instead of O(M)O(M).
  • Interleaved stages: v×v\times less bubble by assigning multiple chunks per GPU.
  • 3D parallelism: TP (intra-node) × PP (inter-node) × DP (replicas).

Exercises

Exercise 1. For P=8,M=64P=8, M=64: compute the bubble fraction for GPipe, 1F1B, and interleaved (v=4v=4).

Exercise 2. Derive the optimal pipeline depth PP for 64 GPUs, given TP=8 and the constraint that bubble < 5%.

Exercise 3. Compute the activation memory per GPU for 1F1B vs GPipe with P=4,M=32,d=4096,T=2048P=4, M=32, d=4096, T=2048.

Exercise 4. Design a 3D parallelism configuration for training a 530B model on 1024 A100 GPUs.

Exercise 5. Explain why pipeline parallelism is preferred over tensor parallelism for inter-node communication.