Repository navigation
Use the sequence-wise balance loss for DeepSeek-V4 routers - #5850
AusarYao28 wants to merge 1 commit into
Conversation
|
🤖 Hi @AusarYao28, I've received your request, and I'm working on it now! You can track my progress in the logs for more details. |
There was a problem hiding this comment.
Code Review
This pull request extends the Megatron-LM sequence-wise auxiliary load-balancing loss to support Sqrt(Softplus) routers, which are used in DeepSeek-V4 models. It updates the configuration files for deepseek4-1600b, deepseek4-284b, and deepseek4-tiny to enable this loss, adjusts the routing logic in moe.py to recognize sqrtsoftplus as a valid routing score function, and updates relevant documentation. Additionally, a new unit test spec is introduced to validate the DeepSeek-V4 balance loss against a reference implementation, and existing tests are updated. There are no review comments, so no further feedback is provided.
There was a problem hiding this comment.
📋 Review Summary
Verdict: Spec met; the implementation correctly extends the DeepSeek-V3 seq aux loss to sqrtsoftplus routers, and the tests accurately validate it against a spec reference.
Top issues:
- [Alignment]
src/maxtext/experimental/lineage/lineage_adapter.pyhas a mirrored copy ofuses_megatron_seq_aux_lossthat was not updated. See inline comment onmoe.py. → Update the Lineage adapter's copy for consistency, even if Lineage currently assertssigmoidfor other reasons.
Inferred spec & validation
- Sources: PR description, code before/after, and newly added tests.
- Problem: DeepSeek-V4 uses
sqrtsoftplusaffinities, which fell back to the Switch-style loss becauseuses_megatron_seq_aux_lossonly checked forsigmoid, leading to incorrect load balancing.
| Spec item | Implementation | Validating test |
|---|---|---|
R1 (stated): Allow sqrtsoftplus to trigger uses_megatron_seq_aux_loss. |
✅ | test_config_matches_spec ✅ |
| R2 (stated): Apply V3-style seq aux loss on Sqrt(Softplus) outputs correctly. | ✅ | test_loss_value_matches_spec, test_router_gradient_matches_spec, test_score_gradient_matches_spec ✅ |
R3 (stated): Update configs to enable moe_use_megatron_seq_aux_loss. |
✅ | test_config_matches_spec ✅ |
C1 (inferred): Keep uses_megatron_seq_aux_loss synced across the codebase. |
❌ partial divergence | none |
| return ( | ||
| getattr(config, "moe_use_megatron_seq_aux_loss", False) | ||
| and getattr(config, "routed_score_func", "") == "sigmoid" | ||
| and getattr(config, "routed_score_func", "") in ("sigmoid", "sqrtsoftplus") |
There was a problem hiding this comment.
🟠 Please also update uses_megatron_seq_aux_loss in src/maxtext/experimental/lineage/lineage_adapter.py (which mirrors this function) to include "sqrtsoftplus". Even though Lineage doesn't yet support V4 (it explicitly asserts sigmoid), keeping the mirrored function in sync prevents silent divergence when V4 support is eventually added to Lineage.
| and getattr(config, "routed_score_func", "") in ("sigmoid", "sqrtsoftplus") | |
| and getattr(config, "routed_score_func", "") in ("sigmoid", "sqrtsoftplus") |
There was a problem hiding this comment.
Done — updated uses_megatron_seq_aux_loss in src/maxtext/experimental/lineage/lineage_adapter.py to include "sqrtsoftplus". (Commit e35cb28cd)
uses_megatron_seq_aux_loss accepted only routed_score_func == "sigmoid", so DeepSeek-V4 (Sqrt(Softplus) router) always fell back to the Switch-style loss (softmax probabilities, bias-adjusted top-k) although its configs set load_balance_loss_weight 1e-4 for the DeepSeek-V3 sequence-wise balance loss. - Accept "sqrtsoftplus" in uses_megatron_seq_aux_loss (src/maxtext/layers/moe.py). Both callers already pass the pre-bias Sqrt(Softplus) affinities (MoeGate applies score_func before recording pre_bias_logits) and skip hash-routed layers, so no other path changes. - Set moe_use_megatron_seq_aux_loss: true in deepseek4-284b.yml, deepseek4-1600b.yml and deepseek4-tiny.yml. As for sigmoid routers, the per-layer losses are now summed over MoE layers (train.py) instead of averaged; the DeepSeek-V4 paper does not state the layer aggregation. - Add tests/unit/deepseek_v4_aux_loss_spec_test.py: deepseek4-tiny RoutedMoE loss value and router gradient vs an independent float64 reference of DeepSeek-V3 Eqs 17-20 with Sqrt(Softplus) scores (fails before, passes after); update the predicate test in moe_megatron_aux_loss_test.py. FIXES: b/521990776
bd21ce5 to
e35cb28
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Description
uses_megatron_seq_aux_lossaccepted onlyrouted_score_func == "sigmoid", so DeepSeek-V4 (routed_score_func: "sqrtsoftplus") fell back to the Switch-style load-balancing loss (softmax probabilities and bias-adjusted top-$k$ averaged across layers) even though its configs setload_balance_loss_weight: 1e-4for the DeepSeek-V3 sequence-wise auxiliary load-balancing loss (DeepSeek-V3 Eqs. 17–20)."sqrtsoftplus"inuses_megatron_seq_aux_loss(src/maxtext/layers/moe.py). Both callers already pass the pre-biasMoeGateappliesscore_funcbefore recordingpre_bias_logits) and skip hash-routed layers (is_hash_routing), so no other path changes.moe_use_megatron_seq_aux_loss: trueindeepseek4-284b.yml,deepseek4-1600b.yml, anddeepseek4-tiny.yml(matching the existingsigmoidconvention inbase.ymlandtypes.pywhere per-layer sequence-wise auxiliary losses are summed over routed MoE layers).tests/unit/deepseek_v4_aux_loss_spec_test.pyverifyingdeepseek4-tinyRoutedMoEauxiliary loss value and router gradient against an independentfloat64reference of DeepSeek-V3 Eqs. 17–20 withtests/unit/moe_megatron_aux_loss_test.py.FIXES: b/521990776
Tests
tests/unit/deepseek_v4_aux_loss_spec_test.pyandtests/unit/moe_megatron_aux_loss_test.py(14 passed),tests/unit/configs_test.py(81 passed).src/maxtext/layers/moe.pyandsrc/maxtext/configs/models/deepseek4-tiny.ymlcausestest_config_matches_spec,test_loss_value_matches_spec, andtest_router_gradient_matches_specintests/unit/deepseek_v4_aux_loss_spec_test.pyto fail (3 failed, 1 passed).pyink,pylint(10.00/10),yamllint, andcodespellclean.Checklist
Before submitting this PR, please make sure (put X in square brackets):
gemini-reviewlabel.