[feat] Add Qwen3 MoE true-on-policy parity#30
Draft
maocheng23 wants to merge 23 commits into
Draft
Conversation
This was referenced May 1, 2026
c63c77f to
9c390a1
Compare
2 tasks
9546575 to
57258c8
Compare
Squash merge of the dense true-on-policy Megatron branch. Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn> Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com> Co-authored-by: Yi Zhang <1109276519@qq.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
ab103f8 to
9c6c2a9
Compare
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn> Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com> Co-authored-by: Yi Zhang <1109276519@qq.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn> Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com> Co-authored-by: Yi Zhang <1109276519@qq.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn> Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com> Co-authored-by: Yi Zhang <1109276519@qq.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn> Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com> Co-authored-by: Yi Zhang <1109276519@qq.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn> Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com> Co-authored-by: Yi Zhang <1109276519@qq.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
- forward.py: single canonical sglang_moe_forward (102 lines) - autograd.py: pure backward wrapper calling shared forward (182 lines) - moe_experts.py: weight-layout adapter only, no forward logic (36 lines) - moe_layer_ext.py: one linear orchestration path (301 lines) - Remove verbose RuntimeError guards, replace with asserts - Remove weight caching (premature optimization) - Consolidate two parallel forward paths into one Net: -574 lines, structurally impossible for forward paths to diverge. Co-authored-by: Cursor <cursoragent@cursor.com>
7ac2a80 to
b0679e2
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
Adds the Qwen3-MoE true-on-policy Megatron implementation on top of the dense true-on-policy stack.
This PR is stacked on
feat/true_on_policy_qwen_dense; the intended review surface is now the direct SGLang MoE path plus the small dense-stack compatibility fixes needed for MoE parity.This is one of three tightly-coupled MoE PRs that should land together because they share the
qwen3_moe_true_on_policy_v1contract.Companion PRs, must land in lockstep:
Stacked on dense, must land first:
Target
Bit-identical logprob parity between SGLang rollout and Megatron training for scored response tokens on Qwen3-30B-A3B MoE, with differentiable Megatron backward.
Previously validated on H200 x8 at Megatron TP=1/EP=4/CP=2/PP=1 with SGLang 2 engines TP=4/EP=4:
train_rollout_logprob_abs_diff = 0.0for the checked steps, with non-zero gradients across attention, embeddings, layernorms, MoE experts, MoE routers, and output layer.Dense-To-MoE Delta
The dense implementation remains the base contract. This PR now keeps the MoE delta narrow:
MoELayercalls the true-on-policy extension at the top of the route phase. When theqwen3_moe_true_on_policy_v1contract requiresqwen3_moe_sglang_math, the extension uses the direct SGLang EP path or raises; unsupported layouts no longer silently fall back based on helper/function existence or EP-size-derived predicates.stable_topk_softmax, using Megatron router weights but without modifying Megatron's generic router implementation.fused_experts; grad-enabled training uses the same SGLang forward wrapped by a local PyTorch autograd function and Triton backward kernels.Main Files
miles_megatron_plugins/true_on_policy/moe_layer_ext.pymiles_megatron_plugins/true_on_policy/moe_experts.pyfused_expertscall.megatron/core/transformer/moe/sgl_fused_moe/megatron/core/transformer/moe/moe_layer.pymegatron/core/models/gpt/gpt_layer_specs.pymegatron/core/optimizer/cpu_offloading/hybrid_optimizer.pyRemoved During Cleanup
GroupedMLPfallback dtype patch.router.py,moe_utils.py, andtoken_dispatcher.pyparity edits; the direct path no longer relies on those fallback routes.Validation
Remote H200 x8 validation from
recovery/qwen3_moe_clean/journal/2026-04-30-moe-onpolicy-normal-validation.md:Current local smoke checks for the latest PR updates:
git diff --checkPYENV_VERSION=system python3 -m py_compile ...on changed Python filesCPU/unit coverage:
tests/unit_tests/extension/test_sglang_extension.pytests/unit_tests/extension/test_sglang_moe_fast_topk_route.pyKnown Constraints
Test Plan