Repository navigation
CUDA: sparse flash attention lists from the TOP_K_MASK picks - #520
danielhanchen wants to merge 2 commits into
Conversation
When a TOP_K_MASK is the whole mask of a FLASH_ATTN_EXT, decode and verify sized (16 rows or fewer, one stream), the mask node is not run at its position. A one-query sparse gather builds its index lists from the picks, pool cells and extra cells in one launch (a bitmap keeps them ascending and duplicate free, the same lists the dense mask compaction gives) and writes only the listed mask cells. Any other kernel gets the dense mask at the attention node. The picks are kept allocated until the attention node. Output is bit-identical. CUDA only; GGML_CUDA_QSA_TOPK_SPARSE=0 turns it off. tests: QSA_TOPK_FA (gather and dense routes, 1 / 3 / 8 / 16 rows, 4k / 16k / 64k cells, tail only, pools under the tail, masked cells)
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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2895a420ae
ℹ️ 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".
| if (t->type != GGML_TYPE_F16 || mask->type != GGML_TYPE_F16 || t->ne[1] > QSA_TOPK_SPARSE_MAX_ROWS || t->ne[2] != 1 || | ||
| t->ne[3] != 1 || !ggml_is_contiguous(t) || size_t((n_kv + 31)/32)*sizeof(uint32_t) > QSA_TOPK_SPARSE_MAX_BITS_SMEM) { |
There was a problem hiding this comment.
Include static shared memory in the sparse-kernel limit
For a one-query sparse gather with n_kv = 393216, this guard accepts a 48 KiB bitmap, but qsa_topk_sparse_lists also declares 1,028 bytes of static shared memory for thread_offsets and row_count. The combined allocation exceeds CUDA's default 48 KiB per-block limit. Neither this launch nor ggml_cuda_kernel_launch opts the kernel into a larger shared-memory allowance, so an otherwise valid long-context attention operation fails at launch instead of using the previous dense-mask path. Subtract the static allocation from the limit, or explicitly opt in after checking the device's supported limit.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in ca67625: the bitmap limit now leaves room for the kernel's 1040 bytes of static shared memory (ptxas -v), so a near-cap n_kv falls back to the dense path.
| const ggml_tensor * t = cgraph->nodes[i]; | ||
| int fa = -1; |
There was a problem hiding this comment.
Preserve mask tensors exposed as graph outputs
When the TOP_K_MASK tensor or one of its views is marked GGML_TENSOR_FLAG_OUTPUT, having attention as its only graph reader does not make it safe to leave the mask partially initialized. This scan ignores output flags and skips views unconditionally, so it permits the sparse-gather path, which writes only selected finite cells and leaves all other mask cells containing stale allocation contents rather than -INFINITY. Reading the exported mask after graph execution therefore returns incorrect results. Reject plans with an exported mask or exported view, as the existing fusion checks do for intermediate outputs.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in ca67625: a TOP_K_MASK node or any view of it flagged as a graph output is no longer planned.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Bravo. 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". |
CUDA: sparse flash attention lists from the TOP_K_MASK picks
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
Qwen4-35B-A3B (
qwen4exp) has QSA sparse-attention layers. Each decode step builds a dense[n_kv, n_tokens]mask withGGML_OP_TOP_K_MASK: it fills the row with-inf, then copies the mask value of every cell of the picked pools plus the tail cells. The sparse flash attention kernel then scans that dense row again (flash_attn_mask_to_sparse_indices) to compact its finite cells back into the index list it gathers. Both passes walk the whole cache, so their cost grows with context while the attention itself does not.On an RTX 5090, per decoded token (10 QSA layers, nsys):
top_k_mask_kernelflash_attn_mask_to_sparse_indicesThe fix
qsa-topk-sparse.cu(new): when aTOP_K_MASKnode is the whole mask of aFLASH_ATTN_EXTand its only reader, the batch is decode / verify sized (16 rows or fewer, one stream) and no node in between writes over the picks or the mask memory, theTOP_K_MASKnode is not run at its position.ncols1 == 1) gets its index lists from one launch that reads the picks directly: a bitmap over the cells collects the cells of the live picks and the extra cells whose mask value is finite, then each thread lists a contiguous run of words. The lists are ascending and duplicate free, the same lists the dense compaction gives, and only the listed mask cells are written, which are the only ones the gather reads.fusion.cu: the plan is made where the other fusions are tried, and the picks are kept allocated until the attention node with the existingadd_alloc_depmechanism.top-k-mask.cu,fattn.cu,fattn-common.cuh: one hook each (skip, list build, dense fallback).GGML_CUDA_QSA_TOPK_SPARSE=0restores the base path. HIP and MUSA never plan it.tests/test-backend-ops.cpp:QSA_TOPK_FAcases, aTOP_K_MASKfeedingFLASH_ATTN_EXT(D 256, GQA 12, pools of 16 cells, 128 picks, 15 tail cells): 1 / 3 / 8 / 16 rows at 4k / 16k / 64k cells, plus tail-only rows, picks that include the pools under the tail, and masks with-infcells. The 1 and 3 row cases at 16k and 64k take the gather route, the others the dense fallback.Evidence
Test trees: the pin-set tree at b13024 (the
scripts/unsloth/pr-set.jsonpins of the next release plus the open round-2 candidates #508, #510, #511, #512, #514, #515, #516 and #517), without (A) and with (SP) this pin, and SP withGGML_CUDA_QSA_TOPK_SPARSE=0(SPK). Device: one RTX 5090 (sm_120a, CUDA 13.0, driver 610.57), Qwen4-35B-A3B UD-Q4_K_XL fully on the GPU, FA on.test-backend-ops -b CUDA0 -o QSA_TOPK_FAtest-backend-ops -b CUDA0 -o TOP_K_MASKtest-backend-ops -b CUDA0 -o FLASH_ATTN_EXTqsa_topk_sparse_lists10 launches,top_k_mask_kernelandflash_attn_mask_to_sparse_indices0. SPK: the two base kernels, 10 launches each, as in Allama_pinset.py sim), release pr-set + #508 #510 #511 #512 #514 #515 #516 #517 + pin-topk-oneblock + this pinpin_contractall intact,merge_checkscleanggml-hip+test-backend-ops)Kernel time per decoded token (nsys):
top_k_mask_kernel+flash_attn_mask_to_sparse_indicesqsa_topk_sparse_listsllama-benchtg128, tok/s, median of 3 rotated rounds (each round-r 3). The A/A spread is the range over the A and A2 runs; the first A run at 64k (184.3, a cold start) is left out:Every SP run is above every A, A2 and SPK run at 16k and 64k. The kill switch matches the base.
llama-server decode tok/s, 256 tokens, raw completion:
Not proven