Skip to content

feat(attention): add architecture-owned SM12x FA4 kernels - #32991

Merged
Fridge003 merged 4 commits into
sgl-project:mainfrom
lucifer1004:feat/fa4-sm12x
Aug 13, 2026
Merged

Fridge003 merged 4 commits into
sgl-project:mainfrom
lucifer1004:feat/fa4-sm12x

Conversation

@lucifer1004

@lucifer1004 lucifer1004 commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

SGLang currently routes SM12x FA4 through an SM80-derived forward path. That
path uses the older mainloop and does not expose the SM12x TMA dataflow, paged
KV, SplitKV, or relative/shared-bias support.

This PR adds an architecture-owned SM12x implementation and keeps its kernel,
scheduler, and runtime policy under
sglang.kernels.ops.attention.fa4_sm120.

Modifications

  • Add TMA-based SM12x forward and paged-decode kernels with warp MMA.
  • Add persistent scheduling and runtime-SM-count-aware tile selection instead
    of assuming a fixed GPU size.
  • Support BF16/FP16, MHA/GQA/MQA, dense and variable-length attention, paged
    KV, SplitKV, global/local/noncausal masks, attention sinks, packed GQA, and
    relative/shared bias with shearing.
  • Add SM12x-owned host policy, launch-plan caching, preallocated-output paths,
    CUDA Graph-safe decode plans, and TVM-FFI launch fast paths.
  • Route SM12x through a narrow architecture dispatch bridge while leaving
    SM80/SM9x/SM10x kernel ownership unchanged.
  • Replace the previous SM80-derived SM120 fallback and add comprehensive
    SM12x regression tests.

Accuracy Tests

The focused SM12x test file passes in full:

test_flash_attention_4_sm120.py: 53 passed

Coverage includes dense/varlen attention, BF16/FP16, MHA/GQA/MQA,
asymmetric Q/V dimensions, packed and unpacked GQA, paged KV, SplitKV,
attention sinks, relative/shared bias, preallocated output, CUDA Graph replay,
PDL, and specialization-cache ordering.

The 684 measured prefill SKU-case pairs all passed comparison against the
SGLang Triton baseline:

GPU Cases Maximum absolute difference
RTX PRO 6000 Blackwell 228 0.0078125
RTX PRO 5000 Blackwell 228 0.0078125
GB10 (SM121) 228 0.015625

Speed Tests and Profiling

Prefill

The common three-way matrix contains 228 matched cases per GPU: BF16/FP16,
MHA/GQA/MQA, head dimensions 32/64/96/128/192->128/256, sequence lengths
128/512/2048/6144, dense and ragged varlen inputs, and causal-global,
causal-local-with-sink, and noncausal-global modes.

Each case uses five warmups followed by three shuffled trials with 20 CUDA
event samples per trial. PR and SM80-fallback runs are each paired with the
same byte-identical Triton implementation. PR / SM80 below is the ratio of
those paired speedups, which normalizes process-level clock drift.

GPU SMs PR / Triton Wins SM80 fallback / Triton Wins PR / SM80 fallback Wins
RTX PRO 6000 Blackwell 188 3.027x 228/228 1.274x 112/228 2.377x 222/228
RTX PRO 5000 Blackwell 110 3.311x 228/228 1.574x 150/228 2.104x 224/228
GB10 (SM121) 48 3.610x 228/228 2.087x 171/228 1.730x 205/228

Across all 684 equally weighted SKU-case pairs, the PR is 3.308x over
Triton with 684/684 wins
, and 2.053x over the SM80-derived fallback with
651/684 wins
.

SM12x FA4 three-way prefill speedup

Representative absolute latency:

GPU Case PR Triton SM80 fallback
RTX PRO 6000 MHA, BF16, HD64, L128 22.66 us 32.22 us 60.85 us
RTX PRO 6000 local+sink GQA, BF16, HD128, L2048 31.12 us 161.34 us 84.64 us
RTX PRO 6000 MHA, BF16, HD256, L6144 456.29 us 2588.77 us 571.95 us
RTX PRO 5000 MHA, BF16, HD64, L128 14.64 us 21.10 us 33.42 us
RTX PRO 5000 local+sink GQA, BF16, HD128, L2048 30.37 us 198.30 us 91.81 us
RTX PRO 5000 MHA, BF16, HD256, L6144 745.12 us 3487.39 us 827.04 us
GB10 (SM121) MHA, BF16, HD64, L128 18.14 us 24.93 us 38.37 us
GB10 (SM121) local+sink GQA, BF16, HD128, L2048 66.75 us 437.20 us 158.91 us
GB10 (SM121) MHA, BF16, HD256, L6144 2108.42 us 13977.18 us 1823.94 us

The few PR/fallback losses are concentrated in long ragged HD256 and dense
noncausal GQA, most visibly on the 48-SM device. Every measured PR case still
beats Triton.

Theoretical useful-compute SOL and logical-memory roofline ratios:

GPU Compute SOL median / max Logical-memory ratio median / max
RTX PRO 6000 Blackwell 14.2% / 72.4% 10.8% / 83.3%
RTX PRO 5000 Blackwell 35.6% / 82.0% 14.2% / 80.0%
GB10 (SM121) 34.0% / 74.3% 40.8% / 165.1%

The logical-memory ratio uses one compulsory Q/K/V read and one output write
against peak DRAM bandwidth. Repeated resident tensors can be served from
cache, so values above 100% are not literal measured DRAM SOL.

Paged decode

