Skip to content

ggml : top-k rows in pick order - #32297

Draft
sanmai wants to merge 6 commits into
ggml-org:aman/qsa-sel-rowsfrom
sanmai:pr32247/pick-order
Draft

sanmai wants to merge 6 commits into
ggml-org:aman/qsa-sel-rowsfrom
sanmai:pr32247/pick-order

Conversation

@sanmai

@sanmai sanmai commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Against #32247

Overview

Updates both kernels to list the picks in index order (smallest pool index first) so that attention always sums it in the same order.

Additional information

Reproducer for the ungrouped path:

#!/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 

Reports here:

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

Without the fix:

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

The grouped path (n_group > 1) already lists picks in index order.

Graph-level alternative: #32299

Requirements

const int32_t p = top_k_row[t*sk0];

// the picks in index order: the order of top_k is arbitrary, the attention over the list depends on it
slot = top_k_rows_rank(top_k_row, sk0, n_top, t)*n_blk + e % n_blk;

@sanmai sanmai Nov 15, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can add a dedicated kernel to sort this separately (78e1c9a), but that adds complexity for no good reason. At least for the current inputs.

@github-actions github-actions Bot added ggml changes relating to the ggml tensor library for machine learning CUDA Related to the CUDA backend labels Nov 15, 2026
@mercant1982

Copy link
Copy Markdown

Checked the ungrouped index-order behavior with a small native CPU harness at 7d8eaa6f8ad5, using 54440826296a as the parent control. The later 34fb50da7330 head retains the tested CPU implementation; subsequent edits touch CUDA and operation documentation only.

With the same explicit expected row lists and masks in both arms:

  • PR: 0/72 cases differ from the index-order expectation.
  • Parent: 56/72 differ, confirming that the check detects the intended ordering change.

The cases cover all six permutations of three picks, two rows, I32/F16/F32 outputs, live/dead scores, two-cell block maps, implicit cells, negative picks, out-of-range cells, extras and padding. They execute the real ggml_top_k_rows graph with two CPU threads. Both arms use the same executable and base library; only the CPU ops.cpp is rebuilt for the parent control.

This checks n_group=1 operation behavior only. It does not validate CUDA, grouped lists, attention outputs, model perplexity or performance.

Harness, literal expectations, source/library hashes and logs: https://gist.github.com/mercant1982/6a9598800dd4622fe7bbad0165849fe7

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 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
…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 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

CUDA Related to the CUDA backend ggml changes relating to the ggml tensor library for machine learning

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants