7.9 — Tensor and pipeline parallelism (TP, PP)
Goal: Derive tensor parallelism (Megatron-LM): column-shard W_1, row-shard W_2 inside the FFN so that the per-device compute is 1/T_p of the original with one all-reduce per block. Derive pipeline parallelism (GPipe, 1F1B): split the L-block stack across P_p devices, manage the bubble overhead. Identify when each is appropriate.
1. Why this session exists
Data parallelism (7.8) replicates the model and splits the batch. For models that don't fit on one device, you need to split the model itself. Two complementary ways:
- Tensor parallelism (TP). Split each weight matrix across devices. Each device computes part of every matmul. Requires fine-grained communication (one all-reduce per sub-layer); needs high intra-node bandwidth (NVLink).
- Pipeline parallelism (PP). Split the layer stack across devices. Device 1 holds blocks 1–8, device 2 holds blocks 9–16, etc. Each microbatch flows through the pipeline. Communication is coarser (one send/recv between stages), so PP works across slower inter-node links.
Frontier LLMs use both, composed with DP and FSDP:
- TP within a node (high-bandwidth NVLink): typically .
- PP across nodes (lower-bandwidth IB): typically .
- DP/FSDP as the outermost dimension to scale to more replicas.
This session derives both axes. By the end you can read a frontier-training paper's "Parallelism configuration" section ("8-way TP, 4-way PP, 16-way DP, etc.") and recognise what is being split where.
2. Tensor parallelism (TP)
Shoeybi, Patwary, Puri, et al., 2019[1], Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism (arXiv:1909.08053). The foundational TP paper.
2.1 The FFN, sharded
Consider one FFN sub-layer (6.8):
(We drop biases for clarity; they shard analogously.) Split the FFN across devices ("TP rank"). The trick: shard along its output dimension and along its input dimension.
Column-shard : divide the rows into groups. Device holds . Compute on device :
Note: is the same on every device. No communication needed; each device computes its own slice of the hidden activations. The non-linearity is element-wise so it commutes with the split.
Row-shard : divide the columns into the same groups. Device holds . Compute on device :
Now is a partial sum — the contribution of device 's share of hidden units to the output. The full output is
Sum-reduce. One all-reduce across the devices at the end of the FFN gives every device the full . The pattern is sometimes notated f-g (Megatron-LM's notation): is the identity at forward, all-reduce at backward; is the all-reduce at forward, identity at backward. The roles flip because of how gradients flow.
The boxed result:
2.2 Attention, sharded
Multi-head attention (6.4) is natively parallel across heads. Distribute the heads across devices: device owns heads. Compute Q, K, V projections, softmax, and value-mixing locally for those heads. Then row-shard the output projection :
and all-reduce across to get the full attention output. Same pattern as the FFN: one all-reduce per sub-layer.
Both attention and FFN: 2 all-reduces per Transformer block under TP.
2.3 Memory and compute savings
Per device, under -way TP:
- Weights: of the original (each matrix is sharded).
- Activations for partial outputs: roughly of the inner widths.
- Compute: of the matmuls per block.
- Communication: 2 all-reduces per block, each of size .
For LLaMA-7B with : per-device weights drop from 14 GB to 1.75 GB. Per-block compute drops 8×. Cost: 2 all-reduces of size MB (at , bf16), blocks → 64 all-reduces per forward, 128 per training step (forward + backward).
2.4 Why TP needs intra-node bandwidth
Each all-reduce of size MB at GB/s IB takes ms. At 128 per step, that's ms of pure communication — bigger than the compute time. TP across IB is impractical.
But on an 8-GPU node with NVLink (~900 GB/s within the node), the same all-reduce takes . 128 all-reduces is ms — easily hidden behind compute. This is why TP is always within a node, and is typically at most 8 (= GPUs per node).
2.5 Sequence parallelism
A refinement of TP: outside the matmuls (LayerNorm, dropout, residual add), shard along the sequence dimension instead of replicating. Korthikanti et al., 2022[2], Reducing Activation Recomputation in Large Transformer Models (arXiv:2205.05198) — the same paper as selective recomputation (7.7 §7). The activations of LN/dropout/residual scale linearly in ; sequence parallelism shards them across the same devices that hold the TP-sharded matmuls. Combined effect: per-device activation memory drops by uniformly.
Adds two communications per block (gather + scatter), but the bytes are no more than TP's all-reduce, and they fit in the same hidden-behind-compute window. Standard in Megatron-LM since 2022.
3. Pipeline parallelism (PP)
Huang, Cheng, Bapna, et al., 2018[3], GPipe: Efficient Training of Giant Neural Networks Using Pipeline Parallelism (arXiv:1811.06965). The foundational PP paper.
3.1 The setup
Split the -block stack into contiguous stages. Device holds stages-worth of consecutive blocks: e.g., for : device 0 holds blocks 1–8, device 1 holds 9–16, etc. Plus the embedding + LM head, which live on devices 0 and respectively.
Forward pass on one microbatch:
- Device 0 runs blocks 1–8, sends the output activation to device 1.
- Device 1 runs blocks 9–16, sends to device 2.
- ... etc., until device runs the final blocks + LM head + loss.
Backward pass: gradient flows back through the pipeline in reverse.
3.2 The bubble
A naïve PP implementation has the obvious problem: while device 1 runs, device 0 is idle (waiting for the backward pass to come back). With one microbatch, the wall-clock is — but only one device works at a time. Total efficiency: . Terrible.
GPipe's fix: microbatching. Split the batch into microbatches; feed them through the pipeline staggered. Device 0 starts microbatch 1, then microbatch 2 (while microbatch 1 is on device 1), etc. Once the pipeline is full, all devices work simultaneously.
The wall-clock becomes time-units (forward), plus the same for backward, plus the optimiser step. The bubble — idle time at the start and end — is out of time-units. Bubble fraction:
Set to keep the bubble small. For : bubble fraction — manageable. For : bubble fraction — wasteful.
The catch: microbatches need activation buffers stored simultaneously (for the eventual backward), so PP's memory cost grows linearly in unless mitigated.
3.3 1F1B (one-forward-one-backward)
Narayanan, Harlap, et al., 2019[4], PipeDream (arXiv:1806.03377). Improves on GPipe: as soon as a microbatch finishes forward, start its backward instead of waiting for all forwards. The schedule becomes alternating: — "one forward, one backward".
Two benefits:
- Memory drops. Each microbatch's activations are released as soon as its backward completes. Steady-state memory holds only activations per device (one per microbatch currently in flight), not .
- Bubble unchanged. Same total wall-clock as GPipe; just different memory profile.
1F1B is the standard PP schedule in 2024–2026 (Megatron-LM, DeepSpeed). GPipe's "all forwards then all backwards" is still used in some frameworks because it's simpler, at the cost of activation memory.
3.4 Interleaved pipeline
Megatron-LM v2 (Narayanan et al., 2021)[5]. A further refinement: instead of contiguous stages (blocks 1–8 on device 0, 9–16 on device 1, etc.), interleave. Device 0 owns blocks (every 4th), device 1 owns , etc. With virtual stages per device, the bubble fraction becomes
Bubble divided by . The cost: more cross-stage sends per microbatch (now instead of ). On NVLink+IB, the extra communication is tolerable; on slower links it isn't.
Interleaved schedules are standard in frontier LLM training (Megatron-LM, FairScale's PP).
3.5 PP across nodes
Unlike TP, PP only sends boundary activations between stages — bytes per microbatch per boundary. For LLaMA-7B at in bf16: 1 GB per microbatch per boundary, sent once per forward and once per backward. Even on IB at 25 GB/s, this is ms per boundary — small relative to the compute per stage.
This is why PP is the natural choice for inter-node parallelism. TP's high-frequency all-reduces need NVLink; PP's coarse activations tolerate IB.
4. Composing TP, PP, DP, FSDP
In a frontier pretraining run, all four axes compose. The standard topology:
| Axis | Width | Located | Communication primitive | |---|---|---|---| | TP | | Within a node (NVLink) | All-reduce per sub-layer | | PP | | Across nodes (IB) | Send/recv between stages | | DP / FSDP | | Across all devices | All-reduce / all-gather | | Total devices | | | |
LLaMA-3-70B was reportedly trained on 16384 GPUs configured roughly as , , DP/FSDP-128. The product: .
4.1 Which axis to pick first?
A rough heuristic:
- TP up to , since that's one node's NVLink domain.
- PP up to whatever you need to fit the model: blocks must fit on one device's TP-shard.
- DP/FSDP to scale to more replicas / shrink optimiser memory.
For LLaMA-70B, blocks, . Per-block parameters (under ): = 320 MB in bf16. Even with all-blocks-checkpointed, you need PP to spread the blocks across multiple devices once you account for activations and ZeRO-sharded optimiser state. gives blocks per device per TP-shard — comfortable.
4.2 The DP-or-FSDP question
DP keeps full parameters on each replica; FSDP shards them. Modern practice:
- For sub-7B models: DP or DP + ZeRO-1 (optimiser-state sharding). Activations + small model fit.
- For 7B–70B: FSDP/ZeRO-3 (full sharding) is needed; TP may also be used for the largest activations.
- For 70B+: TP + PP + FSDP, in roughly that order.
There is no algorithmic rule; you tune by what fits in memory and what minimises communication. Modern training stacks (Megatron-LM, NeMo, ColossalAI) automate the configuration search.
5. Communication summary
Per training step, per device, the four axes contribute:
| Axis | Volume per step | Locality | |---|---|---| | DP | | Across all devices | | FSDP | (all-gather + reduce-scatter) | Across DP ranks | | TP | (all-reduces per block) | Within TP group | | PP | (microbatch boundaries) | Between PP stages |
Where is the gradient size of the full (un-sharded) model. The TP and PP volumes scale with — independent of the parameter count per se. The DP/FSDP volumes scale with the parameter count.
Total per-step communication for a 70B-parameter run can be 50–200 GB per device — a substantial fraction of compute time. Hiding it behind compute via overlap and pipelining is the central engineering problem of large-scale LLM training.
6. Sequence parallelism (briefly) and context parallelism
Two more axes that exist in some frameworks:
- Sequence parallelism (SP) (§2.5). A TP-extension; shards activations along the dimension for sub-layers outside matmuls (LN, dropout). Combined with TP, gives uniform -way activation sharding.
- Context parallelism (CP) (also called Ring Attention, Liu et al., 2023[6], arXiv:2310.01889). Splits the sequence dimension across devices specifically for attention's memory at very long context. Each device computes attention on its sequence chunk; partial results are passed in a ring. Used by Gemini 1.5 for 1M+ context training.
These are the bleeding edge of long-context training; most production stacks include them as optional axes.
7. What we keep going forward
Three things:
- Tensor parallelism shards the matmuls themselves (column-shard , row-shard , all-reduce at the end). Two all-reduces per Transformer block. Use within a node, .
- Pipeline parallelism shards the layer stack into stages and microbatches the input. Use across nodes, or so. 1F1B and interleaved schedules minimise the bubble.
- Compose TP × PP × DP/FSDP for frontier-scale training. LLaMA-70B used GPUs in this configuration.
Next session opens the third lever — FSDP / ZeRO — that shards the parameters, gradients, and optimiser state within the DP dimension, achieving "DP that fits 70B models".
(a) Re-derive the column-row sharding of the FFN. Why does sharding along output and along input only require one all-reduce (rather than two)? Where does the all-reduce occur in the forward pass?
(b) For LLaMA-7B at under -way TP, compute (i) the per-device weight memory, (ii) the all-reduce volume per block, and (iii) the total all-reduce volume per forward pass.
(c) Pipeline parallelism with stages and microbatches has bubble fraction . Compute this. Now compute the same for interleaved PP with virtual stages.
(d) Why is TP almost always intra-node and PP usually inter-node? State the communication-frequency and -bandwidth requirements of each.
(e) A frontier run uses . State the total number of devices, and explain where each axis sits in the network topology (NVLink within a node, IB between nodes, etc.). Why is this called "5D parallelism" when you count sequence parallelism?
References
- [1]
Shoeybi, M., Patwary, M., Puri, R., et al. (2019). "Megatron-LM: Training multi-billion parameter language models using model parallelism". arXiv:1909.08053 — Tensor parallelism (column/row sharding of FFN and MHA) (7.9 §2).
↩ - [2]
Korthikanti, V., Casper, J., Lym, S., et al. (2022). "Reducing activation recomputation in large transformer models". arXiv:2205.05198 — Sequence parallelism + selective activation recomputation (7.9 §2.5; also 7.7 §7).
↩ - [3]
Huang, Y., Cheng, Y., Bapna, A., et al. (2018). "GPipe: Efficient training of giant neural networks using pipeline parallelism". arXiv:1811.06965 — Microbatch-based pipeline parallelism; bubble fraction (7.9 §3).
↩ - [4]
Narayanan, D., Harlap, A., Phanishayee, A., et al. (2019). "PipeDream: Generalized pipeline parallelism for DNN training". arXiv:1806.03377 — 1F1B pipeline schedule (7.9 §3.3).
↩ - [5]
Narayanan, D., Shoeybi, M., Casper, J., et al. (2021). "Efficient large-scale language model training on GPU clusters using Megatron-LM". arXiv:2104.04473 — Interleaved pipeline + TP × PP × DP composition (7.9 §3.4).
↩ - [6]
Liu, H., Zaharia, M., & Abbeel, P. (2023). "Ring Attention with blockwise transformers for near-infinite context". arXiv:2310.01889 — Context parallelism for 1M+ context training (7.9 §6, 7.13 §5.1).
↩