Topic 38 of 62 · Training at scale
Tensor vs pipeline parallelism
HardWhat do tensor parallelism and pipeline parallelism split, and how do their communication costs differ?
1 · Start with the intuition
When a model is too large to fit on a single GPU, you can split it in two ways: Tensor Parallelism (TP) slices the weight matrices inside each individual layer so multiple GPUs cooperate on every single matrix multiply, whereas Pipeline Parallelism (PP) keeps each layer intact and assigns consecutive groups of layers (stages) to different GPUs like an assembly line. Because TP synchronizes large activation tensors twice inside every layer on the critical path, it requires ultra-fast intra-node links (NVLink); because PP only sends activations across the boundary between stages, it can cross slower inter-node links, but introduces idle “bubble” time while stages wait for the pipeline to fill and drain.
2 · How the mechanism works
Mechanism step 1 of 3 · no question to answer
TP: Column + Row sharding
Shard the first linear projection by columns and the second by rows so only one all-reduce (or reduce-scatter + all-gather) is needed per subblock.
Next, we will look at: PP: Partition depth into p stages. Move on when this operation makes sense.
Visualizing the behavior
4 stages × 8 microbatches (forward only): 8 of 11 slots busy per stage
DerivedUtilization m / (m + p − 1) for p = 4
Derived- ideal balanced pipeline
3 · The governing equation
Megatron TP MLP: Y₁ = GeLU(X [W₁⁽¹⁾ | W₁⁽²⁾]), Y = Y₁⁽¹⁾ W₂⁽¹⁾ + Y₁⁽²⁾ W₂⁽²⁾ (all-reduce sum) Pipeline bubble fraction (balanced 1F1B, p stages, m microbatches): bubble_fraction ≈ (p − 1) / (m + p − 1), utilization ≈ m / (m + p − 1)
Notation: p is the number of pipeline stages and m is the number of microbatches per optimizer step.
Look at why Megatron-style TP pairs a column-parallel matrix with a row-parallel matrix: if W₁ is split by columns across 2 GPUs, each GPU computes half the intermediate features and can apply the elementwise GeLU activation locally with zero communication. Then W₂ is split by rows, so each GPU multiplies its local half and a single all-reduce sums the partial results before the residual dropout/norm. Each Transformer block (Attention + MLP) therefore needs 2 all-reduces in forward and 2 in backward—all sitting directly on the critical path.
4 · Put concrete numbers on it
Worked numbers (from stated assumptions)5 · Common misconception to avoid
Setting TP = 16 or 32 across multiple nodes (which stalls every layer on slow inter-node Ethernet/InfiniBand) or increasing PP stages without enough microbatches m to amortize the (p − 1) fill/drain bubble.
In practice · implementation & engineering rule of thumb
Keep Tensor Parallelism ≤ number of GPUs per NVLink node (typically 8), and ensure microbatch count m is at least 4× to 8× the Pipeline Parallel degree p so the pipeline bubble stays small.
def pipeline_bubble_fraction(pp_stages, num_microbatches):
# Balanced 1F1B schedule bubble fraction:
return (pp_stages - 1) / (num_microbatches + pp_stages - 1)Go one level deeper
Follow-ups that often come after a correct first answer.
- The Megatron MLP split
- The first linear layer is column-parallel, so each GPU applies GeLU to its own slice with no communication. The second is row-parallel, producing partial sums that one all-reduce combines. That is one all-reduce per MLP in forward and one in backward.
- 1F1B and interleaved schedules
- GPipe runs all forwards, then all backwards, holding activations for all m microbatches. 1F1B alternates one forward with one backward, so each stage holds at most about p microbatches in flight, with the same bubble. Interleaving several virtual stages per GPU shrinks the bubble at the cost of more communication.
6 · Self-check & interview synthesis
Explain it in your own words first
Before revealing the reference answer, try answering the opening question aloud in four beats: what it is → how the math works → what trade-off it makes → a concrete number or failure mode.
Follow-up question interviewers often ask next
MediumWhy does the 1F1B (one-forward-one-backward) pipeline schedule use less activation memory than running all m forward microbatches before any backward pass (GPipe)?
Show follow-up reasoning
In GPipe, every stage has to hold the forward activations of all m microbatches in memory until the backward phase begins (O(m) activation memory). In 1F1B, once the pipeline warm-up finishes, each stage alternates one forward microbatch with one backward microbatch, immediately freeing that microbatch’s activations—bounding live in-flight microbatches to at most p instead of m.