Skip to content

[MoE] Add FlashInfer SM90 MXFP4 W4A8 CUTLASS MoE - #34967

Merged
Fridge003 merged 6 commits into
sgl-project:mainfrom
yuan-luo:flashinfer-sm90-mxfp4-fp8
Sep 1, 2026
Merged

Fridge003 merged 6 commits into
sgl-project:mainfrom
yuan-luo:flashinfer-sm90-mxfp4-fp8

Conversation

@yuan-luo

@yuan-luo yuan-luo commented Aug 15, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

SGLang currently supports FlashInfer's SM90 MXFP4 W4A16 CUTLASS MoE path, which was introduced in SGLang #24816 on top of FlashInfer #3084. FlashInfer #3738 adds a different Hopper path that dynamically quantizes activations to FP8 and runs MXFP4-weight x FP8-activation MoE GEMMs using Humming-style pre-MMA E8M0 scale fusion.

This PR wires that path into SGLang as an explicit opt-in:

--moe-runner-backend flashinfer_mxfp4 \
--flashinfer-mxfp4-moe-precision fp8

The existing default and bf16 behavior is unchanged.

Related work:

FlashInfer version requirement

This integration intentionally targets the corrected API from FlashInfer #4431, where the FC1 and FC2 residual scales are FP32 tensors in local-expert order with shape [num_local_experts]. FlashInfer reuses its internal expert permutation to map those scales to routed rows, including global-to-local expert mapping under EP.

The original #3738 ABI briefly shipped before being reverted from FlashInfer 0.6.17 by #4411. It required downstream runtimes to construct routed-row residual tensors during every forward. SGLang does not support that older ABI.

The new option therefore requires both:

  • flashinfer-python >= 0.6.18
  • preprocess_moe_weights_for_sm90_mixed_gemm_humming

SGLang currently pins FlashInfer 0.6.17. This PR does not change that global pin. With 0.6.17, selecting fp8 fails immediately with an actionable error, while all existing FlashInfer paths remain compatible. The dependency, cubin, and JIT-cache pins should be upgraded together in a follow-up change.

GPU validation for this PR used FlashInfer 0.6.18 built from commit 8044d94bf9acc5369857baf88d28906bb32bf264, which contains the corrected API from FlashInfer #4431.

Modifications

Runtime selection and compatibility

  • Add fp8 to --flashinfer-mxfp4-moe-precision.
  • Select the new path only for flashinfer_mxfp4 on SM90.
  • Keep W4A16 as the default SM90 behavior.
  • Reject the SM90-specific fp8 option on SM100 and SM120 to avoid confusing it with their existing MXFP8 activation paths.
  • Require the corrected FlashInfer 0.6.18+ API instead of accepting the older routed-row residual ABI.
  • Pass use_wfp4afp8_humming=True only when the new path is selected. The keyword is omitted completely for FlashInfer 0.6.17 compatibility.

Weight preprocessing

  • Use preprocess_moe_weights_for_sm90_mixed_gemm_humming for both FC1 and FC2 during post-load processing.
  • Store the folded interleaved weight scales produced by FlashInfer.
  • Store residual * 64 as one FP32 value per local expert for FC1 and FC2.
  • Preserve GPT-OSS gate/up layout conversion and its existing padding rules.
  • Fill synthetic E8M0 scale padding from an existing scale in the same expert, preventing padding from changing that expert's min/max exponent range.
  • Support both the GPT-OSS MXFP4 quantization path and the DeepSeek-V4 MXFP4 expert path.

Kernel invocation

Construct the five Humming quant-scale slots expected by the corrected FlashInfer API:

  1. FC1 folded weight block scales
  2. FC1 per-local-expert residual scale, including the fixed 2^6 compensation
  3. Reserved scalar FC2 activation scale
  4. FC2 folded weight block scales
  5. FC2 per-local-expert residual scale, including the fixed 2^6 compensation

The existing TP/EP topology is forwarded to FlashInfer so it can map global expert IDs to the corresponding local residual scale.

Tests and benchmark

  • Extend the registered SM90 MXFP4 test suite with:
    • GPT-OSS preprocessing checks against the FlashInfer helper
    • five-scale kernel invocation and direct FlashInfer comparison
    • aligned and non-aligned hidden/intermediate dimensions
    • padding invariance for per-expert residual scales
    • EP rank with global expert IDs
    • DeepSeek-V4 preprocessing
    • fail-fast behavior on FlashInfer versions before 0.6.18
  • Extend the manual SM90 benchmark to compare:
    • SGLang Marlin W4A16
    • FlashInfer CUTLASS W4A16
    • FlashInfer CUTLASS W4A8
  • Update the server-argument documentation.

Accuracy Tests

The registered SM90 MXFP4 suite passed:

python -m pytest \
  test/registered/unit/layers/quantization/test_mxfp4_sm90_cutlass.py -v
18 passed, 1 skipped

The DeepSeek-V4 MXFP4 checkpoint was launched with:

python -m sglang.launch_server \
  --model-path /home/models/DeepSeek-V4-Flash \
  --trust-remote-code \
  --tp 4 \
  --moe-runner-backend flashinfer_mxfp4 \
  --flashinfer-mxfp4-moe-precision fp8 \
  --enforce-disable-flashinfer-allreduce-fusion \
  --host 0.0.0.0 \
  --port 30000

DeepSeek-V4 accuracy was evaluated using the same 200-example GSM8K setup as SGLang #24816. Both runs used the same TP=4 configuration, dataset, sampling parameters, and server options; only --flashinfer-mxfp4-moe-precision changed between bf16 and fp8.

PYTHONPATH=/sgl-workspace/sglang_dev/python \
python -m sglang.test.run_eval \
  --port 30000 \
  --eval-name gsm8k \
  --num-examples 200 \
  --api chat \
  --max-tokens 1024 \
  --num-threads 32 \
  --temperature 0 \
  --gsm8k-data-path /tmp/gsm8k-test.jsonl
MoE precision GSM8K score Total latency Output throughput
W4A16 (bf16) 0.985 37.301 s 611.809 token/s
W4A8 (fp8) 0.985 32.952 s 683.574 token/s

W4A8 preserved the W4A16 GSM8K score while reducing evaluation latency by 11.7% and increasing output throughput by 11.7%. The score also matches the 0.985 W4A16 result reported in SGLang #24816.

Speed Tests and Profiling

The downstream benchmark uses the GPT-OSS-like shape from SGLang #24816: hidden size 4096, intermediate size 2048, 256 experts, and top-k 6. Each FlashInfer path was fully autotuned, followed by 5 warmup iterations and 30 timed iterations. The remaining token shapes were run in parallel with one independent shape per H20 GPU; each W4A16/W4A8 pair ran on the same GPU.

The benchmark can be reproduced with FlashInfer 0.6.18+:

PYTHONPATH=python:/path/to/flashinfer \
FLASHINFER_DISABLE_VERSION_CHECK=1 \
python test/manual/layers/moe/bench_mxfp4_sm90_kernels.py

Results on NVIDIA H20-3e:

Tokens CUTLASS W4A16 median (min) CUTLASS W4A8 median (min) Speedup
4 0.275 ms (0.272 ms) 0.164 ms (0.162 ms) 1.68x
16 0.842 ms (0.840 ms) 0.463 ms (0.460 ms) 1.82x
64 1.894 ms (1.892 ms) 1.037 ms (1.019 ms) 1.83x
256 2.410 ms (2.403 ms) 1.478 ms (1.383 ms) 1.63x
1024 3.912 ms (3.906 ms) 2.093 ms (1.999 ms) 1.87x
2048 6.973 ms (6.954 ms) 3.394 ms (3.384 ms) 2.05x
4096 13.013 ms (12.971 ms) 6.295 ms (6.210 ms) 2.07x
8192 25.624 ms (25.546 ms) 12.308 ms (12.293 ms) 2.08x

FlashInfer CUTLASS W4A8 was faster at every measured token count, with speedups ranging from 1.63x to 2.08x. The gain reaches approximately 2.0x for token counts of 2048 and above. The GSM8K latency and throughput numbers above provide an end-to-end serving sanity check, but the kernel benchmark is the more controlled performance comparison because generated output lengths are not fixed.

Scope and Limitations

  • The implementation is SM90-only.
  • It is opt-in and does not change the default W4A16 path.
  • A coordinated FlashInfer 0.6.18 dependency update is required before production enablement.

Checklist

  • Format the code with pre-commit.
  • Add unit tests according to the repository test conventions.
  • Update server-argument documentation.
  • Run the registered tests on an SM90 GPU with FlashInfer 0.6.18.
  • Add a DeepSeek-V4 end-to-end smoke-test result.
  • Upgrade the FlashInfer Python, cubin, and JIT-cache pins together in a coordinated follow-up change.

CI States

Latest PR Test (Base): ⏳ Run #33373154332
Latest PR Test (Extra): ✅ Run #33373154123
Latest PR Test (AMD ROCm 7.2): ❌ Run #33373154379

