Tensor Parallelism
Splitting individual layers across GPUs: column-parallel and row-parallel linear layers, Megatron-LM style partitioning, communication patterns (all-reduce), and scaling efficiency analysis.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- Column-Parallel Linear Layer
- Row-Parallel Linear Layer
- Megatron-LM Attention Splitting
- Megatron-LM FFN Splitting
- Communication Analysis
- Scaling Efficiency
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Derive column-parallel and row-parallel matrix multiplication.
- Explain how Megatron-LM splits attention heads across GPUs.
- Compute the communication volume per layer.
- Analyze why tensor parallelism is limited to within a node.
- Compare TP scaling efficiency at 2, 4, 8 GPUs.
Notation
- — tensor parallel degree (number of GPUs)
- — partition of matrix
- AR — all-reduce operation
- AG — all-gather operation
Core Intuition
When a single layer is too large for one GPU (or we want faster per-step latency), we split the weight matrices across GPUs. Each GPU computes part of the matrix multiplication, then they communicate to combine results. The key insight: by carefully choosing how to split (column-wise or row-wise), we can minimize communication to just 2 all-reduces per transformer layer.
Tensor Parallelism
Column-Parallel Linear Layer
Split weight along columns: .
Each GPU computes: .
Input: Replicated across all GPUs (each has full ). Output: Partitioned (each GPU has columns ).
Communication: None in forward (if input is already replicated). All-gather needed if output must be full.
Row-Parallel Linear Layer
Split weight along rows: .
Input must be partitioned: .
Each GPU computes: .
Full output: → all-reduce.
Communication: One all-reduce to sum partial results.
Megatron-LM Attention Splitting
Natural partition: Split attention heads across GPUs. Each GPU handles heads.
Per-GPU computation:
- Q, K, V projections for heads: (column-parallel).
- Attention computation: local (no cross-GPU communication).
- Output projection: row-parallel (all-reduce to combine).
Result: Only 1 all-reduce needed for the attention block.
Megatron-LM FFN Splitting
For FFN: .
Split:
- First linear : column-parallel (split output features). GeLU applied locally.
- Second linear : row-parallel (takes partitioned input, all-reduce output).
Result: 1 all-reduce for the FFN block.
Total per transformer layer: 2 all-reduces in forward, 2 in backward = 4 all-reduces.
Communication Analysis
All-reduce volume per operation: bytes, where is the tensor size.
For one transformer layer with sequence length , hidden dim :
- Attention all-reduce: bytes (FP16).
- FFN all-reduce: bytes.
- Total per layer: bytes (for large ).
Bandwidth requirement: For latency per layer: need bandwidth .
Scaling Efficiency
Computation scales linearly: Each GPU does of the work.
Communication overhead: All-reduce time depends on interconnect bandwidth.
Efficiency: .
For NVLink (900 GB/s bidirectional within a node):
- TP=2:
- TP=4:
- TP=8:
For cross-node (InfiniBand, 200 GB/s):
- TP=2: — too slow!
Rule: Tensor parallelism only within a single node (connected by NVLink).
Common Pitfalls
Pitfall 1. Using tensor parallelism across nodes. Cross-node bandwidth is 4-5x lower than NVLink; communication dominates compute.
Pitfall 2. Non-uniform splitting when is not divisible by . Requires padding or uneven distribution, causing load imbalance.
Pitfall 3. Forgetting the dropout synchronization. Random dropout masks must be synchronized across TP ranks to ensure correctness.
Summary
- Column-parallel: Split output features; input replicated, output partitioned.
- Row-parallel: Split input features; input partitioned, output all-reduced.
- Megatron-LM: 2 all-reduces per layer (1 attention + 1 FFN).
- Best within a node: NVLink provides sufficient bandwidth; cross-node is too slow.
- Typical TP degree: 2, 4, or 8 (matching GPUs per node).
Exercises
Exercise 1. For TP=4, , : compute the all-reduce volume per transformer layer.
Exercise 2. Derive the efficiency for TP=8 with NVLink bandwidth 450 GB/s and per-layer compute time 2ms.
Exercise 3. Show that Megatron-LM's attention splitting produces mathematically identical results to single-GPU computation.
Exercise 4. Compute the maximum TP degree for a model with attention heads.
Exercise 5. Compare the end-to-end training throughput for TP=4+DP=8 vs TP=8+DP=4 on 32 GPUs.