Skip to content

perf(attention): batch the GQA query group in decode, one program per KV head - #205

Open
kunkunblueberry wants to merge 2 commits into
InferMatrixLab:mainfrom
kunkunblueberry:pr/gqa-decode-grouped
Open

kunkunblueberry wants to merge 2 commits into
InferMatrixLab:mainfrom
kunkunblueberry:pr/gqa-decode-grouped

Conversation

@kunkunblueberry

@kunkunblueberry kunkunblueberry commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

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 the HEAD_GROUPS
query 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 behind
vLLM to 0.66-1.05x, with MHA unchanged.

ncu at 48/8, 128 rows, 2048 context:

metric before vLLM unified_attention
instructions executed 778.6 M 115.5 M
DRAM bytes 1.11 GB 1.10 GB
DRAM throughput 23.1 % 78.4 %

_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, 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 mapping. 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 exactly
    one owner.
  • Double normalisation. The partial store wrote accumulator / normalizer, but the merge
    kernel weights parts by their normalizers, so the merge divided a second time (measured as
    an output scaled by 1/4.878 for a part whose normalizer was 4.878). It now stores the
    unnormalised numerator, as the per-head kernel already did.

Also in this PR. NUM_SPLITS = 16 was a constant nothing had 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 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, branch
pr/gqa-decode-grouped, based on main at ce200f0.

tests/test_gqa_decode.py is 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_attention oracle, plus batch invariance, windowing and zero-length rows.

python -m ruff check vllm_rlt tests           # All checks passed
python -m ruff format --check vllm_rlt tests  # 148 files already formatted
OMP_NUM_THREADS=1 python -m pytest -q -k 'gqa or ouro or attention'  # 177 passed, 67 skipped
OMP_NUM_THREADS=1 python -m pytest -q                                # 1305 passed, 913 skipped

GPU validation on 8x A100-SXM4-80GB (lyx), page 16, head_dim 128:

pytest tests/test_gqa_decode.py -q -p no:randomly   # 87 passed

Test Result

Operator-level, device ms per call, context 2048, page 16. before is main; vLLM is
vllm.v1.attention.ops.triton_unified_attention.unified_attention on identical inputs.

geometry rows before after gain vLLM after / vLLM
Nanbeige 48q/8kv 8 0.173 0.079 2.19x 0.119 0.66x
Nanbeige 48q/8kv 32 0.544 0.171 3.18x 0.178 0.96x
Nanbeige 48q/8kv 128 2.158 0.653 3.30x 0.621 1.05x
GQA 32q/8kv 8 - 0.079 - 0.109 0.73x
GQA 32q/8kv 32 - 0.170 - 0.172 0.99x
GQA 32q/8kv 128 1.463 0.649 2.25x 0.640 1.01x
Ouro MHA 16q/16kv 8 0.101 0.105 0.96x 0.121 0.87x
Ouro MHA 16q/16kv 32 0.341 0.358 0.95x 0.369 0.97x
Ouro MHA 16q/16kv 128 1.330 1.270 1.05x 1.231 1.03x

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:

rows vLLM stage 1 rlt stage 1 stage-1 ratio merge (rlt vs vLLM)
8 0.0567 0.0554 0.98x 0.0153 vs 0.0767
32 0.1824 0.1804 0.99x 0.0155 vs 0.0807
128 0.7184 0.7088 0.99x 0.0379 vs 0.1227

The attention stage is already at parity; vLLM''s unified_attention simply needs no merge
pass, 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 tensor
into 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

Full experiment record: docs/experiments/V006-gqa-decode-grouped-reuse.md.

… 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
kunkunblueberry marked this pull request as ready for review November 22, 2026 16:36
Signed-off-by: kunkunblueberry <kunkunblueberry@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant