[B, T, 7168], [T, 32], [T, T]to [B, T, 7168]
prefill, empty cache · KV cache: holds positions 0 … T − 1 and reads them
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.6 @ 7eb5002f6aad
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.6 @ 7eb5002f6aad
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.6 @ 7eb5002f6aad
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)