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 Sandip Maity, Applied AI Engineer
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 and query head , with tokens in the cache:
with three complications:
- 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.
- Grouped-query attention (GQA). Several query heads share one KV head. With query heads and KV heads, query head uses KV head .
- Ragged batches. Every sequence in the batch has a different length.
The layout we'll use:
| Tensor | Shape | Notes |
|---|---|---|
q |
one new token per sequence | |
k_cache, v_cache |
the shared page pool | |
block_table |
int32, physical page per logical page | |
seq_lens |
int32, tokens in each sequence |
Contiguous: reserve the maximum length (6 blocks) per sequence
Paged: blocks handed out as tokens arrive
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 values (K and V), at 2 bytes each in FP16. For 32 sequences of 4,096 tokens with 8 KV heads of dimension 128:
On a T4 that's at least . 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
- Stage 1: one program per (sequence, head, split)
- Stage 2: merging the splits
- The wrapper
- Testing on a ragged, scattered batch
- Measuring it
- What this kernel leaves out, and what real ones add
- What you can now do
- Common mistakes
- Exercise
- 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