Repository navigation
Conversation
On gfx95, select_prefill_tokens picks each row chunk's blocks with topk_v2's ragged entry point instead of fast_topk, which drops members of an overfull threshold bin (sgl-project#36807). topk_v2 reads only the order of the scores, so they skip the division by sqrt(head_dim); the logits width rounds up to the multiple of 4 that topk_v2's loads need. In batch-invariant mode topk_v2 runs deterministic, giving ties to the smallest block indices. Co-authored-by: Claude <[email protected]> Signed-off-by: Rony Leppänen <[email protected]>
The one conflict is the sglang.srt.utils import in qsa_indexer.py: main added is_npu and the _is_npu constant, this branch added is_gfx95_supported. Both are kept. Co-authored-by: Claude <[email protected]> Signed-off-by: Rony Leppänen <[email protected]>
On gfx95, select_decode_tokens (decode, target-verify and draft-extend batches) picks each row's blocks with topk_v2's paged entry point, without a page table, instead of fast_topk, which drops members of an overfull threshold bin (sgl-project#36807). topk_v2 needs the logits row stride to be a multiple of 4 for its 16-byte loads; rows with another stride keep fast_topk. The prefill test and benchmark now cover decode as well and are renamed to test_qsa_select_rocm.py and bench_qsa_select.py. Co-authored-by: Claude <[email protected]> Signed-off-by: Rony Leppänen <[email protected]>
select_decode_tokens leaves the scores of rows that fit k unwritten, which topk_v2's trivial path, like fast_topk's, never reads. Comment only; no code change. Co-authored-by: Claude <[email protected]> Signed-off-by: Rony Leppänen <[email protected]>
5 tasks done
Co-authored-by: Claude <[email protected]> Signed-off-by: Rony Leppänen <[email protected]>
main renamed the CI stages (sgl-project#50874); a test registered under stage-b now maps to a suite no workflow runs. Co-authored-by: Claude <[email protected]> Signed-off-by: Rony Leppänen <[email protected]>
This was referenced Nov 18, 2026
eppaneamd
added a commit
to eppaneamd/InferenceX
that referenced
this pull request
Nov 18, 2026
Patches 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: - 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) Co-authored-by: Claude <[email protected]> Signed-off-by: Rony Leppänen <[email protected]>
4 of 11 tasks
5 of 11 tasks
4 of 11 tasks
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]>
Signed-off-by: Rony Leppänen <[email protected]> Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2 tasks done
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
QSA's block selection picks each row's 512 blocks with
fast_topk, which stages at most 4,096 members of a row's threshold bin and drops the rest, so a row with more drops members of its true top 512 (#36807 reports the same overflow infast_topk_v2). On unpatched main, four rows of 8,192 scores in one threshold bin leave 239 to 263 of each row's 512 selected scores below the true 512th largest in two runs (fast_topkagainsttorch.topk; repro in the details).This PR picks the blocks on gfx95 with
topk_v2(#47960), which is exact in its default mode, for extend and for decode, target-verify and draft-extend batches. In extend,topk_v2reads only the order of the scores, so they stay unscaled and the division pass goes away.On one MI355X, one layer's selection takes 24.6 % less time for a 16,384-token extend chunk on an 83,968-token prefix, and 57.8 % less for a verify step of 60 requests (0.50 ms per decode step).
Most of the extend gain is the division pass, which #48274 removes too;
topk_v2alone saves 4.6 % to 11.0 %.Related work:
fast_topkexact with a fallback for an overflowing bin.mqa.pyhunk; whichever lands second drops that hunk.max_model_lenwith no padding (see Assumptions).Modifications
qsa_indexer.py,select_prefill_tokens, on gfx95 when the request-relative TileLang scoring runs:topk_transform_ragged_v2instead ofqsa_fast_topk;score_scale=1.0);topk_v2's 16-byte loads need;topk_v2runs deterministic: ties go to the smallest block indices, asqsa_fast_topkbreaks them with FlashInfer on CUDA.qsa_indexer.py,select_decode_tokens, on gfx95:topk_transform_paged_v2without a page table (it writes raw block indices and leaves the scores unmodified) instead offast_topk;fast_topk.mqa.py,tilelang_qsa_mqa_prefill_relative: ascore_scaleof 1 skips the division.Scope:
qwen4_exp.py) on gfx95;block_topkother than 512, which keep their paths inselect_decode_tokens;amd/Qwen3.8-Flash-Next-Quark-MXFP4-AttnFP8-PLEFP8; GSM8K served with that checkpoint.Assumptions:
fast_topk, so that path is unchanged.topk_v2's split path (a batch of at most 16 rows with a long one) keeps one scratch buffer per device, which each launch leaves clean for the next, so two selections must not overlap on one device.SGLANG_ALT_STREAM=1on ROCm, in decode graph capture) is joined before each layer's attention; the other modes are in the details.Accuracy Tests
main) and 14 (this PR) of its 1,319 requests reach 2,052 tokens, the length from which a decode row has more than 512 blocks and its selection stops taking every block;fast_topkcan drop members of the top 512 (the longest request is 6,520 tokens).topk_v2's default mode (SGLANG_TOPK_V2_MODE=1). On the benchmark's random scores:torch.topk), and no row's block set differs between them;…-20261115), each of the 120 differing rows of a call was checked againsttorch.topk:sqrt(head_dim)merged two distinct scores at the 512th place, so today's set is a top 512 of the divided scores only and this PR's of the undivided ones.test_qsa_select_rocm.py(MI35x suite), throughselect_prefill_tokensandselect_decode_tokens; the exactness tests fail on main, and the twofast_topkguards (off gfx95, a row stride not a multiple of 4) are checked by mutation (cases and mutations in the details below).test_qsa_indexer, 3 intest_qsa_determinism). Each of ten mutations of the change fails a named test; an eleventh, keeping the division, selects the same blocks and passes.GSM8K, all 1,319 questions,
amd/Qwen3.8-Flash-Next-Quark-MXFP4-AttnFP8-PLEFP8at TP1 with MTP on one MI355X, onlmsysorg/sglang-rocm:v0.5.23-rocm10-mi35x-20261117, the same server for both:main(372b35034d, the merge base)1fbe3b8b2e)mainand 14 only with this PR (p = 0.49). Two runs of the same kernels on this image differ as much: 12 strict verdicts, and 16 of 1,319 responses are word for word the same.topk_v2's ragged entry point (7,319 calls) and the captured decode graphs its paged entry point, and nothing calledfast_topk; onmain,fast_topkran in both. Every logged decode batch replayed captured graphs.GSM8K setup
lmsysorg/sglang-rocm:v0.5.23-rocm10-mi35x-20261117(sha256:09b8237296024285e6286ffb54d75c3af6d59342bbbb3558dea56bed86798814, SGLangc67db7c762), one MI355X. Each commit'spython/sglangcopied over the image's editable install (/sgl-workspace/sglang/python/sglang); the sha256 of every file checked against the commit. The two trees differ only inqsa_indexer.pyandmqa.py.amd/Qwen3.8-Flash-Next-Quark-MXFP4-AttnFP8-PLEFP8, revisionbd6e49d9abfe0ce1da53a45593daaee83580035b.\boxed{}answer checked against the reference. Flexible: the last number in the response equals the reference (lm-evaluation-harness's GSM8K flexible-extract regex)._exact_block_topk,_exact_decode_block_topk,qsa_fast_topkandfast_topk, during CUDA graph capture and outside it. The extend selection runs outside the captured graph segments, so its calls are counted as served; a replayed decode graph runs no Python, so served decode batches are counted from the server log's batch lines (cuda graph: True).Speed Tests and Profiling
topk_v2's JIT module, which the extend selection builds too: 9.0 s with an empty cache against 2.2 s forfast_topk's (8 rows of 40,000 scores).One layer's extend selection (
select_prefill_tokens, all row chunks), median µs on one MI355X:topk_v2, divided scorestopk_v2aloneOne layer's decode selection (
select_decode_tokens, 4 verify rows a request, CUDA graph), median µs:A decode step selects 13 times: once for each of the 12 full-attention layers of
amd/Qwen3.8-Flash-Next-Quark-MXFP4-AttnFP8-PLEFP8in the verify pass and once for its MTP layer in the draft-extend pass. The last column is 13 times the saving per call, with the draft-extend call timed at the verify shape.The percentages are medians of the per-repetition ratios. This PR's arm, timed twice in the same loop, lies within −0.2 % to +0.2 % of itself (A/A) in extend and −1.2 % to +1.0 % in decode.
Repro, single-stream check, setup, method, tests and mutations
Repro on unpatched main (
lmsysorg/sglang-rocm:v0.5.23-rocm10-mi35x-20261117, SGLangc67db7c762, one MI355X). The scores lie in[100, 101), one threshold bin offast_topk, which stages at most 4,096 of its members:A second run printed
[250, 239, 263, 253]; the counts vary a little between runs. The same input throughtopk_transform_paged_v2, the call this PR makes (also on main), selects no score below the cut:Single-stream check of
topk_v2's split scratch, from reading the code on this branch (nothing was run concurrently):The selection is called only by
QSAIndexer.select_prefill_tokensandselect_decode_tokens, fromQSAIndexer._forward_impl; the prefill (ragged) entry point does not use the scratch.Qwen4ExpAttentionDecoderLayer.self_attentionruns the indexer onalt_streamonly while a decode graph is captured, for fewer than 1,024 tokens, and on ROCm only whenSGLANG_ALT_STREAM=1creates the stream. The main stream's work meanwhile (_prepare_qkv_gate) does not select, the layer joins the alt stream before attention, and the next layer forks from the joined stream, so selections run one after another.The speculative worker's
plan_streamrunsprepare_for_draft_extend(metadata); the draft extend runs on the main stream.Qwen4 PLE models, the checkpoint above included, raise
NotImplementedErrorunder two-batch overlap, andbatch_overlap/creates no stream.python/sglang/srthas no prefill/decode multiplexing mode (nopdmuxor green-context code); disaggregated prefill and decode are separate servers, each with its own scratch.Image:
lmsysorg/sglang-rocm:v0.5.23-rocm10-mi35x-20261117(SGLangc67db7c762), with this branch's files applied (each file's sha256 checked against the branch).torch 2.11.0+rocm10.0.0, Triton 3.8.0, one MI355X (gfx950).
Benchmark (
test/manual/kernels/attention/bench_qsa_select.py):select_prefill_tokens: today (the branch other platforms take),topk_v2with divided scores, this PR, and this PR again (A/A);select_decode_tokenscaptured in a CUDA graph: today, this PR, this PR again (A/A), and this PR with the extend entry point (topk_transform_ragged_v2) on the same rows, which is 4 % to 26 % slower than the paged one;New tests in
test_qsa_select_rocm.py:fast_topkstill runs off gfx95 and on a row stride that is not a multiple of 4.topk_v2on every platform)test_select_decode_tokens_keeps_fast_topk[not_gfx95]test_select_decode_tokens_keeps_fast_topk[row_stride_not_a_multiple_of_4]test_select_decode_tokens_exact_on_narrow_band_scorestest_select_decode_tokens_exact_under_graph_replay(capture fails)fast_topktest_select_prefill_tokens_exact_on_narrow_band_scorestopk_v2test_select_prefill_tokens_ties_go_to_the_smallest_blocks_when_batch_invarianttest_select_prefill_tokens_exact_on_narrow_band_scores[8190]topk_v2reads past each row's lengthtest_select_prefill_tokens_exact_on_narrow_band_scorestest_select_prefill_tokens_exact_on_narrow_band_scorestest_select_prefill_tokens_exact_on_narrow_band_scoresChecklist
🤖 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⚠️ Run #46307622199
/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):
Tip
Base CI only covers the core cases, and its workload may not exercise your change. Use
/rerun-testto run the tests your change touches, or copy this prompt for an agent: