Repository navigation
Conversation
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
requested review from
a team,
JohannesGaessler and
ggerganov
as code owners
November 14, 2026 06:52
Contributor
Author
|
@sanmai please try this too. |
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.
Contributor
|
With a 100k idle slot no slowdown for the second slot. On Still, there's some non-repeatability: same request in the same slot 1 come out differently with an idle slot 0. |
Contributor
Author
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.
Contributor
|
Contributor
|
Re-testing a proposed fix, #32297, to make sure it works as intended and it does. The reproducer: Without any changes it reports: With either fix, #32297 or #32299: |
Contributor
Author
|
I'll probably break this down into smaller commits for faster review |
This was referenced 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)
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.
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:
set_input_kpoolnow lays out only the pools of the sequences in the ubatch. It also drops astd::sortover the reps of all pools that ran on most decode steps; a marker array picks the same pad cells.[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 toggml_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.GGML_OP_TOP_K_ROWS(ggml_top_k_rows): the cells thatggml_top_k_maskkeeps, as a list per row (I32) or the matching mask. CPU and CUDA, meta backend split rule, RPC op count and patch version bumped.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.GGML_OP_TOP_K_ROWS. Metal supportsggml_flash_attn_ext_rowsbut not the new op, so the probe passed and the selection would have run on the CPU in every QSA layer. TheTOP_K_ROWSoutputs now go through the sameLLM_FUSED_OP_FLASH_ATTN_KV_ROWSprobe, so such a backend stays on the mask path.ggml_is_quantizedtypes, 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).ggml_top_k_rowstakesn_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.ggml_gallocrre-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=1switches 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:
RTX PRO 6000, IQ4_NL:
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):
Commit 5 vs commits 1-4, RTX PRO 6000, same run, ms:
-ub 4096), idle 100kThe 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 ton_kv.Correctness:
test-backend-ops:FLASH_ATTN_EXT4412/4412 andTOP_K_ROWS57/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_ROWS57/57 on ROCm (gfx1151).ggml_top_k_maskkeeps (11 configurations x 3 seeds, random and QSA-shaped, with partial groups).test-llama-archspasses for qwen4exp and glm5-next (its small context stays on the mask path).-ub 512to-ub 256on the mask path (1.1e-2 and 9.0e-3). Commit 1 alone is bit-exact vs master with FA off.-ubnoise floor.-sm tensor(one GPU) runs on the new path.Not covered:
TOP_K_ROWSkernel. The fallback was checked by rejecting the op on CUDA, not on a Mac.-no-kvu) is unchanged.Requirements