Skip to content

[Fix] [NVIDIA] moe: keep the tuned autotune key for deferred-finalize FP4 TRT-LLM MoE calls - #50195

Merged
YAMY1234 merged 1 commit into
sgl-project:mainfrom
YAMY1234:nvidia-fix-fp4-moe-deferred-autotune-key
Nov 20, 2026
Merged

YAMY1234 merged 1 commit into
sgl-project:mainfrom
YAMY1234:nvidia-fix-fp4-moe-deferred-autotune-key

Conversation

@YAMY1234

@YAMY1234 YAMY1234 commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

With deferred finalize, the logits-bypassed NVFP4 TRT-LLM MoE path calls trtllm_fp4_block_scale_moe(do_finalize=False) without an output. FlashInfer then allocates an (N, 0) placeholder, which is the first input of its AutoTuner key (flashinfer-ai/flashinfer#6077, still open). The entries tuned at startup (do_finalize=True) are keyed on (N, hidden), so the deferred call misses them and runs FlashInfer's default tactic. At 512 experts and top-10 that is the tile-8 kernel pair for every N up to 819 rows.

The routed path already works around #6077 (#42654): its deferred call passes an (N, hidden) BF16 buffer at every TP size, and FlashInfer does not write it when do_finalize is False.

Modifications

  • The if defer_finalize: block now allocates the optimize pad operations in fa3 to accelarate 100+us #6077 buffer for both routed and bypassed inputs, and the bypassed call passes it as output, as the routed call does. As on the routed path, this does not depend on the TP size.
  • symm_output is now always set, so its None default and the routed path's is not None check are removed.

Accuracy Tests

The buffer is not written; deferred calls now pick the same tuned tactic the finalized call uses at that shape.

New CPU test TestNvfp4BypassedAutotuneKey in test/registered/unit/layers/quantization/test_mxfp4_trtllm_autotune_key.py. It runs the deferred bypassed path at 8 and 64 rows with the FlashInfer call mocked and checks that the call passes an (N, hidden) output instead of none (which FlashInfer turns into its (N, 0) placeholder). It fails on main; the whole file passes with this change.

Speed Tests and Profiling

Kernel time per trtllm_fp4_block_scale_moe call (all kernels of the call), on one VR200 GPU at Qwen3.8-Flash-Next NVFP4 MoE shapes (512 experts, top-10, hidden 2560, MoE intermediate 640), TP1, FlashInfer 0.7.4. The kernel is tuned first as at server startup (autotune on, do_finalize=True with a full-width output), then each case runs 30 calls under the torch profiler.

Rows Deferred, no output (main) Deferred, (N, hidden) output (this PR) Finalized (reference)
128 137.9 us (tile 8, default) 111.8 us (tile 8, tuned) 124.2 us
256 159.1 us (tile 8) 124.2 us (tile 16) 138.8 us
512 211.6 us (tile 8) 128.4 us (tile 32) 142.6 us
819 265.6 us (tile 8) 130.3 us (tile 32) 144.6 us

On main FlashInfer logs No tuned config covers ... input_shapes=((N, 0), ...) at every row count. The finalized column includes the ~13 us finalize kernel that deferred calls leave to the caller.

Checklist

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ✅ Run #43767267026
Latest PR Test (Extra): ❌ Run #43767266752
Latest PR Test (AMD ROCm 10): ❌ Run #43767266689

Tip

Base CI only covers the core cases, and its workload may not exercise your change. Use /rerun-test to run the tests your change touches, or copy this prompt for an agent:

Find the registered tests this PR affects and run them with /rerun-test, following https://github.com/sgl-project/sglang/blob/main/.agents/skills/rerun-test/SKILL.md

@YAMY1234 YAMY1234 changed the title [NOT READY] [Fix] [NVIDIA] moe: keep the tuned autotune key for deferred-finalize FP4 TRT-LLM MoE calls [Fix] [NVIDIA] moe: keep the tuned autotune key for deferred-finalize FP4 TRT-LLM MoE calls Nov 14, 2026
@YAMY1234
YAMY1234 marked this pull request as ready for review November 14, 2026 00:08

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@YAMY1234
YAMY1234 force-pushed the nvidia-fix-fp4-moe-deferred-autotune-key branch from 7b79ede to fb7e9d6 Compare November 15, 2026 18:56
@YAMY1234

Copy link
Copy Markdown
Collaborator Author

/rerun-test registered/unit/layers/quantization/test_mxfp4_trtllm_autotune_key.py

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

🚀 ubuntu-latest (1 test): ✅ View workflow run

cd test/ && python3 registered/unit/layers/quantization/test_mxfp4_trtllm_autotune_key.py  # base-a

…E calls

With deferred finalize, the logits-bypassed NVFP4 TRT-LLM MoE path called
trtllm_fp4_block_scale_moe(do_finalize=False) without an output. FlashInfer
then allocates an (N, 0) placeholder, which is the first input of its
AutoTuner key, while the entries tuned at startup (do_finalize=True) are
keyed on (N, hidden). The lookup misses and the call runs FlashInfer's
default tactic. At 512 experts and top-10 that is the tile-8 kernel pair for
every N up to 819 rows: the default tile follows the average rows per expert
(N * top_k / num_experts) and moves to 16 only once that average exceeds 16.

The routed path already passes an (N, hidden) bf16 buffer at every TP size
(the flashinfer-ai/flashinfer#6077 workaround from sgl-project#42654). The
deferred-finalize block now allocates that buffer for bypassed inputs too,
and the bypassed call passes it. FlashInfer does not write it when
do_finalize is False, so only the autotune key changes. symm_output is now
always set, so its None default and the routed path's None check go away.

Test: TestNvfp4BypassedAutotuneKey in test_mxfp4_trtllm_autotune_key.py; it
fails on main, where the deferred call passes no output.

Gain: deferred FP4 MoE kernel time per call 159 -> 124 us at 256 rows, 212 -> 128 us at 512 rows (one VR200 GPU, Qwen3.8-Flash-Next NVFP4 MoE shapes, TP1, FlashInfer 0.7.4; torch profiler after startup-style autotuning).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@YAMY1234
YAMY1234 force-pushed the nvidia-fix-fp4-moe-deferred-autotune-key branch from fb7e9d6 to 6d8fbb6 Compare November 15, 2026 22:05
@YAMY1234

Copy link
Copy Markdown
Collaborator Author

/rerun-test registered/unit/layers/quantization/test_mxfp4_trtllm_autotune_key.py

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

🚀 ubuntu-latest (1 test): ✅ View workflow run

cd test/ && python3 registered/unit/layers/quantization/test_mxfp4_trtllm_autotune_key.py  # base-a

@nvpohanh

Copy link
Copy Markdown
Collaborator

cc @leejnau

@nvpohanh nvpohanh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[by Claude Code] This review is done by agents. Please feel free to disagree if they do not make sense.

[by Claude Code] Severity: style | Confidence: High

Issue

The PR has no accuracy result. The Accuracy Tests section gives a reason and a mocked CPU test only. But the change can change model outputs. Your speed table shows deferred calls moving from the tile-8 default tactic to tuned tile-16 and tile-32 kernels. That changes the GEMM kernels and the padded gemm2_out layout. The SGLang finalize kernels then read that layout through expanded_idx_to_permuted_idx.

Fix

Add one before/after accuracy result, such as GSM8K, on a model whose NVFP4 MoE takes the bypassed deferred-finalize path. The Qwen3.8-Flash-Next setup from the speed table is fine. Name the model and GPU.

@YAMY1234 YAMY1234 added the run-ci CI: run the baseline test suite on this PR label Nov 20, 2026
@YAMY1234

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci

@YAMY1234

Copy link
Copy Markdown
Collaborator Author

/rerun-failed-ci

@YAMY1234

Copy link
Copy Markdown
Collaborator Author

All NV pipeline passed

@YAMY1234
YAMY1234 merged commit e7c382c into sgl-project:main Nov 20, 2026
321 of 368 checks passed
YAMY1234 added a commit to sgl-project/sgl-nvidia-mega that referenced this pull request Nov 29, 2026
…PI v3 and contract (#103)

* [Fix] minimax_m31: Speak megakernel API version 3 and follow SGLang main's contract

#66 merged after #81 moved the unit tests to SGLang main, but its last unit
run was against the old pinned SGLang, where the MiniMax-M3.1 checks are
skipped. On SGLang main the spec is refused at registration and four
contract checks fail, so main's unit job has been red since #66.

- Megakernel API 3 (sgl-project/sglang#52054 replaced the verify penalty
  tables with step_penalties): declare 3, as #81 did for the other specs.
  The package reads no verify sampling penalties (its slots are the verify
  forward and the DSpark draft proposal), so nothing else changes.
- sgl-project/sglang#52870 moved minimax_sparse_nvfp4_backend.py to
  minimax_sparse/msa_v1_backend.py; msa_v1_decode_plan is the same field.
  Name the new file first and keep the old one as the alternative.
- fused_experts_none_to_flashinfer_trtllm_fp4: sgl-project/sglang#50195
  passes the ignored output buffer on the logits-routed path too; the
  routed-top-k path moe_nofill follows already passed it. Re-pin.
- _draft_step and _markov_sample_partial_kernel: sgl-project/sglang#51298
  adds a step softcap and vocab limit that only Inkling2's MTP head sets,
  and #52728 a folded-bias kernel path _draft_step never takes. The chain
  serves only VanillaMarkov, which leaves both None. Re-pin.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* [Fix] qwen3_5: Follow SGLang main's GDNAttnBackend.forward_extend

sgl-project/sglang#39595 (FlyDSL GDN prefill on AMD) passes
prefill_metadata=self.kernel_prefill_metadata to the extend kernel in the
prefill arm of forward_extend, None for every kernel but FlyDSL, and adds
kernel_prefill_metadata = None to GDNAttnBackend.

- gdn/prefill.py serves only the Triton prefill kernel (view.kernel_id),
  and gdn/verify.py mirrors the target-verify arm, which the change does
  not touch. Re-pin both mirrors.
- The runtime tests' fake GDN backend gains kernel_prefill_metadata=None,
  as SGLang's class now defines it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* [Fix] kimi_k31: Follow SGLang main's verify sampling stages in the DFlash runtime

sgl-project/sglang#52406 deleted sampling_batch_info.adjusts_logits and
speculative/target_verify.py (adjust_verify_logits). SGLang's DFlash round
now reads VerifyRoute.adjusts_logits and calls VerifyStep.adjust from
SamplingPipeline.plan_verify. The K3.1 DFlash runtime imported both old
names, so it no longer imported on SGLang main.

- _unserved: VerifyRoute.derive(...).adjusts_logits is the same predicate
  as adjusts_logits (grammar, watermark, penalties, logit bias or a custom
  logit processor), so the same rounds go back to SGLang.
- _accept, greedy round on gathered logits: the target's
  sampling_pipeline.plan_verify(batch, watermark_full_mask=False).adjust()
  runs the same stages in the same order as adjust_verify_logits did:
  penalties along the chain, logit bias and the custom logit processor,
  each width rows per request, with no grammar mask. DFlash refuses the
  watermark, so none is bound. As before, _unserved already refuses every
  round these stages would change.
- The test fakes gain the target's sampling_pipeline and
  need_min_p_sampling, which SGLang's SamplingBatchInfo defines.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* [Fix] qwen3_5: Re-pin GDNAttnBackend.forward_extend after sgl-project/sglang#44195

sgl-project/sglang#44195 gives the Triton GDN kernel an FP32 track
snapshot and gates the snapshot in forward_extend's prefill arm on
use_track_state_snapshot(): a snapshot-capable kernel in batch-invariant
mode.

- gdn/prefill.py: only --enable-deterministic-inference turns on
  batch-invariant mode, and it refuses the trtllm_mha attention backend
  this spec requires. The served prefill arm still writes checkpoints from
  the per-chunk states, as the slot does. Re-pin.
- gdn/verify.py mirrors the target-verify arm, which the change does not
  touch. Re-pin.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants