Skip to content

[Perf] [NVIDIA] fp8_fa_sm120: Use the block-scaled MMA encoding - #43390

Merged
BBuf merged 3 commits into
sgl-project:mainfrom
emre570:fp8-fa-sm120-block-scaled-mma
Oct 19, 2026
Merged

BBuf merged 3 commits into
sgl-project:mainfrom
emre570:fp8-fa-sm120-block-scaled-mma

Conversation

@emre570

@emre570 emre570 commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

fp8_fa_sm120 computes QK and PV with the plain FP8 mma.sync (E4M3 x E4M3, FP32
accumulate). On GeForce SM120 this instruction runs at half rate. I measured the raw MMA
peak on an RTX 5080 with back-to-back chains and no memory traffic:

instruction SASS TFLOP/s
plain FP8, FP32 accumulate QMMA.16832.F32.E4M3.E4M3 246
block-scaled FP8, FP32 accumulate QMMA.SF.16832.F32.E4M3.E4M3.E8 488

The block-scaled encoding (kind::mxf8f6f4.block_scale) multiplies each operand by a
UE8M0 scale, one byte per 32 k elements. With every scale byte set to 0x7F (2^0) the math
is the same product. On random finite E4M3 inputs, 0 of 704 M accumulator registers differ
between the two instructions.

So the kernel was limited by the instruction encoding, not by its design.

Modifications

  • kernels/ops/attention/fp8_fa_sm120/kernel.py
    • The MMA atom is warp.MmaMXF8Op when the compile target has it (sm_120a/f,
      sm_121a/f), warp.MmaFP8Op otherwise.
    • Four scale fragments (for Q, K, P, V) hold the constant byte 0x7F. They live in
      registers: no shared memory, no extra loads.
    • The two cute.gemm calls pass [operand, scale] pairs.
  • kernels/ops/attention/fp8_fa_sm120/plan.py: the public fp8_attention() signature is
    unchanged. An internal compile-time parameter selects the encoding. It is not a user
    option, because the output is bit-equal and there is no speed against accuracy choice.
  • One unit test runs both encodings on the same inputs and requires equal bits.

Operand layouts, shared memory, the softmax, the P pack and the prep do not change.
Register count goes from 244 to 250, no stack.

Accuracy Tests

The output is bit-identical to main. Same inputs (seed 42), SHA-256 over the O and LSE
bytes, main against this branch:

H S equal
8 1052 (key mask path) yes
56 20440 yes
56 30272 yes

Unit tests: 10/10, including the new bit-equality test.

Speed Tests and Profiling

RTX 5080, H=56, D=128, median of 5 runs, same process and inputs.

S prep (ms) kernel main (ms) kernel this PR (ms) full call main (ms) full call this PR (ms)
20440 2.60 56.34 37.66 58.91 40.27
30272 3.84 123.60 83.28 127.26 86.29

The full call is 1.47x faster at both lengths. Against cuDNN SDPA BF16 on the same card
(226 ms at S=30272) the backend is now 2.6x faster, was 1.8x.

Checklist


CI States

Latest PR Test (Base): ✅ Run #39159895157
Latest PR Test (Extra): ❌ Run #39159895109
Latest PR Test (AMD ROCm 10): ❌ Run #39159895224

@emre570

emre570 commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

One follow-up question, separate from this PR:

on SM120 the multimodal_gen default is torch_sdpa. across 10 diffusion shapes (FLUX, Wan, HunyuanVideo, H3, ...) fp8_fa_sm120 is 2.1-2.7x faster than that default on a 5080. sageattention is ~3-6% faster still, but it is an optional install.

would you be open to making fp8_fa_sm120 the default on SM120 when sageattention is not installed (starting with H3)? if yes, what evidence would you want: multi-seed e2e PSNR/SSIM, real pipeline s/step, anything else?

note: tested on RTX 5080 only. per the RTX PRO Blackwell whitepaper (as cited in flashinfer#5963), RTX PRO 6000 runs FP32-accumulate FP8 at full rate, so I expect this change to be neutral there. not measured yet.

detain pushed a commit to detain/sglang that referenced this pull request Oct 15, 2026
…MA encoding [batch-apply-b 2026-10-14]

Squash of single upstream commit 65bd9ae (cherry-pick -x, clean: all 3
PR files byte-identical between PR base 408f5d0 and merge-4 HEAD — no
local drift in kernel.py/plan.py; local fp8_fa_sm120 history lives in
fused_prep.py/backends/layer.py which this PR does not touch).
Block-scaled MMA (kind::mxf8f6f4, UE8M0 unit scales) for QK+PV on SM120;
plain-encoding fallback kept via block_scaled_mma=False const_expr.
detain pushed a commit to detain/sglang that referenced this pull request Oct 15, 2026
…layers in BF16 [batch-apply-b 2026-10-14]

Cherry-pick -x of e012ffe (single commit). Backend file byte-identical
to PR base; test file auto-merged 3way — PR base pre-dates sgl-project#43390's
test_block_scaled_mma_is_bit_equal insertion but hunks are disjoint (new
tests land after test_causal_falls_back_to_cudnn). bf16_layers +
min_seq_len --attention-backend-config escape keys; complementary to
sgl-project#43390 (different hunks/files: backends/*.py + docs vs kernels/ops/*).
@BBuf

BBuf commented Oct 17, 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 Oct 17, 2026
@BBuf

BBuf commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator

CI is blocked by the required-base gate (6a39ddf1f2e0); the SM120 tests have not run. Please rebase onto main and rerun CI.

@github-actions

This comment has been minimized.

@emre570 emre570 changed the title [diffusion] fp8_fa_sm120: use the block-scaled MMA encoding [Perf] [NVIDIA] fp8_fa_sm120: Use the block-scaled MMA encoding Oct 18, 2026
@emre570

emre570 commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

@BBuf merged main and CI is green, ready to merge from my side

@BBuf
BBuf merged commit 10ec53a into sgl-project:main Oct 19, 2026
200 of 222 checks passed
DaniilKurlovich added a commit to DaniilKurlovich/sglang that referenced this pull request Oct 19, 2026
The fp8_fa_sm120 CuTe-DSL kernel uses only FP8 mma.sync and cp.async, which
Ada (RTX 40, L4, L40) has. Since sgl-project#43390 it selects the block-scaled MMA only on
sm_120a/f and sm_121a/f and keeps the plain FP8 MMA elsewhere, so it compiles
and runs unchanged on sm_89 with nvidia-cutlass-dsl 4.8.0.

- Share one capability set, FP8_FA_SM120_CAPABILITIES = {(8, 9), (12, 0)},
  between the resolver and the backend's per-call fallback check.
- Run the unit tests on SM89; skip the block-scaled bit-equality test where
  the block-scaled MMA is unavailable.
- Document SM89 support and the cutlass-dsl requirement.

Unit tests on an L4 (sm_89): 9 passed, 1 skipped (bit-equality).

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

diffusion SGLang Diffusion jit-kernel 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