Skip to content
Inference Academy

A decode-attention kernel in Triton: paged KV cache, GQA and split-KV

Write the kernel that dominates long-context decode: one query token per sequence attending over a KV cache stored in scattered pages, with grouped-query attention, online softmax across pages, and a split-KV second stage that spreads long sequences over many SMs. Includes a reference implementation, tests on ragged batches and a bandwidth target to measure against.

By

32 min · verified 2026-112 animated diagramsPro
Share
Open in ColabRuns on a free T4 GPU: pick Runtime → Change runtime type → T4 GPU.

At long context, decode time is dominated by one kernel: attention over the KV cache. Every step, each sequence's single new query token has to read every cached key and value. Matmul tricks don't help much here; it's a bandwidth problem with awkward memory layouts. That makes it a perfect final Triton kernel, because it uses everything from this course: masks and strides, online softmax, pointer indirection, and enough parallelism to fill the GPU.

Read paged KV and split-KV decoding first if they're new; this lesson implements them.

#The problem

For each sequence bb and query head hh, with LL tokens in the cache:

o=∑t=1Lest∑uesuvt,st=q⋅ktDo = \sum_{t=1}^{L} \frac{e^{s_t}}{\sum_{u} e^{s_u}} v_t, \qquad s_t = \frac{q \cdot k_t}{\sqrt{D}}

with three complications:

  1. The cache is paged. Each sequence's KV lives in fixed-size pages scattered through a big pool. A block table maps the sequence's page 0, 1, 2 and so on to physical pages. This is how vLLM-style engines avoid fragmentation.
  2. Grouped-query attention (GQA). Several query heads share one KV head. With HH query heads and HkvH_{kv} KV heads, query head hh uses KV head ⌊h/(H/Hkv)⌋\lfloor h / (H / H_{kv}) \rfloor.
  3. Ragged batches. Every sequence in the batch has a different length.

The layout we'll use:

Tensor Shape Notes
q [B,H,D][B, H, D] one new token per sequence
k_cache, v_cache [num pages,PAGE,Hkv,D][\text{num pages}, \text{PAGE}, H_{kv}, D] the shared page pool
block_table [B,max pages][B, \text{max pages}] int32, physical page per logical page
seq_lens [B][B] int32, tokens in each sequence
Paged attention: KV memory in blocks, allocated on demand
Animated

Contiguous: reserve the maximum length (6 blocks) per sequence

Paged: blocks handed out as tokens arrive

A: 1 blockB: 0 blocksC: 0 blocksfree: 15

Sequence A starts

Its first 16 tokens fill one block. Contiguous allocation (top) must reserve room for the maximum length up front.

Each cell is a 16-token block of KV memory. Colour = which sequence owns it; hatched = reserved but empty.

#What speed to aim for

Each step reads every cached K and V element once. For one sequence that's L×Hkv×D×2L \times H_{kv} \times D \times 2 values (K and V), at 2 bytes each in FP16. For 32 sequences of 4,096 tokens with 8 KV heads of dimension 128:

32×4096×8×128×2×2 bytes≈537 MB32 \times 4096 \times 8 \times 128 \times 2 \times 2 \ \text{bytes} \approx 537 \ \text{MB}

On a T4 that's at least 0.537/320 s≈1.7 ms0.537 / 320 \ \text{s} \approx 1.7 \ \text{ms}. The FLOPs are tiny by comparison: for each KV element, each of the 4 query heads sharing it does one multiply-add. That's about 4 FLOPs per 2-byte element, far below the balance point. So the target is simple: get as close to your measured bandwidth as you can. Tensor cores are optional here.

Pro content

Keep reading with Inference Academy Pro

Unlock the rest of this lesson, including 1 animated diagram, plus every advanced lesson, hands-on lab, the larger playground models and completion certificates.

Still to come in this lesson

  1. Stage 1: one program per (sequence, head, split)
  2. Stage 2: merging the splits
  3. The wrapper
  4. Testing on a ragged, scattered batch
  5. Measuring it
  6. What this kernel leaves out, and what real ones add
  7. What you can now do
  8. Common mistakes
  9. Exercise
  10. Going deeper

You will be able to answer

  • Why is decode attention bandwidth-bound, even though it does matrix maths?
  • How does the kernel find the K and V for token 37 of a sequence when the page size is 16?
  • With 32 query heads and 8 KV heads, which KV head does query head 13 use?
  • Why split one long sequence's KV across several programs?
  • How does stage 2 combine the partial outputs of the splits correctly?

Articles that build on this lesson