In our last lesson, we explored pipeline parallelism, an effective strategy for splitting a model between its layers to train models that are too large for a single GPU. We treated each set of layers as a stage in an assembly line. However, this raises a critical question: what happens if a single stage—or even a single layer within that stage—is itself too large to fit into one GPU's memory? This is a common problem with modern Transformers, where the weight matrices in the MLP and attention blocks can be enormous.
This lesson introduces tensor parallelism, the technique designed to solve this very problem. Instead of splitting the model between layers (inter-layer), tensor parallelism splits the model within layers (intra-layer). It achieves this by partitioning the individual tensors—specifically, the large weight matrices—and distributing the mathematical operations across multiple devices. Your background in distributed systems and DevOps will be valuable here, as we'll see that this technique introduces a different pattern of communication with its own unique trade-offs.
Our goal is to describe tensor parallelism and explain how it enables training massive model layers by splitting individual computations. We will dissect how this is applied to the two core components of a Transformer block: the Multi-Layer Perceptron (MLP) and the self-attention mechanism.
From Inter-Layer to Intra-Layer Parallelism
First, let's firmly distinguish between the two types of model parallelism we've discussed. Pipeline parallelism performs an inter-layer split, where different GPUs are responsible for different, sequential chunks of the model. Tensor parallelism performs an intra-layer split, where multiple GPUs collaborate to execute a single, large operation, like a matrix multiplication.
Ultimate Guide To Scaling ML Models - Megatron-LM | ZeRO | DeepSpeed | Mixed Precision
The video "Ultimate Guide To Scaling ML Models" provides a clear visual distinction between these two approaches.
Please watch the segment from this clip. The presenter contrasts the "inter-layer" splitting of pipeline parallelism with the "intra-layer" or "vertical" splitting of tensor parallelism, setting the stage for the techniques we will now explore in detail.
This intra-layer approach was famously detailed in the Megatron-LM paper, and as such, is often called "Megatron-style" tensor parallelism. Let's dive into how it works.
Parallelizing the MLP Block: A Tale of Two GEMMs
The MLP block in a standard Transformer consists of two linear layers (which are primarily General Matrix Multiplies, or GEMMs) with a non-linear activation function, like GeLU, in between. We can represent this as:
Here, is the input activation tensor, and and are the large weight matrices. Our goal is to split the computations involving and across multiple GPUs.
Let's consider a simple case of splitting across two GPUs. The key challenge lies in choosing a splitting strategy that preserves the mathematical correctness of the operation while minimizing communication between the GPUs.

