Repository navigation
refactor(trainer): add iter_moe_blocks for the MoE layer lookup - #3843
Merged
Merged
Conversation
faresobeid
marked this pull request as ready for review
October 4, 2026 12:29
This was referenced Oct 4, 2026
faresobeid
force-pushed
the
refactor/trainer-moe
branch
from
October 7, 2026 19:10
eca73a5 to
2cf0399
Compare
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
Add
iter_moe_blocks(model)intrainer/model.py. It yields theMoEMLP of each decoder layer. It replaces the five copies of the decoder-layer MoE lookup infreeze_moe_router,apply_fp32_moe_router,get_full_offload_dtype_policy,apply_force_balanced_routingandget_load_balance_stats.What changed
iter_moe_blocksnext to the helpers that use it. The helpers stay inmodel.py, so no imports change.layer.mlp if hasattr(...) else layer.feed_forward ...lookups and thegetattr(mlp)+hasattr(tokens_per_expert)scan inget_load_balance_statsnow calliter_moe_blocks.get_expert_load_stats. The HF fallback was removed in chore(trainer): remove HF model-implementation fallback (model.impl) #3773.Behavior change
None. The
feed_forwardfallback is gone. Every prime-rl decoder layer usesmlp.get_load_balance_statsalready looked only atmlp. OnlyMoEregisterstokens_per_expert, so it selects the same layers as before.Checks
main(throwaway script, not in the repo). Tiny Qwen3-MoE (mlp_only_layers=[1]) and GLM4-MoE (first_k_dense_replace=1) models with fixed router-count buffers. Comparedis_tt_moe_model,get_load_balance_stats,get_global_moe_stats,compute_expert_load_stats,get_expert_load_statsandget_full_offload_dtype_policy, plus parameter dtype andrequires_grad, and routerfp32_gate/force_balancedafter the three router helpers. All 16 outputs were bit-identical.pytest tests/unit/train tests/unit/orchestrator tests/unit/test_configs* -m "not gpu": 450 passed, 1 failed. The failure istest_cpu_adam.py::test_native_cpu_adamw_matches_fused_torch_and_preserves_gradients(ninja build), which fails the same way onmain.ruff checkandruff format --check.samsja/mini-glm-moe, 20 steps, one node),mainvs this PR. Both finished. Both froze 23 router params and ran 23 router gates in fp32. Max Vio mean 0.610 (main) vs 0.613 (PR), per-step range 0.57–0.66 in both. Routing Conf. 0.6514–0.6533 vs 0.6512–0.6536. Rollouts are sampled, so per-step values differ by run noise only.