Repository navigation
perf(draft): skip the KV tiles the DFlash drafter's ring attention masks out - #958
Merged
Merged
Conversation
…sks out The drafter's flash attention spans its whole ring (kv_total = 4128 slots), but until the ring wraps only the committed context and the 16 noise columns are unmasked: at a context of ~800 most of the 129 tiles of 32 slots are -inf for every query. ggml_flash_attn_ext_set_sparse_mask marks such an attention (op param 12). Every flash attention slot is taken, so the mark shares slot 12 with the DS4 inverse RoPE's attn_factor: the setter refuses a DS4 attention and the getter, which the launcher reads, never reports one as marked. For a marked call the CUDA/HIP launcher scans the mask once into a bitmap of live 32-slot granules per mask plane (flash_attn_mask_to_KV_live), and the tile kernel skips every KV tile whose granules are all dead. A skipped tile would have added exact zeros to KQ_sum and VKQ and left KQ_max where it was (fmaxf with -inf, expf(0) = 1), so outputs are bit-identical. The bitmap reaches every kernel through one more fattn_kernel_t parameter; only the tile kernel reads it, and unmarked calls never build it. The mark is a constant op param, so a captured HIP/CUDA graph is not invalidated from step to step. The drafter's ring attention always sets the mark. test_fattn_sparse_mask_gpu compares marked and unmarked runs byte for byte over the ring's layouts, and with NaN in the dead blocks proves the skip. It also checks that a DS4 attn_factor in slot 12 does not read as the mark. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Graffioh
force-pushed
the
perf/draft-fa-dead-tile-skip
branch
from
November 12, 2026 19:06
ff8068b to
6f739de
Compare
Graffioh
marked this pull request as ready for review
November 12, 2026 20:56
davide221
reviewed
Nov 12, 2026
davide221
left a comment
Contributor
There was a problem hiding this comment.
Code review
No issues found. Checked for bugs and logic errors in the changed code.
Reviewed 6f739dea · 0 candidate issues checked, kept at ≥70% confidence · add the code-review label to review again
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.
Summary
The DFlash drafter's flash attention reads its whole context ring on every draft step, but most of that ring is masked out. This PR skips the KV tiles that every query masks out. Outputs stay bit-identical. On current main, decode gets +0.94% faster at one request at a time and +0.8% to +2.4% faster at 2 to 8 concurrent requests.
ggml_flash_attn_ext_set_sparse_mask(attn, true)(op param 12) tells the backend that the mask leaves whole spans of the KV cache at -inf for every query. The drafter's ring attention sets it. Until the ring wraps, only the committed context and the noise columns are unmasked.attn_factor. The setter refuses a DS4 attention, and the getter (which the launcher reads) never reports one as marked. DS4 attentions also take their own kernel before the generic path.launch_fattnscans the mask once (flash_attn_mask_to_KV_live) into a bitmap of live 32-slot granules per mask plane.KQ_sumandVKQand leftKQ_maxwhere it was (fmaxf with -inf,expf(0) = 1), so the result is bit-identical.Evidence
All numbers below are from main
cbc6d7c27against this PR's head, on the R9700 (gfx1201).Throughput, one request at a time. Setup:
--profile qwen38-r9700, greedy, thinking on,max_tokens2048.Concurrency 1 to 8. Setup for C2 to C8: paged attention,
--max-concurrency 8,--kv-pool-tokens 204800, N clients in a closed loop, 40 prompts × 1024 tokens, thinking on, ABBA twice. Values are per-request tokens/s, geometric mean.Where the time goes. In a rocprofv3 trace of 5 requests × 512 tokens (on the PR's original base), the drafter's segment of a round is 8.63 ms. Capping the ring at 1056 slots (
--max-ctx 1024, which leaves only the tiles this PR keeps) shortens it to 7.76 ms, and the target segment does not move (44.74 vs 44.75 ms). Longer answers keep more tiles live, which is why the dev30 gain (2,048 tokens) is below that 0.89 ms.Correctness.
test_fattn_sparse_mask_gpupasses on the R9700 at this head. It runs the drafter's shapes (head 128, 32 query and 8 KV heads) through each attention twice in one graph, unmarked and marked:All 15 cases are byte-identical. A second pass puts NaN in K and V wherever a whole aligned 128-slot block is dead:
A last check gives a DS4 attention an
attn_factorof 1.0 in slot 12 and confirms that it does not read as marked. Without the getter's guard, it would.Merge Danger
Door: two-way, by revert. The new ggml function is additive, and the kernel parameter is internal to ggml-cuda. Op param 12 is shared with DS4's inverse RoPE, and both the setter and the getter guard against that.
Blast Radius: low.
build_draft_kv_steps). Every other flash attention, on every model, keeps its current path: the scan is skipped and the bitmap pointer is null.fattn_kernel_tkernel now takes one more parameter. Only the tile kernel reads it, so a mismatch there would show up as a build error, not a behaviour change.🤖 Generated with Claude Code