Why compress the KV cache
When a transformer generates text it keeps the key and value of every earlier token in every layer, so that each new token can attend to them without recomputing. This KV cache grows with the context and has to be read in full for every new token, so its size limits how long the context and how large the batch can be. Multi-query and grouped-query attention shrink it by letting several query heads share one key and value head. Multi-head Latent Attention (MLA) instead caches one small latent vector per token, from which every head rebuilds its own keys and values.
This page records DeepSeek-V2-Lite through all 27 layers on The capital of France is (6 tokens, counting the start token). Its most likely next token is Paris, with probability 0.2610.
The latent and its up-projections
For a normalised hidden state (2048 dimensions), the Latent KV · cache card makes two things that are stored: a 512-dimensional latent , compressed by and normalised, and a 64-dimensional rotary key , rotated by RoPE. Every head then expands the same latent into its own 128-dimensional content key and value:
The Query card is an ordinary projection in this model: maps 2048 to 3072, 16 heads of 128 content plus 64 rotary coordinates. The larger DeepSeek-V2 also compresses the query through a latent, to save activation memory in training; V2-Lite does not. Keys are 192-dimensional (128 + 64), values 128-dimensional, and values never rotate.
Decoupled RoPE
Rotary position embedding rotates queries and keys by angles set by their positions. Applied to the content keys, it would put a position-dependent rotation between and the query, and the trick in the next section would no longer work. MLA therefore keeps position in a separate, small part: each head's query has its own 64 rotary coordinates, the key's 64 rotary coordinates are shared by all heads, and the 128 content coordinates skip RoPE (the dashed bypass in the attention card). The score adds the two parts:
This is the Scaling · Mask stage of the attention matrix. The scale is not : the checkpoint stretches RoPE from 4,096 positions with YaRN (factor 40), and YaRN's attention factor, squared, raises the scale to the recorded 0.1147.
Absorbing the up-projections
The page computes keys and values as the model's code does, by expanding the latent. An inference engine need not. Because the content key is linear in the latent, the up-projection can move to the query side, and the value up-projection can merge into the output projection:
Attention can then run directly on the cached latents, and no head's full keys or values are ever materialised. The decoupled rotary key is what keeps this possible with RoPE.
How small the cache is
Each layer caches the normalised latent and the rotated key: 512 + 64 = 576 numbers per token, shared by every head. Over the 27 layers that is 15,552 numbers per token. Caching the keys and values of these 16 heads directly would take 3,072 + 2,048 numbers per token per layer. The DeepSeek-V2 report says MLA reduced the KV cache by 93.3 % compared with DeepSeek 67B, while performing better than multi-head attention in its ablations.
The rest of the block: DeepSeekMoE
Layer 1 has an ordinary dense SwiGLU MLP. Layers 2 to 27 are mixtures of experts: the router takes a softmax over 64 small routed experts and runs the 6 with the highest probabilities, weighted by those probabilities without renormalising them, so the weights need not sum to 1. Two shared experts run for every token with weight 1, alongside the routed ones. In total the model has 15,706,484,224 parameters, of which about 2.4B are used for each token. The MoE tab shows a router that renormalises its top-8 weights instead.
Where MLA came from
Shazeer (2019) proposed multi-query attention, one key and value head for all query heads, to speed up decoding; Ainslie et al. (2023) generalised it to grouped-query attention, a few key and value heads each shared by a group. DeepSeek-V2 (2024) introduced MLA together with the DeepSeekMoE layer, and DeepSeek-V3 kept both at a larger scale. The Attention explainer's MLA tab looks at the same mechanism on its own, and the DeltaA tab shows Kimi K3, which combines MLA with linear-attention layers.
Papers
- DeepSeek-V2: A Strong, Economical, and Efficient Mixture-of-Experts Language Model Introduces Multi-head Latent Attention, with decoupled RoPE and the absorbed form, and the V2 models including V2-Lite, which this page records.
- Fast Transformer Decoding: One Write-Head is All You Need Multi-query attention: all query heads share one key and value head, so the cache shrinks by the number of heads.
- GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints Grouped-query attention, the common middle ground between multi-head and multi-query attention that MLA is compared with.
- DeepSeekMoE: Towards Ultimate Expert Specialization in Mixture-of-Experts Language Models Fine-grained routed experts plus always-on shared experts, the MLP of layers 2 to 27.
- RoFormer: Enhanced Transformer with Rotary Position Embedding Rotary position embedding, which MLA confines to a 64-dimensional part of each query and key.
- YaRN: Efficient Context Window Extension of Large Language Models The RoPE extension the checkpoint uses; its attention factor sets the recorded score scale.
- DeepSeek-V3 Technical Report Keeps MLA and DeepSeekMoE at 671B parameters, with a low-rank query in every layer.