Repository navigation
Description
System Info
Hello, the DeepseekV3MoE class in transformers (native) differs from the official remote DeepSeekV3
implementation (which was updated for a bug but not in transformers, hence the difference).
See trf DeepSeekV3 MoE code
transformers/src/transformers/models/deepseek_v3/modular_deepseek_v3.py
Lines 113 to 166 in 155db71
| class DeepseekV3MoE(nn.Module): | |
| """ | |
| A mixed expert module containing shared experts. | |
| """ | |
| def __init__(self, config): | |
| super().__init__() | |
| self.config = config | |
| self.experts = DeepseekV3NaiveMoe(config) | |
| self.gate = DeepseekV3TopkRouter(config) | |
| self.shared_experts = DeepseekV3MLP( | |
| config=config, intermediate_size=config.moe_intermediate_size * config.n_shared_experts | |
| ) | |
| self.n_routed_experts = config.n_routed_experts | |
| self.n_group = config.n_group | |
| self.topk_group = config.topk_group | |
| self.norm_topk_prob = config.norm_topk_prob | |
| self.routed_scaling_factor = config.routed_scaling_factor | |
| self.top_k = config.num_experts_per_tok | |
| def route_tokens_to_experts(self, router_logits): | |
| router_logits = router_logits.sigmoid() | |
| router_logits_for_choice = router_logits + self.gate.e_score_correction_bias | |
| group_scores = ( | |
| router_logits_for_choice.view(-1, self.n_group, self.n_routed_experts // self.n_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.n_group, self.n_routed_experts // self.n_group) | |
| .reshape(-1, self.n_routed_experts) | |
| ) | |
| scores_for_choice = router_logits_for_choice.masked_fill(~score_mask.bool(), 0.0) | |
| topk_indices = torch.topk(scores_for_choice, k=self.top_k, dim=-1, sorted=False)[1] | |
| topk_weights = router_logits.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 topk_indices, topk_weights | |
| def forward(self, hidden_states): | |
| residuals = hidden_states | |
| orig_shape = hidden_states.shape | |
| router_logits = self.gate(hidden_states) | |
| topk_indices, topk_weights = self.route_tokens_to_experts(router_logits) | |
| hidden_states = hidden_states.view(-1, hidden_states.shape[-1]) | |
| hidden_states = self.experts(hidden_states, topk_indices, topk_weights).view(*orig_shape) | |
| hidden_states = hidden_states + self.shared_experts(residuals) | |
| return hidden_states |
The divergence comes from how experts are masked in the specific case of aux-loss free load balancing and negative
biases being added.
scores_for_choice is normally not negative (sigmoid [0,1]) but it can with + negative bias. So 0.0 is not a floor anymore, but just another value in the score distribution hence the need to switch from masked_fill(~score_mask.bool(), 0.0) to masked_fill(~score_mask.bool(), -inf).
See the official remote update:
https://huggingface.co/deepseek-ai/DeepSeek-V3-0324/commit/e9b33add76883f293d6bf61f6bd89b497e80e335#d2h-632685
Same fix is applied across the whole V3-* collection: https://huggingface.co/collections/deepseek-ai/deepseek-v3
(btw, I also need this for the MiMo-V2 model PR #45144 in order to inherit properly.)
Who can help?
Was discussed with Vasqu already cc'ed in the PR
Information
- The official example scripts
- My own modified scripts
Tasks
- An officially supported task in the
examplesfolder (such as GLUE/SQuAD, ...) - My own task or dataset (give details below)
Reproduction
See example below (where a masked expert is chosen over a valid expert with a negative score)
score_mask = [1,1,0,0] (Only experts 0 and 1 should be eligible)
With 0.0:
let's say scores_for_choice = [0.9, -0.1, 0.0, 0.0]
topk(k=2) → expert 0 (0.9) and expert 3 (0.0) are chosen = bad
Expert 3 is in the non-selected group but beats expert 1 (−0.1).
Expected behavior
With float("-inf"):
scores_for_choice = [0.9, -0.1, -inf, -inf]
topk(k=2) → expert 0 (0.9) and expert 1 (-0.1) = good
Activity