Repository navigation
CUDA: radix top-k of few rows in one launch per row - #519
Open
danielhanchen wants to merge 2 commits into
Open
danielhanchen wants to merge 2 commits into
danielhanchen wants to merge 2 commits into
Conversation
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.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Member
Author
|
@codex review |
1 similar comment
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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
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.
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 inscripts/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_Krows 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_blockruns 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 firstrankties in column order, the lowest columns first, astop_k_radix_gatherdoes. The output is the same index set astop_k_radix_cuda. The keys above the threshold now come out in column order instead of the order of an atomic counter.top_k_radix_cudawould 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.__match_any_sync). The kernel is compiled out underGGML_USE_HIPandGGML_USE_MUSA, so ROCm and MUSA behaviour is unchanged.GGML_CUDA_TOPK_ONEBLOCK_MAX_NCOLSsets the column limit (default 32768);0turns 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.test-backend-ops -o TOP_K -b CUDA0test-backend-ops -o ARGSORT -b CUDA0pin_contract: all intact;merge_checks: cleanGGML_USE_HIP; an R9700 hipcc build with TOP_K / ARGSORT tests, base vs pin, is queued on the DEGIMA podDecode tok/s,
llama-bench -p 0 -n 128 -r 3, three rotated rounds (median of rounds [min-max]):Not proven
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.