Skip to content

[Perf] [AMD] qsa: Run extend attention on AITER's FlyDSL sparse prefill - #51179

Draft
eppaneamd wants to merge 3 commits into
sgl-project:mainfrom
eppaneamd:rocm-qsa-flydsl-prefill
Draft

eppaneamd wants to merge 3 commits into
sgl-project:mainfrom
eppaneamd:rocm-qsa-flydsl-prefill

Conversation

@eppaneamd

@eppaneamd eppaneamd commented Nov 18, 2026 •

Copy link
Copy Markdown

Motivation

On gfx950, QwenSparseAttnBackend.forward_extend runs QSA extend attention on Triton: sparse_gqa_fwd_interface_triton for an extend without a prefix, sparse_gqa_fwd_interface_triton_ck on the packed K/V over a prefix.

ROCm/aiter#7556 adds aiter.ops.qsa.qsa_sparse_prefill, FlyDSL kernels for the same operands (BF16 Q, K/V [N, HK, 256], the int32 selection expand_qsa_block_indices writes) that share K/V tiles among the rows of a request. With SGLANG_ROCM_QSA_FLYDSL_PREFILL=1 this PR runs both branches on it. On one MI355X:

  • kernels: the 12 QSA layers of a 16,384-token chunk take 0.28 to 0.95 of the Triton time;
  • served: a 32,768-token prompt prefills in 0.734 and 0.737 s against 0.776 s for the faster of two flag-off server lifetimes;
  • under load: 48 such prompts at concurrency 12 finish in 44.7 and 45.7 s against 49.1 s.

Limits:

  • Needs [FlyDSL] [QSA] Sparse prefill attention on gfx950: union and packed-direct ports, strided K/V, routing ROCm/aiter#7556, which is not merged. With the pinned aiter, the flag logs a warning and every extend stays on Triton. The numbers below come from the image with [FlyDSL] [QSA] Sparse prefill attention on gfx950: union and packed-direct ports, strided K/V, routing ROCm/aiter#7556's files installed (details).
  • Under load, median TPOT is higher with the flag in both lifetimes: 30.10 and 33.63 ms against 26.07 and 27.75 ms without (mean 27.51 and 30.04 against 26.97 and 28.41).
  • Small extends over a long prefix are slower in the kernel benchmark: below 512 rows over 98,304 tokens, 5 of 6 cases take 1.03 to 1.19x the Triton time (64 rows: 1.28 → 1.52 ms per 12 layers).
  • The op plans each new layout on the host: over a prefix its first call takes 0.18 to 0.27 ms longer than the next ones up to 1,024 rows, 0.31 to 1.28 ms at 2,048 to 16,384 rows (Triton: up to 0.43 ms), included in the chunk times.
  • Served, 64 or 512 new tokens over a cached 98,304-token prefix show no gain: up to 3.7% slower than the faster flag-off lifetime (Speed Tests).
  • The gain depends on how much the rows of a request share their selection: 16,384 rows over 83,968 tokens take 0.45 of the Triton time when the rows rank the blocks alike, 0.92 to 1.03 when each row ranks them at random.
  • Not bit-equal: on the same operands the outputs differ by at most 0.0156 (mean 1.6e-4 or less, details).
  • After its calls the op keeps device memory outside SGLang's KV-pool sizing: 0.33 GiB once a 16,384-token chunk over 83,968 tokens has run, 0.64 GiB over 245,760.
  • Prefill CUDA graph runs keep Triton (prefill graphs are off by default on ROCm): their capture warm-ups pass dummy batches whose selection does not match their lengths, which the op does not check.
  • Measured at TP1 with a BF16 KV cache only.

Scope:

  • Configs with a QSA indexer (indexer_n_heads), served by QwenSparseAttnBackend (qwen4_exp.py, e.g. Qwen3.8-Flash-Next): every extend of the target model and, with NEXTN, the draft model's extend after a prefill (13 QSA layers per chunk).
  • Unchanged: decode, target verify and draft extend after decode (_forward_paged_attention); context-parallel extends (_forward_extend_cp).
  • gfx950 only, opt-in. Measured: amd/Qwen3.8-Flash-Next-Quark-MXFP4-AttnFP8-PLEFP8.

Related work:

