perf(draft): skip the KV tiles the DFlash drafter's ring attention masks out
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.
DFlash2 drafter, one draft step, each of its 5 attention layers (Qwen3.8-27B, ring cap 4096 + 16 noise columns)
before after
KV tiles read all 129 tiles of 32 slots the tiles any query can see: the committed
(the whole kv_total = 4128 ring) context and the noise columns (27 of 129 at ctx ~800)
extra work none one mask scan into a live-tile bitmap per call
- The mark.
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. - The slot. All 16 flash attention op param slots are 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. DS4 attentions also take their own kernel before the generic path. - The scan. For a marked call,
launch_fattnscans the mask once (flash_attn_mask_to_KV_live) into a bitmap of live 32-slot granules per mask plane. - The skip. The tile kernel skips every KV tile whose granules are all dead. Such a tile would have added exact zeros to
KQ_sumandVKQand leftKQ_maxwhere it was (fmaxf with -inf,expf(0) = 1), so the result is bit-identical. - Everything else. The other FA kernels take the bitmap pointer and ignore it, and unmarked calls never build it. The mark is a constant op param, so a captured HIP/CUDA graph stays valid from step to step.
server/deps/llama.cpp/ggml/
include/ggml.h + ggml_flash_attn_ext_set_sparse_mask / _get_sparse_mask
src/ggml.c + the setter and getter (op param 12; refused on, and never
read from, a DS4 attention)
src/ggml-cuda/fattn-common.cuh + flash_attn_mask_to_KV_live, fattn_kv_live_any;
launch_fattn builds the bitmap for marked calls
src/ggml-cuda/fattn-tile.cuh ~ the KV loop skips tiles with no live granule
src/ggml-cuda/fattn-vec.cuh,
fattn-mma-f16.cuh, fattn-wmma-f16.cu ~ one more kernel parameter, unused
server/src/draft/draft_graph.cpp ~ the ring attention sets the mark
server/test/unit/test_fattn_sparse_mask_gpu.cpp + marked vs unmarked, byte for byte; the DS4 slot check
Evidence
All numbers below are from main cbc6d7c27 against this PR's head, on the R9700 (gfx1201).
Throughput, one request at a time. Setup:
- Qwen3.8-27B UD-IQ4_XS target, DFlash2 Q8_0 drafter,
--profile qwen38-r9700, greedy, thinking on,max_tokens2048. - 30 prompts, 6 each from gsm8k, math500, humaneval, mbpp and mt-bench.
- Interleaved ABBA twice: 8 server runs, each with a warm-up request.
- Values are tokens/s per request, geometric mean per suite.
| suite | main | this PR |
|---|---|---|
| gsm8k | 114.8 | 115.9 (+1.0%) |
| math500 | 120.5 | 121.7 (+1.0%) |
| humaneval | 92.6 | 93.6 (+1.0%) |
| mbpp | 107.9 | 108.8 (+0.9%) |
| mt-bench | 84.9 | 85.5 (+0.8%) |
| all | 103.24 | 104.21 (+0.94%) |
- Noise: the four main runs span 103.18 to 103.28, and the four PR runs span 103.96 to 104.49. Both blocks gain (+0.74% and +1.13%).
- Identity: every run produced 26,327 tokens. In a separate run with the AR burst pinned off, all 30 outputs equal main's (content, reasoning, token count and finish reason).
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.
| C | main | this PR | gain | ABBA blocks | errors |
|---|---|---|---|---|---|
| 1 | 103.24 | 104.21 | +0.94% | +0.74% / +1.13% | 0 |
| 2 | 84.91 | 85.67 | +0.89% | +0.75% / +1.04% | 0 |
| 3 | 71.22 | 71.89 | +0.94% | +0.97% / +0.92% | 0 |
| 4 | 65.39 | 66.49 | +1.68% | +2.10% / +1.27% | 0 |
| 5 | 57.12 | 57.73 | +1.08% | +1.26% / +0.90% | 0 |
| 6 | 54.97 | 55.40 | +0.79% | +0.45% / +1.13% | 0 |
| 7 | 46.37 | 47.49 | +2.42% | +2.44% / +2.40% | 0 |
| 8 | 44.81 | 45.86 | +2.34% | +2.15% / +2.54% | 0 |
- All 16 blocks gain. At C1 to C4, C7 and C8, every PR run is faster than every main run.
- Outputs at C > 1 are not compared byte for byte: the engine is not batch-invariant, so even two runs of main differ.
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_gpu passes 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:
- ring of 4128 slots at contexts 1, 31, 32, 33, 817 and 4095;
- a wrapped ring, and a ring holding only noise;
- sliding windows with causal noise;
- scattered live spans, and a per-row causal tail;
- a 1056-slot ring;
- 1, 16 and 64 queries.
All 15 cases are byte-identical. A second pass puts NaN in K and V wherever a whole aligned 128-slot block is dead:
- the unmarked run reads that NaN and turns non-finite;
- the marked run stays equal to its finite run, which proves it skipped those tiles.
A last check gives a DS4 attention an attn_factor of 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.
- Only attentions that set the mark change, and today that is only the DFlash drafter's ring attention (
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. - Every
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. - Past the ring's wrap (contexts over 4096) every tile is live. There is no gain then, and one small scan launch per call remains.
- The CPU backend ignores the mark.
🤖 Generated with Claude Code