[PR #575] [CLOSED] qsa select: split the top-k past the register fit - 9% faster long-prompt prefill #2699

Closed
opened 2026-11-04 02:02:28 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Niko1221/Strata/pull/575
Author: @orangeswim
Created: 10/3/2026
Status: ❌ Closed

Base: main ← Head: qsa-topk-split


📝 Commits (1)

  • c850d91 qsa select: past the register kernel's fit the top-k splits over two halves and merges - the same ids

📊 Changes

3 files changed (+199 additions, -10 deletions)

View changed files

📝 include/strata/kernels/qsa_select.hpp (+3 -1)
📝 src/kernels/cuda/qsa_select.cu (+190 -7)
📝 src/kernels/qsa_topk_active_parity.cpp (+6 -2)

📄 Description

The problem. block_topk_reg_kernel holds one query's keys in registers, which caps
it at TK_T × TK_PER = 33,792 blocks — contexts past ~135K cells, and the 204,800
context's 51,202 blocks. Past that, qsa_block_topk falls back to block_topk_kernel,
which re-reads the whole score row six times (four radix passes + count + emit) against
the register kernel's one read. qsa select was 19% of a cold 198K prefill on the 3090,
and every prompt pays it whatever its length: the dispatch keys off the context's
capacity, not the prompt. The sm_75 counted dispatch (0.1.38) bounds by the prompt's
active blocks; nothing covers prompts that genuinely exceed the registers.

The change. Past the fit but within two of them, the range splits at the fit: the
register kernel runs on each half (its block range is now a parameter; whole-range calls
are unchanged), and block_topk_merge_kernel rebuilds the whole-range selection from the
halves' candidates — same radix rule, one count per cell instead of per weighted block,
ties bounded by a single id, each selected cell placed at its rank by id. The rule is
unchanged, so the ids must be. block_excl_scan gains a per-call zero of the warp slots
a 512-thread CTA leaves unfilled (1024-thread callers unaffected); the scratch is one
stream-ordered allocation per call, shared by nothing and held past the merge by nothing.

Scope. Prompt path only — its caller passes an active block count, decode's captured
windows pass none. HIP keeps its own dispatch. STRATA_TOPK_OLD=1 still forces the
reference as the A/B arm. Geometries past a 2,051-wide selection or a 256-query batch
fall back.

Correctness. qsa_topk_active_parity (CMake target, add_test, exit 77 without a
GPU) drives both paths and diverges exactly at this branch: active-bound → counted →
split, no-bound → capacity → reference. 19 cases, bitwise identical, on both sides of
the fit — 135,164 sits at the fit, 135,168 and 135,171 split (the last puts the final
query's half B at one block, 3 cells, and leaves the earlier queries' half B empty) —
with NaN keys on every fifth block, where order_key maps NaN to 0 and a NaN tail loses
its +1e9 boost. End to end, the 26-needle suite's finds and misses are unchanged against
stock.

Measured (RTX 3090, Ryzen 7 5800X, 32 GB RAM, Coder IQ1_M pack, ctx 204,800, cold,
seeds fixed; this tree ± the commit):

stock + split
100K prompt 2,394 t/s 2,437 t/s +1.8%
198K prompt 2,190 t/s 2,368–2,388 (4 runs) +8–9%

Retrievals and decode (~101 t/s) identical on both builds. The gain scales with the
reference kernel's re-reads, and because the old fallback keyed on capacity, it covers
every prompt size on an affected context — including short prompts, which on sm_86 have
no counted dispatch and still take the split.

AI Tools

Claude + GLM-5.3, Strata qwen3.8-flash-next-coder-iq1_m


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Niko1221/Strata/pull/575 **Author:** [@orangeswim](https://github.com/orangeswim) **Created:** 10/3/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `qsa-topk-split` --- ### 📝 Commits (1) - [`c850d91`](https://github.com/Niko1221/Strata/commit/c850d91fda791cd58e3f9dacde6d16c0eb650698) qsa select: past the register kernel's fit the top-k splits over two halves and merges - the same ids ### 📊 Changes **3 files changed** (+199 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `include/strata/kernels/qsa_select.hpp` (+3 -1) 📝 `src/kernels/cuda/qsa_select.cu` (+190 -7) 📝 `src/kernels/qsa_topk_active_parity.cpp` (+6 -2) </details> ### 📄 Description **The problem.** `block_topk_reg_kernel` holds one query's keys in registers, which caps it at `TK_T × TK_PER` = 33,792 blocks — contexts past ~135K cells, and the 204,800 context's 51,202 blocks. Past that, `qsa_block_topk` falls back to `block_topk_kernel`, which re-reads the whole score row six times (four radix passes + count + emit) against the register kernel's one read. `qsa select` was 19% of a cold 198K prefill on the 3090, and every prompt pays it whatever its length: the dispatch keys off the context's capacity, not the prompt. The sm_75 counted dispatch (0.1.38) bounds by the prompt's active blocks; nothing covers prompts that genuinely exceed the registers. **The change.** Past the fit but within two of them, the range splits at the fit: the register kernel runs on each half (its block range is now a parameter; whole-range calls are unchanged), and `block_topk_merge_kernel` rebuilds the whole-range selection from the halves' candidates — same radix rule, one count per cell instead of per weighted block, ties bounded by a single id, each selected cell placed at its rank by id. The rule is unchanged, so the ids must be. `block_excl_scan` gains a per-call zero of the warp slots a 512-thread CTA leaves unfilled (1024-thread callers unaffected); the scratch is one stream-ordered allocation per call, shared by nothing and held past the merge by nothing. **Scope.** Prompt path only — its caller passes an active block count, decode's captured windows pass none. HIP keeps its own dispatch. `STRATA_TOPK_OLD=1` still forces the reference as the A/B arm. Geometries past a 2,051-wide selection or a 256-query batch fall back. **Correctness.** `qsa_topk_active_parity` (CMake target, `add_test`, exit 77 without a GPU) drives both paths and diverges exactly at this branch: active-bound → counted → split, no-bound → capacity → reference. 19 cases, bitwise identical, on both sides of the fit — 135,164 sits at the fit, 135,168 and 135,171 split (the last puts the final query's half B at one block, 3 cells, and leaves the earlier queries' half B empty) — with NaN keys on every fifth block, where order_key maps NaN to 0 and a NaN tail loses its +1e9 boost. End to end, the 26-needle suite's finds and misses are unchanged against stock. **Measured** (RTX 3090, Ryzen 7 5800X, 32 GB RAM, Coder IQ1_M pack, ctx 204,800, cold, seeds fixed; this tree ± the commit): | | stock | + split | | |---|---|---|---| | 100K prompt | 2,394 t/s | 2,437 t/s | +1.8% | | 198K prompt | 2,190 t/s | 2,368–2,388 (4 runs) | +8–9% | Retrievals and decode (~101 t/s) identical on both builds. The gain scales with the reference kernel's re-reads, and because the old fallback keyed on capacity, it covers every prompt size on an affected context — including short prompts, which on sm_86 have no counted dispatch and still take the split. ## AI Tools Claude + GLM-5.3, Strata qwen3.8-flash-next-coder-iq1_m --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-04 02:02:28 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#2699
No description provided.