Sliding-window attention

Attention sink

×

One learned logit per head, 128 in all, the same for every query.

Why

Sink. A learned sink bias lets a head assign little or no attention to the tokens in its window when it needs to; the design follows gpt-oss. MiMo-V2-Flash report §2.2

Without the sink, the hybrid with a window of 128 loses noticeably on general benchmarks against the all-global baseline; with it, it recovers or surpasses that baseline. MiMo-V2-Flash report §2.2.1

bh{\htmlData{light=sinks sink}{b_h}}
out
bb [128 heads]
Parameters128 × 60 layers

128 parameters owned by this component

ComponentParameters
learned vector128

One in each of the 60 window layers, each with its own weights.

128 values, one per query head, stored in bfloat16; learned in training (MiMo-V2-Flash report §2.2), and not updated by this inference code (requires_grad=False, modeling_mimo_v2.py 268–275).

Explanation

Only the window layers have it: add_swa_attention_sink_bias is true and add_full_attention_sink_bias false (config.json).

Sources

In codeself.attention_sink_bias · 268–275

Open pinned code ↗

Direct evidence

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 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 released configuration: 70 layers of width 6,144; hybrid_layer_pattern marks 60 sliding-window layers (1) and 10 global ones (0, at 0-based layers 0, 7, 15, 23, 31, 39, 47, 55, 62 and 69); moe_layer_freq is 0 at layer 0 only; 128 query and 8 key–value heads in both kinds, 192 query–key and 128 value features a head; a sliding window of 128 with a sink bias in the window layers only; attention_value_scale 0.612; partial_rotary_factor 0.334 with θ 10,000,000 (global) and 10,000 (window); 384 experts of 2,048, 8 used, sigmoid scores, noaux_tc with one group, no shared expert; a dense MLP of 16,384; RMSNorm ε 10⁻⁵; vocabulary 152,576; untied head; 1,048,576 positions.

config.json 1–441 ↗ · XiaomiMiMo/MiMo-V2.6-Pro-RL @ b61f21852680

