Foundations to Frontier
Tests
C. Programming, Tools and HPC › C6 Distributed Training

Model Parallelism: Tensor, Pipeline, Sequence and Expert

When a model no longer fits on one GPU, or data parallelism alone stops scaling, the model itself is split. Tensor parallelism splits individual matrix multiplications across GPUs (Megatron-LM's column- and row-parallel layers, one all-reduce per block); pipeline parallelism places groups of layers on different GPUs and streams micro-batches through them (GPipe, 1F1B, the pipeline bubble); sequence and context parallelism split activations along the sequence; expert parallelism places the experts of a mixture-of-experts layer on different GPUs with all-to-all routing. Real training combines them with data parallelism (3D parallelism) according to the hardware: tensor parallelism inside a node, pipeline and data parallelism across nodes. In the exercises you implement tensor-parallel MLP and attention blocks and verify them against unsplit layers, simulate pipeline schedules, and route tokens to experts with a capacity limit; then you search TP × PP × DP layouts for 1.3B, 13B and 70B models on 64 GPUs with a memory and time model.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

When one desk isn’t big enough

If a single giant map doesn’t fit on any desk, you can cut it into strips and give each person one strip (they must talk at the edges), or arrange people in an assembly line where each does one stage of the work and passes it on. Huge neural networks are split the same ways: tensor parallelism cuts each layer’s big matrices into strips; pipeline parallelism gives different layers to different GPUs and streams small batches through them like an assembly line.

Pipeline parallelism: schedules and bubbles
GPU 0GPU 1GPU 2GPU 3F0F0F0F0F1F1F1B0F2F2B0F1F3B0F2B1B0F3B1F2F4B1F3B2B1F4B2F3F5B2F4B3B2F5B3F4F6B3F5B4B3F6B4F5F7B4F6B5B4F7B5F6B5F7B6B5B6F7B6B7B6B7B7B7blue: forward (1 unit) · purple: backward (2 units) · grey: idle
step time 33 units
bubble 27% = (S−1)/(M+S−1)
peak stored micro-batches: 4 (GPU 0)
Both schedules have the same bubble; more micro-batches shrink it. GPipe keeps the activations of all M micro-batches until their backward passes, 1F1B at most S, which is what makes many micro-batches affordable. Interleaved schedules (several smaller stages per GPU) and zero-bubble variants shrink the bubble further.
Why engineers care

Foundation models for Earth observation, weather and language have tens to hundreds of billions of parameters: they don’t fit in one GPU’s memory. Even moderately large scientific models (3D vision transformers, long-context sequence models for telemetry) need model parallelism once activations or parameters outgrow a single device.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Model parallelism is needed when…
  2. 2. Pipeline parallelism gives each GPU…
  3. 3. In the demo, what shrinks the idle bubble?

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

The four ways to split

Strategy What is split Communication Typical placement
tensor (TP) weight matrices of each layer all-reduce per block (2 per layer forward) within a node (NVLink)
pipeline (PP) layers into stages point-to-point activations between stages across nodes
sequence / context (SP/CP) activations along the sequence all-gather / reduce-scatter, ring attention with TP, long contexts
expert (EP) experts of MoE layers all-to-all token routing across GPUs
data (DP) the batch gradient all-reduce everywhere

N GPUs are factored as TP × PP × DP (× EP, CP). Frameworks: Megatron-LM, DeepSpeed, PyTorch’s DTensor and pipelining APIs, JAX’s sharding (C3.4).

📝 Quick check (Level 1)
0/3 answered
  1. 1. Which split communicates most often and so belongs within a node?
  2. 2. 64 GPUs as TP = 8, PP = 2: how many data-parallel replicas?
  3. 3. Expert parallelism routes tokens with…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.

Tensor parallelism (Megatron-LM)

Split the first MLP weight W₁ by columns: each GPU computes its slice of XW₁ with no communication, and GELU (element-wise) applies locally. Split W₂ by rows: each GPU multiplies its slice of the hidden activations by its rows of W₂, producing a partial sum of the output; one all-reduce combines them. Attention splits the same way by heads (Q, K, V columns per head, output projection by rows). Each transformer layer therefore needs two all-reduces in forward and two in backward, of size batch × sequence × hidden.

Pipeline parallelism

Stages of consecutive layers on different GPUs; a batch is cut into M micro-batches. With S stages, forward and backward times t_f and t_b, a step takes (M + S − 1)(t_f + t_b): the bubble fraction is (S − 1)/(M + S − 1). GPipe runs all forwards then all backwards, storing M micro-batches of activations; 1F1B interleaves them so stage 0 stores at most S. Interleaved 1F1B (several virtual stages per GPU) and zero-bubble schedules reduce the bubble further.

Mixture of experts

A router sends each token to its top-k experts; each expert accepts at most C = capacity factor × tokens/experts tokens, extra tokens are dropped (passed through the residual). Load-balancing losses keep experts evenly used.

🐍 Exercise 1: splitting a model
Implement column-parallel and row-parallel linear layers, a tensor-parallel MLP (one all-reduce) and tensor-parallel attention split by heads, and check them against unsplit layers; compute GPipe and 1F1B step time, bubble and stored activations and verify them with an event simulation of the schedules; route tokens to experts with top-1 routing and a capacity limit.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Why split W₁ by columns and W₂ by rows?
  2. 2. Bubble for S = 4 stages and M = 8 micro-batches?
  3. 3. 1F1B's advantage over GPipe is…

Level 3: Advanced

★★★★☆ Advanced⏱ 35 minGraduate level, with full derivations and proofs.

Choosing a layout

  • Fit first: parameters, gradients and optimizer states (16 bytes per parameter in mixed-precision Adam, C5.5; sharded further by ZeRO, C6.3) plus activations must fit in GPU memory.
  • Tensor parallelism inside a node: its all-reduces happen every layer; over NVLink (hundreds of GB/s) they are cheap, across nodes (tens of GB/s) they dominate.
  • Pipeline across nodes: only activations at stage boundaries cross the network; keep M ≫ S to make the bubble small.
  • Data parallelism for the rest: it scales throughput, its gradient all-reduce overlaps backward, and ZeRO removes its memory redundancy.
  • Long sequences: sequence/context parallelism splits the attention activations that grow with sequence length.

Typical large-model recipes: TP = 8 within each node, PP over a few nodes, DP (with ZeRO/FSDP) over the rest.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Tensor parallelism of 16 on 8-GPU nodes forces…
  2. 2. A 70B model in mixed-precision Adam needs roughly how much memory for model states alone?
  3. 3. Pipelines want many micro-batches because…

Level 4: Expert

★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.

Model parallelism, measured

Exercise 1. The tensor-parallel MLP and attention reproduce the unsplit layers exactly for 1, 2, 4 and 8 ranks, with one all-reduce each. For S = 4 stages and M = 8 micro-batches (forward 1, backward 2 time units), both schedules take 33 units (bubble 3/11 = 27%), confirmed by an event simulation; GPipe stores 8 micro-batches per stage, 1F1B 4; with M = 32 the bubble falls below 10%. Routing 1000 tokens to 8 experts (one expert favoured by the router) with capacity factor 1.25 (157 tokens per expert) drops 222 tokens; capacity factor 2.0 drops 129.

Exercise 2: searching 3D layouts. 64 A100s (8 per node, NVLink 300 GB/s inside a node, 25 GB/s per GPU between nodes), global batch 512 sequences of 2048 tokens, micro-batch 1, 1F1B, ZeRO-1, no compute/communication overlap, 50% kernel efficiency:

Model best (TP, PP, DP) time per step memory per GPU MFU notable alternatives
1.3B (2, 2, 16) 0.89 s 3.1 GB 43% pure DP: 0.95 s, fits easily
13B (2, 4, 8) 8.81 s 15.8 GB 45% pure DP: 9.91 s at 67 GB (gradient all-reduce of 26 GB)
70B (4, 4, 4) 44.1 s 40 GB 46% pure DP: needs 315 GB, impossible; TP = 16 across nodes: 144 s

Many layouts land within 1% of the best, because with 32–128 micro-batches per pipeline the bubble is small and NVLink makes intra-node tensor parallelism cheap; the decisive mistakes are layouts that don’t fit and tensor parallelism across nodes, whose per-layer all-reduces take 2.5× longer than the computation itself. The model ignores overlap and communication-hiding tricks, so absolute times are pessimistic.

🐍 Exercise 2 (research): searching 3D-parallel layouts
For GPT-style models of 1.3B, 13B and 70B parameters on 64 GPUs, enumerate TP × PP × DP layouts; compute memory per GPU (ZeRO-1 model states plus 1F1B activations) and time per step (compute with the pipeline bubble, tensor-parallel all-reduces over NVLink or the network, pipeline transfers, data-parallel gradient all-reduce); find the best feasible layout and compare with pure data parallelism and with tensor parallelism across nodes.

Model parallelism in AI, robotics and aerospace

  • Foundation models for language, Earth observation (multi-spectral vision transformers) and weather (global forecasting transformers) trained with 3D parallelism.
  • Long-context models for telemetry or genomics using sequence/context parallelism.
  • Mixture-of-experts models that scale parameters without proportionally scaling compute.
  • Inference: tensor parallelism across the GPUs of one server to serve a model too large for one GPU at low latency.

References

  1. M. Shoeybi et al. (2019). “Megatron-LM: training multi-billion parameter language models using model parallelism.” arXiv:1909.08053.
  2. Y. Huang et al. (2019). “GPipe: efficient training of giant neural networks using pipeline parallelism.” NeurIPS.
  3. D. Narayanan et al. (2021). “Efficient large-scale language model training on GPU clusters using Megatron-LM.” SC21 (1F1B, interleaving, 3D parallelism).
  4. W. Fedus, B. Zoph, N. Shazeer (2022). “Switch transformers: scaling to trillion parameter models with simple and efficient sparsity.” JMLR, 23.
  5. V. Korthikanti et al. (2023). “Reducing activation recomputation in large transformer models.” MLSys (sequence parallelism, activation memory).