Skip to content

[Perf] [AMD] gdn: ReplaySSM verify q/k prep kernel and layer-loop commit - #51180

Draft
eppaneamd wants to merge 4 commits into
sgl-project:mainfrom
eppaneamd:rocm-gdn-verify-split
Draft

eppaneamd wants to merge 4 commits into
sgl-project:mainfrom
eppaneamd:rocm-gdn-verify-split

Conversation

@eppaneamd

@eppaneamd eppaneamd commented Nov 18, 2026 •

Copy link
Copy Markdown

Motivation

With --enable-linear-replayssm-spec --mamba-ssm-dtype bfloat16 and MTP (4 draft tokens, the default ring of 16), every verify step runs gdn_replayssm_spec_decode once per GDN layer and commit_gdn_replayssm_circular once for all of them. On gfx950 two parts of that work repeat:

  • The verify grid is (V tiles, requests, value heads). Each program L2-normalizes its key head's q and k, splits k into BF16 high and low parts and forms k @ k^T and k @ q^T, which depend only on the request and the key head. The kernel takes 262 VGPRs (64-wide V tile) or 334 (128-wide), one wave per SIMD, so MI355X runs 1,024 of its programs at a time; at 16 key and 48 value heads a step of 11 requests needs a second round.
  • The commit grid is (V tiles, requests, value heads x layers). With track rows, which the radix cache passes, the kernel takes 168 VGPRs (3 waves per SIMD; 6 without track rows), and every program reloads its request's slot, cursors and track entry to fold one layer.

This PR computes the q/k part once per (request, key head) in a new kernel, which brings the verify kernel to 144 VGPRs (3 waves per SIMD), and folds all layers of a (V tile, request, value head) in one program. The arithmetic and its order are unchanged, so outputs, rings and checkpoints are bitwise those of main. On one MI355X at 16 key and 48 value heads and 36 GDN layers (Qwen3.8-Flash-Next at TP1), the GDN verify and commit of a step take 8.8 to 25.4 % less from 11 to 128 requests; at six other head and layer counts, 8.3 to 26.9 % less where the new launches run.

Limits:

Scope:

  • gdn_replayssm_spec_decode runs in the GDN backend's ReplaySSM spec verify (--enable-linear-replayssm-spec), for models with GatedDeltaNet layers (qwen3_next.py, qwen3_5.py, qwen4_exp.py, gigachat35.py, interns2_mobius.py, jet_nemotron.py). The prep needs 4 draft tokens (MTP, top-k 1).
  • commit_gdn_replayssm_circular follows every such verify: the MTP commit in spec_utils.py and the DFLASH / DSPARK commit in hybrid_linear_attn_backend.py. Its change does not depend on the draft count.
  • Unchanged: an FP32 checkpoint (the default with --enable-linear-replayssm-spec), other shapes, launches under the thresholds, other GPUs (apart from the PLE row masks in qwen4_exp.py, below). On gfx950 the launches that do not change compile to the same instructions as on main.

Related work:

Modifications

  • New gdn_replayssm_qk_prep_kernel, grid (requests, key heads): the L2 norms, the normalized q, k and k low-part tiles, and k @ k^T and k @ q^T with the low-part terms, into a [B, H, 3, 4, K] BF16 and a [B, H, 2, 4, 4] FP32 buffer.
  • gdn_replayssm_spec_circular_kernel: under the new QK_PREP constexpr it reads those buffers through its q and k arguments instead of computing them (a tl.static_assert checks the preconditions). The q/k arithmetic moved into _qk_rnorm, _qk_tiles and _qk_products, used by both kernels. No new pointer or stride argument, so the launches that do not prep keep main's kernel arguments and instructions.
  • gdn_replayssm_compact_commit_kernel: the per-layer body runs in a tl.range loop over NUM_LAYERS that advances the layer pointers; with LAYER_LOOP the grid is (1, requests, value heads), a 128-wide V tile and 8 warps.
  • Routing on gfx950 with K = V = 128, a ring of 16, a BF16 checkpoint and low-part rings:
    • verify (4 draft tokens, BF16 q and k, in-kernel q/k norm): the prep launch (64-wide V tile, one stage) when main's launch has more than _VERIFY_RESIDENT_PROGRAMS = 1024 programs;
    • commit: the layer loop with track rows from _LAYER_LOOP_MIN_PROGRAMS = 1536 (requests x value heads).
  • qwen4_exp.py, on any device: both PLE verify call sites, the N-gram commit and the short conv, pass their row mask (batch.lengths.ne(0)) to select_verify_intermediate_state_indices as a callable, as the GDN and KDA verify layers do since [Feat] kimi_k3: Merge the rest of the Kimi-K3.1 Day-0 branch #48414. The selector reads the mask only with PP spec stable rows (SGLANG_ENABLE_PP_SPEC), so a verify step without them skips the compare: once a step in the N-gram commit, and once per PLE layer in the short conv when the fused verify conv does not run (PLE fusion off, NPU, or inputs it does not take). The rows the selector returns are unchanged. test_ple_state_coordinates.py (CPU) checks, for both sites, the rows written with and without stable rows and that the mask is computed only with them; it fails without either change.
  • test_gdn_replayssm_batched_launch.py (gfx950, MI35x suite) and the benchmark test/manual/kernels/benchmark/attention/bench_gdn_replayssm_spec_decode.py.

