from collections.abc import Callable import torch import torch.nn.functional as F from torch import nn from ... import initialization as init from ...cache_utils import Cache from ...modeling_flash_attention_utils import FlashAttentionKwargs from ...modeling_layers import GenericForSequenceClassification, GenericForTokenClassification from ...modeling_utils import ALL_ATTENTION_FUNCTIONS, PreTrainedModel from ...processing_utils import Unpack from ...utils import logging from ..deepseek_v2.modeling_deepseek_v2 import ( DeepseekV2Attention, DeepseekV2Moe, DeepseekV2TopkRouter, ) from ..llama.modeling_llama import ( LlamaDecoderLayer, LlamaForCausalLM, LlamaModel, LlamaPreTrainedModel, LlamaRMSNorm, LlamaRotaryEmbedding, apply_rotary_pos_emb, eager_attention_forward, ) from ..qwen2_moe.modeling_qwen2_moe import Qwen2MoeExperts, Qwen2MoeMLP from .configuration_deepseek_v3 import DeepseekV3Config logger = logging.get_logger(__name__) class DeepseekV3RMSNorm(LlamaRMSNorm): pass class DeepseekV3RotaryEmbedding(LlamaRotaryEmbedding): pass class DeepseekV3MLP(Qwen2MoeMLP): pass def apply_rotary_pos_emb_interleave(q, k, cos, sin, position_ids=None, unsqueeze_dim=1): r""" Applies interleaved Rotary Position Embedding to the query and key tensors. DeepSeek lays the rotary dimensions out in interleaved pairs `(x0, x1), (x2, x3), ...`, each rotated by a single frequency. We compute that rotation directly on the even/odd slices instead of de-interleaving with a `view`/`transpose`/`reshape`; the output is bit-identical to the de-interleaved `rotate_half` formulation while avoiding the extra contiguous copy. Args: q (`torch.Tensor`): The query tensor. k (`torch.Tensor`): The key tensor. cos (`torch.Tensor`): The cosine part of the rotary embedding. sin (`torch.Tensor`): The sine part of the rotary embedding. position_ids (`torch.Tensor`): The position indices of the tokens corresponding to the query and key tensors. For example, this can be used to pass offsetted position ids when working with a KV-cache. unsqueeze_dim (`int`, *optional*, defaults to 1): The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2. Returns: `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding. """ # `cos`/`sin` are `cat(freqs, freqs)`; the first half holds the per-pair angle. cos = cos[..., : cos.shape[-1] // 2].unsqueeze(unsqueeze_dim) sin = sin[..., : sin.shape[-1] // 2].unsqueeze(unsqueeze_dim) q1, q2 = q[..., 0::2], q[..., 1::2] k1, k2 = k[..., 0::2], k[..., 1::2] q_embed = torch.cat([q1 * cos - q2 * sin, q2 * cos + q1 * sin], dim=-1) k_embed = torch.cat([k1 * cos - k2 * sin, k2 * cos + k1 * sin], dim=-1) return q_embed, k_embed class DeepseekV3TopkRouter(DeepseekV2TopkRouter): def __init__(self, config): super().__init__(config) del self.topk_method self.num_experts = config.num_local_experts self.norm_topk_prob = config.norm_topk_prob self.e_score_correction_bias = nn.Buffer(torch.zeros(self.num_experts)) def forward(self, hidden_states): hidden_states = hidden_states.view(-1, self.hidden_dim) router_logits = F.linear(hidden_states.type(torch.float32), self.weight.type(torch.float32)) scores = router_logits.sigmoid() scores_for_choice = scores + self.e_score_correction_bias group_scores = ( scores_for_choice.view(-1, self.num_group, self.num_experts // self.num_group) .topk(2, dim=-1)[0] .sum(dim=-1) ) group_idx = torch.topk(group_scores, k=self.topk_group, dim=-1, sorted=False)[1] group_mask = torch.zeros_like(group_scores) group_mask.scatter_(1, group_idx, 1) score_mask = ( group_mask.unsqueeze(-1) .expand(-1, self.num_group, self.num_experts // self.num_group) .reshape(-1, self.num_experts) ) scores_for_choice = scores_for_choice.masked_fill(~score_mask.bool(), float("-inf")) topk_indices = torch.topk(scores_for_choice, k=self.top_k, dim=-1, sorted=False)[1] topk_weights = scores.gather(1, topk_indices) if self.norm_topk_prob: denominator = topk_weights.sum(dim=-1, keepdim=True) + 1e-20 topk_weights /= denominator topk_weights = topk_weights * self.routed_scaling_factor return router_logits, topk_weights, topk_indices class DeepseekV3Experts(Qwen2MoeExperts): def __init__(self, config): super().__init__(config) self.num_experts = config.num_local_experts class DeepseekV3MoE(DeepseekV2Moe): """ A mixed expert module containing shared experts. """ class DeepseekV3Attention(DeepseekV2Attention): """Multi-headed Latent Attention (MLA) from Deepseek V2""" def forward( self, hidden_states: torch.Tensor, position_embeddings: tuple[torch.Tensor, torch.Tensor], attention_mask: torch.Tensor | None, past_key_values: Cache | None = None, **kwargs: Unpack[FlashAttentionKwargs], ) -> tuple[torch.Tensor, torch.Tensor | None, tuple[torch.Tensor] | None]: batch_size, seq_length = hidden_states.shape[:-1] query_shape = (batch_size, seq_length, -1, self.qk_head_dim) if self.q_lora_rank is None: q_states = self.q_proj(hidden_states) else: q_states = self.q_b_proj(self.q_a_layernorm(self.q_a_proj(hidden_states))) q_states = q_states.view(query_shape).transpose(1, 2) q_pass, q_rot = torch.split(q_states, [self.qk_nope_head_dim, self.qk_rope_head_dim], dim=-1) compressed_kv = self.kv_a_proj_with_mqa(hidden_states) kv_nope, k_rot = torch.split(compressed_kv, [self.kv_lora_rank, self.qk_rope_head_dim], dim=-1) kv_nope = self.kv_a_layernorm(kv_nope) # Both latents are viewed as single-head, 4D tensors so all cache layers handle them correctly kv_nope = kv_nope.view(batch_size, 1, seq_length, self.kv_lora_rank) k_rot = k_rot.view(batch_size, 1, seq_length, self.qk_rope_head_dim) cos, sin = position_embeddings if self.config.rope_interleave: # support using interleaved weights for efficiency q_rot, k_rot = apply_rotary_pos_emb_interleave(q_rot, k_rot, cos, sin) else: q_rot, k_rot = apply_rotary_pos_emb(q_rot, k_rot, cos, sin) # Cache read / write is performed while latent KV is still compressed if past_key_values is not None: kv_nope, k_rot = past_key_values.update(kv_nope, k_rot, self.layer_idx) query_states = torch.cat((q_pass, q_rot), dim=-1) key_states, value_states = self.expand_kv(kv_nope, k_rot) attention_interface: Callable = ALL_ATTENTION_FUNCTIONS.get_interface( self.config._attn_implementation, eager_attention_forward ) attn_output, attn_weights = attention_interface( self, query_states, key_states, value_states, attention_mask, dropout=0.0 if not self.training else self.attention_dropout, scaling=self.scaling, **kwargs, ) attn_output = attn_output.reshape(batch_size, seq_length, -1).contiguous() attn_output = self.o_proj(attn_output) return attn_output, attn_weights class DeepseekV3DecoderLayer(LlamaDecoderLayer): def __init__(self, config: DeepseekV3Config, layer_idx: int): nn.Module.__init__(self) self.hidden_size = config.hidden_size self.self_attn = DeepseekV3Attention(config=config, layer_idx=layer_idx) if layer_idx >= config.first_k_dense_replace: self.mlp = DeepseekV3MoE(config) else: self.mlp = DeepseekV3MLP(config) self.input_layernorm = DeepseekV3RMSNorm(config.hidden_size, eps=config.rms_norm_eps) self.post_attention_layernorm = DeepseekV3RMSNorm(config.hidden_size, eps=config.rms_norm_eps) class DeepseekV3PreTrainedModel(LlamaPreTrainedModel): _keep_in_fp32_modules_strict = ["e_score_correction_bias"] _keys_to_ignore_on_load_unexpected = [r"model\.layers\.61.*"] @torch.no_grad() def _init_weights(self, module): PreTrainedModel._init_weights(self, module) if isinstance(module, DeepseekV3TopkRouter): init.normal_(module.weight, mean=0.0, std=self.config.initializer_range) init.zeros_(module.e_score_correction_bias) elif isinstance(module, DeepseekV3Experts): init.normal_(module.gate_up_proj, mean=0.0, std=self.config.initializer_range) init.normal_(module.down_proj, mean=0.0, std=self.config.initializer_range) class DeepseekV3Model(LlamaModel): pass class DeepseekV3ForCausalLM(LlamaForCausalLM): pass class DeepseekV3ForSequenceClassification(GenericForSequenceClassification, DeepseekV3PreTrainedModel): pass class DeepseekV3ForTokenClassification(GenericForTokenClassification, DeepseekV3PreTrainedModel): pass __all__ = [ "DeepseekV3PreTrainedModel", "DeepseekV3Model", "DeepseekV3ForCausalLM", "DeepseekV3ForSequenceClassification", "DeepseekV3ForTokenClassification", ]