Modifications

  • qwen_sparse_attn_backend.py:
    • _resolve_flydsl_sparse_prefill() returns aiter.ops.qsa when the flag is set, on gfx950, if aiter has qsa_sparse_prefill_problem and qsa_sparse_prefill_warmup (TODO(aiter#7556), as _flydsl_v4_decode() guards an unmerged aiter API).
    • _flydsl_sparse_prefill(...) takes the Triton kernels' operands plus the host query and prefix lengths. It returns None (keep Triton) in a prefill graph scope or when qsa_sparse_prefill_problem names a broken rule (logged once). Both forward_extend branches call it first.
    • __init__ compiles the op's kernels for each attention geometry of the model through qsa_sparse_prefill_warmup, once per process, so no served extend compiles them.
  • environ.py and the environment variable docs: SGLANG_ROCM_QSA_FLYDSL_PREFILL (default off).
  • Tests in test_qsa_rocm.py; manual benchmark test/registered/kernels/benchmark/attention/bench_qsa_flydsl_prefill.py.

Accuracy Tests

Served with the cookbook's MI355X concurrency 12–16 command, one server lifetime per row, in this order (details). GSM8K: lm_eval, 5-shot, chat template, all 1,319 questions (stderr 0.0053). NIAH: 100 prompts of 32,039 to 32,140 tokens, each asking for a 7-digit number placed in the context.

Lifetime Flag GSM8K strict GSM8K flexible NIAH
1 off 0.9613 0.9636 100/100
2 on 0.9575 0.9621 100/100
3 on 0.9606 0.9621 100/100
4 off 0.9606 0.9636 100/100
  • The 100 NIAH answers are the same strings in all four lifetimes.
  • Path: in a GPU profile of one 32,768-token request (2 chunks of 13 QSA layers: 12 target, 1 MTP draft), off ran _sparse_gqa_prefill 13 and _sparse_gqa_chunk_prefill 13 times and no FlyDSL kernel; on ran 52 FlyDSL sparse_prefill_* kernels (26 calls) and neither Triton kernel. Both on lifetimes log QSA FlyDSL sparse prefill warmed for 24 query heads, 2 KV heads, head dim 256, selection width 2051 at start-up and never QSA extend attention stays on Triton.
  • Numerical difference through forward_extend on the same operands (10 layouts): at most 0.0156, mean 7.8e-5 to 1.6e-4, 45% of the elements equal. Max error against float32 attention on 64 sampled rows: FlyDSL 0.00057 to 0.0038, Triton 0.00066 to 0.0044.
  • test_qsa_extend_flydsl_sparse_prefill_matches_reference (one request without a prefix; two requests, one over a prefix) runs forward_extend with the real op against a float32 reference and Triton; it fails without this PR and under each of 11 mutations (details).

Speed Tests and Profiling

Served on one MI355X, one server lifetime per column; the kernel benchmark (all layouts) is in the details.

Served, per lifetime 1 off 2 on 3 on 4 off
32,768-token prefill (s) 1.222 0.734 0.737 0.776
98,304-token prefill (s) 2.784 2.402 2.401 2.496
64 tokens over a cached 98,304 (s) 0.1237 0.1228 0.1220 0.1185
512 tokens over a cached 98,304 (s) 0.1305 0.1299 0.1340 0.1295
load: duration (s) 57.93 44.74 45.65 49.09
load: input throughput (tok/s) 27,149 35,153 34,455 32,038
load: mean TTFT (ms) 6,195 4,011 3,648 4,911
load: mean TPOT (ms) 26.97 27.51 30.04 28.41
load: median TPOT (ms) 26.07 30.10 33.63 27.75
  • Single requests: median of 6 with max_new_tokens 1. Load: 48 prompts of 32,768 random token ids, 256 output tokens, at most 12 at a time.
  • Lifetime 1, the first server in the container, is slower than lifetime 4 in every prefill measure and in GSM8K and NIAH wall time (165 s against 105 s, 109 s against 102 s). Its 32,768-token gap to the flag-on lifetimes (0.49 s) exceeds the time all Triton extend attention of that prompt takes in the kernel benchmark (0.11 to 0.16 s), so the comparison uses lifetime 4.
  • Against lifetime 4, the flag-on lifetimes take 0.946 to 0.951 of the time at 32,768 tokens and 0.962 at 98,304, and reach 1.075 to 1.097 of the load's input throughput.
Setup, commands, full sweep, tests and mutations

Setup. One MI355X (gfx950), TP1, lmsysorg/sglang-rocm:v0.5.23-rocm10-mi35x-20261117 (SGLang c67db7c762, aiter 581579ab5, FlyDSL 0.4.2, torch 2.11.0+rocm10.0.0). In a new container of the image for each run:

Flag off and on run in the same container, one server lifetime each, with SGLANG_ROCM_QSA_FLYDSL_PREFILL=0 or 1.

Server. The cookbook's MI355X concurrency 12–16 command (docs/cookbook/autoregressive/Qwen/Qwen3.8-Flash-Next.mdx: SGLANG_AITER_HONOR_EXPLICIT_MEM_FRACTION=1 AITER_GDR_FLYDSL=1, NEXTN 3/1/4, --chunked-prefill-size 16384, --mem-fraction-static 0.9, BF16 KV) with --host 127.0.0.1 and --admin-api-key (needed by /start_profile).

GSM8K (lm_eval 0.4.13, openai/gsm8k main, revision 740312a):

lm_eval run --model local-chat-completions \
  --model_args model=amd/Qwen3.8-Flash-Next-Quark-MXFP4-AttnFP8-PLEFP8,base_url=http://127.0.0.1:30000/v1/chat/completions,num_concurrent=32,max_retries=3,tokenized_requests=False,timeout=1800 \
  --tasks gsm8k --num_fewshot 5 --apply_chat_template --fewshot_as_multiturn --batch_size 1 \
  --gen_kwargs '{"max_gen_toks": 1024, "chat_template_kwargs": {"enable_thinking": false}}'

NIAH. 100 prompts built once with a fixed seed: GSM8K train questions as the haystack and one sentence "The special magic number for is <7 digits>." at a random depth (5 to 95%), then the question; greedy, max_tokens 16, 8 at a time; correct when the reply contains the number.

Path. /start_profile (activities GPU), one request of 32,768 token ids with max_new_tokens 1 and nothing else running, /stop_profile; kernel names counted in the trace.

Single requests. /generate, one request at a time, max_new_tokens 1, temperature 0; token ids drawn from [1000, 150000) by a seeded generator, the same in every lifetime. Prefill: one warm-up request, then 6 timed. Over a prefix: 98,304 ids sent once, then 6 requests of those ids plus 64 (or 512) new ids; every reply reports 98,304 cached tokens. Steps of each lifetime, in order: path, GSM8K, NIAH, single requests, load.

Load.

python3 -m sglang.benchmark.serving --backend sglang --host 127.0.0.1 --port 30000 \
  --tokenizer amd/Qwen3.8-Flash-Next-Quark-MXFP4-AttnFP8-PLEFP8 --dataset-name random-ids \
  --random-input-len 32768 --random-output-len 256 --random-range-ratio 1.0 \
  --num-prompts 48 --max-concurrency 12 --seed 1
Load, other measures, per lifetime 1 off 2 on 3 on 4 off
output throughput (tok/s) 212.1 274.6 269.2 250.3
median TTFT (ms) 5,567 3,003 2,493 4,297
p99 TTFT (ms) 13,722 9,277 8,746 12,071
median E2E (ms) 12,439 11,275 11,514 12,187

Server start to ready: 216, 521, 226 and 151 s, of which loading the target weights took 41, 328, 160 and 84 s. The warm-up line follows Using QSA for sparse full-attention layers. by 26 s in lifetime 2 (empty FlyDSL kernel cache) and 1 s in lifetime 3.

Kernels. python test/registered/kernels/benchmark/attention/bench_qsa_flydsl_prefill.py --sweep, three runs, each on a different GPU. Per case: median over the runs of the time of 12 layers on one layout (new for each chunk over a prefix) and of the ratio [min-max]; first-call extra = median first call minus median later call of one layer.

Rows Prefix Selection Triton ms FlyDSL ms Ratio [range] First-call extra ms, Triton / FlyDSL
16,384 0 random 60.72 39.84 0.66 [0.63-0.68] 0.08 / 0.16
16,384 0 shared 49.96 13.98 0.28 [0.27-0.29] 0.11 / 0.10
16,384 16,384 random 89.84 74.72 0.83 [0.81-0.83] 0.09 / 0.90
16,384 16,384 shared 54.15 17.84 0.32 [0.31-0.33] 0.23 / 0.67
16,384 83,968 random 104.04 99.00 0.95 [0.92-1.03] 0.15 / 1.20
16,384 83,968 shared 55.39 25.40 0.45 [0.44-0.46] 0.21 / 0.82
8,192 32,768 random 48.44 37.49 0.77 [0.77-0.78] 0.08 / 0.67
8,192 32,768 shared 26.01 10.58 0.39 [0.38-0.41] 0.20 / 0.55
4,096 96,256 random 26.52 22.43 0.85 [0.84-0.89] 0.07 / 0.47
4,096 96,256 shared 14.14 8.10 0.57 [0.55-0.58] 0.09 / 0.47
2,048 98,304 random 13.61 10.36 0.76 [0.76-0.77] 0.05 / 0.35
2,048 98,304 shared 7.49 4.28 0.55 [0.54-0.60] 0.07 / 0.36
1,024 98,304 random 7.15 4.83 0.67 [0.67-0.68] 0.04 / 0.22
1,024 98,304 shared 4.16 2.55 0.60 [0.59-0.61] 0.06 / 0.22
512 98,304 random 3.85 2.10 0.54 [0.54-0.55] 0.04 / 0.20
512 98,304 shared 2.55 1.77 0.69 [0.68-0.70] 0.05 / 0.22
256 98,304 random 2.26 1.58 0.69 [0.69-0.70] 0.04 / 0.20
256 98,304 shared 1.61 1.68 1.03 [1.02-1.04] 0.04 / 0.20
64 98,304 random 1.29 1.52 1.17 [1.17-1.18] 0.04 / 0.20
64 98,304 shared 1.28 1.52 1.19 [1.19-1.21] 0.05 / 0.22
1 98,304 random 1.29 1.33 1.04 [1.03-1.04] 0.05 / 0.20
1 98,304 shared 1.28 1.34 1.04 [1.04-1.05] 0.05 / 0.21

Device memory the op keeps after its calls (planner cache and work buffers, outside the KV-pool sizing; one process, layouts in this order): 6.8 MiB after 1,536 rows, 148.6 after 16,384 rows, 201.5 after 16,384 over 16,384, 334.1 after 16,384 over 83,968 (unchanged by 2,048 over 98,304), 650.9 after 16,384 over 245,760. Served, available_gpu_mem after graph capture is 24.79 and 24.94 GB with the flag against 25.44 and 25.49 GB without; the KV pool is the same (3,595,456 tokens).

Tests (test_qsa_rocm.py in the container): with this PR, 7 passed (38 s with an empty FlyDSL cache, 13 s warm); on the image's sources without it, the three new cases fail. Each mutation of the PR below makes at least one new case fail:

Mutation Fails
prefix branch keeps Triton prefix
no-prefix branch keeps Triton no-prefix
prefix branch passes zero prefix lengths prefix
op used without the flag no-prefix, prefix
no probe for the ROCm/aiter#7556 API guard
softmax scale x 1.05 no-prefix, prefix
prefix branch passes the unpacked K/V prefix
prefill graph scope ignored no-prefix, prefix
qsa_sparse_prefill_problem ignored no-prefix, prefix
no-prefix branch discards the op's output no-prefix
prefix branch discards the op's output prefix

Numerical difference (forward_extend, flag on against off, same operands; max error against float32 on 64 sampled rows):

Query rows Prefix Selection max |F-T| mean |F-T| equal FlyDSL err Triton err
1,536 0 random (test seed) 0.01562 1.60e-04 46.5% 0.0038 0.0041
1,536 + 700 3,000 + 0 random (test seed) 0.01562 1.24e-04 45.4% 0.0037 0.0044
16,384 0 shared 0.01562 8.83e-05 44.6% 0.0012 0.0014
16,384 0 random 0.01562 8.84e-05 44.8% 0.0012 0.0012
16,384 16,384 shared 0.001953 8.07e-05 44.9% 0.00059 0.00067
16,384 16,384 random 0.001953 7.84e-05 45.4% 0.00057 0.0014
16,384 83,968 shared 0.001953 8.08e-05 44.6% 0.00072 0.00076
16,384 83,968 random 0.001953 7.84e-05 45.4% 0.00057 0.00082
2,000 + 1 + 1 + 5,000 90,000 + 30,000 + 0 + 12,000 shared 0.001953 8.07e-05 44.7% 0.00068 0.00066
2,000 + 1 + 1 + 5,000 90,000 + 30,000 + 0 + 12,000 random 0.001953 7.83e-05 45.5% 0.00058 0.00077

Checklist

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ⊘ Skipped -- the PR is a draft; marking it ready starts a new run (AMD: then /rerun-failed-ci).
Latest PR Test (Extra): ⊘ Skipped -- the PR is a draft; marking it ready starts a new run (AMD: then /rerun-failed-ci).
Latest PR Test (AMD ROCm 10): ⚠️ Run #46306930568

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

On gfx950 with SGLANG_ROCM_QSA_FLYDSL_PREFILL=1, both extend branches of
QwenSparseAttnBackend.forward_extend (no prefix, and over a prefix on the
packed K/V) run the sparse attention on aiter.ops.qsa.qsa_sparse_prefill
(ROCm/aiter#7556) instead of the Triton kernels. Calls the op refuses
(qsa_sparse_prefill_problem), prefill graph runs and an aiter without the
API keep Triton. The backend compiles the op's kernels at start-up through
qsa_sparse_prefill_warmup.

Adds a real-kernel test of both branches against a float32 reference, a
manual microbenchmark of the two paths and the variable's docs entry.

Co-authored-by: Claude <[email protected]>
Signed-off-by: Rony Leppänen <[email protected]>
@mintlify

mintlify Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit 2728c20.

Approve preview

@github-actions github-actions Bot added documentation Improvements or additions to documentation amd labels Nov 18, 2026
…kens

aiter#7556's qsa_sparse_prefill_warmup now also runs a layout whose
request has a 4-token prefix (it keeps the request on the base union
tile with batch_invariant=True) and returns the (query_lens,
prefix_lens) that ran. run() sized K/V by the query rows alone, so the
op refused that layout: its kernels stayed uncompiled and start-up
logged "QSA extend attention stays on Triton". K/V now hold each
request's prefix and query tokens.

Checked on MI355X against aiter#7556's head: the warm-up's source runs
every layout at 24/2, 12/1 and 48/4 query/KV heads (selection width
2051); with the previous run() the op refused the prefix layout.

Signed-off-by: Rony Leppänen <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
maeehart pushed a commit to SemiAnalysisAI/InferenceX that referenced this pull request Nov 24, 2026
…draft head at its shipped precision

Patches 11-16, 21 and 24 add opt-in kernels, each off unless the recipe sets its SGLANG_AMD_*
variable; c12, c24, c48 and c80 set all eight:
- 11 grouped QSA verify scoring (sgl-project/sglang#48625, carries #46631)
- 12-13 FlyDSL QSA sparse prefill (ROCm/aiter#7556 kernels in the image's AITER checkout,
  sgl-project/sglang#51179 wiring)
- 14 MXFP4 MoE intermediate kept 128-aligned at TP 1 (sgl-project/sglang#49852)
- 15 Triton HC prefill path (sgl-project/sglang#50863)
- 16 QSA indexer prefill blocks selected by the exact topk_v2, one pipeline stage on gfx95
  (sgl-project/sglang#50871, #51263)
- 21 QSA decode, verify and draft-extend blocks selected by the exact topk_v2, draft-extend
  rows scored grouped as in verify (sgl-project/sglang#50871, #51242)
- 24 GDN ReplaySSM verify and commit kernels (sgl-project/sglang#51180)

c1 and c2 set SGLANG_AMD_MONO_QWEN38_DRAFT_HEAD_FP8=0, and the recipe drops
default-chat-template-kwargs so the server serves the chat template's default thinking mode.
The setup script uses set -eo pipefail (AGENTS.md Bash: no nounset).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Rony Leppänen <[email protected]>

This branch has not been deployed

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

Labels

amd documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant