In 2019, NVIDIA researchers published Megatron-LM (Shoeybi et al.), introducing Tensor Parallelism (TP)—the gold standard technique for sharding individual Large Language Model layers across multiple GPUs within a high-speed NVLink domain.
Rather than placing entire layers on different GPUs (which introduces pipeline latency bubbles), Tensor Parallelism splits the internal weight matrices of every Multi-Head Attention and Feed-Forward Network block across GPUs, computing matrix multiplications in parallel with only two All-Reduce communication operations per transformer layer.
1. Matrix Partitioning: Column-Parallel vs Row-Parallel Linear Layers
The mathematical genius of Megatron-LM is pairing a Column-Parallel linear layer directly with a Row-Parallel linear layer. This eliminates intermediate communication steps and requires an All-Reduce only at the very end of the sub-block.
A. Column-Parallel Linear Layer
In a column-parallel layer, the weight matrix is split along its columns across GPUs:
Each GPU holds a duplicate of input and computes:
Zero inter-GPU communication is required. The output is naturally split along its feature columns.
B. Row-Parallel Linear Layer
In a row-parallel layer, the weight matrix is split along its rows:
Each GPU takes its local input slice from the preceding column-parallel layer and computes:
To reconstruct the final mathematical product , the GPUs execute an All-Reduce (Sum) collective operation:
2. Applying Tensor Parallelism to Transformer Blocks
A. The Attention Block
- Column-Parallel Projections: are column-partitioned.
- If the model has 64 attention heads and , each GPU computes attention for 16 heads independently in local VRAM.
- Local Attention Computation: Each GPU computes scaled dot-product attention on its assigned subset of heads using local KV cache blocks.
- Row-Parallel Output Projection: is row-partitioned.
- Synchronization: An
All-Reducesums the partial output vectors across the 4 GPUs.
B. The Feed-Forward (MLP / SwiGLU) Block
- Column-Parallel Gate & Up Projections: and are column-partitioned.
- Local Activation Function: Elementwise multiplication: happens completely locally.
- Row-Parallel Down Projection: is row-partitioned.
- Synchronization: A second
All-Reducesums the FFN output vectors.
3. Communication Cost per Transformer Layer
For a model with hidden dimension , batch size , and sequence length :
For with FP16 () and :
- Each
All-Reducetransfers per token across NVLink. - Over NVLink 4 (), this takes less than 5 microseconds per layer, introducing practically zero latency overhead!
4. Production Failure Modes: Attention Head Divisibility
Failure Mode: Crash on Startup with Assertion Error on Head Counts
- Symptom:
RuntimeError: Number of attention heads (64) must be divisible by tensor_parallel_size (5). - Root Cause: Tensor Parallelism shards attention heads evenly across GPUs. If or is not evenly divisible by , the model cannot partition its heads.
- Resolution: must always be a power of two () that evenly divides both and . For Llama 3 70B (), the valid values are and .
5. Summary & Key Takeaways
- Tensor Parallelism Shards Layers Internally: By partitioning matrices along column and row dimensions, TP distributes memory and compute across GPUs within the same node.
- Only 2 All-Reduces per Layer: The Column-to-Row pairing eliminates intermediate communications, requiring synchronization only after the Attention and FFN blocks.
- NVLink is Mandatory: Due to 160 All-Reduce operations per generated token, TP requires the sub-microsecond latency of NVLink interconnects.
- Even Head Partitioning: The number of attention heads and KV heads must always be divisible by the Tensor Parallel size.