Excerpt
{
  "add_full_attention_sink_bias": false,
  "add_swa_attention_sink_bias": true,
  "architectures": [
    "MiMoV2ForCausalLM"
  ],
  "attention_bias": false,
  "attention_chunk_size": 128,
  "attention_dropout": 0.0,
  "attention_projection_layout": "fused_qkv",
  "attention_value_scale": 0.612,
  "audio_config": {
    "add_post_norm": true,
    "audio_channels": 20,
    "audio_segment_size": 6000,
    "group_size": 4,
    "input_full_attention": true,
    "input_local_attn_heads": 16,
    "input_local_dim": 1024,
    "input_local_head_dim": 64,
    "input_local_hidden_dropout": 0.0,
    "input_local_intermediate_size": 4096,
    "input_local_layers": 6,
    "out_hidden_size": 6144,
    "partial_rotary_factor": 1.0,
    "projection_layers": 2,
    "rope_theta": 640000,
    "speech_vocab_size": "1280",
    "speech_zeroemb_idx": "1024"
  },
  "audio_end_token_id": 151674,
  "audio_start_token_id": 151673,
  "audio_token_id": 151669,
  "auto_map": {
    "AutoConfig": "configuration_mimo_v2.MiMoV2Config",
    "AutoModel": "modeling_mimo_v2.MiMoV2Model",
    "AutoModelForCausalLM": "modeling_mimo_v2.MiMoV2ForCausalLM"
  },
  "moe_router_dtype": "bfloat16",
  "bos_token_id": null,
  "dtype": "bfloat16",
  "eos_token_id": 151645,
  "head_dim": 192,
  "hidden_act": "silu",
  "hidden_size": 6144,
  "hybrid_block_size": null,
  "hybrid_layer_pattern": [
    0,
    1,
    1,
    1,
    1,
    1,
    1,
    0,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    0,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    0,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    0,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    0,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    0,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    0,
    1,
    1,
    1,
    1,
    1,
    1,
    0,
    1,
    1,
    1,
    1,
    1,
    1,
    0
  ],
  "image_token_id": 151655,
  "initializer_range": 0.02,
  "intermediate_size": 16384,
  "layernorm_epsilon": 1e-05,
  "max_position_embeddings": 1048576,
  "model_type": "mimo_v2",
  "moe_intermediate_size": 2048,
  "moe_layer_freq": [
    0,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1,
    1
  ],
  "n_group": 1,
  "n_routed_experts": 384,
  "n_shared_experts": null,
  "norm_topk_prob": true,
  "num_attention_heads": 128,
  "num_experts_per_tok": 8,
  "num_hidden_layers": 70,
  "num_key_value_heads": 8,
  "pad_token_id": 151643,
  "partial_rotary_factor": 0.334,
  "processor_config": {
    "audio_avg_pooler": 2,
    "audio_channels": 20,
    "audio_end_token_id": 151674,
    "audio_fmax": null,
    "audio_fmin": 0,
    "audio_group_size": 4,
    "audio_hop_length": 240,
    "audio_input_id_per_second": 25.0,
    "audio_kernel_size": 3,
    "audio_n_mels": 128,
    "audio_nfft": 960,
    "audio_sampling_rate": 24000,
    "audio_segment_size": 6000,
    "audio_start_token_id": 151673,
    "audio_stride_size": 2,
    "audio_token_id": 151669,
    "audio_window_size": 960,
    "audio_zeroemb_idx": [
      1024,
      1024,
      1024,
      1024,
      1024,
      1024,
      1024,
      1024,
      1024,
      1024,
      1024,
      1024,
      1024,
      1024,
      1024,
      1024,
      1024,
      1024,
      1024,
      1024
    ],
    "fps": 1.0,
    "image_max_pixels": 8388608,
    "image_min_pixels": 8192,
    "image_token_id": 151655,
    "max_frames": 3600,
    "merge_size": 2,
    "min_frames": null,
    "num_frames": null,
    "pad_token_id": 151643,
    "patch_size": 16,
    "rope_type": "rope",
    "temporal_compression_ratio": 1,
    "temporal_patch_size": 2,
    "use_per_grid_t_timestamps": false,
    "use_video_timestamps": true,
    "video_audio_interleave_length": 0.0,
    "video_end_token_id": 151671,
    "video_max_pixels": 8388608,
    "video_min_pixels": 8192,
    "video_process_num_threads": 16,
    "video_start_token_id": 151670,
    "video_token_id": 151656,
    "video_tokens_per_second": 2,
    "video_total_max_pixels": 268435456,
    "vision_end_token_id": 151653,
    "vision_start_token_id": 151652
  },
  "quantization_config": {
    "activation_scheme": "dynamic",
    "fmt": "e4m3",
    "ignored_layers": [
      "model.layers.0.self_attn.o_proj",
      "model.layers.1.self_attn.o_proj",
      "model.layers.10.self_attn.o_proj",
      "model.layers.11.self_attn.o_proj",
      "model.layers.12.self_attn.o_proj",
      "model.layers.13.self_attn.o_proj",
      "model.layers.14.self_attn.o_proj",
      "model.layers.15.self_attn.o_proj",
      "model.layers.16.self_attn.o_proj",
      "model.layers.17.self_attn.o_proj",
      "model.layers.18.self_attn.o_proj",
      "model.layers.19.self_attn.o_proj",
      "model.layers.2.self_attn.o_proj",
      "model.layers.20.self_attn.o_proj",
      "model.layers.21.self_attn.o_proj",
      "model.layers.22.self_attn.o_proj",
      "model.layers.23.self_attn.o_proj",
      "model.layers.24.self_attn.o_proj",
      "model.layers.25.self_attn.o_proj",
      "model.layers.26.self_attn.o_proj",
      "model.layers.27.self_attn.o_proj",
      "model.layers.28.self_attn.o_proj",
      "model.layers.29.self_attn.o_proj",
      "model.layers.3.self_attn.o_proj",
      "model.layers.30.self_attn.o_proj",
      "model.layers.31.self_attn.o_proj",
      "model.layers.32.self_attn.o_proj",
      "model.layers.33.self_attn.o_proj",
      "model.layers.34.self_attn.o_proj",
      "model.layers.35.self_attn.o_proj",
      "model.layers.36.self_attn.o_proj",
      "model.layers.37.self_attn.o_proj",
      "model.layers.38.self_attn.o_proj",
      "model.layers.39.self_attn.o_proj",
      "model.layers.4.self_attn.o_proj",
      "model.layers.40.self_attn.o_proj",
      "model.layers.41.self_attn.o_proj",
      "model.layers.42.self_attn.o_proj",
      "model.layers.43.self_attn.o_proj",
      "model.layers.44.self_attn.o_proj",
      "model.layers.45.self_attn.o_proj",
      "model.layers.46.self_attn.o_proj",
      "model.layers.47.self_attn.o_proj",
      "model.layers.48.self_attn.o_proj",
      "model.layers.49.self_attn.o_proj",
      "model.layers.5.self_attn.o_proj",
      "model.layers.50.self_attn.o_proj",
      "model.layers.51.self_attn.o_proj",
      "model.layers.52.self_attn.o_proj",
      "model.layers.53.self_attn.o_proj",
      "model.layers.54.self_attn.o_proj",
      "model.layers.55.self_attn.o_proj",
      "model.layers.56.self_attn.o_proj",
      "model.layers.57.self_attn.o_proj",
      "model.layers.58.self_attn.o_proj",
      "model.layers.59.self_attn.o_proj",
      "model.layers.6.self_attn.o_proj",
      "model.layers.60.self_attn.o_proj",
      "model.layers.61.self_attn.o_proj",
      "model.layers.62.self_attn.o_proj",
      "model.layers.63.self_attn.o_proj",
      "model.layers.64.self_attn.o_proj",
      "model.layers.65.self_attn.o_proj",
      "model.layers.66.self_attn.o_proj",
      "model.layers.67.self_attn.o_proj",
      "model.layers.68.self_attn.o_proj",
      "model.layers.69.self_attn.o_proj",
      "model.layers.7.self_attn.o_proj",
      "model.layers.8.self_attn.o_proj",
      "model.layers.9.self_attn.o_proj",
      "model.decoder.self_attn.o_proj"
    ],
    "mxfp4_block_size": 32,
    "quant_method": "fp8",
    "store_dtype": "mxfp4",
    "weight_block_size": [
      128,
      128
    ]
  },
  "rope_parameters": {
    "partial_rotary_factor": 0.334,
    "rope_theta": 10000000,
    "rope_type": "default",
    "type": "default"
  },
  "rope_theta": 10000000,
  "routed_scaling_factor": null,
  "scoring_func": "sigmoid",
  "sliding_window": 128,
  "sliding_window_size": 128,
  "swa_head_dim": 192,
  "swa_num_attention_heads": 128,
  "swa_num_key_value_heads": 8,
  "swa_rope_theta": 10000,
  "swa_v_head_dim": 128,
  "tie_word_embeddings": false,
  "topk_group": 1,
  "topk_method": "noaux_tc",
  "transformers_version": "5.3.0",
  "use_cache": true,
  "v_head_dim": 128,
  "video_token_id": 151656,
  "vision_config": {
    "depth": 28,
    "fullatt_block_indexes": [
      0,
      9,
      18,
      27
    ],
    "hidden_act": "silu",
    "hidden_size": 1280,
    "in_chans": 3,
    "intermediate_size": 4608,
    "num_heads": 32,
    "num_key_value_heads": 8,
    "num_query_groups": 4,
    "out_hidden_size": 6144,
    "patch_size": 16,
    "spatial_merge_size": 2,
    "spatial_patch_size": 16,
    "temporal_patch_size": 2,
    "tokens_per_second": 2,
    "use_sink": true,
    "visual_token_window_size": 64,
    "vit_window_attn_types": [
      -1,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      -1,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      -1,
      0,
      0,
      0,
      0,
      1,
      1,
      1,
      1,
      -1
    ],
    "window_size": 128
  },
  "vision_end_token_id": 151653,
  "vision_model_type": "mimovl",
  "vision_start_token_id": 151652,
  "vocab_size": 152576
}