@github-actions github-actions Bot added the documentation Improvements or additions to documentation label Aug 15, 2026
@yuan-luo yuan-luo added quant LLM Quantization deepseek benchmark flashinfer hopper SM90 run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci) labels Aug 15, 2026
@yuan-luo
yuan-luo force-pushed the flashinfer-sm90-mxfp4-fp8 branch from c357163 to 7c02b25 Compare August 16, 2026 04:58
@yuan-luo yuan-luo changed the title [MoE] Add FlashInfer SM90 MXFP4 W4A8 MoE [MoE] Add FlashInfer SM90 MXFP4 W4A8 CUTLASS MoE Aug 16, 2026
@yuan-luo
yuan-luo force-pushed the flashinfer-sm90-mxfp4-fp8 branch from 7c02b25 to f2c659b Compare August 16, 2026 15:15
@guzekai01

Copy link
Copy Markdown
Contributor
image

Minor naming issue in the performance table: Humming W4A8 may be read as the upstream inclusionAI/Humming backend (--moe-runner-backend humming), but this benchmark actually calls FlashInfer's cutlass_fused_moe(..., use_wfp4afp8_humming=True).

@yuan-luo yuan-luo added the release-highlight Candidate PR for release note highlight label Aug 20, 2026
@yuan-luo
yuan-luo force-pushed the flashinfer-sm90-mxfp4-fp8 branch 4 times, most recently from b0e2276 to a2350e5 Compare August 23, 2026 08:30

@Fridge003 Fridge003 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.

Wait for upgrade of flashinfer 0.6.18

@Fridge003

Copy link
Copy Markdown
Collaborator

Flashinfer upgraded here #36954

luoyuan.luo and others added 5 commits August 30, 2026 23:50
Wire the corrected FlashInfer Humming-style MXFP4 x FP8 API into
the Hopper MoE paths while preserving the W4A16 default and
FlashInfer 0.6.17 compatibility.

Add GPT-OSS and DeepSeek-V4 coverage, EP and padding checks,
documentation, and a three-way SM90 benchmark.
FusedMoE rounds hidden_size up (GPT-OSS 2880 -> 3072) BEFORE
create_weights whenever the flashinfer_mxfp4 backend is selected -- the
same gate that later selects _fi_kernel = "cutlass_sm90". The SM90
post-load processor therefore sees K_un = 3072 = K_pad, so the
preserve_expert_range fill guarded an empty region, while scale columns
[2880, 3072) INSIDE K_un keep the _UE8M0_ONE (127 = 2^0) buffer fill
that the naive-copy loader never overwrites.

Humming derives each expert's residual from its min/max E8M0 exponents.
The stale 127s sit far above any real per-expert max, so they inflate
the range, shift the residual, and perturb the REAL weights on repack.
Same story on w2's K rows.

Fix: FusedMoE records hidden_size_unpadded before the round-up,
create_weights stores it as _unpadded_hidden, and the w13/w2 scale
copies stop at the checkpoint's K (ceil'd to the 32-wide group boundary)
so the never-written tail is covered by the existing per-expert fill.
The tail's weight bytes are zeros (never written), so whatever scale
sits there multiplies zero -- numerically inert in the GEMM. The trim is
gated on preserve_expert_range, so the W4A16 path stays byte-identical.
The SM120 helper needs no change: it has no per-expert range statistic,
and its stale scales multiply zero weights.

