Repository navigation
feat(attention): add architecture-owned SM12x FA4 kernels - #32991
Conversation
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
|
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.
d608b9e to
84a00b6
Compare
|
Rebased onto current upstream |
|
/rerun-test test/registered/kernels/ops/attention/test_flash_attention_4_sm120.py |
|
Results for 🚀 |
|
/rerun-test test/registered/kernels/ops/attention/test_flash_attention_4_sm120.py |
|
Results for 🚀 |
|
/rerun-test test/registered/kernels/ops/attention/test_flash_attention_4_sm120.py |
|
Results for 🚀 |
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]>
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]>
|
Hello @lucifer1004, i find a change bug in this kernel. This pr introduced the reproduce method and fix method. |
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
of assuming a fixed GPU size.
KV, SplitKV, global/local/noncausal masks, attention sinks, packed GQA, and
relative/shared bias with shearing.
CUDA Graph-safe decode plans, and TVM-FFI launch fast paths.
SM80/SM9x/SM10x kernel ownership unchanged.
SM12x regression tests.
Accuracy Tests
The focused SM12x test file passes in full:
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:
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 / SM80below is the ratio ofthose paired speedups, which normalizes process-level clock drift.
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.
Representative absolute latency:
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:
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.
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
prekhooks.attention-kernel implementation.
CI States
Latest PR Test (Base): ❌ Run #31651905677
Latest PR Test (Extra): ✅ Run #31651905587