3 The DeepSeek breakthrough: Multi-Head Latent Attention (MLA)
The chapter opens by framing a central inference bottleneck in large language models: the KV cache is essential for avoiding repeated work, but it becomes extremely memory-hungry at long context lengths. Earlier efficiency tricks like MQA and GQA reduce storage by sharing information across heads, yet they weaken the expressive power that makes multi-head attention effective. DeepSeek’s answer is Multi-Head Latent Attention, which changes the problem from “how do we store fewer heads?” to “how do we store a compressed representation of the same information?” Instead of caching separate Keys and Values, the model caches a smaller latent representation that can later be expanded back into head-specific K and V vectors, preserving performance while cutting memory sharply.
The chapter then explains how this latent design works mathematically and operationally. Inputs are first projected into a compact cKV space, and only that compressed matrix is kept in memory during inference. When attention is needed, the latent cache is up-projected into full Key and Value matrices on the fly, allowing standard attention computations to proceed. The key engineering insight is the absorption trick: by rearranging matrix multiplications, the model can precombine fixed weight matrices so that the expensive historical state never needs to be stored as full K and V tensors. This means the cached state grows only with the lightweight latent vectors, while each attention head still reconstructs its own distinct Key and Value projections at computation time.
After establishing MLA, the chapter turns to positional information and why ordinary Rotary Positional Encoding cannot be combined with MLA in a naive way. Standard RoPE depends on token position, which breaks the absorption trick if applied directly to the compressed cache. To resolve this, DeepSeek uses a decoupled architecture with two parallel streams: a content path that uses pure MLA for semantic information, and a position path that applies RoPE only where needed. Their outputs are combined so the model gets both efficient caching and strong positional awareness. The chapter closes with a concrete PyTorch implementation and empirical comparisons showing that MLA matches or beats standard multi-head attention in quality while dramatically reducing cache memory, confirming that the architecture delivers both efficiency and expressive power.
Stage2 focuses on the highlighted component, Multi-Head Latent Attention (MLA) & Decoupled RoPE, the first major innovation in the core architecture.
The full architectural data flow of Multi-Head Latent Attention (MLA).
A step-by-step mathematical derivation of the Query, Key, and Value matrices in the MLA architecture.
The mathematical rearrangement of the attention score calculation in MLA.
The mathematical rearrangement for calculating the final output in MLA.
Computing the Query vector for the new token.
Computing the new Latent Vector cKV for "bright."
Updating the Latent KV Cache. The newly computed latent vector for "bright" is appended to the existing cache for the previous tokens.
Decompressing the updated latent cache into the full Key and Value matrices using the up-projection matrices.
The final attention calculation using the fresh Query and the decompressed Key and Value matrices.
Without positional information, the Transformer processes identical tokens in different positions in the same way, leading to identical context vectors and a loss of crucial contextual understanding.
The problem with Integer Positional Encoding. The large magnitude of the positional values completely dominates the small, semantic values of the token encoding, making it very difficult for the model to separate the two sources of information.
Binary Positional Encoding. The position number is converted to its binary representation, resulting in a vector of 0s and 1s that can be safely added to the token embedding without overwhelming its semantic information.
The oscillating frequency of bits in binary positional encodings. Lower indices (like the LSB) oscillate rapidly across positions, while higher indices (like the MSB) oscillate slowly.
The oscillation frequency for each bit index in an 8-bit binary representation. The frequency is highest for the lowest index and decreases exponentially for higher indices.
The oscillation patterns of each bit index across 128 positions. Index 1 (top) oscillates very quickly, while Index 8 (bottom) oscillates very slowly.
Sinusoidal Positional Encoding. The binary values are replaced with continuous values derived from sine and cosine functions, which are then added to the token embedding.
Visualization of sinusoidal positional encodings for a GPT-2-sized model. The plots show the encoding values for different indices (i) across 1024 positions.
A pair of sinusoidal positional encoding values can be viewed as the coordinates of a 2D vector v1 on a unit circle, defined by the angle θ.
The positional encoding vector v2 for a future position p + k is a simple rotation of the original vector v1 for position p.
The fundamental issue with sinusoidal positional encodings. The positional information is added directly to the token's semantic embedding, potentially altering or "polluting" its original meaning.
The attention score calculation. The influence of one token on another is quantified by the dot product of their respective Query and Key vectors. This core interaction is where relative position truly matters.
The core ideas of RoPE. We can avoid polluting the original vectors by rotating them instead of adding to them, and we can apply this rotation directly to the Query and Key vectors where positional information is most relevant.
The dimensions of a Query or Key vector are paired up for rotation.
Each pair of dimensions is treated as a 2D vector and rotated by a position-dependent angle θ.
The formula for the rotation angle θ.
The final step of the RoPE mechanism. The rotated 2D pairs are reassembled to create the final position-encoded Query or Key vector.
The Content Path of the Decoupled Architecture. This is a standard MLA block that produces content-based Query (Qc), Key (Kc), and Value (Vc) matrices.
The Position Path of the Decoupled Architecture. This new path creates specialized Query (Qr) and Key (Kr) matrices that are infused with Rotary Positional Encodings.
The final attention score calculation. The scores are the sum of the content-based scores and the position-based scores.
The final step. The combined attention scores are processed and multiplied by the content-based Value matrix (Vc) to produce the final output.
The computational flow for the content-based attention score.
This single multiplication gives us the query part we need for the content score.
Updating the Latent KV Cache. The new token's embedding is down-projected to create a new latent vector, which is then appended to the existing cache.
The computational flow for the position-based attention score.
A detailed flowchart of the DeepSeekAttention module. The input tensor x is processed through two parallel streams: the blue Content Path, which uses pure MLA for cache efficiency, and the purple Position Path, which applies RoPE. The two cached tensors, c_kv and k_r, are highlighted with key icons. The outputs are combined to produce the final context vector.
A comparison of the validation loss curves for the four attention mechanisms. The MHA and MLA models achieve a consistently lower final loss than their MQA and GQA counterparts, indicating superior learning. Both models were trained for 5,000 iterations.
Summary
- Multi-Head Latent Attention (MLA) breaks the performance-memory trade-off by caching a compressed latent representation of Keys and Values, which is then decompressed on-the-fly to reconstruct unique, full-sized matrices for each head during the attention calculation.
- Applying positional information by adding it to the initial token embeddings pollutes their semantic meaning before they are processed by the attention mechanism.
- Rotary Positional Encoding (RoPE) solves this by rotating the Query and Key vectors directly within the attention block, which injects positional information later in the process and preserves the vectors' original learned magnitudes.
- The efficiency of MLA depends on a mathematical "absorption trick" that requires position-agnostic weights, which is fundamentally incompatible with the position-dependent transformations of standard RoPE.
- The Decoupled RoPE architecture resolves this conflict by separating attention into two parallel streams: a content path that uses a cache-efficient MLA and a position path that handles RoPE. The final attention scores are the sum of the outputs from both paths.
Build a DeepSeek Model (From Scratch) ebook for free