Add test_humming_range_ignores_prerounded_hidden_tail, which builds the
production shape (buffers at 3072, checkpoint data to 2880, real scales
well below 127) and asserts the per-expert residual is invariant to the
tail content. The existing tests missed this twice over: they construct
the method at 2880 directly (never seeing FusedMoE's pre-round), and
their random scales span 125..130, which straddles 127.
The 0.6.18 gate compared Version(...).release[:3] >= (0, 6, 18). The
release tuple drops prerelease markers -- Version("0.6.18rc1").release
is (0, 6, 18) -- so 0.6.18rc*/0.6.18.dev* builds, cut before the
corrected per-expert Humming API landed in the 0.6.18 release, slipped
through the gate. Compare full Versions instead:

    Version(flashinfer_version) >= Version("0.6.18")

which rejects rc/dev prereleases of 0.6.18 while still accepting
0.6.18.post*/+local and anything newer (including dev builds of later
versions). Applied to all three copies of the gate: the module-level
_FI_HAS_SM90_CUTLASS_MXFP4_FP8 flag, the DSV4 runner's constructor
check, and the unit test's HAS_CORRECTED_HUMMING_API skip guard.
Replace both hand-rolled FlashInfer version gates (try/except around
__version__ parse + hasattr probe) with the existing
check_pkg_version_at_least("flashinfer_python", "0.6.18") helper. It
already does the prerelease-aware full-Version compare the previous
commit introduced, and returns False when the package is absent, so the
try/except scaffolding and the InvalidVersion handling collapse into one
call at each site.

Dropping the hasattr probe is safe: both weight processors import
preprocess_moe_weights_for_sm90_mixed_gemm_humming directly at use time,
so a build that misreports its version still fails loudly with a clear
ImportError there. As a kernel package, flashinfer_python also honors
SGLANG_SKIP_SGL_KERNEL_VERSION_CHECK=1 through the helper -- the
standard escape hatch for source builds with stale metadata.

The unit test's HAS_CORRECTED_HUMMING_API guard keeps its explicit
Version + hasattr form on purpose: under the skip env the helper returns
True unconditionally, which would un-skip the Humming tests on an old
FlashInfer and fail them spuriously; the test guard must reflect the
actually-installed API.
…0 cells

Switch the three H200 + FP4 low-latency single-node cells (flash-official,
flash, pro) to the new W4A8 path -- MXFP4 weights x FP8 activations via
FlashInfer's Humming kernels:

    --moe-runner-backend flashinfer_mxfp4
    --flashinfer-mxfp4-moe-precision fp8

flash-official and flash previously ran Marlin W4A16; pro already used the
FlashInfer W4A16 runner and only gains the precision flag. The cells carry
verificationStatus: "in-progress" until they are re-benchmarked with the
new kernel, and a comment records the FlashInfer >= 0.6.18 requirement and
the W4A16 fallbacks. Balanced / high-throughput cells are left on their
verified W4A16 commands for now.

Update the Hopper note in the MDX page accordingly: FP4 on Hopper is no
longer W4A16-only; the W4A8 opt-in and its version requirement are
documented, and the low-latency cells are called out as generating the
new form.

The MoE backend card is deliberately unchanged: backend-option requiresHw
filtering only applies to the MegaMoE entry, so a card-level W4A8 option
would be offered on Blackwell where --flashinfer-mxfp4-moe-precision fp8
raises by design.
@yuan-luo
yuan-luo force-pushed the flashinfer-sm90-mxfp4-fp8 branch from 2a80e18 to cac2ea1 Compare August 31, 2026 07:02

@mmangkad mmangkad left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we remove the defensive flashinfer version checks now?

Remove the four "flashinfer_mxfp4_moe_precision=fp8 selects the SM90
Humming path" ValueErrors (DSV4 cutlass constructor per review, plus the
same guard in the trtllm SM100 constructor and mxfp4.py's SM100/SM120
_fi_kernel branches for consistency -- all four were added by this PR).

The flag is an SM90 knob. The Blackwell paths (trtllm_sm100,
cutlass_sm120) already compute the grouped GEMMs on MXFP8 activations,
so precision=fp8 there matches actual behavior; rejecting it only broke
config portability between H200 and Blackwell deployments. Outside SM90
the flag now simply has no effect (_use_sm90_humming stays False), which
also matches the server-arg docstring, which scopes fp8 to SM90.
@yuan-luo
yuan-luo force-pushed the flashinfer-sm90-mxfp4-fp8 branch from cac2ea1 to eba9c45 Compare August 31, 2026 08:29
@yuan-luo

Copy link
Copy Markdown
Collaborator Author

Can we remove the defensive flashinfer version checks now?

@mmangkad Removed the defensive flashinfer version checks and related import/comments.

@yuan-luo yuan-luo removed documentation Improvements or additions to documentation benchmark labels Aug 31, 2026
@Fridge003
Fridge003 merged commit 5b04408 into sgl-project:main Sep 1, 2026
223 of 274 checks passed
@yuan-luo
yuan-luo deleted the flashinfer-sm90-mxfp4-fp8 branch September 1, 2026 03:37
Leoyzen pushed a commit to Leoyzen/sglang that referenced this pull request Sep 5, 2026
StevenChenSE pushed a commit to StevenChenSE/sglang that referenced this pull request Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deepseek flashinfer hopper SM90 quant LLM Quantization release-highlight Candidate PR for release note highlight run-ci CI: run the baseline test suite on this PR run-ci-extra CI: also run the extra suite (requires run-ci)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants