Skip to content

[Perf] [NVIDIA] fp8: Use FlashInfer b12x for block-fp8-as-MXFP8 linears on SM12x - #45262

Merged
BBuf merged 4 commits into
sgl-project:mainfrom
moria97:sm120-b12x-block-fp8
Nov 10, 2026
Merged

BBuf merged 4 commits into
sgl-project:mainfrom
moria97:sm120-b12x-block-fp8

Conversation

@moria97

@moria97 moria97 commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

With --fp8-gemm-backend flashinfer_cutlass, block-fp8 weights served as MXFP8 always use FlashInfer's CUTLASS MXFP8 GEMM. On SM120/SM121, FlashInfer 0.7.2 also ships the b12x warp-MMA backend. It has small-M tiles and is faster at every M. FlashInfer's own backend="auto" already prefers it on SM12x.

Modifications

dispatch_block_fp8_mxfp8_linear: when FlashInfer reports b12x as supported for the device, use b12x when K % 128 == 0 and CUTLASS otherwise. Non-SM12x devices are unchanged.

Accuracy Tests

  • Kernel level: the output error is identical across the Triton block, CUTLASS and b12x paths (rel ≈ 2.67e-2, which comes from activation quantization).
  • GSM8K 5-shot, DeepSeek-V4.1-Flash: 0.913 (b12x) vs 0.904 (CUTLASS); SE ≈ 0.8pt.

Speed Tests and Profiling

8x RTX PRO 5000 72GB, DeepSeek-V4.1-Flash, DP8 attention + EP8, MXFP4 MoE.

Kernel latency (us, CUDA graph):

N×K M CUTLASS b12x
1792×5120 1 33.5 7.6
25600×6144 1 420 142
25600×6144 8192 9906 7207

End to end, CUTLASS → b12x:

Metric CUTLASS b12x Change
32K-in/1K-out saturation 0.766 req/s 0.801 req/s +4.6%
3K-in/100-out saturation 9.26 req/s 9.59 req/s +3.6%
Single-request 32K prefill 11188 tok/s 11734 tok/s +4.9%
bs1 TPOT 27.3 ms 18.0 ms 1.52×

Validation of the combined branches

I validated a clean tree: upstream a90e9c5d plus #42371 plus this PR and the three companion SM120 PRs (DeepGEMM metadata, b12x MXFP8, mHC BLOCK_K, RTX PRO 5000 configs). It ran on 8x RTX PRO 5000, DP8 attention + EP8, with --fp8-gemm-backend flashinfer_cutlass:

GSM8K 32K/1K req/s 3K/100 req/s 32K prefill tok/s bs1 TPOT
clean branches 0.905 0.807 9.59 12020 17.4 ms
env-gated dev build (reference) 0.913 0.801 9.59 11734 18.0 ms

Checklist

  • Format with pre-commit
  • Accuracy and speed tests above

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ✅ Run #42735360824
Latest PR Test (Extra): ❌ Run #42735360887
Latest PR Test (AMD ROCm 10): ❌ Run #42735360743

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

With --fp8-gemm-backend flashinfer_cutlass, block-fp8 weights served as
MXFP8 always ran FlashInfer's CUTLASS MXFP8 GEMM. On SM120/SM121
FlashInfer also ships the b12x warp-MMA backend, which has small-M tiles
and is faster at every M (4.4x at M=1 and 1.4x at M=8192 for the
DeepSeek-V4.1-Flash shapes on RTX PRO 5000). Use it when FlashInfer
reports it as supported and K % 128 == 0; keep CUTLASS otherwise.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@moria97 moria97 changed the title [Quant] Use FlashInfer b12x for block-fp8-as-MXFP8 linears on SM12x [Perf] [NVIDIA] fp8: Use FlashInfer b12x for block-fp8-as-MXFP8 linears on SM12x Oct 20, 2026
@moria97

moria97 commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

@BBuf @ch-wan Could you take a look and /tag-and-rerun-ci when you get a chance? On SM12x this routes block-fp8-as-MXFP8 linears to FlashInfer's b12x backend (+4-5% e2e throughput, 1.5x bs1 TPOT on RTX PRO 5000); other archs are unchanged. Thanks!

detain pushed a commit to detain/sglang that referenced this pull request Oct 20, 2026
With --fp8-gemm-backend flashinfer_cutlass, block-fp8 weights served as
MXFP8 always ran FlashInfer's CUTLASS MXFP8 GEMM. On SM120/SM121
FlashInfer also ships the b12x warp-MMA backend, which has small-M tiles
and is faster at every M (4.4x at M=1 and 1.4x at M=8192 for the
DeepSeek-V4.1-Flash shapes on RTX PRO 5000). Use it when FlashInfer
reports it as supported and K % 128 == 0; keep CUTLASS otherwise.

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

sgl-project#45262, ported byte-faithfully: cherry-pick -x of the single PR commit
1b3f16f landed CLEAN with zero conflicts (the hunk pre-image is
byte-identical to ours; only its line numbers differ because of drift
elsewhere in the +41/-163 file), 1 file +14. No fork adaptation, no test
gating (the PR ships no test).

VERDICT ON THE FLAGGED SHAPE CHECK -- NOT a bug. The sweep asked whether
`weight.shape[1] % 128 == 0` tests K as the comment claims, or N. It tests
K. Proof chain for the only route that reaches this dispatch:
  * fp8.py:_prepare_block_fp8_as_mxfp8 unpacks `n, k = layer.weight.shape`
    and never rewrites layer.weight (only the scale gets a swizzled copy,
    weight_scale_inv stays in place for the Triton fallback), so the weight
    on this route is the standard PyTorch (out_features=N, in_features=K).
  * fp8.py apply() passes `weight=layer.weight` unchanged (:1279/:1287).
  * flashinfer_mxfp8_blockscaled_linear then derives output_shape from
    weight.shape[0] (=N) and validates K with `k_w = weight.shape[1]`
    against input_2d.shape[1], and hands FlashInfer `weight.t()`.
    A (K, N) weight would raise "Input K=... does not match weight K=..."
    on every non-square layer, so the layout is settled by the code that
    already ships.
  * The two `Parameter(x.t())` transpositions in fp8.py (:1102 per-tensor
    static, :1184 dynamic requant) belong to other routes and never reach
    this dispatch.
So the new gate reads exactly the reduction dim it documents; nothing to
annotate.

GATE AVAILABILITY (executed probes, both venvs, cc passed explicitly):
flashinfer.mm_mxfp8.is_backend_supported exists in .venv2 (0.6.18) and in
the prod venv (0.7.2); b12x reports True for cc 120/121 and False for
100/103 on both wheels, cutlass True everywhere, cute-dsl only 100/103.
So the branch is selectable on this box's SM120 hardware with the wheel we
run, and never fires on SM10x (where the old cutlass partial is kept).
The call also mirrors the in-tree precedent two functions above (:722
cutedsl check, :753 auto-selection) using the same _raw_flashinfer_mm_mxfp8
handle and get_device_sm().

IMPORT SAFETY: _raw_flashinfer_mm_mxfp8 is imported under a module-level
`if get_platform().is_blackwell and is_flashinfer_available()`, so a
FLASHINFER_CUTLASS dispatch on a non-Blackwell host would NameError. That
is pre-existing (identical usage at :722/:753) and unreachable in practice:
every path that produces FLASHINFER_CUTLASS is guarded by the same two
conditions, is_blackwell() checks capability majors [10, 11, 12] + CUDA
>= 12.8 (utils/common.py:290) so SM120 IS Blackwell here, and the new
`and` short-circuits on backend.is_flashinfer_cutlass() first, which is why
the AMD gfx95 file (test_mxfp8_amd_gfx95.py, calls dispatch directly) is
unaffected.

INERT FOR PROD / OPT-IN AS REQUESTED: the route needs (a) a block-fp8
checkpoint with weight_block_size[1] == 32 and scale_fmt == "ue8m0"
(can_serve_block_fp8_as_mxfp8), i.e. the DeepSeek-V4.1-Flash family -- not
Qwen3.8-Flash-Next, which is [128, 128] blockwise and stays on the Triton
block kernel -- and (b) --fp8-gemm-backend flashinfer_cutlass, while prod
pins cutlass. So nothing changes for the fp8 arm today; when we run a
dsv41flash-style checkpoint with flashinfer_cutlass on the 4x SM120 box we
get b12x, which is what makes the A/B arm (b12x vs cutlass) possible.
`keep_plain_weight_layout` layers and legacy (q, scale) tuple inputs keep
the block kernel as before (mxfp8_view False), unchanged by this port.

DISCOVERED WHILE READING (pre-existing, NOT introduced here, NOT fixed):
fp8.py:1256-1267 adds extra_kwargs["pin_tactic"] whenever
mxfp8_prefill_autotune_min_tokens is not None, but that attribute is only
ever set for the flashinfer_cutedsl backend (:553-554), whose dispatched
callable is `partial(flashinfer_mxfp8_blockscaled_linear, backend="cute-dsl",
pin_tactic=True)` -- a second pin_tactic for the same parameter, i.e.
TypeError: got multiple values for keyword argument, at the first prefill
of >= 4096 tokens. It cannot fire on this stack (cutedsl needs SM100/103:
is_backend_supported("cute-dsl", 120) is False, and prod pins cutlass), and
neither the CUTLASS nor the new b12x callable can see it because
min_tokens stays None for them. Left for an SM10x owner: the honest fix is
a setdefault on the dispatch side, which changes tactic-pinning semantics of
a feature I cannot exercise here (batch-invariance-adjacent), so guessing
it would be worse than reporting it.

TESTS (CUDA_VISIBLE_DEVICES=9, per-file, PYTHONPATH=worktree/python,
.venv2 python, all rc=0, nothing regressed):
  test_flashinfer_autotune_gate.py          9P   (entry-229 baseline)
  test_fp4_gemm_backend_sm120.py            12P  (entry-225 b12x baseline;
                                                   NVFP4 mm_fp4 b12x is a
                                                   different family from
                                                   this mm_mxfp8 route)
  test_fp8_utils.py                 1F/2P/5S/5sub  (the 1F = pre-existing
                                                   TestInverseTransformScaleUe8m0::test_round_trip)
  test_flashinfer_trtllm_fp8_fallback.py    1F/6P/1sub  (1F PROVEN
                                                   pre-existing at
                                                   3251fbd in r-red)
  test_fp8_blockwise_linear_backends.py     22 skipped (CUDA-gated)
  test_mxfp8_amd_gfx95.py                   4 skipped (non-AMD)
  test_resolution_hook_registry.py          10P
  import smoke sglang.srt.layers.quantization.fp8_utils OK on this host.

GPU-window debt: b12x vs cutlass numerics + throughput for a block-fp8
ue8m0/[x,32] checkpoint on SM120 (the PR's 4.4x@M=1 / 1.4x@M=8192 claim on
RTX PRO 5000 is unverified here), including whether pin_tactic=True is the
right call for b12x (its small-M tiles are exactly the case where the
autotuner would change reduction order) and the 22 skipped
test_fp8_blockwise_linear_backends.py cases.

(cherry picked from commit 1b3f16f)
@waynehacking8

Copy link
Copy Markdown
Contributor

I ran the GEMM on an RTX PRO 6000 Blackwell Max-Q (SM120, 188 SMs), and the kernel speedup holds on this card too. I timed 8 DeepSeek-V4.1-Flash dense linears at 14 values of M from 1 to 8192, 112 points, each in two separate runs. b12x is faster in both runs at 108 points, slower in both at one, and faster in one run and slower in the other at three. Its output matched CUTLASS bit for bit at every point. This is kernel time only; I didn't run end to end.

The timed call is mm_mxfp8 with backend="cutlass" and with backend="b12x", as this PR calls it. The activation is quantized with mxfp8_quantize beforehand and that step is not timed. The table shows b12x time over CUTLASS time, so lower means b12x is faster:

linear (N x K) M = 1 M = 64 M = 128 M = 256 M = 8192
wqkv_a 1792 x 5120 0.30 0.91 0.96 1.10 0.71
wq_b 32768 x 1280 0.17 0.30 1.02 0.77 0.72
wo_b 5120 x 8192 0.19 0.63 1.07 0.66 0.69
indexer wq_b 4096 x 1280 0.23 0.40 0.90 0.70 0.73
shared gate_up 4608 x 5120 0.21 0.70 0.83 0.66 0.71
shared down 5120 x 2304 0.28 0.67 0.83 0.71 0.71
engram wkv 25600 x 6144 0.18 0.65 1.06 0.75 0.69
MTP main_proj 5120 x 15360 0.20 0.67 0.93 0.67 0.76

Over all 112 points the median is 0.67. The one point where b12x is slower in both runs is wqkv_a at M = 256: 23.3 us against 21.2 us (+9.8%). The three M = 128 cells above 1 go different ways in the two runs, so I can't call them either way.

On random weights and activations, b12x, CUTLASS and FlashInfer's backend="auto" gave the same bits at all 112 points in both runs. So the GSM8K gap in the description (0.913 against 0.904) is unlikely to come from this GEMM. auto picks b12x for these shapes and CUTLASS for a K = 2112 one.

Setup

Shapes are the full weights from the DeepSeek-V4.1-Flash safetensors headers. The two shared-expert rows are the unsharded (TP1) case. With DP8 attention, EP8 and no a2a backend, sglang main splits the shared experts 8 ways, so per rank shared down is 5120 x 288 and goes to CUTLASS under this PR's K % 128 check. I didn't time the per-rank shared shapes.

Both backends are pinned under autotune(False, skip_ops={"mxfp8_gemm"}). The weight scale goes through block_scale_interleave as sglang main does, and the output is bf16. Weights stay DRAM-resident: enough copies to cover 8x L2 (at least 1 GiB) are replayed round-robin in one CUDA graph. Each time is the median of 30 replays divided by the number of copies, from CUDA events. The table ratio is the mean of the two runs' paired ratios. Every compile happens before timing. The two runs are separate processes with opposite backend orders. A single-call CUDA graph replay of b12x matched eager at M = 1 and 64 on every shape. flashinfer-python 0.7.3, nvidia-cutlass-dsl 4.8.0, torch 2.14.0+cu130, driver 597.16, WSL2.

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

[BBuf Review Agent] LGTM. Let's wait for the related CI.

@BBuf

BBuf commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Nov 8, 2026
@BBuf

BBuf commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator

[BBuf Review Agent] Could you merge the latest main into this branch? CI's maintenance gate won't run the tests until it does.

@BBuf

BBuf commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

/tag-and-rerun-ci

2 similar comments
@BBuf

BBuf commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

/tag-and-rerun-ci

@BBuf

BBuf commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

/tag-and-rerun-ci

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.

3 participants