Merge heads
×Concatenate the 128 heads into 16,384 features.
- in
- [B batch, 128 heads, 1 position, 128 value d_h]
- out
- [B batch, 1 position, 16384 heads × value d_h]
Explanation
Heads are concatenated in order: head fills features .
Sources
In codeattn_output.transpose(1, 2), .reshape(*input_shape, -1) · 101, 354
Direct evidence
Values are multiplied by the value scale; the first rope_dim features of each query and key head are rotated and the rest concatenated after them; keys and values go to the cache; the eager path receives the sink bias, other attention functions the sink as s_aux and the window; the heads are joined and projected.
modeling_mimo_v2.py 290–356 ↗ · XiaomiMiMo/MiMo-V2.6-Pro-RL @ b61f21852680
Excerpt
def _forward_attention(
self,
query_states: torch.Tensor,
key_states: torch.Tensor,
value_states: torch.Tensor,
input_shape: torch.Size,
position_embeddings: tuple[torch.Tensor, torch.Tensor],
attention_mask: Optional[torch.Tensor],
past_key_values: Optional[Cache] = None,
cache_position: Optional[torch.LongTensor] = None,
position_ids: Optional[torch.LongTensor] = None,
) -> tuple[torch.Tensor, torch.Tensor]:
if self.v_scale is not None:
value_states = value_states * self.v_scale
cos, sin = position_embeddings
query_rope, query_nope = query_states.split([self.rope_dim, self.head_dim - self.rope_dim], dim=-1)
key_rope, key_nope = key_states.split([self.rope_dim, self.head_dim - self.rope_dim], dim=-1)
query_rope, key_rope = apply_rotary_pos_emb(query_rope, key_rope, cos, sin)
query_states = torch.cat([query_rope, query_nope], dim=-1)
key_states = torch.cat([key_rope, key_nope], dim=-1)
if past_key_values is not None:
cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position}
key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx, cache_kwargs)
attn_implementation = self.config._attn_implementation
if attn_implementation is not None and attn_implementation.startswith("paged|"):
raise ValueError(
"MiMoV2 remote code does not support paged attention cache. "
"Please use eager, sdpa, flex_attention, or flash_attention_2."
)
attention_interface: Callable = ALL_ATTENTION_FUNCTIONS.get_interface(
attn_implementation, eager_attention_forward
)
if self.attention_sink_bias is not None and attn_implementation == "sdpa":
logger.warning_once(
"MiMoV2 attention sink bias is not supported by SDPA; falling back to eager attention for correctness."
)
attention_interface = eager_attention_forward
attention_kwargs = {
"dropout": 0.0 if not self.training else self.attention_dropout,
"scaling": self.scaling,
"position_ids": position_ids,
"is_causal": self.is_causal,
}
if attention_interface is eager_attention_forward:
attention_kwargs["sinks"] = self.attention_sink_bias
else:
if self.attention_sink_bias is not None:
attention_kwargs["s_aux"] = self.attention_sink_bias
if self.sliding_window is not None:
attention_kwargs["sliding_window"] = self.sliding_window
attn_output, attn_weights = attention_interface(
self,
query_states,
key_states,
value_states,
attention_mask,
**attention_kwargs,
)
attn_output = attn_output.reshape(*input_shape, -1).contiguous()
attn_output = self.o_proj(attn_output)
return attn_output, attn_weights
The eager attention: keys and values repeated to the query heads, scores scaled, the mask added; with sinks, each head’s sink bias is appended to every row of scores as one more column; the row’s maximum is subtracted, the softmax taken in float32, the sink’s column dropped, and the values mixed by the weights.
modeling_mimo_v2.py 71–102 ↗ · XiaomiMiMo/MiMo-V2.6-Pro-RL @ b61f21852680
Excerpt
def eager_attention_forward(
module: nn.Module,
query: torch.Tensor,
key: torch.Tensor,
value: torch.Tensor,
attention_mask: Optional[torch.Tensor],
scaling: float,
dropout: float = 0.0,
sinks: Optional[torch.Tensor] = None,
**kwargs,
):
key_states = repeat_kv(key, module.num_key_value_groups)
value_states = repeat_kv(value, module.num_key_value_groups)
attn_weights = torch.matmul(query, key_states.transpose(2, 3)) * scaling
if attention_mask is not None:
causal_mask = attention_mask[:, :, :, : key_states.shape[-2]]
attn_weights = attn_weights + causal_mask
if sinks is not None:
sinks = module.attention_sink_bias.reshape(1, -1, 1, 1).expand(query.shape[0], -1, query.shape[-2], -1)
attn_weights = torch.cat([attn_weights, sinks], dim=-1)
attn_weights = attn_weights - attn_weights.max(dim=-1, keepdim=True).values
probs = F.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query.dtype)
if sinks is not None:
probs = probs[..., :-1]
attn_weights = nn.functional.dropout(probs, p=dropout, training=module.training)
attn_output = torch.matmul(attn_weights, value_states)
attn_output = attn_output.transpose(1, 2).contiguous()
return attn_output, attn_weights