← Back to Pull requests

feat(moe): support after-down router weights in TE grouped experts #4474

Closed
unknown closed this pull request without merging on Oct 25, 2026 07:06 UTC
View on GitHub
unknown unknown commented on Oct 21, 2026 22:16 UTC
Author

What does this PR do ?

Models that apply the router weight after the down projection (apply_router_weight_after_down=True: Step 5, Muse Spark Open, Kimi K3, Kimi Linear, GLM-4 MoE, GLM-5 Next) can't use experts: te today, GroupedExpertsTE raises at construction. This PR adds that order to GroupedExpertsTE, so these models can run their experts on TE grouped GEMMs and the TE FP8 recipes.

Changelog

  • GroupedExpertsTE.forward: with apply_router_weight_after_down, the expert MLP runs unweighted and the router weight scales its output after the down projection, in fp32 through _apply_router_weight_fp32. Same order and helper as GroupedExpertsDeepEP. The down bias stays unweighted inside the GEMM, so the result is (expert_out + down_bias) * prob. The multiply uses the probabilities from before FP8 padding.

  • GroupedExpertsTE.forward takes post_expert_output, applied before the router weight like in GroupedExpertsDeepEP. Muse Spark uses it for its per-expert norm.

  • Remove the after-down ValueError in GroupedExpertsTE.__init__ and the experts == "te" check in MuseSpark1xOmniForCausalLM.

  • GroupedExpertsTE now honors compile_router_weight, like GroupedExperts and GroupedExpertsDeepEP.

  • FP32 expert weights: with ep_shard 1 no FSDP mixed-precision wrapper casts the EP-local TE experts, so FP32 master weights meet BF16 dispatched tokens. When the expert weights are not in the token dtype, the two GroupedLinear calls run under torch.autocast in the token dtype, which casts the weights per GEMM like .to(compute_dtype) in GroupedExpertsDeepEP. The empty-rank branch casts the same way. Before-down models on TE get this too.

  • Tests:

  • - CPU unit test on the fake TE fixture: exact output and gradients, the activation gets all-ones weights, with and without a post-expert op. It fails on main at construction.
    - GPU functional test against a per-expert fp32 reference with expert bias, a post-expert RMS norm, bf16 and FP8 current scaling. Expert row counts are not multiples of 16, so the FP8 padding path runs. The test also checks that weighting inside the activation lands outside the tolerance.
    - GPU functional test: FP32 expert weights against BF16 weights with the same values, before-down and after-down, bf16 and FP8 current scaling. Outputs, input and router gradients and weight gradients are bitwise equal and the gradients stay FP32. Another case covers a rank that gets no routed tokens.
    - 2-GPU functional test L2_MoE_TE_FP32_Master_EP2.sh: Qwen3.5-MoE proxy finetune with FP32 parameters, TE experts, HybridEP EP=2 (ep_shard 1), activation checkpointing off vs on.
  • Muse Spark Open docs list TE GroupedLinear experts as supported.
  • Before your PR is "Ready for review"

    Pre checks:

  • Make sure you read and followed Contributor guidelines

  • Did you write any new necessary tests?

  • Did you add or update any necessary documentation?
  • Additional Information

    Numerics, real EP=2 dispatch on 2x H100 (HybridEP and DeepEP), 16 experts top-4, Step 5 style clamped SwiGLU, FP32 router weights, 10% masked tokens, with and without a Muse style post-expert RMS norm. GroupedExpertsDeepEP (torch_mm) and GroupedExpertsTE hold the same weights. Relative error vs torch_mm, max over ranks:

    | | out | dx | d router weight | d gate_up | d down |
    |---|---:|---:|---:|---:|---:|
    | TE bf16, all 8 combos | 0 | 0 | 0 | 0 | 0 |
    | TE FP8 current scaling | 6.6% | 9.4-9.5% | 6.6% | 9.4-9.5% | 8.0% |

    So TE bf16 is bitwise equal to the torch_mm path, outputs and every gradient. The FP8 numbers are the usual FP8 quantization error.

    Expert compute, fwd + bwd, 1 GPU, real shapes per rank, identity dispatch (only the expert MLP, router weight multiply and post norm are timed), TE 2.14.1, 5 rounds with rotating order, median ms. "% of peak" is expert GEMM FLOPs over dense BF16 / FP8 peak (H100: 989 / 1979 TFLOPS).

    H100 80GB HBM3:

    | per rank | torch_mm bf16 | TE bf16 | TE FP8 current | TE FP8 block | % of peak (torch_mm / TE FP8 current) |
    |---|---:|---:|---:|---:|---:|
    | Step 5 SFT EP44, 8 experts x ~4096 rows | 9.83 | 10.05 | 10.97 | 10.76 | 38% / 17% |
    | Step 5 H100 bench EP32, 11 experts x ~5958 rows | 19.79 | 20.10 | 20.78 | 20.74 | 38% / 18% |
    | Muse VLM EP64, 4 experts x ~8192 rows | 22.43 | 21.86 | 18.60 (-17%) | 19.46 | 45% / 27% |
    | Muse LLM EP32, 8 experts x ~4096 rows | 22.89 | 22.51 | 20.25 (-12%) | 20.74 | 44% / 25% |

    B200:

    | per rank | torch_mm bf16 | TE bf16 | TE FP8 current | TE MXFP8 |
    |---|---:|---:|---:|---:|
    | Step 5 SFT EP44 | 5.81 | 5.95 | 6.02 | 5.93 |
    | Step 5 H100 bench EP32 | 11.57 | 11.76 | 11.38 | 11.22 |
    | Muse VLM EP64 | 12.46 | 12.00 | 10.02 (-20%) | 10.15 |
    | Muse LLM EP32 | 12.35 | 12.42 | 10.70 (-13%) | 10.72 |

    Step 5's experts are narrow (inter 1536). On H100 FP8 cuts its expert GEMM time from 4.87 to 3.58 ms, but quantize and FP8 padding add about 2.5 ms, so it doesn't pay off there. Muse's experts (inter 4096) gain 12-20%.

    Few-layer proxies, 2x H100, EP=2 HybridEP, random init, BenchmarkingRecipeForNextTokenPrediction, activation checkpointing, seq 4096, 16 steps (4 warm-up). The per-rank expert load matches the recipes above (Step 5: 16 experts, 8 per rank; Muse: 8 experts, 4 per rank). One discarded warm-up run, then 2 rounds with rotating order, average step time of each run:

    | | torch_mm bf16 | TE bf16 | TE FP8 current |
    |---|---:|---:|---:|
    | Muse Spark, layers 0-3 | 476.1 / 472.8 ms | 467.7 / 466.9 ms | 449.2 / 450.8 ms (-5%) |
    | Step 5, layers 3-6 | 287.3 / 269.0 ms | 286.4 / 275.4 ms | 288.9 / 290.3 ms |

    Muse step-0 loss is 12.2163 for all three. On Step 5 the TE experts get TE's own random init (GroupedLinear.reset_parameters), so its step-0 loss isn't comparable across backends; the parity runs above cover the numerics. FP8 adds 1.2-1.6 GiB of allocated memory in these proxies.

    FP32 master weights, 2x H100, EP=2 HybridEP, ep_shard 1, FP32 parameters with the default BF16 FSDP compute policy:

    | | before the FP32 fix (cd839fa) | this PR |
    |---|---|---|
    | Muse Spark 4-layer proxy, TE experts | first forward fails: Found input dtype: torch.bfloat16 and 'weight0' dtype: torch.float32 | trains, AC off and on give the same step-0 grad norms |
    | L2_MoE_TE_FP32_Master_EP2.sh (Qwen3.5-MoE proxy) | same error | 4 steps, AC off vs on: step-0 loss and grad norm equal, later losses within 1.2e-4 |
    | FP32-vs-BF16-weight GPU tests | 5 fail (4 TE dtype errors, 1 empty-rank matmul) | pass |

    The L2 test sets optimizer.foreach false, like the TE recipes do (see the note in examples/llm_finetune/moonlight/moonlight_16b_te.yaml): the TE expert params are plain tensors next to the FSDP DTensors, and foreach AdamW rejects the mix.

    Part of the "MoE Backend Improvements" item in #2952.

    GroupedExpertsTE.forward (including the empty-rank branch) also changes in #4429 (draft), happy to rebase whichever lands first.

    Not tested: real weights, multi-node, TE >= 2.19 native grouped tensor path (the 26.08 image has TE 2.14.1), and the other after-down models one by one (they go through the same expert class).

    Tests run on the head commit (26.08.00 image + main's Python deps, --git-base set to this PR's base for the runtime-budget plugin):

  • 1x H100: tests/functional_tests/moe/test_te_routing_gradients.py: 17 passed. tests/unit_tests/moe/test_experts.py tests/unit_tests/models/muse_spark/test_model.py tests/unit_tests/models/step5: 542 passed.

  • 2x H100: L2_MoE_TE_FP32_Master_EP2.sh passes.

  • CPU: tests/unit_tests/moe/test_experts.py tests/unit_tests/moe/test_router_weight_mul.py tests/unit_tests/models/muse_spark tests/unit_tests/models/step5 tests/unit_tests/models/common: 802 passed, 2 failed. The 2 are muse_spark/test_layers.py::test_compiled_norm_residual_preserves_branch_rounding, which fail the same way on main in this container.

  • B200 (at cd839fa, before the FP32 fix): tests/functional_tests/moe/test_te_routing_gradients.py: 12 passed.

  • ruff format, ruff check, python tools/lint_no_globals.py.
  • Implementation and validation were AI-assisted.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: ischencheng Review duration: 4850m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #4474"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%