Key heads
×Reconstruct 128 key heads of 192 features: per-head content from the latent and one rotated key shared by all heads.
Why
The authors of latent attention explain that a key has two parts because rotation by position does not go together with the compression. If the rebuilt keys were rotated, the rotation would stand between the query matrix and the key’s up-projection, and the keys of all earlier tokens would have to be recomputed for every new token. So position is carried by a separate rotary part, one per position and shared by all heads, joined to each head’s content part. DeepSeek-V2 §2.1.3
All 5 steps
- in
- [B batch, T positions, 512 kv latent] [B batch, T positions, 7168 d] [T positions, 32 pairs]
- out
- [B batch, 128 heads, T positions, 192 content + rotary]
Parameters8,847,360 × 3 layers
8,847,360 parameters per instance, across its learned components
| Component | Calculation | Parameters |
|---|---|---|
| Key content up-projection | 512 × 16,384 | 8,388,608 |
| Rotary key projection | 7,168 × 64 | 458,752 |
| Per-instance total | 8,847,360 | |
One in each of the 3 dense layers, each with its own weights.
Explanation
One matrix, kv_b_proj, produces the content keys and the values together; the implementation splits them after the product (mla_attention.py 3207–3210).
Content keys carry no rotation, so when decoding vLLM folds into the queries and scores them against the cached latents directly, which gives the same scores (mla_attention.py 93–117).
Sources
In codek
Direct evidence
MLA owns a fused down-projection of q_lora_rank + kv_lora_rank + qk_rope_head_dim outputs, the query RMSNorm and up-projection, the latent RMSNorm, the key–value up-projection and the output projection, all without bias; the latent norms take the configuration’s ε; the softmax scale is qk_head_dim^−0.5; with attention_factor 1 the rope type becomes deepseek_llama_scaling, the rotation is not NeoX style, and the YaRN mscale is folded into the softmax scale only for deepseek_yarn.
deepseek_v2.py 1033–1136 ↗ · vllm-project/vllm @ 5df63a7203cd
Excerpt
self.qk_head_dim = qk_nope_head_dim + qk_rope_head_dim
self.v_head_dim = v_head_dim
self.q_lora_rank = q_lora_rank
self.kv_lora_rank = kv_lora_rank
self.num_heads = num_heads
tp_size = get_tensor_model_parallel_world_size()
assert num_heads % tp_size == 0
self.num_local_heads = num_heads // tp_size
self.scaling = self.qk_head_dim**-0.5… lines 1045–1050 omitted …
if self.q_lora_rank is not None:
self.fused_qkv_a_proj = DeepSeekV2FusedQkvAProjLinear(
proj_input_size,
[self.q_lora_rank, self.kv_lora_rank + self.qk_rope_head_dim],
quant_config=quant_config,
prefix=f"{prefix}.fused_qkv_a_proj",
)… lines 1058–1080 omitted …
if self.q_lora_rank is not None:
self.q_a_layernorm = RMSNorm(self.q_lora_rank, eps=config.rms_norm_eps)
self.q_b_proj = q_proj_cls(
self.q_lora_rank,
self.num_heads * self.qk_head_dim,
bias=False,
quant_config=quant_config,
prefix=f"{prefix}.q_b_proj",
)
else:
self.q_proj = q_proj_cls(
proj_input_size,
self.num_heads * self.qk_head_dim,
bias=False,
quant_config=quant_config,
prefix=f"{prefix}.q_proj",
)
self.kv_a_layernorm = RMSNorm(self.kv_lora_rank, eps=config.rms_norm_eps)
self.kv_b_proj = ColumnParallelLinear(
self.kv_lora_rank,
self.num_heads * (self.qk_nope_head_dim + self.v_head_dim),
bias=False,
quant_config=quant_config,
prefix=f"{prefix}.kv_b_proj",
)
self.o_proj = RowParallelLinear(
self.num_heads * self.v_head_dim,
self.hidden_size,
bias=False,
reduce_results=reduce_results,
quant_config=quant_config,
prefix=f"{prefix}.o_proj",… lines 1113–1114 omitted …
if config.rope_parameters["rope_type"] != "default":
config.rope_parameters["rope_type"] = (
"deepseek_llama_scaling"
if config.rope_parameters.get("attention_factor") == 1.0
else "deepseek_yarn"
)
self.rotary_emb = get_rope(
qk_rope_head_dim,
max_position=max_position_embeddings,
rope_parameters=config.rope_parameters,
is_neox_style=False,
)
if (
config.rope_parameters["rope_type"] != "default"
and config.rope_parameters["rope_type"] == "deepseek_yarn"
):
mscale_all_dim = config.rope_parameters.get("mscale_all_dim", False)
scaling_factor = config.rope_parameters["factor"]
mscale = yarn_get_mscale(scaling_factor, float(mscale_all_dim))
self.scaling = self.scaling * mscale * mscaleThe key–value part is split into the latent and the rotary key, the latent is normalized, and the rotary key gets a head axis of one; RoPE rotates it with the queries.
mla.py 203–230 ↗ · vllm-project/vllm @ 5df63a7203cd
Excerpt
kv_c, k_pe = kv_lora.split([self.kv_lora_rank, self.qk_rope_head_dim], dim=-1)
if self.fuse_qkv_rmsnorm and q_c is not None:
# Fuse q_a + kv_a RMSNorm into one kernel launch (q_c is still the
# pre-norm projection output here).
assert self.q_a_layernorm is not None
q_proj_input, kv_c_normed = fused_q_kv_rmsnorm(
q_c,
kv_c,
self.q_a_layernorm.weight.data,
self.kv_a_layernorm.weight.data,
self.q_a_layernorm.variance_epsilon,
)
q_c = q_proj_input
else:
kv_c_normed = self.kv_a_layernorm(kv_c)
# Add head dim of 1 to k_pe
k_pe = k_pe.unsqueeze(1)… lines 220–226 omitted …
if self.rotary_emb is not None:
q[..., self.qk_nope_head_dim :], k_pe = self.rotary_emb(
positions, q[..., self.qk_nope_head_dim :], k_pe
)On the prefill path kv_b_proj rebuilds each head’s content keys and values from the normalized latent, each head’s key joins its content part and the shared rotary key, and the prefill backend runs attention over the new tokens.
mla_attention.py 3207–3227 ↗ · vllm-project/vllm @ 5df63a7203cd
Excerpt
kv_nope = self.kv_b_proj(kv_c_normed)[0].view(
-1, self.num_heads, self.qk_nope_head_dim + self.v_head_dim
)
k_nope, v = kv_nope.split([self.qk_nope_head_dim, self.v_head_dim], dim=-1)
k = self._concat_k_nope_k_pe(k_nope, k_pe)
if use_fp8_prefill:
k = k.to(prefill_metadata.q_data_type)
v = v.to(prefill_metadata.q_data_type)
output_prefill = prefill_metadata.prefill_backend.run_prefill_new_tokens(
q=q,
k=k,
v=v,
return_softmax_lse=has_context,
out=(
output.view(-1, self.num_heads, self.v_head_dim)
if output_scale is not None
else None
),
output_scale=output_scale,