Window RoPE coefficients
×supplied
The cos and sin by which queries and keys turn, from the model.
- shape
- [B batch, 1 position, 64 rotary features]
Sources
Contextual evidence from Window RoPE coefficients
Each rotary embedding takes its base from rope_theta, or swa_rope_theta for the window layers, and its inverse frequencies over int(head_dim · partial_rotary_factor) features; cos and sin of position × frequency, each angle written twice.
modeling_mimo_v2.py 445–505 ↗ · XiaomiMiMo/MiMo-V2.6-Pro-RL @ b61f21852680
Excerpt
class MiMoV2RotaryEmbedding(nn.Module):
inv_freq: torch.Tensor
def __init__(self, config, is_swa: bool, device=None):
super().__init__()
if hasattr(config, "rope_scaling") and isinstance(config.rope_scaling, dict):
self.rope_type = config.rope_scaling.get("rope_type", config.rope_scaling.get("type", "default"))
else:
self.rope_type = "default"
self.max_seq_len_cached = config.max_position_embeddings
self.original_max_seq_len = config.max_position_embeddings
self.config = copy(config)
self.config.rope_parameters = copy(getattr(config, "rope_parameters", None) or {})
if is_swa:
self.config.rope_theta = getattr(config, "swa_rope_theta", config.rope_theta)
self.config.head_dim = getattr(config, "swa_head_dim", getattr(config, "head_dim", None))
if self.config.rope_parameters:
self.config.rope_parameters["rope_theta"] = self.config.rope_theta
self.rope_init_fn = (
self.compute_default_rope_parameters
if self.rope_type == "default"
else ROPE_INIT_FUNCTIONS[self.rope_type]
)
inv_freq, self.attention_scaling = self.rope_init_fn(self.config, device)
self.register_buffer("inv_freq", inv_freq, persistent=False)
self.original_inv_freq = self.inv_freq
@staticmethod
def compute_default_rope_parameters(config, device=None, seq_len=None, layer_type=None):
config.standardize_rope_params()
rope_parameters = config.rope_parameters[layer_type] if layer_type is not None else config.rope_parameters
base = rope_parameters["rope_theta"]
partial_rotary_factor = rope_parameters.get("partial_rotary_factor", 1.0)
head_dim = getattr(config, "head_dim", None) or config.hidden_size // config.num_attention_heads
dim = int(head_dim * partial_rotary_factor)
if dim % 2 != 0:
raise ValueError(
f"MiMoV2 rotary dimension must be even, got {dim} from "
f"head_dim={head_dim} and partial_rotary_factor={partial_rotary_factor}"
)
inv_freq = 1.0 / (
base ** (torch.arange(0, dim, 2, dtype=torch.int64).to(device=device, dtype=torch.float) / dim)
)
return inv_freq, 1.0
@torch.no_grad()
@dynamic_rope_update
def forward(self, x, position_ids):
inv_freq_expanded = self.inv_freq[None, :, None].float().expand(position_ids.shape[0], -1, 1).to(x.device)
position_ids_expanded = position_ids[:, None, :].float()
device_type = x.device.type if isinstance(x.device.type, str) and x.device.type != "mps" else "cpu"
with torch.autocast(device_type=device_type, enabled=False):
freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2)
emb = torch.cat((freqs, freqs), dim=-1)
cos = emb.cos() * self.attention_scaling
sin = emb.sin() * self.attention_scaling
return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype)
The text model holds the token embedding, the 70 layers, the final RMSNorm and two rotary embeddings, one global and one for the window layers; it names each layer’s cache type sliding_attention or full_attention.
modeling_mimo_v2.py 1574–1603 ↗ · XiaomiMiMo/MiMo-V2.6-Pro-RL @ b61f21852680
Excerpt
class MiMoV2Model(PreTrainedModel):
config_class = MiMoV2Config
attention_projection_layout = "split"
def __init__(self, config):
super().__init__(config)
self.attention_projection_layout = getattr(
config, "attention_projection_layout", self.attention_projection_layout
)
self.vocab_size = config.vocab_size
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size)
self.layers = nn.ModuleList(
[
MiMoV2DecoderLayer(
config,
layer_idx,
attention_projection_layout=self.attention_projection_layout,
)
for layer_idx in range(config.num_hidden_layers)
]
)
self.norm = MiMoV2RMSNorm(config.hidden_size, eps=config.layernorm_epsilon)
self.rotary_emb = MiMoV2RotaryEmbedding(config=config, is_swa=False)
self.swa_rotary_emb = MiMoV2RotaryEmbedding(config=config, is_swa=True)
self.has_sliding_layers = any(pattern == 1 for pattern in config.hybrid_layer_pattern)
self.config.layer_types = [
"sliding_attention" if config.hybrid_layer_pattern[i] == 1 else "full_attention"
for i in range(config.num_hidden_layers)
]
self.post_init()
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
}Contextual evidence from Prepare Q, K, V
The fused projection is split into queries, keys and values, read as 128 query heads of 192, 8 key heads of 192 and 8 value heads of 128.
modeling_mimo_v2.py 358–391 ↗ · XiaomiMiMo/MiMo-V2.6-Pro-RL @ b61f21852680
Excerpt
def forward(
self,
hidden_states: torch.Tensor,
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,
**kwargs: Unpack[TransformersKwargs],
) -> tuple[torch.Tensor, torch.Tensor]:
input_shape = hidden_states.shape[:-1]
if self.projection_layout == "fused_qkv":
qkv_states = self.qkv_proj(hidden_states)
query_states, key_states, value_states = qkv_states.split([self.q_size, self.k_size, self.v_size], dim=-1)
else:
query_states = self.q_proj(hidden_states)
key_states = self.k_proj(hidden_states)
value_states = self.v_proj(hidden_states)
query_states = query_states.view(*input_shape, self.num_attention_heads, self.head_dim).transpose(1, 2)
key_states = key_states.view(*input_shape, self.num_key_value_heads, self.head_dim).transpose(1, 2)
value_states = value_states.view(*input_shape, self.num_key_value_heads, self.v_head_dim).transpose(1, 2)
return self._forward_attention(
query_states,
key_states,
value_states,
input_shape,
position_embeddings,
attention_mask,
past_key_values=past_key_values,
cache_position=cache_position,
position_ids=position_ids,
)
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 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)