Welcome back! In our last lesson, we explored Grouped-Query Attention (GQA), a technique that reduces the KV cache memory footprint by having groups of query heads share a single key-value head. We saw this as a practical compromise between the full expressiveness of Multi-Head Attention (MHA) and the extreme compression of Multi-Query Attention (MQA).
The core problem remains the same: the KV cache is a major memory bottleneck, limiting the context length of large models. Today, we will investigate a more advanced and powerful solution pioneered by the DeepSeek team: Multi-head Latent Attention (MLA). Instead of just reducing the number of key-value heads, MLA fundamentally changes how they are stored.
Our learning outcome is to explain Multi-head Latent Attention (MLA) and its use of low-rank compression for the KV cache. We'll deconstruct how MLA achieves massive memory savings—reportedly a 57x reduction for DeepSeek's models—while maintaining, and sometimes even improving, model performance.
1. The Core Idea: Compressing the Cache Itself
In our previous lessons on MQA and GQA, the strategy was to reduce the number of distinct Key and Value vectors we store. MLA takes a different approach. The central question it asks is:
What if we could compress the Key and Value representations themselves into a much smaller, combined vector before caching?
This is the essence of MLA. It employs a technique known as low-rank compression. Instead of caching the full, separate Key and Value vectors for each head (or group of heads), MLA projects the input's hidden state down into a single, low-dimensional latent vector. This compressed latent vector is the only thing that gets stored in the KV cache.
This diagram provides an excellent high-level view of how MLA's architecture differs from the others we've discussed.

To get a concise and well-animated overview of this core idea, let's watch a short segment from Welch Labs.
How DeepSeek Rewrote the Transformer [MLA]
This video, 'How DeepSeek Rewrote the Transformer [MLA]', clearly explains the motivation and the main insight behind MLA.
Please watch from 13:25 to 14:34. Focus on how the video describes projecting the input into a 'compressed latent space' that is shared across heads, and then projecting it back up.
2. The MLA Mechanism: Down-Projection and On-the-Fly Up-Projection
As the video explained, MLA is a two-step process that cleverly manipulates the attention computation to work with this compressed latent vector.
-
Down-Projection: The input hidden state is projected into a low-dimensional latent vector, let's call it , using a learned weight matrix . This matrix is shared across all attention heads in the layer.
This much smaller is what gets stored in the KV cache.
-
Up-Projection: During the actual attention calculation, this cached latent vector is projected back up to generate the final Key () and Value () vectors for each head . This is done using separate, per-head weight matrices, and .
This is the crucial innovation. By using unique up-projection matrices for each head, MLA allows every head to learn its own interpretation of the shared latent information. This preserves the representational diversity and performance of MHA, a capability that MQA and GQA sacrifice by forcing heads to share identical K/V vectors.

To dive deeper into this workflow and understand the mathematical elegance behind it, we'll watch a more detailed explanation.
Multi-Head Latent Attention From Scratch | One of the major DeepSeek innovation
The video 'Multi-Head Latent Attention From Scratch' by Vizuara provides a step-by-step walkthrough of the MLA mechanism, including an important implementation detail called the 'absorption trick'.
Please watch from 35:37 to 46:38. The first part (until 38:42) introduces the concept of using a single, smaller latent matrix. The second part explains the full workflow, including the down- and up-projections and the 'absorption trick' that makes the computation efficient during inference.
The "Absorption Trick"
As the video highlighted, a naive implementation would require expanding the entire latent cache at every step, which would be computationally expensive. The "absorption trick" is a clever rearrangement of the matrix multiplications. It combines the query projection with the key's up-projection, allowing the attention scores to be computed directly between the query and the compressed latent cache. This makes MLA highly efficient and compatible with optimized kernels like FlashAttention, without adding extra compute during inference.
3. A Deeper Dive and Quantifying the Savings
Now that we have the conceptual and mechanistic understanding, let's solidify it with a detailed reading and a concrete calculation of the memory savings.
Understanding DeepSeek's Multi-Head Latent Attention (MLA)
The article 'Understanding DeepSeek's Multi-Head Latent Attention (MLA)' provides a superb technical breakdown, including PyTorch code and a summary table.
First, read the section titled 'Multi-Head Latent Attention (MLA)', which explains the core idea and provides a PyTorch implementation. Note how it differs from the GQA code you saw in the last lesson. Then, review the summary table in the 'Summary' section. This table provides a fantastic side-by-side comparison of the KV cache sizes for MHA, GQA, MQA, and MLA.
Let's use the numbers from the article to make the savings tangible. For a model at the scale of DeepSeek-V2 with a 128,000 token context:
- MHA Cache: Would require ~512 GB. This is unfeasible on a single GPU.
- GQA Cache (8 groups): Reduces this to a more manageable 32 GB.
- MLA Cache (latent dim 512): Slashes this to just ~8 GB.
The formula for the MLA cache size is:
Where:
- = Batch size
- = Sequence length
- = Number of layers
- = Dimension of the latent cache
Compared to MHA's 2 \times n_h \times d_h factor inside the formula, MLA's d_c is vastly smaller. This is the source of the ~57x memory reduction that DeepSeek reports.
Conclusion
In this lesson, we have deconstructed Multi-head Latent Attention, a key architectural innovation that makes extremely long context windows practical.
Key Takeaways:
- MLA tackles the KV cache bottleneck by applying low-rank compression. It projects hidden states into a single, shared, low-dimensional latent vector that is then cached.
- This latent vector is projected back up on-the-fly for each attention head using unique weights, preserving the model's representational capacity and performance, unlike MQA or GQA which share heads.
- The result is a dramatic reduction in KV cache memory—up to ~57x in DeepSeek's case—without a corresponding drop in model quality. This makes MLA a true improvement to the Transformer architecture.
Preview of the Next Lesson:
We've now seen two powerful ways to optimize the attention mechanism: sharing K/V heads (GQA) and compressing the K/V representations (MLA). The other major computational component of a Transformer block is the Feed-Forward Network (FFN). In our next lesson, we will begin the "Scaling with Mixture-of-Experts" module and explore how Mixture-of-Experts (MoE) layers allow models to scale their parameter counts into the trillions without a proportional increase in compute cost per token.