Multi-head latent attention
×Queries, keys and values come from compressed latents; every query attends to the current and all earlier positions.
Why
Latent. Standard attention caches a full key and value for every head at every position, and the authors of latent attention call that cache a large bottleneck that limits batch size and sequence length when a model is served. Their design caches one latent per position and rebuilds keys and values from it. Kimi K2.5’s latent has 512 features; its pinned implementation caches the rebuilt keys and values of all 64 heads (modeling_deepseek.py 803–810). DeepSeek-V2 §2.1.1–2.1.2
Kimi K2.5 keeps the latent attention of Kimi K2, which took it from DeepSeek-V2; Kimi K3 keeps it in its global-attention layers. Kimi K3 report §2.1.2
64 heads. DeepSeek-V3 sets about twice as many attention heads as layers, 128 heads for 61 layers, to make better use of memory bandwidth. Kimi K2’s authors say that at long contexts doubling the heads costs a great deal at inference: at 128K positions, going from 64 heads to 128 adds 83% to the inference FLOPs. In their experiments twice the heads lowered the validation loss by only 0.5% to 1.2%, so they choose 64. Kimi K2.5 keeps Kimi K2’s choice. Kimi K2 report §2.3 and Table 2, pp. 6–7
All 30 steps
- in
- [B batch, T positions, 7168 d] [T positions, 32 pairs] [T queries, T keys]
- out
- [B batch, T positions, 7168 d]
Parameters101,124,096 × 60 layers
101,124,096 parameters per instance, across its learned components
| Component | Parameters |
|---|---|
| Query compression | 11,011,584 |
| Key–value compression | 3,670,528 |
| Query heads | 18,874,368 |
| Key heads | 4,653,056 |
| Value heads | 4,194,304 |
| Causal attention | 58,720,256 |
| Per-instance total | 101,124,096 |
One in each of the 60 MoE layers, each with its own weights.
KV cache
KV cache per token · all layers
Per layer and position: 64 keys of 192 and 64 values of 128 features in bfloat16; × 61 layers = 2,498,560 bytes. The 512-feature latent and 64-feature rotary key would take 70,272 bytes, 36× less.
The flash-attention path first pads each value to 192 features: 64 × (192 + 192) × 2 × 61 = 2,998,272 bytes.
modeling_deepseek.py 807–810, 938–940config.json 39Sources
In codeself.self_attn
Direct evidence
MLA owns a query down-projection, query RMSNorm and up-projection, a joint key–value down-projection that also emits the shared rotary key, a latent RMSNorm, a key–value up-projection and an output projection.
modeling_deepseek.py 665–691 ↗ · moonshotai/Kimi-K2.5 @ 4d01dfe0332d
Excerpt
self.q_a_proj = nn.Linear(self.hidden_size,
config.q_lora_rank,
bias=config.attention_bias)
self.q_a_layernorm = DeepseekV3RMSNorm(config.q_lora_rank)
self.q_b_proj = nn.Linear(config.q_lora_rank,
self.num_heads * self.q_head_dim,
bias=False)
self.kv_a_proj_with_mqa = nn.Linear(
self.hidden_size,
config.kv_lora_rank + config.qk_rope_head_dim,
bias=config.attention_bias,
)
self.kv_a_layernorm = DeepseekV3RMSNorm(config.kv_lora_rank)
self.kv_b_proj = nn.Linear(
config.kv_lora_rank,
self.num_heads *
(self.q_head_dim - self.qk_rope_head_dim + self.v_head_dim),
bias=False,
)
self.o_proj = nn.Linear(
self.num_heads * self.v_head_dim,
self.hidden_size,
bias=config.attention_bias,
)
self._init_rope()The rotary parts are rotated; each head’s query and key join their non-rotated and rotated parts, the key’s rotated part being the shared rotary key; scores are scaled, masked by the causal mask, normalized and used to mix values.
modeling_deepseek.py 794–836 ↗ · moonshotai/Kimi-K2.5 @ 4d01dfe0332d
Excerpt
cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
q_pe, k_pe = apply_rotary_pos_emb(q_pe, k_pe, cos, sin, position_ids)
query_states = k_pe.new_empty(bsz, self.num_heads, q_len,
self.q_head_dim)
query_states[:, :, :, :self.qk_nope_head_dim] = q_nope
query_states[:, :, :, self.qk_nope_head_dim:] = q_pe
key_states = k_pe.new_empty(bsz, self.num_heads, q_len,
self.q_head_dim)
key_states[:, :, :, :self.qk_nope_head_dim] = k_nope
key_states[:, :, :, self.qk_nope_head_dim:] = k_pe
if past_key_value is not None:
cache_kwargs = {"sin": sin, "cos": cos} # Specific to RoPE models
key_states, value_states = past_key_value.update(
key_states, value_states, self.layer_idx, cache_kwargs)
attn_weights = (
torch.matmul(query_states, key_states.transpose(2, 3)) *
self.softmax_scale)
if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
raise ValueError(
f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is"
f" {attn_weights.size()}")
assert attention_mask is not None
if attention_mask is not None:
if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
raise ValueError(
f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
)
attn_weights = attn_weights + attention_mask
# upcast attention to fp32
attn_weights = nn.functional.softmax(attn_weights,
dim=-1,
dtype=torch.float32).to(
query_states.dtype)
attn_weights = nn.functional.dropout(attn_weights,
p=self.attention_dropout,
training=self.training)
attn_output = torch.matmul(attn_weights, value_states)Head outputs are flattened and projected back to the model width.
modeling_deepseek.py 843–848 ↗ · moonshotai/Kimi-K2.5 @ 4d01dfe0332d
Excerpt
attn_output = attn_output.transpose(1, 2).contiguous()
attn_output = attn_output.reshape(bsz, q_len,
self.num_heads * self.v_head_dim)
attn_output = self.o_proj(attn_output)