Unit 3 of 12 · about 4 hours · explain, implement

Paged and variable-length decode attention, GQA grouping and KV addressing

Build on Variable-length, paged and decode attention. Here the page table must remain correct as a live serving batch changes, and its plan must match the wrapper that actually executes.

You'll be able to

  • Connect variable request lengths and GQA heads to a paged decode wrapper's metadata.
  • Implement a CPU page-gather and head-mapping oracle independent of backend scheduling.
  • Explain split-KV, FlashDecoding and decode megakernels as distinct execution choices.

Covers

Paged and variable-length decode attentionGQA grouping and KV address calculationFlashDecoding and decode mega-kernel vocabulary
1

Read

In this order, only the parts listed

0 of 3 done

  1. Page-table and decode contractsFlashInfer Documentation

    FlashInfer documentation: BatchDecodeWithPagedKVCacheWrapper, constructor and plan/run Parameters and Notes (page read 2026-10-14, release 0.7.1)

    Read ordinary paged GQA decode only. The wrapper needs a planned supported shape and dtype; leave architecture-specific backends for reference.

  2. Dynamic split-KV and scheduling, arXiv:2501.01005v1FlashInfer: Efficient and Customizable Attention Engine for LLM Inference Serving

    Sections 3.1.1 and 3.3.1, including Algorithm 1 and Figure 6; Section 5.1, FlashDecoding discussion

  3. Decode-kernel vocabularyAI Systems Performance Engineering - Chapters 18 and 19, decode kernels and engine optimizations

    Chapter 18, Optimized Decode Kernels: FlashMLA (DeepSeek), ThunderMLA (Stanford) and FlexDecoding (PyTorch)

    Read for vocabulary and fusion boundaries, not quoted speedups or a requirement to run MLA kernels. An attention decode kernel is not the full transformer layer.

2

Focus on

  • Reuse your earlier page translation: logical position maps through entry and slot for page size . A request's metadata must exclude another request's pages and the invalid tail.
  • For divisible head counts, and KV head . The API requires that divisibility; unsupported mappings must reject or use a documented alternative.
  • FlashDecoding splits a long KV range into parallel partial attention states and merges them. A megakernel may fuse scheduling and reduction; neither term implies fusion of every model operator.
  • Planning follows live lengths and can be reused across compatible layers in one step. Reusing a plan from a different request order, shape or page mapping can produce plausible but wrong outputs.
Common mistakes · 3
  • Treating the CSR page offsets as token offsets. Each offset counts page-table entries, not token rows.
  • Setting last-page length to zero for a full page. The nonempty-page convention uses the page size.
  • Equally averaging split-KV outputs instead of merging their normalizers.
3

Try it yourself

Closed-book, before checking anything

From a blank file, build a CPU double-precision decode oracle for two requests of lengths 5 and 7, page size 4, physical page lists [4, 1] and [3, 0], four query heads, two KV heads and width 8. Generate standard-normal queries, keys and values with seed 0. Gather each request independently, map heads as [0, 0, 1, 1] and compare with contiguous attention at rtol=1e-12, atol=1e-12. Permute the physical allocation and swap batch order with matching metadata; outputs must follow request identity. Poison unused slots and confirm they cannot affect valid outputs.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.With page size 4 and page list [4, 1], where is logical token 4?Answer

    Physical page 1, slot 0. Logical and physical page numbers need not match.

  2. 2.What length does two pages with last-page occupancy 3 represent at page size 4?Answer

    7 valid tokens. Only three slots of the final page are readable.

  3. 3.Why might split-KV lose on short contexts?Answer

    Extra partial-state writes, reduction and dispatch can exceed the benefit of added parallelism. Measure the crossover rather than assuming it.

You're done when

The independent oracle passes reordered and remapped batches, a seeded wrong-page or wrong-head defect fails, and you distinguish split-KV from full-layer fusion.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 11 more in the section
  • FlashInfer: Efficient and Customizable Attention Engine for LLM Inference ServingFlashInfer Project · primary

    Read the storage heterogeneity, plan and run separation, load-balanced scheduling, customization, CUDA Graph compatibility and end-to-end evaluation. Use it to connect serving-shaped attention kernels to dynamic workloads and dispatch constraints.

  • AI Systems Performance Engineering - Chapters 18 and 19, decode kernels and engine optimizationsChris Fregly (O'Reilly) · required

    Read "Optimized Decode Kernels" in Chapter 18 (FlashMLA, ThunderMLA, FlexDecoding), then the Chapter 19 sections on kernel autotuning, occupancy-aware kernel selection, hot-swappable kernels and CUDA-graph prewarming. Choose one bounded critical path to verify.

  • FlashInfer DocumentationFlashInfer Project · required

    Use the current attention, PageAttention, sampling, JIT and CUDA Graph interfaces while implementing. Pin the version and supported GPU architecture, and validate dispatch and fallback over the real workload shape distribution.

Also in this section