Skip to content

[Perf] moe: Defer finalize for MXFP8 experts on the routed TRT-LLM runner - #46387

Closed
wenchen76 wants to merge 7 commits into
sgl-project:mainfrom
wenchen76:mxfp8-routed-deferred-finalize
Closed

wenchen76 wants to merge 7 commits into
sgl-project:mainfrom
wenchen76:mxfp8-routed-deferred-finalize

Conversation

@wenchen76

@wenchen76 wenchen76 commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #42654. The diff below includes that PR's commits until it merges. The changes in this PR are only in the last commit: ac5cde8.

Motivation

This PR covers the MXFP8 case of #39299. #42654 covers the NVFP4 case, and this is its follow-up 1: MXFP8 experts on the routed runner (Fp8MoEMethod with use_mxfp8, the RL rollout path from #28676). The scope, limited to MXFP8, and the kernel-level comparison at V4.1-Flash shapes (worst max abs diff / max abs ref 4.9e-3) come from @vincentzed's description in #42654. This PR adds the wiring and an end-to-end run on an MXFP8 checkpoint.

Routed deferred finalize applies to the same models as NVFP4 in #42654.

Modifications

  • FusedMoE: supports_routed_deferred_finalize also accepts Fp8MoEMethod with MXFP8 experts. Experts converted to block FP8 (convert_mxfp8_to_block) are excluded. The other conditions are the same as NVFP4: SGLANG_ENABLE_MOE_DEFERRED_FINALIZE, no A2A backend, and the routed scaling factor fused into the top-k weights.
  • fused_experts_none_to_flashinfer_trtllm_fp8, routed branch:
    • It builds one kwargs set. The eager path calls the existing custom-op wrapper with the same arguments as before.
    • With deferral on, it calls FlashInfer trtllm_fp8_block_scale_routed_moe with do_finalize=False and wraps the result with _make_deferred_finalize_output.
    • Routed deferral requires MXFP8. Block FP8 and per-tensor routed calls still raise. The logits-based condition is unchanged.
  • The deferred routed call passes a [T, hidden] BF16 buffer that the op never writes. This is the same [Bug] trtllm-gen MoE with do_finalize=False misses the tuned autotune entry (key includes output shape) flashinfer-ai/flashinfer#6077 workaround as the NVFP4 path, and it can be deleted once that issue is fixed.
  • Precomputed routing returns the caller's top-k weights as-is. They already carry the routed scaling factor, so the finalize applies it once.

Note: The results below are a quick smoke test on a small MXFP8 DeepSeek-architecture checkpoint on one GPU. The full evaluation, on DeepSeek-V4.1-Flash MXFP8 on multiple GPUs, will follow; see "Remaining before ready for review".

Accuracy Tests

Environment: 1x B200, lmsysorg/sglang:nightly-dev-cu13-20261018-b234b5c3, FlashInfer 0.7.2, this branch at ac5cde8.

Model: zianglih/JoyAI-LLM-Flash-MXFP8-last-6-BF16. It is DeepseekV3ForCausalLM with one shared expert and routed_scaling_factor 2.5. MoE layers 1-33 are MXFP8 and layers 34-39 are BF16. The serve flags follow CI's TestFlashinferTrtllmRoutedMxfp8MixedBF16, except TP1 instead of TP4:

SGLANG_ENABLE_JIT_DEEPGEMM=False SGLANG_ENABLE_MOE_DEFERRED_FINALIZE={0,1} \
python -m sglang.launch_server --model-path zianglih/JoyAI-LLM-Flash-MXFP8-last-6-BF16 \
  --trust-remote-code --tp-size 1 --kv-cache-dtype bf16 \
  --fp8-gemm-backend flashinfer_trtllm --moe-runner-backend flashinfer_trtllm_routed

Path check. GPU profile of 20 forward steps: 4 requests, 48 greedy tokens each.

kernel deferred finalize off this PR
sglang::moeFinalizeKernel (moe_finalize_fuse_shared) 0 594
FlashInfer moe::dev::finalize::finalizeKernel 780 186
  • With deferred finalize off, 780 = 39 MoE layers x 20 steps.
  • With this PR, 594 = 33 MXFP8 layers x 18 decode steps. The 186 FlashInfer finalizes are the 6 BF16 layers on every step (120) plus the 33 MXFP8 layers on the 2 prefill steps (66).
  • Prefill does not defer because DeepseekV2MoE only takes the deferred path in forward_normal_dual_stream, which is unchanged from [Perf] moe: Enable deferred finalize for NVFP4 on the routed TRT-LLM runner #42654.
  • Neither server logged No tuned config covers.

GSM8K, 500 questions, greedy, one server launch per arm:

python -m sglang.test.run_eval --port 30000 --eval-name gsm8k --api completion \
  --num-examples 500 --num-threads 32 --temperature 0 --max-tokens 2048
arm score latency
deferred finalize off (SGLANG_ENABLE_MOE_DEFERRED_FINALIZE=0) 0.932 52.9 s
this PR 0.930 51.6 s

The gap is 1 question; one standard error is about 0.011. Both arms are above CI's 0.92 threshold for this model.

Unit tests:

  • test/registered/unit/layers/moe/test_flashinfer_trtllm_routed_deferred_finalize.py: 5 passed.
  • test/registered/unit/models/test_deepseek_v4_amd_tp4.py: skipped on NVIDIA. It only runs through its four-GPU gfx950 entry point.

Speed Tests and Profiling

Not measured yet. On TP1 there is no all-reduce to fuse, so deferring only removes the separate finalize step and the result is expected to be within noise. The gain #42654 reports on V4.1 comes from the fused finalize + all-reduce kernel, which needs TP > 1.

Remaining before ready for review

These follow #42654's test flow, run on DeepSeek-V4.1-Flash MXFP8 on multiple GPUs. "Off" means the same code with SGLANG_ENABLE_MOE_DEFERRED_FINALIZE=0.

  • Path check: confirm the MoE post takes moe_finalize_all_reduce_mhc_post, the fused finalize + shared add + all-reduce kernel. Confirm that no No tuned config covers fallback is logged.
  • End-to-end logprobs: 12 prompts x 48 greedy tokens, without speculation. Compare off vs. off across two launches, this PR vs. this PR across two launches, and this PR vs. off.
  • GSM8K: 500 questions, greedy, --max-tokens 2048, run twice with the arm order swapped.
  • Unit tests: the same suite as [Perf] moe: Enable deferred finalize for NVFP4 on the routed TRT-LLM runner #42654, on this branch and on its base. Compare the failing test ids.
  • Speed: ITL p50 with aiperf (ISL 8192, OSL 1024, concurrency 1 / 4 / 8) on the DSpark serve config, plus the plain serve config at higher concurrency, off vs. this PR.
  • RL weight update: TestServerUpdateWeightsFromDiskMXFP8, the [RL] fix deepseek v4 MXFP8 flashinfer_trtllm_routed MoE weight update #28676 use case, with the deferred path on.

…nner

Follow-up 1 of sgl-project#42654, stacked on it.

- FusedMoE: supports_routed_deferred_finalize also accepts Fp8MoEMethod
  with MXFP8 experts (not converted to block FP8), under the same env,
  no-A2A and routed-scaling-in-top-k conditions as NVFP4.
- FP8 runner: the routed branch builds one kwargs set; with deferral on and
  MXFP8 experts it calls FlashInfer trtllm_fp8_block_scale_routed_moe with
  do_finalize=False and wraps the result. Block FP8 and per-tensor routed
  calls still reject deferral.
- The deferred routed call passes a [T, hidden] bf16 buffer it never writes,
  the same flashinfer-ai/flashinfer#6077 workaround as NVFP4.
- Precomputed routing returns the caller's top-k weights unchanged, so the
  routed scaling factor folded into them is applied once, by the finalize.
@github-actions

Copy link
Copy Markdown
Contributor

Thanks @wenchen76. Closing this because it is still a draft and has not been updated in 22 days.

Reopen it if the work is still relevant.

Some directories moved recently, so an older branch may need retargeting:
sgl-kernel/ -> python/sglang/kernels/aot/, python/sglang/jit_kernel/
-> python/sglang/kernels/jit/, docs/ -> docs/docs/ (.mdx),
bench_serving.py -> benchmark/serving.py, test/srt/ -> test/registered/.

@github-actions github-actions Bot closed this Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants