Fix double softmax in MoE router load-balancing loss#45111
Closed
ionut-anghelina wants to merge 1 commit intohuggingface:mainfrom
Closed
Fix double softmax in MoE router load-balancing loss#45111ionut-anghelina wants to merge 1 commit intohuggingface:mainfrom
ionut-anghelina wants to merge 1 commit intohuggingface:mainfrom
Conversation
Several MoE routers applied softmax to raw logits inside forward() but returned the result as `router_logits`. The load_balancing_loss_func then applied softmax again, computing the aux loss on softmax(softmax(logits)) which flattens the distribution toward uniform, rendering the load-balancing loss ineffective. Fix: use a separate `router_probs` variable for the softmaxed values used in top-k routing, keeping `router_logits` as raw logits so the loss function's single softmax is correct. Source modular files fixed: - mixtral/modular_mixtral.py (MixtralTopKRouter) - qwen2_moe/modular_qwen2_moe.py (Qwen2MoeTopKRouter) - qwen3_vl_moe/modular_qwen3_vl_moe.py (Qwen3VLMoeTextTopKRouter) Downstream models regenerated by make fix-repo: mixtral, minimax, qwen2_moe, olmoe, flex_olmo, qwen3_moe, qwen3_next, qwen3_omni_moe, qwen3_vl_moe, qwen3_5_moe Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: flex_olmo, minimax, mixtral, olmoe, qwen2_moe, qwen3_5_moe, qwen3_moe, qwen3_next, qwen3_omni_moe, qwen3_vl_moe |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
forward()but returned the result asrouter_logits. Theload_balancing_loss_functhen applied softmax again, computing the aux loss onsoftmax(softmax(logits))which flattens the distribution toward uniform, rendering the load-balancing loss ineffective.router_probsvariable for the softmaxed values used in top-k routing, keepingrouter_logitsas raw logits so the loss function's single softmax is correct.Source modular files fixed (3):
mixtral/modular_mixtral.py—MixtralTopKRouterqwen2_moe/modular_qwen2_moe.py—Qwen2MoeTopKRouterqwen3_vl_moe/modular_qwen3_vl_moe.py—Qwen3VLMoeTextTopKRouterDownstream models regenerated by
make fix-repo(10):mixtral, minimax, qwen2_moe, olmoe, flex_olmo, qwen3_moe, qwen3_next, qwen3_omni_moe, qwen3_vl_moe, qwen3_5_moe
Test plan
router_logitsnow returns raw logits (row sums ≠ 1.0) instead of probabilitiesload_balancing_loss_funcapplies softmax exactly oncepytest tests/models/mixtral/ tests/models/qwen2_moe/ tests/models/qwen3_vl_moe/ -v🤖 Generated with Claude Code