How DeepSeek Rewrote the Transformer
In January 2025, DeepSeek shocked the world with R1, a model that matched the best Western LLMs on a fraction of the compute. More surprising, DeepSeek published the weights, the inference code, and a technical report roughly monthly through 2024. This video is about one of those reports, from mid-2024, and the idea strikes at the Transformer's core. It is called multi-head latent attention (MLA), and it shrinks a key bottleneck, the key-value cache, by a factor of 57, letting the model generate text more than six times faster than a traditional Transformer.
Here is how attention runs. When a model generates text, it produces one token at a time, each depending on all the ones before it. Attention is the mechanism that moves information between token positions, using matrices called attention patterns. GPT-2 small uses 12 attention heads per layer across 12 layers, making 144 patterns. DeepSeek R1 has 128 heads per layer across 61 layers, making 7,808. For the nine-token example, every pattern is a 9 by 9 matrix. One head links "American" to "flag"; another pulls "flag", "red", and "white" together to predict "blue".
Start with the input matrix X, one row per token. Multiply X by learned weights WQ and WK to get the query and key matrices. Similar queries and keys mean related tokens. The dot products of all query-key pairs, masked to hide the future and normalized with a softmax, give the attention pattern. A third weight matrix gives the values, and the pattern times the values produces the output. Every head does this with different weights, so heads specialize.
The problem is scale. An attention pattern is as wide as the input, so its entries grow with the square of the token count. At 100,000 tokens, about the first Harry Potter book, computing every pair is out of reach. The standard fix is KV caching. When a new token arrives, the earlier rows are unchanged, so you store the keys and values and only compute the new row. Compute then grows linearly, but the cost moves to memory. For R1 at 100,000 tokens, that means 4 megabytes per token, or 400 gigabytes of memory reads for every new token.
Attention patterns per modelData from the sources listed below.
The obvious fix is sharing keys and values across heads. Multi-query attention shares one key and value matrix across all heads, cutting the cache by a factor of 128 for R1, but it costs performance, because heads can no longer specialize. Grouped-query attention is gentler: groups of heads share, and Meta's Llama 3 uses groups of eight, cutting the cache by a factor of eight, still with a performance hit.
Multi-head latent attention gets the best of both. It projects the input into a compressed latent space shared across all heads, then back up to keys and values with weights unique to each head. The model learns its own compression, and the heads keep their specialization. The clever part is the linear algebra: the up-projection weights can be absorbed into the query computation and the final output computation. Those weights are fixed after training, so they are computed once, and there is no extra work during inference. The cache no longer depends on the number of heads, only on the shared latent space.
With traditional attention, R1 needs 4 megabytes of KV cache per token. Grouped-query attention with groups of eight cuts that to 500 kilobytes. MLA cuts it to 70 kilobytes, a 57x reduction, while actually improving performance. The result is a model that generates tokens more than six times faster than a vanilla Transformer. A genuine improvement, not a trade-off.
Try this
bashcurl -s https://arxiv.org/abs/2405.04434 pip install transformers python -c "import transformers; print(transformers.__version__)"
Sources
- Source: How DeepSeek Rewrote the Transformer [MLA] - Welch Labs
- DeepSeek-V2: A Strong, Economical, and Efficient Mixture-of-Experts Language Model, arXiv:2405.04434 - https://arxiv.org/abs/2405.04434