A high-level view of splitting a two-layer MLP. The first weight matrix A is split by columns, while the second matrix B is split by rows. This specific pattern is crucial for efficiency.
The article "An illustrated deep-dive into Megatron-style tensor parallelism" provides an exceptional step-by-step derivation of this process. It's one of the clearest explanations available.
An illustrated deep-dive into Megatron-style tensor ...
This article explains the core logic behind partitioning the MLP block. It contrasts a naive approach with the optimized one used in Megatron.
Please read the sections titled "MLP blocks", "1st GEMM of the MLP forward pass: the bad option", and "1st GEMM of the MLP forward pass: the good option". You can find these by starting from this text. As you read, focus on this critical question: Why is splitting the first matrix A column-wise superior to splitting it row-wise? Pay special attention to the interaction with the non-linear GeLU function.
As you've just read, the choice of how to split the first matrix, , is dictated by the non-linear activation function.
- Row-wise split (the "bad" option): If we split by rows, so , the computation becomes . Because the GeLU function is non-linear, we have . This means we would be forced to sum the partial products and before applying GeLU. This summation is a communication-heavy
all-reduceoperation, and needing it so early negates much of the benefit of parallelism. - Column-wise split (the "good" option): By splitting by columns, so , the computation becomes . Here, each GPU can compute its part of the operation and apply GeLU independently, with no communication required. This is the key insight of Megatron-style parallelism.
Now, the output of this first step, , is a sharded tensor. This sharded tensor becomes the input to the second linear layer, . To make the matrix multiplication dimensions compatible, we must now split the second weight matrix, , row-wise: .
The full computation on the two GPUs is:
- GPU 1 computes
- GPU 2 computes
The final result should be . At this stage, communication is unavoidable. The partial results and must be summed together across the devices using an all-reduce operation.
The brilliant stroke of this design is that it requires only one synchronization point (the final all-reduce) in the forward pass of the entire two-layer MLP block.
The following diagram from the original Megatron-LM paper illustrates this flow. The operator represents a forward-pass all-reduce, while represents its conjugate for the backward pass (an all-reduce of gradients).
This figure from the Megatron-LM paper shows the data flow for both (a) MLP and (b) Self-Attention blocks under tensor parallelism. Notice the column-wise split for the first matrix (XA) and row-wise for the second (YB), with a single synchronization (g) at the end of the block.
To see this logic animated, let's turn back to Aleksa Gordić's video.
Ultimate Guide To Scaling ML Models - Megatron-LM | ZeRO | DeepSpeed | Mixed Precision
This part of the video walks through the mathematics of splitting the MLP block, reinforcing the concepts from the article with helpful drawings.
Please watch this detailed breakdown from the video. The presenter clearly explains the column-parallel and row-parallel strategy and why it's structured this way to delay synchronization.
Parallelizing the Self-Attention Block
The same principle can be applied to the self-attention block. In fact, it's even more natural here because the multi-head attention mechanism is already composed of independent computations—one for each head.
The core idea is to split the attention heads across the available GPUs.
- The large projection matrices for Query, Key, and Value () are split column-wise across the
num_headsdimension. - Each GPU now holds the weights for a subset of the attention heads. It can independently compute the Q, K, and V vectors for its heads, perform the scaled dot-product attention, and produce an output for its subset of heads. All of this happens with zero communication.
- The final output projection layer, which combines the results from all heads, has its weight matrix () split row-wise.
- Each GPU computes a partial result by multiplying its attention head outputs with its shard of the matrix.
- Finally, just as with the MLP block, an
all-reduceoperation sums these partial results to produce the final output of the attention block.
This elegant parallelization results in just one all-reduce in the forward pass and one in the backward pass for the entire multi-head attention block.
An illustrated deep-dive into Megatron-style tensor ...
This article also provides a clear, diagrammed explanation for the attention mechanism.
Please read the section "Attention layers", which starts here.
The Cost of Communication: System-Level Tradeoffs
We have established a powerful technique, but it doesn't come for free. Tensor parallelism introduces high-frequency communication. Unlike data parallelism, which requires one all-reduce per batch, or pipeline parallelism, which involves passing activations between stages, tensor parallelism requires all-reduce operations inside every Transformer layer.
This has profound implications for hardware and system architecture, a topic where your DevOps expertise provides a unique lens. These all-reduce operations are extremely sensitive to network latency and bandwidth.
- Intra-node communication: Communication between GPUs within the same physical server is often handled by ultra-fast, low-latency interconnects like NVIDIA's NVLink.
- Inter-node communication: Communication between different servers across a network (e.g., via Infiniband or Ethernet) is significantly slower.
Because of this, tensor parallelism is almost exclusively used for GPUs within a single node. The communication overhead would be prohibitively expensive across nodes. This creates a practical limit on the tensor parallel size, typically 8 or 16 GPUs, corresponding to the number of GPUs in a high-density server.
The following video provides an expert perspective on these tradeoffs, backed by empirical data.
Training LLMs at Scale - Deepak Narayanan | Stanford MLSys #83
This seminar from Stanford by Deepak Narayanan, one of the authors of the Megatron papers, delves into the practical performance implications of combining different parallelism strategies.
First, watch the brief introduction to tensor parallelism from the beginning. Next, focus on the discussion of communication overhead from this section. The speaker explains why the all-reduce in tensor parallelism is so expensive, especially cross-node, and sets up a tradeoff between tensor and pipeline parallelism. Finally, watch the analysis of the empirical results from this part. The graph clearly shows a "happy middle ground" for tensor parallel size. The key takeaway is that performance drops if the tensor parallel size is too small (due to pipeline bubbles) or too large (forcing expensive cross-node communication). This is because the optimal configuration keeps the high-frequency tensor parallel communication within a server using NVLink.
This analysis reveals that scaling LLMs is not just about using one technique, but about composing them intelligently. A typical setup for a massive model might use tensor parallelism within each node, pipeline parallelism across nodes, and data parallelism across replicas of the entire pipeline.
Conclusion
Today we've completed our trio of large-scale training strategies by adding tensor parallelism to our toolkit. While data parallelism replicates the model and pipeline parallelism splits it between layers, tensor parallelism gives us the fine-grained ability to split the computations within a single, massive layer.
Here are the key takeaways:
- Tensor Parallelism splits a model's computations intra-layer, partitioning large weight matrices (tensors) and their associated GEMMs across multiple devices.
- It is essential when a single model layer is too large to fit in one GPU's memory.
- The partitioning scheme is carefully designed to minimize communication, typically involving a column-wise split followed by a row-wise split in MLP and attention blocks to delay synchronization.
- This introduces high-frequency
all-reducecommunication, making it highly sensitive to network performance and best suited for fast intra-node interconnects like NVLink. - Effective scaling often involves a hybrid approach, combining data, pipeline, and tensor parallelism, each playing to its strengths at different levels of the hardware topology.
With these three parallelism techniques, we now have a comprehensive understanding of the engineering required to train models at the scale of billions or even trillions of parameters. We know the mechanisms for scaling. In our next module, we will explore the principles that guide these scaling efforts: the Scaling Laws that describe the predictable relationships between model performance, size, data, and compute.