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
Read
In this order, only the parts listed
0 of 3 done
- Page-table and decode contractsFlashInfer Documentation
FlashInfer documentation:
BatchDecodeWithPagedKVCacheWrapper, constructor andplan/runParameters 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.
- 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
- 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.
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.
Try it yourself
Closed-book, before checking anything
[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.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.With page size 4 and page list
[4, 1], where is logical token 4?AnswerPhysical page 1, slot 0. Logical and physical page numbers need not match.
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.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
- AI Systems Performance Engineering companion code - Chapter 18Chris Fregly · required
Start with paged_attn_common.py, the dense versus paged layout pair, CUDA graph bucketing and the vLLM integration pair. Run correctness tests first, then a hardware-compatible benchmark with newly captured artifacts.
- FlashAttention-2: Faster Attention with Better Parallelism and Work PartitioningTri Dao · required
Derive online-softmax state, memory traffic and work partitioning. Implement only a small oracle-backed version or use the CS336 assignment; production kernel tuning belongs to the GPU track.
- CS336 Assignment 2 - FlashAttention and Triton SectionsStanford University · required
Complete the relevant derivation, PyTorch reference and Triton sections using your own code. Follow assignment integrity: use external help for concepts, invariants, tests and profiling, not copied solutions or edited answer code.
- Mini-SGLang - Kernel, Model Runner and Overlap BoundariesSGLang Project · support
Trace how FlashAttention or FlashInfer, KV layouts, tensor parallel communication and overlap scheduling meet the model runner. Identify the fallback and state-reset boundary before attempting a change.
- UCSD CSE 291A (Spring 2026) - Guest lecture: Engineering vLLMUC San Diego · support
Read the scribe notes, Sections 4 and 5: CPU overhead, the API server and renderer, synchronous and asynchronous engine-core scheduling, GPU-side input preparation, piecewise CUDA graphs and the hybrid kernel strategy.
- vLLM CUDA Graphs design (v0.31.0)vLLM Project · support
Use the capture modes, batch-size buckets and piecewise splitting around attention to explain what a graph replay can and cannot include.
- LLM Inference: From scratch, one token at a time - Module 07Tushar Amrit · support
Work the Attention Kernels module for the decode-attention memory traffic and the paged-attention gather before the matching units.
- AI Performance Engineering - Kernel Optimization, Profiling and AttentionWafer · reference
A curated index. Use Sections 2 and 3 only to find a kernel or profiler source the other entries lack.
- TensorRT-LLM Architecture OverviewNVIDIA · support
Use the runtime-optimization section to compare graph replay, overlap scheduling and backend dispatch with your selected engine. Record current version and hardware support before treating a path as available.
- vLLM structured outputs (v0.31.0)vLLM Project · required
Read Offline Inference and the API-field migration warning; trace grammar bitmask application before sampling in the versioned model-runner API.
- PyTorch compiler troubleshooting guide (2.14)PyTorch · reference
Read Graph break, Guards, Recompilation and Dynamic Shapes; run
TORCH_LOGSset tograph_breaksandrecompileson a small serving-shaped function.