Scale, causal
×Divide the scores by √192; each query sees its own and earlier positions only.
- in
- [B batch, 128 heads, 1 query, 1001 keys]
- out
- [B batch, 128 heads, 1 query, 1001 keys]
The model builds the mask once per call (create_causal_mask, modeling_mimo_v2.py 1652) and hands it to every global layer. The scale divides by √192, the width of the query–key features, not of the values (modeling_mimo_v2.py 261). When decoding, the one new query is the last position, so every cached position is at or before it.
Sources
In code* scaling · 84; attn_weights + causal_mask · 85–87
Direct evidence
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
The attention of a window layer takes the swa_* heads, widths and window, of a global layer the main ones; the rotary dimension is int(head_dim · partial_rotary_factor); scaling is head_dim^−0.5; the value scale is attention_value_scale; a sink bias of one value per query head exists where the configuration adds it; Pro stores one fused query–key–value projection, and the output projection maps heads × v_head_dim back to the width.
modeling_mimo_v2.py 220–288 ↗ · XiaomiMiMo/MiMo-V2.6-Pro-RL @ b61f21852680
Excerpt
class MiMoV2Attention(nn.Module):
"""MiMoV2 attention.
`projection_layout` only controls how checkpoint weights are named and
stored: Flash uses separate q/k/v projections, while Pro uses fused qkv.
The attention computation after projection is shared.
"""
def __init__(self, config, is_swa: bool, layer_idx: int, projection_layout: str = "split"):
super().__init__()
if projection_layout not in {"split", "fused_qkv"}:
raise ValueError(f"Unsupported MiMoV2 attention projection layout: {projection_layout}")
self.config = config
self.layer_idx = layer_idx
self.is_swa = is_swa
self.is_causal = True
self.projection_layout = projection_layout
default_head_dim = config.hidden_size // config.num_attention_heads
default_v_head_dim = getattr(config, "v_head_dim", default_head_dim)
if is_swa:
self.head_dim = getattr(config, "swa_head_dim", getattr(config, "head_dim", default_head_dim))
self.v_head_dim = getattr(config, "swa_v_head_dim", default_v_head_dim)
self.num_attention_heads = getattr(config, "swa_num_attention_heads", config.num_attention_heads)
self.num_key_value_heads = getattr(config, "swa_num_key_value_heads", config.num_key_value_heads)
else:
self.head_dim = getattr(config, "head_dim", default_head_dim)
self.v_head_dim = getattr(config, "v_head_dim", self.head_dim)
self.num_attention_heads = config.num_attention_heads
self.num_key_value_heads = config.num_key_value_heads
self.rope_dim = int(self.head_dim * getattr(config, "partial_rotary_factor", 1.0))
if self.rope_dim % 2 != 0:
raise ValueError(
f"MiMoV2 rotary dimension must be even, got {self.rope_dim} from "
f"head_dim={self.head_dim} and partial_rotary_factor={getattr(config, 'partial_rotary_factor', 1.0)}"
)
self.num_key_value_groups = self.num_attention_heads // self.num_key_value_heads
self.attention_dropout = getattr(config, "attention_dropout", 0.0)
self.scaling = self.head_dim**-0.5
self.sliding_window = getattr(config, "sliding_window", None) if is_swa else None
self.q_size = self.num_attention_heads * self.head_dim
self.k_size = self.num_key_value_heads * self.head_dim
self.v_size = self.num_key_value_heads * self.v_head_dim
self.o_hidden_size = self.num_attention_heads * self.v_head_dim
self.v_scale = getattr(config, "attention_value_scale", None)
self.attention_sink_bias = (
nn.Parameter(torch.empty(self.num_attention_heads), requires_grad=False)
if (
(getattr(config, "add_full_attention_sink_bias", False) and not is_swa)
or (getattr(config, "add_swa_attention_sink_bias", False) and is_swa)
)
else None
)
attention_bias = getattr(config, "attention_bias", False)
if self.projection_layout == "fused_qkv":
self.qkv_proj = nn.Linear(
config.hidden_size,
self.q_size + self.k_size + self.v_size,
bias=attention_bias,
)
else:
self.q_proj = nn.Linear(config.hidden_size, self.q_size, bias=attention_bias)
self.k_proj = nn.Linear(config.hidden_size, self.k_size, bias=attention_bias)
self.v_proj = nn.Linear(config.hidden_size, self.v_size, bias=attention_bias)
self.o_proj = nn.Linear(self.o_hidden_size, config.hidden_size, bias=False)
The causal mask lets a query see keys at or before its position.
masking_utils.py 74–78 ↗ · huggingface/transformers @ aad13b87ed59
Excerpt
def causal_mask_function(batch_idx: int, head_idx: int, q_idx: int, kv_idx: int) -> bool:
"""
This creates a basic lower-diagonal causal mask.
"""
return kv_idx <= q_idx