Home
ArenaGraphSignalTopics
/Large Language Model Infrastructure: Building and Deploying Production AI Systems
Chapter 6 • Module 2 9 min breakdown +15 XP Module

Tensor Parallelism Explained: Splitting Attention and FFN Weights Across GPUs

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.

Interactive Blueprint
Rendering diagram...

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:

Interactive Blueprint
Rendering diagram...

2. Applying Tensor Parallelism to Transformer Blocks

A. The Attention Block

  1. 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.
  2. Local Attention Computation: Each GPU computes scaled dot-product attention on its assigned subset of heads using local KV cache blocks.
  3. Row-Parallel Output Projection: is row-partitioned.
  4. Synchronization: An All-Reduce sums the partial output vectors across the 4 GPUs.

B. The Feed-Forward (MLP / SwiGLU) Block

  1. Column-Parallel Gate & Up Projections: and are column-partitioned.
  2. Local Activation Function: Elementwise multiplication: happens completely locally.
  3. Row-Parallel Down Projection: is row-partitioned.
  4. Synchronization: A second All-Reduce sums 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-Reduce transfers 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

  1. Tensor Parallelism Shards Layers Internally: By partitioning matrices along column and row dimensions, TP distributes memory and compute across GPUs within the same node.
  2. Only 2 All-Reduces per Layer: The Column-to-Row pairing eliminates intermediate communications, requiring synchronization only after the Attention and FFN blocks.
  3. NVLink is Mandatory: Due to 160 All-Reduce operations per generated token, TP requires the sub-microsecond latency of NVLink interconnects.
  4. Even Head Partitioning: The number of attention heads and KV heads must always be divisible by the Tensor Parallel size.
Milestone Verification

Ready for the next lesson?

Mark this module complete to record verified progress and earn +15 XP toward your architect profile.