Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
211 changes: 211 additions & 0 deletions docs/experiments/V006-gqa-decode-grouped-reuse.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
# GQA decode: reusing the in-repo paged-prefill structure

Version V006. Branch `perf/fuse-norm-rope`. Host `lyx`, 8x A100-SXM4-80GB (108 SMs, 40 MB
L2), torch `2.14.0+cu130`, triton `3.8.0`, vLLM `0.33.0` wheel, page size 16, head_dim 128.

This entry closes the one operator gap V005 identified: GQA decode was 1.45x / 3.02x / 3.47x
slower than vLLM at 8 / 32 / 128 packed rows. The fix was not a new kernel. It reuses
`_paged_prefill_kernel`, which already solves the same problem shape, and drops a split-count
constant that had never been measured.

## What was measured before changing anything

V005 recorded the gap but not its cause. Profiling it first (ncu, 48/8 geometry, 128 rows,
2048 context, identical 1.11 GB DRAM traffic on both sides) gave:

| metric | rlt (per-head kernel) | vLLM (grouped) |
| --- | --- | --- |
| instructions executed | 778.6 M | 115.5 M |
| DRAM bytes | 1.11 GB | 1.10 GB |
| DRAM throughput | 23.1 % | 78.4 % |

So the kernels moved the same bytes; rlt executed 6.7x the instructions. The launch shape
explains it exactly: rlt ran one program per `(row, query_head, split)`, so the 6 query heads
of a KV group each walked the same KV tile. The work is redundant, not the memory.

## Reference implementation and what was adapted

Two in-repo pieces were referenced, and no kernel was written from scratch.

### 1. `_paged_prefill_kernel` (same file, `vllm_rlt/kernels/triton_attention.py`)

It already does "many query rows share one KV tile": a per-row online-softmax state
(`maximum`/`normalizer`/`accumulator`), a `[BLOCK_M, BLOCK_N]` score tile, and two `tl.dot`s
per KV tile. `_paged_attention_group_kernel` keeps that structure verbatim, including the
accumulator/correction rescale order and the FP8 dequantize-then-narrow path, and changes
only what a "row" is:

| | `_paged_prefill_kernel` | `_paged_attention_group_kernel` |
| --- | --- | --- |
| query rows | `BLOCK_M` tokens of one sequence | the query heads of one KV group |
| program id 1 | query head | **KV head** |
| mask | causal + window | none (decode's query position is the context end) |
| output | stored directly | split partials, merged by the existing merge kernel |

Two defects had to be fixed before it was correct, both caught by
`tests/test_gqa_decode.py` rather than by a benchmark:

- **KV-head mapping.** The first version took the program id as a *block of query heads* and
derived `kv_head = first_head // HEAD_GROUPS`. With `HEAD_GROUPS=6` and a padded
`BLOCK_H`, one block spans heads 0..15 while `kv_head 0` only covers heads 0..5, so heads
6..15 were scored against the wrong KV head, and two programs could claim the same output
head. vLLM avoids this by deriving the heads from the KV head
(`cur_head = cur_head_id * VALID_BLOCK_H + arange`); the kernel now does the same
(`heads = kv_head * HEAD_GROUPS + lane`) and masks every store by `in_group`, which makes
each output head have exactly one owner.
- **Double normalisation.** The prefill kernel's `accumulator` is unnormalised because the
merge kernel weights parts by their normalizers; the first version stored
`accumulator / normalizer`, so the merge divided by the normalizer a second time. Measured
as an output scaled by `1/4.878` for a part whose normalizer was `4.878`.

### 2. vLLM `triton_decode_attention.py` (read, not copied)

Read to confirm the contracts, not to lift code: it confirms `BLOCK_H = 16`, one program per
KV head with `VALID_BLOCK_H = min(BLOCK_H, kv_group_num)`, `BLOCK_N = 32`,
`num_warps = 4`, `num_stages = 2`, and that stage 2 is a separate merge pass. rlt's `BLOCK_T`,
`num_warps` and `num_stages` were then swept independently (16/32/64/128 x 4/8 warps x 2/3
stages) and the shipped values were already optimal, so they were left alone.

### 3. The split-count constant

`NUM_SPLITS = 16` was never measured. It is now derived from the batch shape
(`_split_count`), which keeps both properties the constant was there for: a power of two for
`tl.arange`, and a value that depends only on tensor shapes so a captured CUDA graph stays
valid. Sweeping splits 1/2/4/8/16 at 108 SMs shows 16 is never the best and the optimum tracks
a program count of roughly 512 (`rows x head_axis x splits`):

| rows | best splits (MHA) | best splits (GQA 48/8) |
| --- | --- | --- |
| 1 | 16 | 16 |
| 8 | 8 | 4-8 |
| 32 | 2-8 | 2 |
| 128 | 2-4 | 1 |

The two kernels need different bounds because their second grid axis carries different work:
the grouped kernel spends one program per KV head (covering the whole query group), the
per-head kernel one per query head. Hence `_GROUPED_SPLITS = (1, 512)` and
`_PER_HEAD_SPLITS = (4, 2048)`: the per-head kernel needs real splits even at 128 rows
(measured 1 vs 4 splits: 1.37 vs 1.27 ms at length 2048), while the grouped kernel prefers a
single part there (4 splits cost 1.11x).

## Results

### Correctness gate (runs before any timing)

`tests/test_gqa_decode.py` - 4 shipped geometries (16/16 MHA, 32/8, 48/8, 16/4) x rows
1/3/8/32/128 x lengths 1/33/512/2048, plus batch-invariance, windowing, and zero-length rows,
all against the FP32 `torch_paged_attention` oracle: **87 passed**. Full suite:
**1447 passed, 914 skipped, 0 failed**. This is the gate the first attempt lacked: it was
3.5x faster and numerically wrong (0.21 vs reference, where vLLM was 0.001).

### Operator-level, GQA 48/8, context 2048, page 16 (device ms)

| rows | before (V005) | after | vs before | vLLM unified | after / vLLM |
| --- | --- | --- | --- | --- | --- |
| 8 | 0.173 | **0.080** | **2.16x** | 0.119 | **0.67x (rlt faster)** |
| 32 | 0.544 | **0.172** | **3.17x** | 0.180 | **0.95x (rlt faster)** |
| 128 | 2.158 | **0.653** | **3.31x** | 0.621 | 1.05x |

GQA 32/8 behaves the same (0.079 / 0.171 / 0.650), and MHA was not regressed
(0.101 / 0.357 / 1.269 at rows 8/32/128; 1.05x faster than before at rows 128).

### Where the residual difference comes from (measured, not inferred)

Timing both implementations' two phases separately at context 2048:

| rows | vLLM stage1 | vLLM merge | rlt stage1 | rlt merge | stage1 | total |
| --- | --- | --- | --- | --- | --- | --- |
| 8 | 0.0567 | 0.0767 | 0.0554 | 0.0153 | **0.98x** | 0.53x |
| 32 | 0.1824 | 0.0807 | 0.1804 | 0.0155 | **0.99x** | 0.74x |
| 128 | 0.7184 | 0.1227 | 0.7088 | 0.0379 | **0.99x** | 0.89x |

The attention stage is at parity (0.98-0.99x). The residual 1.05x at 128 rows is vLLM's
`unified_attention` not splitting KV at all, while rlt still launches a merge pass.

### Short context is a known, bounded gap

At context 512 the operator is 1.23-1.37x slower than vLLM's single-pass kernel, because both
rlt and vLLM sit at kernel-launch floor (~0.057 ms device, ~0.080 ms measured) and rlt pays
one extra launch. Two candidate fixes were measured and rejected on evidence:

- Inlining the merge for `splits == 1` (divide `part_o` and copy, no second kernel): correct,
but slightly *slower* (0.1800 -> 0.1876 ms at 512/128), so it was reverted.
- Dispatching to the existing `paged_prefill_attention`, which is genuinely faster here
(0.0448 vs 0.0791 ms at 128/8 - faster than vLLM's 0.0559) and needs no merge pass. It
cannot be selected safely yet: which path wins depends on `rows x context_length`, and
context lengths live on device (the CUDA-graph path fills `context_lengths` into a
preallocated buffer), so a host-side branch on the length would either force a device sync
or be wrong under graph capture. The decision needs a measured
host-length-at-graph-capture contract before it can ship.

### End to end

`bench_e2e.py --prompt 512 --decode 128 --batch 8` (Ouro-1.4B, CUDA graphs on): 14.587 s
after vs 14.553 s before, 70.2 vs 70.36 tok/s - **inside run-to-run noise (0.2%)**. That is
expected: V002's per-step budget attributes 0.85 ms of 13.86 ms to attention, so a 2-3x
attention improvement moves the loop by roughly 1%, and the Ouro path is MHA, which this
change does not alter.

## What this says about the operator layer

- The GQA gap was a launch-shape bug with a redundant-work cause, not a missing optimization:
the same bytes were moved 6.7x more often than necessary.
- The reusable implementation was already in the repository. Adapting
`_paged_prefill_kernel` needed ~40 lines of structural change (row meaning, mask, store
target) rather than a new kernel.
- An unmeasured constant (`NUM_SPLITS = 16`) cost up to 1.6x depending on shape. Deriving it
from shapes kept the numerical contract (bitwise-identical to the old split alignment at the
same split count, verified by the batch-invariance test) and removed the waste.

## Reproduce

```bash
# correctness gate
/root/kun/vllm-omni/.venv/bin/python -m pytest tests/test_gqa_decode.py -q -p no:randomly

# operator-level comparison (rlt vs vLLM decode vs vLLM unified), packed rows
/root/kun/vllm-omni/.venv/bin/python /tmp/pd2.py --rows 8,32,128 --length 512,2048 \
--geom ouro-mha,nanbeige-gqa6,gqa4 --output out/pd_final6.json

# phase attribution against vLLM
/root/kun/vllm-omni/.venv/bin/python /root/kun/rlt-perf/phase_attr.py

# splits sweep (the data behind _split_count)
/root/kun/vllm-omni/.venv/bin/python /root/kun/rlt-perf/split_len.py

# end to end
/root/kun/vllm-omni/.venv/bin/python bench/bench_e2e.py --prompt 512 --decode 128 --batch 8
```

Scripts are kept at `C:\Users\kunku\.pi-desktop\scratch\<session>\` locally and mirrored under
`/root/kun/rlt-perf/` on `lyx`.

## Follow-up: mixed decode lengths

The original shape-only MHA policy selected four splits for 32 rows. A batch
of 24 contexts of length 128, four of length 512 and four of length 2048 exposed
a long-row tail. Eight splits recover the prior MHA latency. The grouped
kernel reads the current device lengths in medium-sized batches. It uses
eight splits for long rows when they occupy at most one quarter of the batch,
and writes short rows directly. The merge skips rows already written and
masks out unused splits. With a single split the grouped launch writes the
output directly and skips the merge launch altogether.

Measured on A100-SXM4-80GB with BF16, 16-token pages, head dimension 128,
Torch 2.14.0+cu130, Triton 3.8.0 and vLLM 0.33.0. CUDA-event medians from
five interleaved repetitions, rotating physical KV pools for cases with
less than 96 MiB of KV.

| Shape | Initial #205 | Updated | Best measured vLLM unified |
| --- | ---: | ---: | ---: |
| MHA 16/16, 32 mixed rows | 104.32 µs | 83.71 µs | 117.76 µs |
| GQA 48/8, 32 mixed rows | 83.54 µs | 47.10 µs | 69.85 µs |
| GQA 48/8, 32 rows × 128 | 18.15 µs | 21.16 µs | 18.46 µs |
| GQA 48/8, 128 rows × 128 | 56.96 µs | 49.37 µs | 53.76 µs |
| GQA 48/8, 128 rows × 2048 | 652.37 µs | 644.44 µs | 621.23 µs |

The length test adds about 3 µs for the 32-row uniform short batch. A graph
replay regression switches between uniform and mixed rows, including zero
lengths and sliding windows. An A100 run passed 90 tests. Two E4M3FN tests
require SM89 or newer. The sweep uses synthetic inputs and does not establish
serving throughput.
155 changes: 155 additions & 0 deletions tests/test_gqa_decode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# SPDX-License-Identifier: Apache-2.0
"""GQA decode: every geometry this repo ships must match the FP32 torch reference.

This test exists because the first attempt at batching the query group produced a kernel
that was 3.5x faster and numerically wrong, and nothing failed loudly enough to catch it
before the speed number was reported. The gate here is deliberately exact-ish (BF16
rounding against a FP32 oracle) over every (heads, kv_heads) pair in the model configs, at
row counts from 1 to 128, so a wrong group mapping cannot hide behind one lucky shape.
"""

import pytest
import torch

from vllm_rlt.kernels.paged_attention import torch_paged_attention
from vllm_rlt.kernels.triton_attention import paged_attention

requires_cuda = pytest.mark.skipif(not torch.cuda.is_available(), reason="needs a CUDA device")

# (query heads, kv heads) from vllm_rlt/models/*.py
GEOMETRIES = [
(16, 16), # Ouro: MHA, must keep using the per-head kernel
(32, 8), # group of 4
(48, 8), # Nanbeige, group of 6
(16, 4), # group of 4
]


def _make(rows, length, heads, kv_heads, dim, page, device="cuda", seed=0):
torch.manual_seed(seed)
per_row = (length + page - 1) // page
total = rows * per_row + 8
dtype = torch.bfloat16
key = torch.randn(total, page, kv_heads, dim, device=device, dtype=dtype)
val = torch.randn_like(key)
tables = torch.zeros(rows, per_row + 1, device=device, dtype=torch.int32)
for r in range(rows):
tables[r, :per_row] = torch.arange(
r * per_row, (r + 1) * per_row, device=device, dtype=torch.int32
)
ctx = torch.full((rows,), length, device=device, dtype=torch.int32)
q = torch.randn(rows, heads, dim, device=device, dtype=dtype)
return q, key, val, tables, ctx


@requires_cuda
@pytest.mark.parametrize("heads,kv_heads", GEOMETRIES)
@pytest.mark.parametrize("rows", [1, 3, 8, 32, 128])
@pytest.mark.parametrize("length", [1, 33, 512, 2048])
def test_gqa_decode_matches_torch_reference(heads, kv_heads, rows, length):
page, dim = 16, 128
q, key, val, tables, ctx = _make(rows, length, heads, kv_heads, dim, page)
reference = torch_paged_attention(q, key, val, tables, ctx).float()
got = paged_attention(q, key, val, tables, ctx).float()

diff = (got - reference).abs()
scale = reference.abs().max().item()
# BF16 rounding against an FP32 oracle; the whole point is that a wrong group mapping or
# a mis-sliced output is orders of magnitude above this.
assert diff.max().item() <= max(2e-2, 0.02 * scale), (
f"max |diff| {diff.max().item():.6f} vs reference scale {scale:.6f} "
f"for heads={heads} kv_heads={kv_heads} rows={rows} length={length}"
)
assert torch.isfinite(got).all(), "non-finite output"


@requires_cuda
@pytest.mark.parametrize("heads,kv_heads", [(32, 8), (48, 8), (16, 4)])
def test_gqa_decode_is_batch_invariant(heads, kv_heads):
"""Split alignment makes a row's result independent of the batch it ran in."""
page, dim, length = 16, 128, 512
single = []
for rows in (1, 4):
q, key, val, tables, ctx = _make(rows, length, heads, kv_heads, dim, page, seed=7)
single.append((paged_attention(q, key, val, tables, ctx).float(), q, key, val, tables, ctx))

out4 = single[1][0]
for r in range(4):
deep = torch_paged_attention(
single[1][1][r : r + 1],
single[1][2],
single[1][3],
single[1][4][r : r + 1],
single[1][5][r : r + 1],
).float()
assert (out4[r : r + 1] - deep).abs().max().item() <= 2e-2


@requires_cuda
@pytest.mark.parametrize("heads,kv_heads", [(32, 8), (48, 8)])
def test_gqa_decode_window_masks_to_last_keys(heads, kv_heads):
page, dim, rows, length, window = 16, 128, 4, 512, 128
q, key, val, tables, ctx = _make(rows, length, heads, kv_heads, dim, page, seed=11)
got = paged_attention(q, key, val, tables, ctx, window=window).float()
reference = torch_paged_attention_windowed(q, key, val, tables, ctx, window).float()
assert (got - reference).abs().max().item() <= 2e-2


def torch_paged_attention_windowed(q, key, val, tables, ctx, window):
from vllm_rlt.kernels.paged_attention import torch_paged_attention_windowed as impl

return impl(q, key, val, tables, ctx, window)


@requires_cuda
@pytest.mark.parametrize("heads,kv_heads", [(32, 8), (48, 8)])
def test_gqa_decode_zero_length_row_returns_zero(heads, kv_heads):
"""A row with no context must produce zeros, not NaN, through the split/merge path."""
page, dim, rows = 16, 128, 3
q, key, val, tables, ctx = _make(rows, 256, heads, kv_heads, dim, page, seed=13)
ctx[1] = 0
out = paged_attention(q, key, val, tables, ctx).float()
assert torch.isfinite(out).all()
assert out[1].abs().max().item() == 0.0


@requires_cuda
@pytest.mark.parametrize("quantized", [False, True])
@pytest.mark.parametrize("window", [0, 193])
def test_mixed_gqa_decode_switches_paths_on_graph_replay(quantized, window):
if quantized and torch.cuda.get_device_capability() < (8, 9):
pytest.skip("Triton FP8 E4M3FN loads require SM89 or later")
q, key, val, tables, lengths = _make(32, 544, 48, 8, 128, 16, seed=29)
key_scale = value_scale = None
if quantized:
key_scale = key.abs().amax(-1).float() / 448
value_scale = val.abs().amax(-1).float() / 448
key = (key.float() / key_scale[..., None]).to(torch.float8_e4m3fn)
val = (val.float() / value_scale[..., None]).to(torch.float8_e4m3fn)
mixed = torch.tensor([0, 17, 128, 33] * 6 + [129, 193, 544, 512] * 2, device="cuda")
uniform = torch.full_like(mixed, 512)
lengths.copy_(mixed)
paged_attention(q, key, val, tables, lengths, key_scale, value_scale, window=window)
graph = torch.cuda.CUDAGraph()
with torch.cuda.graph(graph):
out = paged_attention(q, key, val, tables, lengths, key_scale, value_scale, window=window)
for replay_lengths in (mixed, uniform, mixed.roll(3)):
lengths.copy_(replay_lengths)
graph.replay()
if window:
from vllm_rlt.kernels.paged_attention import torch_paged_attention_windowed

expected = torch_paged_attention_windowed(
q, key, val, tables, lengths, window, key_scale, value_scale
)
else:
expected = torch_paged_attention(q, key, val, tables, lengths, key_scale, value_scale)
torch.testing.assert_close(out, expected, rtol=0.02, atol=0.002)


@requires_cuda
def test_wide_gqa_group_produces_every_query_head():
q, key, val, tables, lengths = _make(3, 65, 32, 1, 64, 16, seed=31)
actual = paged_attention(q, key, val, tables, lengths)
expected = torch_paged_attention(q, key, val, tables, lengths)
torch.testing.assert_close(actual, expected, rtol=0.008, atol=0.0001)
Loading
Loading