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.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- Naive Pipeline (All-Forward Then All-Backward)
- GPipe: Micro-Batching
- 1F1B Schedule
- Interleaved Stages
- Bubble Fraction Analysis
- Combining PP with TP and DP
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Derive the bubble fraction for naive pipeline parallelism.
- Explain how micro-batching reduces the bubble.
- Derive the 1F1B schedule and its memory advantage.
- Compute the optimal number of micro-batches for a given pipeline depth.
- Design a 3D parallelism strategy combining PP, TP, and DP.
Notation
- — number of pipeline stages
- — number of micro-batches
- — time for one micro-batch forward/backward per stage
- — 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
Naive Pipeline (All-Forward Then All-Backward)
Schedule: All micro-batches go forward through all stages, then all go backward.
Timeline for , :
- 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: idle slots per phase (forward and backward).
Bubble fraction: for . With : 75% idle!
GPipe: Micro-Batching
Idea: Split batch into 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:
For large : . With : bubble = .
Drawback: Must store activations for all micro-batches simultaneously (for backward pass). Memory: .
1F1B Schedule
One Forward, One Backward: Alternate forward and backward micro-batches after the pipeline fills up.
Schedule (after warm-up):
- Warm-up: stages complete one forward pass each (pipeline filling).
- Steady state: each stage alternates 1 forward + 1 backward.
- Cool-down: complete remaining backward passes.
Memory advantage: At any time, each stage holds at most micro-batch activations (not ). Memory: vs for GPipe.
Bubble fraction: Same as GPipe: . But much less memory.
Interleaved Stages
Idea (Megatron-LM v3): Assign multiple non-consecutive stages to each GPU.
With 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 virtual stages. Bubble fraction becomes:
With : less bubble for same .
Cost: More communication (activations must transfer between GPUs more frequently).
Bubble Fraction Analysis
Optimal micro-batches: To achieve bubble with stages:
For : need micro-batches.
Global batch size constraint: . Large 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: .
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: 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 : bubble > 50%. Always ensure .
Pitfall 3. Ignoring activation memory in 1F1B. While 1F1B saves memory vs GPipe, each stage still holds activations. For very deep pipelines (), this is significant.
Summary
- Pipeline parallelism splits layers across GPUs; data flows sequentially.
- Bubble overhead: — reduced by more micro-batches.
- 1F1B: Same efficiency as GPipe but memory instead of .
- Interleaved stages: less bubble by assigning multiple chunks per GPU.
- 3D parallelism: TP (intra-node) × PP (inter-node) × DP (replicas).
Exercises
Exercise 1. For : compute the bubble fraction for GPipe, 1F1B, and interleaved ().
Exercise 2. Derive the optimal pipeline depth 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 .
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.