inference-optimization/MiniMax-M2.5-BF16
022
1# ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ2# This file was automatically generated from src/transformers/models/minimax_m2/modular_minimax_m2.py.3# Do NOT edit this file manually as any edits will be overwritten by the generation of4# the file from the modular. If any change should be done, please apply the change to the5# modular_minimax_m2.py file directly. One of our CI enforces this.6# ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ๐จ7# coding=utf-88# Copyright 2025 the HuggingFace Team. All rights reserved.9#10# Licensed under the Apache License, Version 2.0 (the "License");11# you may not use this file except in compliance with the License.12# You may obtain a copy of the License at13#14# http://www.apache.org/licenses/LICENSE-2.015#16# Unless required by applicable law or agreed to in writing, software17# distributed under the License is distributed on an "AS IS" BASIS,18# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.19# See the License for the specific language governing permissions and20# limitations under the License.21 22 23from collections.abc import Callable24from typing import Optional, Union, Unpack25 26import torch27from torch import nn28 29from transformers.activations import ACT2FN30from transformers.cache_utils import Cache, DynamicCache31from transformers.generation import GenerationMixin32from transformers.integrations import use_kernel_forward_from_hub33from transformers.masking_utils import create_causal_mask, create_sliding_window_causal_mask34from transformers.modeling_flash_attention_utils import FlashAttentionKwargs35from transformers.modeling_layers import (36 GenericForQuestionAnswering,37 GenericForSequenceClassification,38 GenericForTokenClassification,39 GradientCheckpointingLayer,40)41from transformers.modeling_outputs import MoeCausalLMOutputWithPast, MoeModelOutputWithPast42from transformers.modeling_rope_utils import ROPE_INIT_FUNCTIONS, dynamic_rope_update43from transformers.modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel44from transformers.utils import TransformersKwargs, auto_docstring, can_return_tuple45from transformers.utils.deprecation import deprecate_kwarg46from transformers.utils.generic import OutputRecorder, check_model_inputs47from .configuration_minimax_m2 import MiniMaxM2Config48 49 50class MiniMaxM2MLP(nn.Module):51 def __init__(self, config: MiniMaxM2Config):52 super().__init__()53 self.ffn_dim = config.intermediate_size54 self.hidden_dim = config.hidden_size55 56 self.w1 = nn.Linear(self.hidden_dim, self.ffn_dim, bias=False)57 self.w2 = nn.Linear(self.ffn_dim, self.hidden_dim, bias=False)58 self.w3 = nn.Linear(self.hidden_dim, self.ffn_dim, bias=False)59 60 self.act_fn = ACT2FN[config.hidden_act]61 62 def forward(self, hidden_states):63 current_hidden_states = self.act_fn(self.w1(hidden_states)) * self.w3(hidden_states)64 current_hidden_states = self.w2(current_hidden_states)65 return current_hidden_states66 67 68class MiniMaxM2Experts(nn.ModuleList):69 """70 ModuleList of experts.71 """72 73 def __init__(self, config: MiniMaxM2Config):74 super().__init__()75 self.top_k = config.num_experts_per_tok76 self.num_experts = config.num_local_experts77 for _ in range(self.num_experts):78 self.append(MiniMaxM2MLP(config))79 80 def forward(81 self, hidden_states: torch.Tensor, top_k_index: torch.Tensor, top_k_weights: torch.Tensor82 ) -> torch.Tensor:83 """84 Args:85 hidden_states: (batch_size * sequence_length, hidden_dim)86 selected_experts: (batch_size * sequence_length, top_k)87 routing_weights: (batch_size * sequence_length, top_k)88 Returns:89 (batch_size * sequence_length, hidden_dim)90 """91 final_hidden_states = torch.zeros_like(hidden_states)92 expert_mask = torch.nn.functional.one_hot(top_k_index, num_classes=self.num_experts).permute(2, 1, 0)93 94 expert_hit = torch.greater(expert_mask.sum(dim=(-1, -2)), 0).nonzero()95 for expert_idx in expert_hit:96 idx, top_x = torch.where(expert_mask[expert_idx].squeeze(0))97 current_state = hidden_states[None, top_x].reshape(-1, hidden_states.shape[-1])98 current_hidden_states = self[expert_idx](current_state) * top_k_weights[top_x, idx, None]99 final_hidden_states.index_add_(0, top_x, current_hidden_states.to(hidden_states.dtype))100 return final_hidden_states101 102 103class MiniMaxM2SparseMoeBlock(nn.Module):104 def __init__(self, config):105 super().__init__()106 self.top_k = config.num_experts_per_tok107 self.jitter_noise = config.router_jitter_noise108 self.gate = nn.Linear(config.hidden_size, config.num_local_experts, bias=False)109 self.experts = MiniMaxM2Experts(config)110 self.register_buffer("e_score_correction_bias", torch.zeros(config.num_local_experts))111 112 def route_tokens_to_experts(self, router_logits):113 routing_weights = torch.nn.functional.sigmoid(router_logits.float())114 scores_for_choice = routing_weights + self.e_score_correction_bias115 _, top_k_index = torch.topk(scores_for_choice, self.top_k, dim=-1, sorted=False)116 top_k_weights = routing_weights.gather(1, top_k_index)117 top_k_weights /= top_k_weights.sum(dim=-1, keepdim=True)118 return top_k_index, top_k_weights.to(router_logits.dtype)119 120 def forward(self, hidden_states: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:121 batch_size, sequence_length, hidden_dim = hidden_states.shape122 if self.training and self.jitter_noise > 0:123 hidden_states *= torch.empty_like(hidden_states).uniform_(1.0 - self.jitter_noise, 1.0 + self.jitter_noise)124 hidden_states = hidden_states.view(-1, hidden_states.shape[-1])125 router_logits = self.gate(hidden_states)126 top_k_index, top_k_weights = self.route_tokens_to_experts(router_logits)127 hidden_states = self.experts(hidden_states, top_k_index, top_k_weights.to(hidden_states.dtype))128 hidden_states = hidden_states.reshape(batch_size, sequence_length, hidden_dim)129 return hidden_states, router_logits130 131 132@use_kernel_forward_from_hub("RMSNorm")133class MiniMaxM2RMSNorm(nn.Module):134 def __init__(self, hidden_size, eps=1e-6):135 """136 MiniMaxM2RMSNorm is equivalent to T5LayerNorm137 """138 super().__init__()139 self.weight = nn.Parameter(torch.ones(hidden_size))140 self.variance_epsilon = eps141 142 def forward(self, hidden_states):143 input_dtype = hidden_states.dtype144 hidden_states = hidden_states.to(torch.float32)145 variance = hidden_states.pow(2).mean(-1, keepdim=True)146 hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)147 return self.weight * hidden_states.to(input_dtype)148 149 def extra_repr(self):150 return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}"151 152 153def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:154 """155 This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,156 num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)157 """158 batch, num_key_value_heads, slen, head_dim = hidden_states.shape159 if n_rep == 1:160 return hidden_states161 hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim)162 return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)163 164 165def eager_attention_forward(166 module: nn.Module,167 query: torch.Tensor,168 key: torch.Tensor,169 value: torch.Tensor,170 attention_mask: Optional[torch.Tensor],171 scaling: float,172 dropout: float = 0.0,173 **kwargs: Unpack[TransformersKwargs],174):175 key_states = repeat_kv(key, module.num_key_value_groups)176 value_states = repeat_kv(value, module.num_key_value_groups)177 178 attn_weights = torch.matmul(query, key_states.transpose(2, 3)) * scaling179 if attention_mask is not None:180 causal_mask = attention_mask[:, :, :, : key_states.shape[-2]]181 attn_weights = attn_weights + causal_mask182 183 attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query.dtype)184 attn_weights = nn.functional.dropout(attn_weights, p=dropout, training=module.training)185 attn_output = torch.matmul(attn_weights, value_states)186 attn_output = attn_output.transpose(1, 2).contiguous()187 188 return attn_output, attn_weights189 190 191def rotate_half(x):192 """Rotates half the hidden dims of the input."""193 x1 = x[..., : x.shape[-1] // 2]194 x2 = x[..., x.shape[-1] // 2 :]195 return torch.cat((-x2, x1), dim=-1)196 197 198def apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1):199 """Applies Rotary Position Embedding to the query and key tensors.200 201 Args:202 q (`torch.Tensor`): The query tensor.203 k (`torch.Tensor`): The key tensor.204 cos (`torch.Tensor`): The cosine part of the rotary embedding.205 sin (`torch.Tensor`): The sine part of the rotary embedding.206 position_ids (`torch.Tensor`, *optional*):207 Deprecated and unused.208 unsqueeze_dim (`int`, *optional*, defaults to 1):209 The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and210 sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note211 that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and212 k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes213 cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have214 the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.215 Returns:216 `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.217 """218 cos = cos.unsqueeze(unsqueeze_dim)219 sin = sin.unsqueeze(unsqueeze_dim)220 221 # Keep half or full tensor for later concatenation222 rotary_dim = cos.shape[-1]223 q_rot, q_pass = q[..., :rotary_dim], q[..., rotary_dim:]224 k_rot, k_pass = k[..., :rotary_dim], k[..., rotary_dim:]225 226 # Apply rotary embeddings on the first half or full tensor227 q_embed = (q_rot * cos) + (rotate_half(q_rot) * sin)228 k_embed = (k_rot * cos) + (rotate_half(k_rot) * sin)229 230 # Concatenate back to full shape231 q_embed = torch.cat([q_embed, q_pass], dim=-1)232 k_embed = torch.cat([k_embed, k_pass], dim=-1)233 return q_embed, k_embed234 235 236class MiniMaxM2Attention(nn.Module):237 """Multi-headed attention from 'Attention Is All You Need' paper"""238 239 def __init__(self, config: MiniMaxM2Config, layer_idx: int):240 super().__init__()241 self.config = config242 self.layer_idx = layer_idx243 self.head_dim = getattr(config, "head_dim", None) or config.hidden_size // config.num_attention_heads244 self.num_key_value_groups = config.num_attention_heads // config.num_key_value_heads245 self.scaling = self.head_dim**-0.5246 self.attention_dropout = config.attention_dropout247 self.is_causal = True248 self.q_proj = nn.Linear(config.hidden_size, config.num_attention_heads * self.head_dim, bias=False)249 self.k_proj = nn.Linear(config.hidden_size, config.num_key_value_heads * self.head_dim, bias=False)250 self.v_proj = nn.Linear(config.hidden_size, config.num_key_value_heads * self.head_dim, bias=False)251 self.o_proj = nn.Linear(config.num_attention_heads * self.head_dim, config.hidden_size, bias=False)252 253 self.use_qk_norm = config.use_qk_norm254 if self.use_qk_norm:255 self.q_norm = MiniMaxM2RMSNorm(self.head_dim * config.num_attention_heads, eps=config.rms_norm_eps)256 self.k_norm = MiniMaxM2RMSNorm(self.head_dim * config.num_key_value_heads, eps=config.rms_norm_eps)257 258 @deprecate_kwarg("past_key_value", new_name="past_key_values", version="4.58")259 def forward(260 self,261 hidden_states: torch.Tensor,262 position_embeddings: tuple[torch.Tensor, torch.Tensor],263 attention_mask: Optional[torch.Tensor],264 past_key_values: Optional[Cache] = None,265 cache_position: Optional[torch.LongTensor] = None,266 **kwargs: Unpack[FlashAttentionKwargs],267 ) -> tuple[torch.Tensor, Optional[torch.Tensor]]:268 input_shape = hidden_states.shape[:-1]269 hidden_shape = (*input_shape, -1, self.head_dim)270 271 query_states = self.q_proj(hidden_states)272 key_states = self.k_proj(hidden_states)273 value_states = self.v_proj(hidden_states)274 275 if self.use_qk_norm: # main diff from Llama276 query_states = self.q_norm(query_states)277 key_states = self.k_norm(key_states)278 279 key_states = key_states.view(hidden_shape)280 query_states = query_states.view(hidden_shape)281 value_states = value_states.view(hidden_shape)282 283 query_states = query_states.transpose(1, 2)284 key_states = key_states.transpose(1, 2)285 value_states = value_states.transpose(1, 2)286 287 cos, sin = position_embeddings288 query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)289 290 if past_key_values is not None:291 # sin and cos are specific to RoPE models; position_ids needed for the static cache292 cache_kwargs = {"sin": sin, "cos": cos, "cache_position": cache_position}293 key_states, value_states = past_key_values.update(key_states, value_states, self.layer_idx, cache_kwargs)294 295 attention_interface: Callable = eager_attention_forward296 if self.config._attn_implementation != "eager":297 attention_interface = ALL_ATTENTION_FUNCTIONS[self.config._attn_implementation]298 299 attn_output, attn_weights = attention_interface(300 self,301 query_states,302 key_states,303 value_states,304 attention_mask,305 dropout=0.0 if not self.training else self.attention_dropout,306 scaling=self.scaling,307 **kwargs,308 )309 310 attn_output = attn_output.reshape(*input_shape, -1).contiguous()311 attn_output = self.o_proj(attn_output)312 return attn_output, attn_weights313 314 315class MiniMaxM2DecoderLayer(GradientCheckpointingLayer):316 def __init__(self, config: MiniMaxM2Config, layer_idx: int):317 super().__init__()318 self.hidden_size = config.hidden_size319 320 self.self_attn = MiniMaxM2Attention(config, layer_idx)321 322 self.block_sparse_moe = MiniMaxM2SparseMoeBlock(config)323 self.input_layernorm = MiniMaxM2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)324 self.post_attention_layernorm = MiniMaxM2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)325 326 @deprecate_kwarg("past_key_value", new_name="past_key_values", version="4.58")327 def forward(328 self,329 hidden_states: torch.Tensor,330 position_embeddings: tuple[torch.Tensor, torch.Tensor],331 attention_mask: Optional[torch.Tensor] = None,332 position_ids: Optional[torch.LongTensor] = None,333 past_key_values: Optional[Cache] = None,334 cache_position: Optional[torch.LongTensor] = None,335 **kwargs: Unpack[TransformersKwargs],336 ) -> torch.FloatTensor:337 residual = hidden_states338 339 hidden_states = self.input_layernorm(hidden_states)340 341 # Self Attention342 hidden_states, _ = self.self_attn(343 hidden_states=hidden_states,344 position_embeddings=position_embeddings,345 attention_mask=attention_mask,346 position_ids=position_ids,347 past_key_values=past_key_values,348 cache_position=cache_position,349 **kwargs,350 )351 hidden_states = residual + hidden_states352 353 # Fully Connected354 residual = hidden_states355 hidden_states = self.post_attention_layernorm(hidden_states)356 hidden_states, _ = self.block_sparse_moe(hidden_states)357 hidden_states = residual + hidden_states358 359 return hidden_states360 361 362class MiniMaxM2RotaryEmbedding(nn.Module):363 inv_freq: torch.Tensor # fix linting for `register_buffer`364 365 def __init__(self, config: MiniMaxM2Config, device=None):366 super().__init__()367 # BC: "rope_type" was originally "type"368 if hasattr(config, "rope_scaling") and isinstance(config.rope_scaling, dict):369 self.rope_type = config.rope_scaling.get("rope_type", config.rope_scaling.get("type"))370 else:371 self.rope_type = "default"372 self.max_seq_len_cached = config.max_position_embeddings373 self.original_max_seq_len = config.max_position_embeddings374 375 self.config = config376 self.rope_init_fn = ROPE_INIT_FUNCTIONS[self.rope_type]377 378 inv_freq, self.attention_scaling = self.rope_init_fn(self.config, device)379 self.register_buffer("inv_freq", inv_freq, persistent=False)380 self.original_inv_freq = self.inv_freq381 382 @torch.no_grad()383 @dynamic_rope_update # power user: used with advanced RoPE types (e.g. dynamic rope)384 def forward(self, x, position_ids):385 inv_freq_expanded = self.inv_freq[None, :, None].float().expand(position_ids.shape[0], -1, 1).to(x.device)386 position_ids_expanded = position_ids[:, None, :].float()387 388 device_type = x.device.type if isinstance(x.device.type, str) and x.device.type != "mps" else "cpu"389 with torch.autocast(device_type=device_type, enabled=False): # Force float32390 freqs = (inv_freq_expanded.float() @ position_ids_expanded.float()).transpose(1, 2)391 emb = torch.cat((freqs, freqs), dim=-1)392 cos = emb.cos() * self.attention_scaling393 sin = emb.sin() * self.attention_scaling394 395 return cos.to(dtype=x.dtype), sin.to(dtype=x.dtype)396 397 398@auto_docstring399class MiniMaxM2PreTrainedModel(PreTrainedModel):400 config: MiniMaxM2Config401 base_model_prefix = "model"402 supports_gradient_checkpointing = True403 _no_split_modules = ["MiniMaxM2DecoderLayer"]404 _skip_keys_device_placement = ["past_key_values"]405 _supports_flash_attn = True406 _supports_sdpa = True407 _supports_flex_attn = True408 _can_compile_fullgraph = False # MoE models don't work with torch.compile (`torch.where(condition)` not supported)409 _supports_attention_backend = True410 _can_record_outputs = {411 "router_logits": OutputRecorder(MiniMaxM2SparseMoeBlock, index=1),412 "hidden_states": MiniMaxM2DecoderLayer,413 "attentions": MiniMaxM2Attention,414 }415 416 417@auto_docstring418class MiniMaxM2Model(MiniMaxM2PreTrainedModel):419 def __init__(self, config: MiniMaxM2Config):420 super().__init__(config)421 self.padding_idx = config.pad_token_id422 self.vocab_size = config.vocab_size423 424 self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)425 self.layers = nn.ModuleList(426 [MiniMaxM2DecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]427 )428 self.norm = MiniMaxM2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)429 self.rotary_emb = MiniMaxM2RotaryEmbedding(config=config)430 self.gradient_checkpointing = False431 432 # Initialize weights and apply final processing433 self.post_init()434 435 @check_model_inputs436 @auto_docstring437 def forward(438 self,439 input_ids: Optional[torch.LongTensor] = None,440 attention_mask: Optional[torch.Tensor] = None,441 position_ids: Optional[torch.LongTensor] = None,442 past_key_values: Optional[Cache] = None,443 inputs_embeds: Optional[torch.FloatTensor] = None,444 use_cache: Optional[bool] = None,445 cache_position: Optional[torch.LongTensor] = None,446 **kwargs: Unpack[TransformersKwargs],447 ) -> MoeModelOutputWithPast:448 if (input_ids is None) ^ (inputs_embeds is not None):449 raise ValueError("You must specify exactly one of input_ids or inputs_embeds")450 451 if use_cache and past_key_values is None:452 past_key_values = DynamicCache(config=self.config)453 454 if inputs_embeds is None:455 inputs_embeds = self.embed_tokens(input_ids)456 457 if cache_position is None:458 past_seen_tokens = past_key_values.get_seq_length() if past_key_values is not None else 0459 cache_position = torch.arange(460 past_seen_tokens, past_seen_tokens + inputs_embeds.shape[1], device=inputs_embeds.device461 )462 if position_ids is None:463 position_ids = cache_position.unsqueeze(0)464 465 mask_function = create_causal_mask if self.config.sliding_window is None else create_sliding_window_causal_mask466 causal_mask = mask_function(467 config=self.config,468 input_embeds=inputs_embeds,469 attention_mask=attention_mask,470 cache_position=cache_position,471 past_key_values=past_key_values,472 position_ids=position_ids,473 )474 475 hidden_states = inputs_embeds476 477 # create position embeddings to be shared across the decoder layers478 position_embeddings = self.rotary_emb(hidden_states, position_ids)479 480 for decoder_layer in self.layers[: self.config.num_hidden_layers]:481 hidden_states = decoder_layer(482 hidden_states,483 position_embeddings=position_embeddings,484 attention_mask=causal_mask,485 position_ids=position_ids,486 past_key_values=past_key_values,487 use_cache=use_cache,488 cache_position=cache_position,489 **kwargs,490 )491 492 hidden_states = self.norm(hidden_states)493 494 return MoeModelOutputWithPast( # only diff with Mistral is the output type, we need MoE495 last_hidden_state=hidden_states,496 past_key_values=past_key_values,497 )498 499 500def load_balancing_loss_func(501 gate_logits: Union[torch.Tensor, tuple[torch.Tensor], None],502 num_experts: Optional[int] = None,503 top_k=2,504 attention_mask: Optional[torch.Tensor] = None,505) -> Union[torch.Tensor, int]:506 r"""507 Computes auxiliary load balancing loss as in Switch Transformer - implemented in Pytorch.508 509 See Switch Transformer (https://huggingface.co/papers/2101.03961) for more details. This function implements the loss510 function presented in equations (4) - (6) of the paper. It aims at penalizing cases where the routing between511 experts is too unbalanced.512 513 Args:514 gate_logits:515 Logits from the `gate`, should be a tuple of model.config.num_hidden_layers tensors of516 shape [batch_size X sequence_length, num_experts].517 num_experts:518 Number of experts519 top_k:520 The number of experts to route per-token, can be also interpreted as the `top-k` routing521 parameter.522 attention_mask (`torch.Tensor`, *optional*):523 The attention_mask used in forward function524 shape [batch_size X sequence_length] if not None.525 526 Returns:527 The auxiliary loss.528 """529 if gate_logits is None or not isinstance(gate_logits, tuple):530 return 0531 532 if isinstance(gate_logits, tuple):533 compute_device = gate_logits[0].device534 concatenated_gate_logits = torch.cat([layer_gate.to(compute_device) for layer_gate in gate_logits], dim=0)535 536 routing_weights = torch.nn.functional.softmax(concatenated_gate_logits, dim=-1)537 538 _, selected_experts = torch.topk(routing_weights, top_k, dim=-1)539 540 expert_mask = torch.nn.functional.one_hot(selected_experts, num_experts)541 542 if attention_mask is None:543 # Compute the percentage of tokens routed to each experts544 tokens_per_expert = torch.mean(expert_mask.float(), dim=0)545 546 # Compute the average probability of routing to these experts547 router_prob_per_expert = torch.mean(routing_weights, dim=0)548 else:549 batch_size, sequence_length = attention_mask.shape550 num_hidden_layers = concatenated_gate_logits.shape[0] // (batch_size * sequence_length)551 552 # Compute the mask that masks all padding tokens as 0 with the same shape of expert_mask553 expert_attention_mask = (554 attention_mask[None, :, :, None, None]555 .expand((num_hidden_layers, batch_size, sequence_length, top_k, num_experts))556 .reshape(-1, top_k, num_experts)557 .to(compute_device)558 )559 560 # Compute the percentage of tokens routed to each experts561 tokens_per_expert = torch.sum(expert_mask.float() * expert_attention_mask, dim=0) / torch.sum(562 expert_attention_mask, dim=0563 )564 565 # Compute the mask that masks all padding tokens as 0 with the same shape of tokens_per_expert566 router_per_expert_attention_mask = (567 attention_mask[None, :, :, None]568 .expand((num_hidden_layers, batch_size, sequence_length, num_experts))569 .reshape(-1, num_experts)570 .to(compute_device)571 )572 573 # Compute the average probability of routing to these experts574 router_prob_per_expert = torch.sum(routing_weights * router_per_expert_attention_mask, dim=0) / torch.sum(575 router_per_expert_attention_mask, dim=0576 )577 578 overall_loss = torch.sum(tokens_per_expert * router_prob_per_expert.unsqueeze(0))579 return overall_loss * num_experts580 581 582@auto_docstring583class MiniMaxM2ForCausalLM(MiniMaxM2PreTrainedModel, GenerationMixin):584 _tied_weights_keys = ["lm_head.weight"]585 _tp_plan = {"lm_head": "colwise_rep"}586 _pp_plan = {"lm_head": (["hidden_states"], ["logits"])}587 588 def __init__(self, config):589 super().__init__(config)590 self.model = MiniMaxM2Model(config)591 self.vocab_size = config.vocab_size592 self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)593 self.router_aux_loss_coef = config.router_aux_loss_coef594 self.num_experts = config.num_local_experts595 self.num_experts_per_tok = config.num_experts_per_tok596 597 # Initialize weights and apply final processing598 self.post_init()599 600 @can_return_tuple601 @auto_docstring602 def forward(603 self,604 input_ids: Optional[torch.LongTensor] = None,605 attention_mask: Optional[torch.Tensor] = None,606 position_ids: Optional[torch.LongTensor] = None,607 past_key_values: Optional[Cache] = None,608 inputs_embeds: Optional[torch.FloatTensor] = None,609 labels: Optional[torch.LongTensor] = None,610 use_cache: Optional[bool] = None,611 output_router_logits: Optional[bool] = None,612 cache_position: Optional[torch.LongTensor] = None,613 logits_to_keep: Union[int, torch.Tensor] = 0,614 **kwargs: Unpack[TransformersKwargs],615 ) -> MoeCausalLMOutputWithPast:616 r"""617 labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):618 Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,619 config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored620 (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.621 622 Example:623 624 ```python625 >>> from transformers import AutoTokenizer, MiniMaxM2ForCausalLM626 627 >>> model = MiniMaxM2ForCausalLM.from_pretrained("mistralai/MiniMaxM2-8x7B-v0.1")628 >>> tokenizer = AutoTokenizer.from_pretrained("mistralai/MiniMaxM2-8x7B-v0.1")629 630 >>> prompt = "Hey, are you conscious? Can you talk to me?"631 >>> inputs = tokenizer(prompt, return_tensors="pt")632 633 >>> # Generate634 >>> generate_ids = model.generate(inputs.input_ids, max_length=30)635 >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]636 "Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."637 ```"""638 639 output_router_logits = (640 output_router_logits if output_router_logits is not None else self.config.output_router_logits641 )642 643 # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)644 outputs: MoeModelOutputWithPast = self.model(645 input_ids=input_ids,646 attention_mask=attention_mask,647 position_ids=position_ids,648 past_key_values=past_key_values,649 inputs_embeds=inputs_embeds,650 use_cache=use_cache,651 output_router_logits=output_router_logits,652 cache_position=cache_position,653 **kwargs,654 )655 656 hidden_states = outputs.last_hidden_state657 # Only compute necessary logits, and do not upcast them to float if we are not computing the loss658 slice_indices = slice(-logits_to_keep, None) if isinstance(logits_to_keep, int) else logits_to_keep659 logits = self.lm_head(hidden_states[:, slice_indices, :])660 661 loss = None662 if labels is not None:663 loss = self.loss_function(logits, labels, self.vocab_size, **kwargs)664 665 aux_loss = None666 if output_router_logits:667 aux_loss = load_balancing_loss_func(668 outputs.router_logits,669 self.num_experts,670 self.num_experts_per_tok,671 attention_mask,672 )673 if labels is not None:674 loss += self.router_aux_loss_coef * aux_loss.to(loss.device) # make sure to reside in the same device675 676 return MoeCausalLMOutputWithPast(677 loss=loss,678 aux_loss=aux_loss,679 logits=logits,680 past_key_values=outputs.past_key_values,681 hidden_states=outputs.hidden_states,682 attentions=outputs.attentions,683 router_logits=outputs.router_logits,684 )685 686 687class MiniMaxM2ForSequenceClassification(GenericForSequenceClassification, MiniMaxM2PreTrainedModel):688 pass689 690 691class MiniMaxM2ForTokenClassification(GenericForTokenClassification, MiniMaxM2PreTrainedModel):692 pass693 694 695class MiniMaxM2ForQuestionAnswering(GenericForQuestionAnswering, MiniMaxM2PreTrainedModel):696 pass697 698 699__all__ = [700 "MiniMaxM2ForCausalLM",701 "MiniMaxM2ForQuestionAnswering",702 "MiniMaxM2Model",703 "MiniMaxM2PreTrainedModel",704 "MiniMaxM2ForSequenceClassification",705 "MiniMaxM2ForTokenClassification",706]707 