Accuracy Tests

  • Bitwise: at 16 key and 48 value heads, verify outputs and ring writes and the committed checkpoints and cursors equal main's at 2 to 64 requests; at the six other head and layer counts of the forced-launch runs (details) the verify outputs and ring writes do at 4 to 128 requests. The new test checks the verify at 10 and 32 requests and the commit at 32, each plus a padding row; it fails on main and under each of 5 mutations.
  • GSM8K, all 1,319 questions, amd/Qwen3.8-Flash-Next-Quark-MXFP4 at TP1 with MTP on one MI355X, lmsysorg/sglang-rocm:v0.5.23-rocm10-mi35x-20261117, the same server flags for both:
GSM8K main this PR
strict-match 0.9659 0.9621
flexible-extract 0.9659 0.9621

The difference is 5 of 1,319 questions: 17 right only on main, 12 right only with this PR (paired standard error 0.41 points). With this PR the server compiled gdn_replayssm_qk_prep_kernel and the 8-warp commit kernel (the layer loop, so the radix cache passed track rows); main's compiled neither. 21 of the 26 logged decode batches of each run had 61 to 64 running requests.

GSM8K setup and path evidence

Server (both runs, SGLANG_USE_AITER=1, main = 372b35034d's python/ or this PR's first on PYTHONPATH, each with an empty Triton cache):

python3 -m sglang.launch_server --model-path amd/Qwen3.8-Flash-Next-Quark-MXFP4 \
  --revision 89d245391842bf53fb9b0700646843c1f40de341 --tp 1 --trust-remote-code --quantization quark \
  --reasoning-parser qwen3 --attention-backend aiter --context-length 16384 --mem-fraction-static 0.95 \
  --enable-linear-replayssm-spec --mamba-ssm-dtype bfloat16 \
  --speculative-algorithm NEXTN --speculative-num-steps 3 --speculative-eagle-topk 1 \
  --speculative-num-draft-tokens 4 --max-running-requests 128 --cuda-graph-max-bs-decode 128 \
  --max-mamba-cache-size 400

--max-mamba-cache-size 400 lets 80 requests run (5 state slots each); with the default the server capped them at 30, below the layer loop's 32. Client: lm-eval 0.4.13, local-chat-completions, gsm8k, 5-shot as multi-turn chat, temperature 0, max_gen_toks 1024, thinking off, 64 concurrent requests, seed 42.

Kernels compiled into each server's empty Triton cache (gdn_replayssm*, name, warps, stages):

main:     spec_circular 1/1, spec_circular 1/2, compact_commit 4/2, _finish_..._fold 4/2
this PR:  spec_circular 1/1, spec_circular 1/2, qk_prep 1/1, compact_commit 4/2, compact_commit 8/2, _finish_..._fold 4/2

Speed Tests and Profiling

One MI355X, lmsysorg/sglang-rocm:v0.5.23-rocm10-mi35x-20261117, the benchmark added here: the verify of 36 GDN layers plus the commit with track rows, 16 key and 48 value heads, µs a step, mean of two runs:

requests main this PR change
8 668.6 668.5 0.0 %
11 1,073.8 822.2 -23.4 %
16 1,290.2 1,029.6 -20.2 %
24 1,876.0 1,398.7 -25.4 %
32 1,884.8 1,641.1 -12.9 %
48 2,632.2 2,346.8 -10.8 %
64 3,322.1 2,984.4 -10.2 %
128 6,263.1 5,710.7 -8.8 %

At six other head and layer counts every step that takes a new launch is 8.3 to 26.9 % shorter, and every other step within 0.5 % (details).

Setup, method, other head counts, tests and mutations

Image lmsysorg/sglang-rocm:v0.5.23-rocm10-mi35x-20261117 (sha256:09b8237296024285e6286ffb54d75c3af6d59342bbbb3558dea56bed86798814, Triton 3.8.0, PyTorch 2.11.0+rocm10.0.0), one MI355X. main is 372b35034d; its GDN files equal the image's. This PR's script runs with each side's python/ first on PYTHONPATH, over the image's sglang:

PYTHONPATH=$CHECKOUT/python python3 test/manual/kernels/benchmark/attention/bench_gdn_replayssm_spec_decode.py \
  --h 16 --hv 48 --num-layers 36 --batch-sizes 1 2 4 8 11 16 24 32 48 64 96 128

The benchmark sizes its pools as a server with 128 running requests does (640 state slots, 129 ring rows), so the checkpoint pool exceeds 2 GiB and Triton does not compile its pointer with 32-bit offsets, as in serving. q, k and v are views of one conv output. Each part runs in a CUDA graph (triton.testing.do_bench_cudagraph, median), replayed back to back. The commit without track rows takes main's kernel on both sides; its difference between the runs (up to 3.8 %) is the run-to-run noise.

Step change by head count (key/value heads/GDN layers), from the same command:

heads / layers steps with a new launch (requests) change there other steps
16/48/36 prep 11-128, loop 32-128 -8.8 to -25.4 % within 0.1 %
8/24/36 prep 24, 48-128; loop 64-128 -8.3 to -23.9 % within 0.3 %
4/12/36 prep 96-128; loop 128 -10.4, -12.8 % within 0.4 %
16/32/36 prep 24, 48-128; loop 48-128 -9.5 to -19.9 % within 0.3 %
16/64/45 prep 11-128; loop 24-128 -11.7 to -26.8 % within 0.5 %
4/16/45 prep and loop 96-128 -10.4, -13.8 % within 0.3 %
16/48/48 prep 11-128; loop 32-128 -12.4 to -26.9 % within 0.5 %

Where the thresholds come from: from the kernels' ISA (; Occupancy, waves per SIMD; MI355X has 1,024 SIMDs), main's verify takes 262 or 334 VGPRs (1 wave), the prep launch's verify 144 (3); main's commit takes 168 with track rows (3) and 74 without (6), the layer loop 240 (2, so one 8-warp program per CU). With forced launches at 16/48/36, 8/24/36, 4/12/36, 16/32/36, 8/16/36, 16/64/45 and 4/16/45 and 4 to 128 requests (cold L2, main's MambaPool, verify µs a layer, commit with track rows), the prep was 4 to 38 % faster than main's launch at every point above 1,024 main programs; from 448 to 1,024 it was 0.3 % faster to 14 % slower, and at 384 and fewer 8 % faster to 7 % slower, where main's kernel with one stage and a 128-wide tile (#47433) was faster than both. The layer loop was faster at every point from 1,536 programs and slower at some below (8, 24, 36 and 40 requests at 16 value heads: 0.5, 1.5, 2.25 and 2.5 rounds of one program per CU). All of these verify launches were bitwise equal to main's.

Tests: test_gdn_replayssm_batched_launch.py passes; on main it errors (no _VERIFY_RESIDENT_PROGRAMS); it fails under each of: the prep kernel without the low-part terms, _VERIFY_RESIDENT_PROGRAMS = 1 << 40, the layer loop without the k_cache or the d_residual_cache layer step, _LAYER_LOOP_MIN_PROGRAMS = 1 << 40. test_gdn_replayssm_direct_commit.py (8 tests) and test_gdn_replayssm_dflash_commit.py (10) pass with and without this PR. The launches this PR leaves to main's code (BF16 and FP32 checkpoints, 64- and 128-wide verify tiles, commits with and without track rows) compile to the same instructions as on main, compared from empty Triton caches.

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 #46306933681

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, for a BF16 checkpoint with low-part rings at K = V = 128 and
a ring of 16:

- When the verify launch (4 draft tokens) has more than 1024 programs,
  the most main's launches run at once on MI355X (one wave per SIMD),
  gdn_replayssm_qk_prep_kernel normalizes q and k and forms k @ k^T and
  k @ q^T once per (request, key head). The verify kernel reads them
  under QK_PREP, through its q and k arguments, instead of recomputing
  them in every (V tile, value head) program, and runs three waves per
  SIMD.
- With track rows and at least 1536 (request, value head) programs, the
  compact commit folds every layer of a (V tile, request, value head) in
  one program (LAYER_LOOP).

Both are bitwise equal to the launches they replace. Other cases launch
as before; on gfx950 those launches compile to the same instructions.

Co-authored-by: Claude <[email protected]>
Signed-off-by: Rony Leppänen <[email protected]>
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]>
eppaneamd and others added 2 commits November 18, 2026 16:21
select_verify_intermediate_state_indices reads its row mask only with
PP spec stable rows (SGLANG_ENABLE_PP_SPEC) and otherwise returns its
default. Since sgl-project#48414 the GDN and KDA verify layers pass the mask as a
callable. The PLE N-gram verify commit now passes batch.lengths.ne(0)
the same way, so a verify step without stable rows launches no compare
for it.

The new test checks the rows the commit writes with and without stable
rows, and that the mask is computed only with them.

Co-authored-by: Claude <[email protected]>
Signed-off-by: Rony Leppänen <[email protected]>
The fused verify conv handles the PLE short conv in verify when PLE
fusion is on (the default) and it takes the inputs (BF16 on the GPU,
not NPU). Otherwise each PLE layer's _short_conv passes
batch.lengths.ne(0) to select_verify_intermediate_state_indices. It
now passes the mask as a callable, as the N-gram verify commit does, so
a verify step without PP spec stable rows launches no compare for it.

The test now covers both PLE verify sites: the rows each writes with
and without stable rows, and that each computes its mask only with
them. The test file imports the PLE pools and the layer router once,
at module level.

Co-authored-by: Claude <[email protected]>
Signed-off-by: Rony Leppänen <[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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant