Skip to content

[Perf] moe: apply the routed scaling factor inside the fused gate for FP8 MegaMoE - #44878

Closed
kpham-sgl wants to merge 1 commit into
sgl-project:mainfrom
kpham-sgl:kpham/dsv41-routed-scale-in-gate
Closed

kpham-sgl wants to merge 1 commit into
sgl-project:mainfrom
kpham-sgl:kpham/dsv41-routed-scale-in-gate

Conversation

@kpham-sgl

@kpham-sgl kpham-sgl commented Oct 17, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

In a DeepSeek-V4.1-Flash DEP2 prefill step on GB300 (MegaMoE all-to-all backend, FP8 expert method, eager prefill), every MoE layer runs one vectorized_elementwise_kernel of 2 µs right before mega_moe_pre_dispatch: topk_weights_in * routed_scaling_factor in run_mega_routed_experts. 40 launches per 16K-token step, each costing 40 to 65 µs of dispatch on the eager path for 2 µs of GPU work.

The multiply exists because _fuses_routed_scaling_factor_in_topk returns False for Fp8MoEMethod unless the runner is CUTLASS or FlashInfer TRT-LLM routed, so the top-k config gets apply_routed_scaling_factor_on_output=False, the fused gate leaves the weights unscaled, and the MegaMoE runner scales them afterwards.

Modifications

  • fused_moe_triton/layer.py: _fuses_routed_scaling_factor_in_topk also returns True for Fp8MoEMethod when the MoE all-to-all backend is MegaMoE. The model then builds its top-k with apply_routed_scaling_factor_on_output=True, the fused gate multiplies the selected weights by the factor in registers (_router_triton_kernel, APPLY_SCALE), and run_mega_routed_experts takes its existing routed_scaling_factor=1.0 branch. The FlashInfer MegaMoE and FlyDSL MegaMoE paths read the same flag and already skip their own multiply when it is set.
  • test_mega_moe_deepgemm_api.py: the flag is True for an FP8 method under MegaMoE and False under the plain all-to-all backend.

Numerics: the same fp32 weights, scaled inside the gate after renormalization instead of in a separate kernel after it; fused shared-expert slots are not used on this path (num_fused_shared_experts is 0 with MegaMoE).

Measured

GB300, DEP2 (TP2 + DP attention, EP2), MegaMoE, Mega-mHC, decoder bounded replay, eager prefill, 16K-token prefill budget per rank, same node, on top of #44875 (job 3487) against that plus this change and #44879 together (job 3488; the two landed in the same run because the cluster worktree is a live mount).

Rank 0, cold 16K chunk, profiled step before after
Kernel launches 1,407 1,303
topk_weights * routed_scaling_factor launches 40 0
Launches removed by the page-slot kernel of #44879 64
Unprofiled, median of 3 (ms) before after
1 x 16K cold chunk per rank 191 190
1 x 128K cold prompt, eight chunks 1,511 1,501

GPU kernel time is unchanged; the gain is dispatch on the eager path. GSM8K on GB300 (DEP2, DSpark with real acceptance, greedy, 1,314 examples): 0.9741 with all three of #44875, #44878 and #44879 applied (job 3495) and 0.9741 on the same stack without them (job 3496), the same 1,280 correct.

Before and after

Perfetto, GPU stream of rank 0, the MoE prologue of layer 6 (from the Mega-mHC kernel to the MegaMoE kernel).

Before (job 3487): 7 slices; the 2.1 µs vectorized_elementwise_kernel<4, AUnaryFunctor<float>> between _router_triton_kernel and mega_moe_pre_dispatch is the routed-scaling multiply.

before, MoE prologue

After (job 3488): 6 slices; the multiply is inside the gate.

after, MoE prologue

Is the gain significant?

Marginal. It removes 40 launches of 2 µs each per 16K step, about 2 ms of dispatch on the eager path and nothing inside a graph; the step-time change is below what one run can resolve. It is a one-line consistency fix, the gate already had the code path, and it is worth taking for that reason rather than for speed.

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ❌ Run #39021145543
Latest PR Test (Extra): ❌ Run #39021145533
Latest PR Test (AMD ROCm 10): ❌ Run #39021145563

… FP8 MegaMoE

With the MegaMoE all-to-all backend the FP8 expert method did not claim
the routed scaling factor, so the top-k left it to the expert runner and
run_mega_routed_experts multiplied the weights in a separate elementwise
launch on every MoE layer, 40 per DeepSeek-V4.1-Flash prefill step. The
fused gate already applies the factor in registers when the top-k config
asks for it, and the MegaMoE runner already skips its multiply when the
expert method claims the fusion; this joins the two for FP8 experts under
MegaMoE. The weights are the same fp32 values, scaled in the gate instead
of after it.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@kpham-sgl

Copy link
Copy Markdown
Collaborator Author

/rerun-test registered/unit/layers/moe/test_mega_moe_deepgemm_api.py registered/unit/layers/moe/test_flashinfer_megamoe.py registered/e2e/models/test_deepseek_v41_flash_gb300_tp4ep4.py registered/e2e/models/test_deepseek_v4_flash_fp4_megamoe_b200.py

@github-actions

github-actions Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

🚀 ubuntu-latest (2 tests): ✅ View workflow run

cd test/ && python3 registered/unit/layers/moe/test_mega_moe_deepgemm_api.py
cd test/ && python3 registered/unit/layers/moe/test_flashinfer_megamoe.py

🚀 4-gpu-gb300 (1 test): ✅ View workflow run

cd test/ && python3 registered/e2e/models/test_deepseek_v41_flash_gb300_tp4ep4.py

🚀 4-gpu-b200 (1 test): ✅ View workflow run

cd test/ && python3 registered/e2e/models/test_deepseek_v4_flash_fp4_megamoe_b200.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant