Repository navigation
perf(attention): batch the GQA query group in decode, one program per KV head - #205
Open
kunkunblueberry wants to merge 2 commits into
Open
kunkunblueberry wants to merge 2 commits into
kunkunblueberry wants to merge 2 commits into
Conversation
… KV head Decode ran one program per (row, query_head, split), so the HEAD_GROUPS query heads of one KV group each walked the same KV tile. ncu on the 48/8 geometry at 128 rows and 2048 context confirms the redundancy: both implementations move the same 1.11 GB of DRAM (vLLM 1.10 GB), but rlt executes 778.6M instructions against 115.5M at 23.1% versus 78.4% DRAM throughput. The work is repeated, not the memory. _paged_attention_group_kernel keeps _paged_prefill_kernel's structure verbatim - the per-row online-softmax state, the [BLOCK_H, BLOCK_T] score tile, two tl.dots per tile, the accumulator/correction rescale order and the FP8 dequantize-then-narrow path - and changes only what a row is: the query heads of one KV group instead of tokens. There is no causal mask, because a decode query sits at the end of its context. Two defects had to be fixed before it was correct, both caught by the new gate: - the program id named a block of query heads and derived kv_head = head // HEAD_GROUPS, which does not line up with KV groups when HEAD_GROUPS does not divide BLOCK_H; heads were scored against the wrong KV head and two programs could claim one output head. Deriving heads from the KV head (as vLLM's VALID_BLOCK_H does) makes it exact, and masking every store by in_group gives each output head one owner. - the partial store wrote accumulator/normalizer, but the merge weights parts by their normalizers, so the merge divided a second time (output scaled by 1/4.878 for a part whose normalizer was 4.878). It now stores the unnormalised numerator. Splitting the KV also stops being a constant: NUM_SPLITS = 16 was never measured. The split axis only helps while rows x head_axis underfills the GPU, so the count is now derived from the batch shape (peak throughput near 512 programs), with a per-kernel floor - the per-head kernel needs real splits even at rows=128, the grouped kernel prefers a single part there. It stays a power of two and depends only on shapes, so the CUDA-graph grid is static and no context length is read back. Measured at context 2048, page 16, device ms (nanbeige 48/8): 0.173 -> 0.080 at 8 rows (2.16x), 0.544 -> 0.172 at 32 rows (3.17x), 2.158 -> 0.653 at 128 rows (3.31x), i.e. 0.67x / 0.95x / 1.05x of vLLM's unified_attention instead of 1.45x / 3.02x / 3.47x. MHA does not regress (1.330 -> 1.269 at 128 rows). Attribution shows stage 1 alone is 0.98-0.99x of vLLM's stage 1; the residual is vLLM not needing a merge pass at all. tests/test_gqa_decode.py gates every shipped geometry (16/16, 32/8, 48/8, 16/4) at rows 1..128 and lengths 1..2048 against the FP32 torch reference, plus batch invariance, windowing and zero-length rows: 92 passed. Full suite 1447 passed, 0 failed.
kunkunblueberry
marked this pull request as ready for review
November 22, 2026 16:36
Signed-off-by: kunkunblueberry <kunkunblueberry@gmail.com>
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.
Required human contributor answers
Recorded from the contributor''s instruction: the goal of this work is to bring vllm-rlt''s
operator performance to vLLM parity, for the developers who build and run on this engine,
because these operators are the performance-critical components. Figures cited below are the
measurements in this PR.
1. What outcome are you trying to achieve?
Match vLLM''s operator performance in vllm-rlt. Decode attention was the one operator that
missed it, and it is on the critical path of every decode step for the GQA models this repo
ships (Nanbeige 48/8, 32/8), so the engine lost to vLLM on the operator level there.
2. What is the highest-leverage variable for that outcome?
Instructions executed per KV tile - how many times the same KV bytes are walked. Measured at
the 48/8 geometry, 128 rows, 2048 context: rlt executed 778.6M instructions against vLLM''s
115.5M at essentially identical DRAM traffic (1.11 GB vs 1.10 GB), 23.1% versus 78.4% DRAM
throughput. The variable was therefore redundant work, not bandwidth.
3. How will this work move that variable rather than create busywork?
One program now owns a whole KV head''s query group instead of one query head, so the tile is
loaded once and a tensor-core dot serves the group. Measured: 0.173 -> 0.079 ms at 8 rows
(2.19x), 0.544 -> 0.171 at 32 rows (3.18x), 2.158 -> 0.653 at 128 rows (3.30x), which moves
the operator from 1.45x / 3.02x / 3.47x behind vLLM to 0.66x / 0.96x / 1.05x. The same change
removed an unmeasured split constant that cost a further 1.20-1.6x depending on shape. It
does not add a kernel, a build step, a flag, or a numerical contract change.
4. Who is the customer or user, and what do they actually need?
Developers who deploy or benchmark looped-transformer models on vllm-rlt, and contributors who
need a trustworthy parity baseline to compare further work against. They need decode attention
to stop being the reason the engine loses to vLLM - without changing model code, numerics or
the shipped configuration.
PR necessity and bugfix evidence
A PR is needed: the gap is measured rather than hypothetical, it is on the decode critical
path, and the fix lives in this repository''s kernel. This is a performance change, not a
bugfix. The "observed failure" evidence is the ncu comparison and the operator A/B above,
reproducible with the commands in Test Plan; the numerical gate exists because an earlier
attempt at this kernel was 3.5x faster and numerically wrong.
Purpose
Summary. GQA decode ran one program per
(row, query_head, split), so theHEAD_GROUPSquery heads of a KV group each walked the same KV tile. One program per KV head fixes that by
reusing
_paged_prefill_kernel''s structure, and takes the operator from 1.45-3.47x behindvLLM to 0.66-1.05x, with MHA unchanged.
ncuat 48/8, 128 rows, 2048 context:unified_attention_paged_attention_group_kernelkeeps_paged_prefill_kernel''s structure verbatim - theper-row online-softmax state, the
[BLOCK_H, BLOCK_T]score tile, twotl.dots per tile,the accumulator/correction rescale order, the FP8 dequantize-then-narrow path - and changes
only what a row is: the query heads of one KV group instead of tokens. There is no causal
mask, because a decode query sits at the end of its context.
Two defects had to be fixed before it was correct, both caught by the new gate:
kv_head = head // HEAD_GROUPS, which does not line up with KV groups whenHEAD_GROUPSdoes not divide
BLOCK_H; heads were scored against the wrong KV head and two programscould claim one output head. Deriving
headsfrom the KV head (as vLLM''sVALID_BLOCK_Hdoes) makes it exact, and masking every store by
in_groupgives each output head exactlyone owner.
accumulator / normalizer, but the mergekernel weights parts by their normalizers, so the merge divided a second time (measured as
an output scaled by
1/4.878for a part whose normalizer was4.878). It now stores theunnormalised numerator, as the per-head kernel already did.
Also in this PR.
NUM_SPLITS = 16was a constant nothing had measured. The split axisonly helps while
rows x head_axisunderfills the GPU, so the count is now derived from thebatch shape (peak throughput near 512 programs), with a per-kernel floor: the per-head kernel
needs real splits even at 128 rows (1 vs 4 splits measured 1.37 vs 1.27 ms), the grouped
kernel prefers a single part there (4 splits cost 1.11x). It stays a power of two and depends
only on tensor shapes, so the CUDA-graph grid is static and no context length is read back to
the host. Sweeping 1/2/4/8/16 at 108 SMs: 16 was never optimal, and it cost 1.20x at 128 rows
and 1.6x at length 128.
Test Plan
vllm-rlt Version / Commit:
cb1cce263ace0644c2c574b069b6de808d29010c, branchpr/gqa-decode-grouped, based onmainatce200f0.tests/test_gqa_decode.pyis new and gates every shipped geometry (16/16 MHA, 32/8, 48/8,16/4) at rows 1/3/8/32/128 and lengths 1/33/512/2048 against the repo''s FP32
torch_paged_attentionoracle, plus batch invariance, windowing and zero-length rows.GPU validation on 8x A100-SXM4-80GB (
lyx), page 16, head_dim 128:pytest tests/test_gqa_decode.py -q -p no:randomly # 87 passedTest Result
Operator-level, device ms per call, context 2048, page 16.
beforeismain;vLLMisvllm.v1.attention.ops.triton_unified_attention.unified_attentionon identical inputs.MHA takes the unchanged per-head path, so it is a regression check rather than a target: it
stays within noise and stays ahead of vLLM.
Where the residual 1.05x at 128 rows comes from - timed per phase, not inferred:
The attention stage is already at parity; vLLM''s
unified_attentionsimply needs no mergepass, which is a design difference, not a defect in this kernel.
Known limitation, stated rather than hidden. At context <= 512 this operator is still
1.40-1.52x behind vLLM''s single-pass kernel, because splitting the KV is what pays for long
context and what costs short context (two launches where vLLM needs one). A measured
single-pass path closes it (0.0323 ms vs 0.0556 at 512/8, i.e. 1.7x faster than vLLM) but must
choose per row on the device from
context_lengths: the CUDA-graph path fills that tensorinto a preallocated buffer, so a host-side branch would either sync or be wrong under capture.
That is a follow-up, with the design and the two rejected shortcuts recorded in
docs/experiments/V007-short-context-gqa-decode.md.Related work
vllm_rlt/kernels/triton_attention.py. Sparse decode attention: per-token reference lifecycle for async self-speculation #157''s only change to thatfile is
table_stridelosingtl.constexpr, which is disjoint from this diff; this PR doesnot reorder or re-parameterise that kernel.
this kernel, and are complementary.
Full experiment record:
docs/experiments/V006-gqa-decode-grouped-reuse.md.