Paged decode covers 108 cases per GPU across MHA/GQA/MQA, head dimensions
64/128/256, batch sizes 1/8/32, KV lengths 128/512/2048/8192, and page size
64. CUDA Graph replay is the kernel-focused comparison; eager and synchronized
wall timings include host overhead.

GPU Graph PR / Triton Graph wins Eager PR / Triton Wall PR / Triton
RTX PRO 6000 Blackwell 1.311x 92/108 1.432x 1.317x
RTX PRO 5000 Blackwell 1.317x 96/108 1.424x 1.286x
GB10 (SM121) 1.214x 90/108 1.377x 1.307x

The SM80-derived fallback explicitly rejects paged KV and SplitKV, so it is
reported as unsupported rather than assigned a synthetic timing. The fallback
also does not support the relative/shared-bias path.

Checklist

  • Format changed code with the repository prek hooks.
  • Add focused SM12x unit and regression tests.
  • No user-facing documentation update is required for this internal
    attention-kernel implementation.
  • Provide accuracy and speed benchmark results.
  • Follow the SGLang code-style guidance.

CI States

Latest PR Test (Base): ❌ Run #31651905677
Latest PR Test (Extra): ✅ Run #31651905587

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@Fridge003

Copy link
Copy Markdown
Collaborator

Please fix the conflicts

Add SM12x-owned TMA forward and decode kernels with persistent scheduling,
paged KV, SplitKV, attention sinks, and relative-bias support.

Integrate architecture-scoped runtime policy and launch-plan caching with the
FA4 frontend and SGLang attention backend, and cover the supported feature
matrix, CUDA Graph replay, and specialization-cache ordering in tests.
@lucifer1004

Copy link
Copy Markdown
Contributor Author

Rebased onto current upstream main (5469faec45) and resolved the conflicts. I also addressed the three SM120 API organization/naming review comments. Local validation: all pre-commit hooks passed, the complete registered SM120 FA4 test file passed (53 tests), and an SM120 backend-dispatch construction probe passed.

@Fridge003

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/kernels/ops/attention/test_flash_attention_4_sm120.py

@github-actions

github-actions Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/kernels/ops/attention/test_flash_attention_4_sm120.py:

🚀 1-gpu-5090 (1 test): ✅ View workflow run

cd test/ && python3 registered/kernels/ops/attention/test_flash_attention_4_sm120.py

@Fridge003 Fridge003 added 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 12, 2026

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/kernels/ops/attention/test_flash_attention_4_sm120.py

@github-actions

github-actions Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/kernels/ops/attention/test_flash_attention_4_sm120.py:

🚀 1-gpu-5090 (1 test): ✅ View workflow run

cd test/ && python3 registered/kernels/ops/attention/test_flash_attention_4_sm120.py

Copy link
Copy Markdown
Collaborator

/rerun-test test/registered/kernels/ops/attention/test_flash_attention_4_sm120.py

@github-actions

github-actions Bot commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Results for /rerun-test test/registered/kernels/ops/attention/test_flash_attention_4_sm120.py:

🚀 1-gpu-5090 (1 test): ✅ View workflow run

cd test/ && python3 registered/kernels/ops/attention/test_flash_attention_4_sm120.py

@Fridge003
Fridge003 merged commit 889c2f3 into sgl-project:main Aug 13, 2026
318 of 372 checks passed
saturn-acc pushed a commit to saturn-acc/sglang that referenced this pull request Aug 16, 2026
Atituiset pushed a commit to Atituiset/sglang that referenced this pull request Sep 10, 2026
lovedheart added a commit to lovedheart/sglang that referenced this pull request Sep 25, 2026
Route QSA sparse decode/verify attention through the SM12x FA4
cute-dsl paged kernel (flash_attn_with_kvcache, PR sgl-project#32991) when
SGLANG_QSA_ATTN_FA4=1 on SM120, replacing the fused Triton
gather+dequant+decode kernel. The FA4 path bypasses the fused gather
path, reads the gathered page-aligned scratch in canonical KV layout,
and uses a persistent 4-D output buffer so eager and CUDA-graph replay
execute the identical kernel (bit-identical across replays).

Validated on RTX PRO 6000 (SM120): GSM8K pass@1 t=0 unchanged at
294/300, accept length stable, FA4 kernel replaces
fused_sparse_gqa_decode fully (7.9us avg vs ~12us Triton).

Co-authored-by: Copilot App <[email protected]>
lauthu added a commit to lauthu/sglang that referenced this pull request Nov 3, 2026
FlashAttentionBackend calls FA4 through the version dispatcher
(kernels/ops/attention/flash_attention.py, ver=4), which always used the
generic FA4 entry points. sgl-project#32991 added SM12x ones (flash_attention_v4_sm120)
and selected them in FlashAttentionBackend.__init__, but no call site uses
that selection. So on SM12x every FA4 call through the dispatcher:
- dropped `out` and `max_seqlen_k`, which the generic flash_attn_with_kvcache
  absorbs into **_: FA4 allocated a fresh output, and RadixAttention copied
  it into its static buffer, 8 MiB per layer of a Qwen3-0.6B 2,048-token
  prefill;
- computed and returned the softmax LSE, which the generic entry point always
  requests, so no call qualified for the SM12x forward host's cached launch
  plans.

On SM12x the dispatcher now calls the SM12x entry points. They honor `out`,
request the LSE only when the caller does, and try the launch-plan caches
first. Other architectures keep the generic entry points.

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

xupinjie commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Hello @lucifer1004, i find a change bug in this kernel. This pr introduced the reproduce method and fix method.

#49044

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jit-kernel 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.

3 participants