Skip to content

qwen4exp : QSA decode independent of other sequences in a unified KV cache - #32247

Closed
am17an wants to merge 5 commits into
masterfrom
aman/qsa-sel-rows
Closed

am17an wants to merge 5 commits into
masterfrom
aman/qsa-sel-rows

Conversation

@am17an

@am17an am17an commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Fixes #32181. With --kv-unified, qwen4exp QSA decode got slower with the total size of the cache, so an idle long conversation in one slot slowed down decode in the others (-37% on a DGX Spark with q8_0 KV).

Five commits:

  1. Score only the k-pools of the ubatch sequences. The indexer gathered, scored and ranked the pools of every sequence, and only then masked the pools of the other sequences. set_input_kpool now lays out only the pools of the sequences in the ubatch. It also drops a std::sort over the reps of all pools that ran on most decode steps; a marker array picks the same pad cells.
  2. Attend through per-token K/V row lists. The QSA selection was an [n_kv, n_tokens] mask, so flash attention scanned every cell (F16 KV, sparse index compaction) or read every cell (quantized KV, vec kernel). For ubatches with at most 8 tokens per sequence (at most 64 tokens), each token now passes its selected cells to ggml_flash_attn_ext_rows (llama + CUDA: flash_attn_ext_rows (fix unified kv for multi-seq) #29510), so attention reads only those rows. Commit 5 extends this to prompts.
    • New op GGML_OP_TOP_K_ROWS (ggml_top_k_rows): the cells that ggml_top_k_mask keeps, as a list per row (I32) or the matching mask. CPU and CUDA, meta backend split rule, RPC op count and patch version bumped.
    • The CUDA vec FA kernel reads K/V through kv_rows (a template variant for one query per slice), so quantized KV is read in place instead of being converted to F16 by the MMA kernel.
  3. Keep the mask path on backends without GGML_OP_TOP_K_ROWS. Metal supports ggml_flash_attn_ext_rows but not the new op, so the probe passed and the selection would have run on the CPU in every QSA layer. The TOP_K_ROWS outputs now go through the same LLM_FUSED_OP_FLASH_ATTN_KV_ROWS probe, so such a backend stays on the mask path.
  4. Read BF16 and F8 K/V in place. The decode dispatch took the vec kernel only for ggml_is_quantized types, so a BF16 or F8_E4M3 cache went to the MMA kernel and was converted to F16 whole on every step (RTX PRO 6000, BF16, idle 100k: 11.10 -> 8.04 ms; master 13.01).
  5. Prompts through grouped row lists. ggml_top_k_rows takes n_group: one list per group of rows (the picks live in any row of the group, then each row's own tail cells) and a mask per row over its group's list. A prompt shares one list per 8 tokens, the query tile of the FA kernels, and pads Q to whole groups.
    • Every graph now uses the row lists at any cache size, with the same nodes (decode pads Q by 0 rows). With commits 1-4, decode used the row lists and prompts used the mask. A graph of another shape makes ggml_gallocr re-plan sized to that graph, and the growing graphs of the next prompt then re-plan at every ubatch: a 4093-token prompt after decode steps re-planned 7 times.

LLAMA_KV_ROWS_DISABLE=1 switches back to the mask path.

Additional information

Qwen3.8-Flash-Next, -c 262144 -np 4 -kvu -fa on. Decode median in ms per token: the decoding sequence has a 512-token prompt, another sequence holds 100k tokens and stays idle.

DGX Spark, UD-IQ4_XS:

KV empty cache, master idle 100k, master idle 100k, PR
q8_0 32.6 51.2 32.8
f16 32.5 36.4 32.6

RTX PRO 6000, IQ4_NL:

case master commit 1 only PR
f16, idle 100k 9.64 8.20 8.06
q8_0, idle 100k 12.56 11.56 7.96
f16, own context 100k, no idle 9.04 - 8.28
q8_0, own context 100k, no idle 12.12 - 8.42
f16, 4 sequences x 32k, ms per step 14.42 13.48 12.78
q8_0, 4 sequences x 32k, ms per step 17.53 16.85 13.19

With commits 1-4 the empty-cache case does not take the new path and is unchanged (RTX PRO 6000 7.8-8.1 ms for both KV types; Spark q8_0 32.56 vs 32.61 ms on master, same output). Commit 5 takes the row lists at any cache size, see the empty-cache decode row below.

Prefill of a 4096-token prompt into the active sequence in ms (best of 3), same models and settings, with the other sequence empty or holding 100k idle tokens. Commits 1-3 (prefill on the mask path, so the gain comes from commit 1):

case RTX PRO 6000, master RTX PRO 6000, PR Spark, master Spark, PR
f16, empty cache 807 813 4521 4517
f16, idle 100k 908 824 4993 4647
q8_0, empty cache 803 810 4544 4543
q8_0, idle 100k 927 886 5130 4824

Commit 5 vs commits 1-4, RTX PRO 6000, same run, ms:

case commits 1-4 commits 1-5
f16 4096 prefill, empty cache 822.5 824.8
f16 4096 prefill, idle 100k 843.4 833.2
q8_0 4096 prefill, empty cache 823.6 831.8
q8_0 4096 prefill, idle 100k 881.2 864.1
f16 4093-token prompt after 16 decode steps, idle 100k (re-plans) 844.1 (7) 778.2 (0)
q8_0 4093-token prompt after 16 decode steps, idle 100k (re-plans) 905.1 (7) 828.7 (0)
4093 tokens in one ubatch (-ub 4096), idle 100k 588.6 579.7
f16 / q8_0 decode, empty cache 7.95 / 8.48 7.89 / 8.21

The 4096-token prefill rows are within the +-3% run-to-run noise at -ub 512. The remaining q8_0 prefill cost with an idle sequence is mostly the MMA kernel converting the whole quantized cache to F16, proportional to n_kv.

Correctness:

  • test-backend-ops: FLASH_ATTN_EXT 4412/4412 and TOP_K_ROWS 57/57 on CUDA vs CPU, including the QSA prefill shapes (8 tokens per slice over 8x2304 cells, partial groups). The commit 2 kv_rows cases at the QSA shape (D 256, GQA 12, f16/q8_0/q4_0) also pass on GB10. TOP_K_ROWS 57/57 on ROCm (gfx1151).
  • On the CPU, the cells each row sees in its group's list are exactly its per-token list and the set ggml_top_k_mask keeps (11 configurations x 3 seeds, random and QSA-shaped, with partial groups).
  • test-llama-archs passes for qwen4exp and glm5-next (its small context stays on the mask path).
  • Real text, 8192-token prompt, teacher-forced decode: row lists vs mask path KLD 5.2e-3 (f16) and 3.9e-3 (q8_0), below the gap from changing only -ub 512 to -ub 256 on the mask path (1.1e-2 and 9.0e-3). Commit 1 alone is bit-exact vs master with FA off.
  • 4 sequences with different prompts decoding together vs one alone: KLD 6.6e-3, the same as the mask path's own gap.
  • Commit 5 vs commits 1-4, 8189-token prompt (the last ubatch is padded), 100k idle, 64 decode steps: KLD 9.5e-3 (f16) and 7.5e-3 (q8_0), at the -ub noise floor.
  • -sm tensor (one GPU) runs on the new path.

Not covered:

  • Commits 4-5 were measured on the RTX PRO 6000 only, not on the Spark.
  • HIP has no kv_rows FA, so it stays on the mask path.
  • Metal stays on the mask path until it has a TOP_K_ROWS kernel. The fallback was checked by rejecting the op on CUDA, not on a Mac.
  • The split cache (-no-kvu) is unchanged.
  • The MTP draft graph takes the same path but was not tested.
  • The lists pad the 2051 selected cells to 2304, a multiple of the FA tile.

Requirements

The QSA indexer gathered, scored and ranked the pools of every sequence in a unified cache, and only then masked the pools of the other sequences. set_input_kpool now lays out only the pools of the sequences in the ubatch, so an idle long sequence adds no indexer work. The new-pool list still covers all sequences.

The pad entries of the new-pool list took their cells by sorting the reps of all pools, on most decode steps. A marker array over the cache cells picks the same cells without the sort.

ref #32181
QSA built its selection as a [n_kv, n_tokens] mask over the whole unified cache. Flash attention then scanned every cell (F16 KV, sparse index compaction) or read every cell (quantized KV, vec kernel), so decode slowed down with the total cache size, idle sequences included.

For ubatches with few tokens per sequence (decode, drafts), each token now passes its selected cells as a K/V row list to ggml_flash_attn_ext_rows, so attention reads only those rows. Prefill-size ubatches and short caches keep the mask.

- ggml : add GGML_OP_TOP_K_ROWS, the cells that ggml_top_k_mask keeps as a list per row (I32) or the matching mask, on CPU and CUDA, with a meta backend split rule and the RPC op count
- CUDA : the vec FA kernel reads K/V through kv_rows (a template variant for one query per slice), so quantized K/V are read in place instead of converted to F16 by the MMA kernel
- tests : TOP_K_ROWS cases and kv_rows FA cases at the QSA shape (D 256, GQA 12, f16/q8_0/q4_0)

ref #32181
@am17an

am17an commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

@sanmai please try this too.

@github-actions github-actions Bot added model Model specific testing Everything test related ggml changes relating to the ggml tensor library for machine learning CUDA Related to the CUDA backend labels Nov 14, 2026
Metal supports flash attention with kv_rows but not GGML_OP_TOP_K_ROWS, so the probe passed and the selection ran on the CPU in every QSA layer. Add both TOP_K_ROWS outputs to the LLM_FUSED_OP_FLASH_ATTN_KV_ROWS probe so such a backend keeps the mask path.
@sanmai

sanmai commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

With a 100k idle slot no slowdown for the second slot. On master I saw TG dropping from 58 to 45 t/s.

Still, there's some non-repeatability: same request in the same slot 1 come out differently with an idle slot 0.

@am17an

am17an commented Nov 14, 2026

Copy link
Copy Markdown
Contributor Author

Still, there's some non-repeatability: same request in the same slot 1 come out differently with an idle slot 0.

Probably some more top-k shenanigans? Are they same on master?

The kv_rows dispatch took the vec kernel only for quantized K/V, so a BF16 or F8_E4M3 cache went to the MMA kernel, which converts the whole cache to F16 for every QSA decode step.
ggml_top_k_rows takes n_group: one cell list per group of rows (the picks live in any row of the group, in pick order, then each row's own extra cells) and a mask per row over its group's list. A partial last group gets -inf rows.

A prompt shares one list per 8 tokens, the query tile of the flash attention kernels, and pads its queries to whole groups. Decode and drafts keep one list per token.

Every graph now takes the row lists at any cache size, with the same nodes. A graph of another shape replaces the reserved allocation, and the growing graphs of a prompt then re-plan it at every ubatch. Before this, decode used the row lists and prompts the mask, so a prompt after decode steps re-planned at every ubatch.
@sanmai

sanmai commented Nov 14, 2026

Copy link
Copy Markdown
Contributor
  • summation/multiplication order varies PPL by about 0.01
  • DeviceTopK ties are relatively rare, only 0.0005 PPL

@sanmai

sanmai commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Re-testing a proposed fix, #32297, to make sure it works as intended and it does.

The reproducer:

#!/bin/sh
for _ in 1 2 3; do
    llama-perplexity -hf unsloth/Qwen3.8-Flash-Next-GGUF:UD-Q4_K_XL -fa on -kvu \
        -c 14336 -b 14336 -ub 8 --chunks 1 -f wikitext-2-raw/wiki.test.raw "$@" 2>&1 | grep "Final estimate"
done 

Without any changes it reports:

3.43.511.316 I Final estimate: PPL = 2.1656 +/- 0.04519
3.34.702.372 I Final estimate: PPL = 2.1712 +/- 0.04553
3.32.637.166 I Final estimate: PPL = 2.1702 +/- 0.04535

With either fix, #32297 or #32299:

6.15.639.565 I Final estimate: PPL = 2.1697 +/- 0.04542
3.36.306.624 I Final estimate: PPL = 2.1697 +/- 0.04542
3.35.907.065 I Final estimate: PPL = 2.1697 +/- 0.04542

@am17an

am17an commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

I'll probably break this down into smaller commits for faster review

@am17an am17an closed this Nov 18, 2026
amangupta-tether added a commit to tetherto/qvac-fabric-llm.cpp that referenced this pull request Nov 18, 2026
…end-ops

The radix top-k (ggml-org#31084) and the QSA row lists (ggml-org#32247) use __ballot_sync, which
the HIP headers do not provide before 7.0 with HIP_DISABLE_WARP_SYNC_BUILTINS:
the hip CI job failed to compile top-k-mask.cu and top-k.cu. Take the mapping
upstream added in ggml-org#31259. Compiled on ROCm 7.2 with and
without the mapping (gfx1151, gfx90a).

test-backend-ops uses std::iota for the row-list indexer cases, which needs
<numeric> on MSVC (windows / x64-cpu-static).

Assisted-by: Claude Opus 5.5
amangupta-tether added a commit to tetherto/qvac-fabric-llm.cpp that referenced this pull request Nov 18, 2026
The row-list port (ggml-org#32247) gave flash_attn_ext_vec a use_kv_rows template
parameter, which changes the mangled names of the allow-listed D=256 FP8-K vec
kernels (a0bbf11). They spill 2-4 VGPRs on gfx908 as before (base: the same
255 + 2 / 255 + 4), but the old names no longer matched, so the HIP quality
check failed. Rename the vec entries and add the kv_rows variants of the
ncols=1 entries (HIP never launches them, but they are compiled).

Checked on ROCm 7.2: the gfx908 check flags the same 11 kernels as CI before
this change and passes after it.

Assisted-by: Claude Opus 5.5
amangupta-tether pushed a commit to tetherto/qvac-fabric-llm.cpp that referenced this pull request Nov 23, 2026
The QSA indexer gathered, scored and ranked the pools of every sequence in a unified cache, and only then masked the pools of the other sequences. set_input_kpool now lays out only the pools of the sequences in the ubatch, so an idle long sequence adds no indexer work. The new-pool list still covers all sequences.

The pad entries of the new-pool list took their cells by sorting the reps of all pools, on most decode steps. A marker array over the cache cells picks the same cells without the sort.

ref ggml-org#32181

Port of ggml-org#32247 commit 1. Fabric: pooled keys live in rep or private rows
(kpool_layout_update), so the pad entries skip the rows of all pools (get_kpool_n_rows), not only
the cells.

Assisted-by: Claude Opus 5.5
(cherry picked from commit 4161abc)
amangupta-tether added a commit to tetherto/qvac-fabric-llm.cpp that referenced this pull request Nov 23, 2026
…gml-org#32247 + ggml-org#32297)

Port of the remaining commits of ggml-org#32247 (648c950, b46da3e,
cabf968, 5444082) and the top-k order fix ggml-org#32297 (7d8eaa6..34fb50d):

- GGML_OP_TOP_K_ROWS: the cells that ggml_top_k_mask keeps as a list per row or per group
  of rows (I32), or the matching mask. CPU and CUDA, picks listed in index order.
- qwen4exp attends through the row lists with ggml_flash_attn_ext_rows, one list per token
  for decode and drafts, one per 8 tokens for prompts.
- the CUDA vec FA kernel reads quantized, BF16 and F8 K/V in place through kv_rows.
- the row lists are probed together with the row-list attention, a backend without
  TOP_K_ROWS keeps the mask path.

Fabric adaptations: build_qsa_sel keeps the fabric sections argument, RPC major 119
(temp-12032 is at 118 with TOP_K_MASK already in the enum).

Assisted-by: Claude Opus 5.5
amangupta-tether added a commit to tetherto/qvac-fabric-llm.cpp that referenced this pull request Nov 23, 2026
The row-list port (ggml-org#32247) gave flash_attn_ext_vec a use_kv_rows template
parameter, which changes the mangled names of the allow-listed D=256 FP8-K vec
kernels (a0bbf11). They spill 2-4 VGPRs on gfx908 as before (base: the same
255 + 2 / 255 + 4), but the old names no longer matched, so the HIP quality
check failed. Rename the vec entries and add the kv_rows variants of the
ncols=1 entries (HIP never launches them, but they are compiled).

Checked on ROCm 7.2: the gfx908 check flags the same 11 kernels as CI before
this change and passes after it.

Assisted-by: Claude Opus 5.5
amangupta-tether pushed a commit to tetherto/qvac-fabric-llm.cpp that referenced this pull request Nov 24, 2026
The QSA indexer gathered, scored and ranked the pools of every sequence in a unified cache, and only then masked the pools of the other sequences. set_input_kpool now lays out only the pools of the sequences in the ubatch, so an idle long sequence adds no indexer work. The new-pool list still covers all sequences.

The pad entries of the new-pool list took their cells by sorting the reps of all pools, on most decode steps. A marker array over the cache cells picks the same cells without the sort.

ref ggml-org#32181

Port of ggml-org#32247 commit 1. Fabric: pooled keys live in rep or private rows
(kpool_layout_update), so the pad entries skip the rows of all pools (get_kpool_n_rows), not only
the cells.

Assisted-by: Claude Opus 5.5
(cherry picked from commit 4161abc)
amangupta-tether added a commit to tetherto/qvac-fabric-llm.cpp that referenced this pull request Nov 24, 2026
…gml-org#32247 + ggml-org#32297)

Port of the remaining commits of ggml-org#32247 (648c950, b46da3e,
cabf968, 5444082) and the top-k order fix ggml-org#32297 (7d8eaa6..34fb50d):

- GGML_OP_TOP_K_ROWS: the cells that ggml_top_k_mask keeps as a list per row or per group
  of rows (I32), or the matching mask. CPU and CUDA, picks listed in index order.
- qwen4exp attends through the row lists with ggml_flash_attn_ext_rows, one list per token
  for decode and drafts, one per 8 tokens for prompts.
- the CUDA vec FA kernel reads quantized, BF16 and F8 K/V in place through kv_rows.
- the row lists are probed together with the row-list attention, a backend without
  TOP_K_ROWS keeps the mask path.

Fabric adaptations: build_qsa_sel keeps the fabric sections argument, RPC major 119
(temp-12032 is at 118 with TOP_K_MASK already in the enum).

Assisted-by: Claude Opus 5.5
amangupta-tether added a commit to tetherto/qvac-fabric-llm.cpp that referenced this pull request Nov 25, 2026
qwen4exp : faster QSA sparse attention (ports of ggml-org#32247, ggml-org#32297, ggml-org#31084 + row-list indexer)
gagallo7 pushed a commit to tetherto/qvac-fabric-llm.cpp that referenced this pull request Nov 25, 2026
The QSA indexer gathered, scored and ranked the pools of every sequence in a unified cache, and only then masked the pools of the other sequences. set_input_kpool now lays out only the pools of the sequences in the ubatch, so an idle long sequence adds no indexer work. The new-pool list still covers all sequences.

The pad entries of the new-pool list took their cells by sorting the reps of all pools, on most decode steps. A marker array over the cache cells picks the same cells without the sort.

ref ggml-org#32181

Port of ggml-org#32247 commit 1. Fabric: pooled keys live in rep or private rows
(kpool_layout_update), so the pad entries skip the rows of all pools (get_kpool_n_rows), not only
the cells.

Assisted-by: Claude Opus 5.5
(cherry picked from commit 4161abc)
(cherry picked from commit f315918)
gagallo7 pushed a commit to tetherto/qvac-fabric-llm.cpp that referenced this pull request Nov 25, 2026
…gml-org#32247 + ggml-org#32297)

Port of the remaining commits of ggml-org#32247 (648c950, b46da3e,
cabf968, 5444082) and the top-k order fix ggml-org#32297 (7d8eaa6..34fb50d):

- GGML_OP_TOP_K_ROWS: the cells that ggml_top_k_mask keeps as a list per row or per group
  of rows (I32), or the matching mask. CPU and CUDA, picks listed in index order.
- qwen4exp attends through the row lists with ggml_flash_attn_ext_rows, one list per token
  for decode and drafts, one per 8 tokens for prompts.
- the CUDA vec FA kernel reads quantized, BF16 and F8 K/V in place through kv_rows.
- the row lists are probed together with the row-list attention, a backend without
  TOP_K_ROWS keeps the mask path.

Fabric adaptations: build_qsa_sel keeps the fabric sections argument, RPC major 119
(temp-12032 is at 118 with TOP_K_MASK already in the enum).

b12429 adaptation: b12429's build_qsa_sel has no fabric sections argument,
so only sel_rows is added to it. RPC goes to major 121 with GGML_OP_COUNT
131 instead of 119/134: qvac-b12429 was at 120 with 130 ops (upstream's
prune, ggml-org#29801, removed ADD1, DIAG_MASK_INF and DIAG_MASK_ZERO, and ROPE_FLUX
took 120), and TOP_K_ROWS goes in right after TOP_K_MASK, which moves every
later op id.

Assisted-by: Claude Opus 5.5
(cherry picked from commit ea7ee93)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CUDA Related to the CUDA backend ggml changes relating to the ggml tensor library for machine learning model Model specific testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: restrict qwen4exp QSA to a sequence's own cells with a unified KV cache

2 participants