Skip to content

qwen4exp: score QSA blocks through the k-pool indexer - #326

Closed
amangupta-tether wants to merge 4 commits into
temp-11018from
qwen4exp/01-qsa-kpool
Closed

amangupta-tether wants to merge 4 commits into
temp-11018from
qwen4exp/01-qsa-kpool

Conversation

@amangupta-tether

@amangupta-tether amangupta-tether commented Oct 1, 2026 •

Copy link
Copy Markdown

Port of ggml-org/llama.cpp aman/qwen4-opt (f72ec5a, "llama: fix qwen4exp") onto temp-11018. First of two stacked PRs; the MTP port follows on top of this one.

Rebased from temp-10549 to temp-11018 on 2026-10-02.

What it does

QSA now goes through the GLM5-Next k-pool indexer instead of its own set_input_qsa:

  • a block is compress_ratio consecutive cells in sequence order, scored by one cached pooled key (mean of the raw indexer keys, normed and rotated to the M-RoPE position of its first member)
  • only blocks that a ubatch completes or regroups get re-pooled; older pooled keys are read from the indexer cache
  • the selection is the top blocks plus the incomplete tail, as in the reference

Differences from upstream

The fork's k-pool code is the per-ubatch layout from before upstream merged GLM5-Next (ggml-org#27773), not upstream's persistent kpool_layout. Rather than syncing GLM5-Next, the fix sits on top of the existing API:

  • new hparams indexer_kpool_row (qwen4exp rows are raw key | pooled key) and indexer_kpool_by_order
  • set_input_kpool takes new_pool_pos. In by-order mode a token is placed by the rank of its own cell, so M-RoPE image tokens that share a position get distinct ranks. Merged layouts for shared tokens are unchanged.
  • qwen4exp reads the pooled slots through views on the indexer storage, the same way glm5-next does. The ubatch-sized get_n_kpool/get_n_kpool_new signatures are kept.
  • test-llama-archs --qsa-unified-multiseq now checks that QSA runs with a unified multi-sequence cache and still matches dense attention, instead of asserting the old dense fallback.

Otherwise qwen4exp.cpp is identical to upstream's fixed version, apart from the fork's PLE input (no llama_prefetch_rows).

Follow-up commits from upstream

Testing

  • test-llama-archs: --qsa-unified-multiseq, --glm5-kpool-sequences, and -a qwen4exp|glm5-next on CPU (NMSE + save/load roundtrip) all pass.
    • The CUDA MoE rows abort with SOFT_MAX failed. Plain -a llama MoE aborts the same way on this branch, and the base did the same on temp-10549, so it isn't from this change.
  • Qwen3.8-Flash-Next UD-IQ1_S on GB10, wikitext-2 perplexity, this branch vs upstream aman/qwen4-opt:
ctx this PR upstream
8192 (QSA active, 3 chunks) 3.4056 ± 0.0625 3.3967 ± 0.0624
2048 (every block in budget, 6 chunks) 2.7404 ± 0.0687 2.7513 ± 0.0691

38.7 s per pass at 8k.

  • At 8k this branch gave 3.3965 before the kq_mask-shape follow-up and 3.4056 after it. Both numbers are deterministic.
  • That commit is numerically neutral: a llama-eval-callback run (one op at a time) produces identical sums for all 13,260 named tensors with and without it.
  • The shift appears only in normal execution, where the changed graph layout alters which kernels fuse and in what order. It shows on the CPU backend too, in both directions (4k: 1.8157 vs 1.8134).

Heads-up: #316 also edits llama-memory-hybrid-idx.{h,cpp}, in different functions. Whichever lands second will need a small rebase.

🤖 Generated with Claude Code

Port of ggml-org/llama.cpp aman/qwen4-opt (f72ec5a "llama: fix qwen4exp").

QSA now reuses the GLM5-Next k-pool machinery instead of its own
set_input_qsa: blocks are compress_ratio consecutive cells in sequence
order, each scored by one cached pooled key (mean of the raw indexer keys,
normed and rotated to the M-RoPE position of its first member). Only the
blocks a ubatch completes or regroups are re-pooled, and the selection is
the top blocks plus the incomplete tail, as in the reference.

Adapted to the fork's k-pool API (per-ubatch layout, ubatch-sized
get_n_kpool/get_n_kpool_new, merged layouts for shared tokens) rather than
upstream's persistent kpool_layout:
- indexer_kpool_row / indexer_kpool_by_order hparams; qwen4exp rows are
  raw key | pooled key
- set_input_kpool takes new_pool_pos, and in order mode places a token by
  the rank of its own cell, so M-RoPE image tokens sharing a position get
  distinct ranks
- qwen4exp reads the pooled slots through views on the indexer storage,
  as glm5-next does
- the unified multi-sequence QSA fallback to dense attention is dropped:
  pools are per sequence now; test-llama-archs --qsa-unified-multiseq
  now checks that QSA runs there and still matches dense attention

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@amangupta-tether
amangupta-tether changed the base branch from temp-10549 to temp-11018 October 2, 2026 02:53
am17an and others added 3 commits October 2, 2026 11:26
Follow-up from the merged ggml-org#29751: add the QSA selection
to the kq_mask in its own shape instead of a flattened 2D view.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Graph part of ggml-org#29819 (qwen4exp: fix tests).

Padded pools and missing tail cells share the n_kv sentinel, and top_k
can fill a short selection with invisible pools that overlap the tail,
so the selection scatter wrote some rows from several threads. Map every
dead slot to its own dump row n_kv + slot instead.

The hybrid-idx half of that change is not needed here: this k-pool code
pads the new pool list with at most one entry, so there are no shared
padding cells.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
)

Graph part of ggml-org#28569 (re-enable -sm tensor for
qwen4exp); the fork never disabled -sm tensor for qwen4exp, so the
llama-arch.cpp half does not apply.

With host-resident embeddings the PLE gather is a CPU node, and hc_init
was first reached through layer 0's PLE path after that gather, so the
REPEAT stayed on the CPU and the meta split later viewed a host-resident
node. Expanding hc_init right after it is built puts the REPEAT directly
before the first device node.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@gianni-cor
gianni-cor deleted the qwen4exp/01-qsa-kpool branch October 27, 2026 11:01
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.

3 participants