Skip to content

CUDA: radix top-k of few rows in one launch per row - #519

Open
danielhanchen wants to merge 2 commits into
base/upstream-9a811bab3from
pin-topk-oneblock
Open

danielhanchen wants to merge 2 commits into
base/upstream-9a811bab3from
pin-topk-oneblock

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

CUDA: radix top-k of few rows in one launch per row

Summary

The unslothai/llama.cpp prebuilt is an aged upstream b#### tag plus the pins listed in scripts/unsloth/pr-set.json, merged in order. This pin is one commit on top of upstream b12966 (9a811bab3c4), so it needs no other pin. Once upstream has an equivalent change, the pin is dropped.

The problem

GGML_OP_TOP_K rows that take the radix path (top_k_radix_cuda: more than 4096 columns for one row, or several rows) run as 10 launches per call: an init, four histogram + select passes, and a gather. Each pass spreads one row over up to 64 blocks, and the select kernel runs single-threaded per row. For decode this is latency bound. Qwen4-35B-A3B (qwen4exp) runs one such top-k per QSA layer and token (the lightning-indexer pool selection). On an RTX 5090 at a 16K context, that is 191 us per decoded token (40 select + 40 histogram + 10 gather + 10 init launches), about 4% of the decode time.

The fix

  • top-k.cu: top_k_radix_one_block runs the same radix select for one row in one 1024-thread block. Each thread reads 8 contiguous columns per pass (float4 loads when aligned). The block builds the same per-pass histograms, picks the same bin (the highest bin whose count of keys at or above it reaches the rank), keeps the same threshold, and gathers the keys above the threshold and then the first rank ties in column order, the lowest columns first, as top_k_radix_gather does. The output is the same index set as top_k_radix_cuda. The keys above the threshold now come out in column order instead of the order of an atomic counter.
  • It is used only where top_k_radix_cuda would run, for rows of at most 32768 columns and no more rows than SMs (one wave). Longer rows and more rows keep the multi-block path, which is as fast or faster there.
  • NVIDIA only (cc 7.0 and newer, for __match_any_sync). The kernel is compiled out under GGML_USE_HIP and GGML_USE_MUSA, so ROCm and MUSA behaviour is unchanged.
  • GGML_CUDA_TOPK_ONEBLOCK_MAX_NCOLS sets the column limit (default 32768); 0 turns the new path off.
  • tests/test-backend-ops.cpp: TOP_K cases at 8192 / 16387 / 32768 / 32771 columns with k up to 2048, one and three rows, with and without ties.

Evidence

RTX 5090 (Vast, driver 610.57, CUDA 13.0, sm_120a). Test tree: the pin-set tree at b13024 (the round-3 pr-set plus the open candidate pins #508, #510, #511, #512, #514, #515, #516, #517), with and without this pin. Arms: A = pin-set tree, B = with this pin, B16 = B with the limit at 16384 columns, K = B with GGML_CUDA_TOPK_ONEBLOCK_MAX_NCOLS=0, A2 = A again. Model: Qwen4-35B-A3B UD-Q4_K_XL, all layers on the GPU, FA on.

check result
test-backend-ops -o TOP_K -b CUDA0 base 550/550; head 576/576 (26 new cases); kill 576/576; with the limit at 1M columns 576/576
test-backend-ops -o ARGSORT -b CUDA0 101/101 on base, head and kill
greedy decode md5, llama-server, 256 tokens after 0 / 16K / 64K tokens of context, no MTP and MTP 2 identical across A, B, K and A2 at every depth
greedy md5, 64 tokens, Qwen4-35B-A3B, Qwen3.8-27B, Qwen3.8-Flash-Next (short and 10K-token prompt), Inkling-2-Mini, Qwen3-30B-A3B, Qwen3-0.6B, gemma-3-1b, gemma-4-E2B, granite-4.0-h-tiny identical across A, B and K on every model (10 cells)
pin-set sim at b13024 with the round-3 pr-set, the candidates above and this pin all 76 pins merge; pin_contract: all intact; merge_checks: clean
HIP the new code is compiled out under GGML_USE_HIP; an R9700 hipcc build with TOP_K / ARGSORT tests, base vs pin, is queued on the DEGIMA pod

Decode tok/s, llama-bench -p 0 -n 128 -r 3, three rotated rounds (median of rounds [min-max]):

arm d0 d16384 d65536
A (pin-set) 216.8 205.6 [205.6-206.0] 194.5 [194.5-194.5]
A2 (pin-set) 216.7 205.6 [205.4-205.6] 194.5 [194.5-194.6]
K (pin, off) 216.9 205.7 [205.6-205.7] 194.5 [194.3-194.7]
B (pin) 216.6 210.4 [210.1-210.8] 195.0 [194.9-195.1]
B16 (limit 16384) 216.8 210.6 [209.3-210.6] 194.4 [194.3-194.7]
  • At 16K the pin is +2.3% (the A/A spread is 0.2%). At 64K the row has more columns and the one-block kernel is about as fast as the multi-block path: +0.2%, and never slower. d0 does not reach the radix path.
  • llama-server, no MTP: 206.0 vs 201.1 tok/s at 16K (+2.4%), 191.2 vs 190.8 at 64K. MTP 2 (draft-mtp, n_max 2, two rounds): 309.0 vs 303.8 tok/s at 16K (+1.7%, medians of B against A, A2 and K), 291.8 vs 289.4 at 64K (+0.8%).
  • The first A run in the first round was a cold outlier (168 tok/s at 64K) and is excluded.

Not proven

  • Speed was measured only on an RTX 5090 (sm_120). Ampere, Ada, Hopper and datacenter Blackwell were not timed; the column limit of 32768 comes from this card and from earlier T4 kernel timings of the same kernel.
  • HIP and MUSA keep the old path by construction; the R9700 compile and test run is pending.
  • Toolchains whose CCCL ships cub::DeviceTopK (3.4.3 and newer) send one or two rows to DeviceTopK before this path is considered; there the pin only changes multi-row calls.

A row of at most 32768 columns, with no more rows than SMs, runs the radix
select as one 1024-thread block per row instead of the init, 4 x
(histogram, select) and gather launches. Each thread reads 8 contiguous
columns per pass. The histograms, the threshold and the kept ties (lowest
columns first) are those of top_k_radix_cuda, so the index set is the same.
NVIDIA only. GGML_CUDA_TOPK_ONEBLOCK_MAX_NCOLS sets the column limit, 0
turns it off.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 6, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-06T14:09:35.942950Z ff41281 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen danielhanchen self-assigned this Dec 6, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: ff4128171d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant