Unit 8 of 12 · about 5 hours · explain, measure
CUDA graph capture and replay, static shapes, buckets, padding and fallback
CUDA graphs and validated dispatch with fallback and Launch overhead, bucketing and graph misses covered generic capture and bucketing. Serving adds mixed batch shapes and attention backends that veto full capture, so one static graph never fits the whole workload.
You'll be able to
- Explain full, piecewise and eager dispatch for uniform-decode versus prefill/mixed batches, including the automatic backend-compatibility downgrade.
- Capture and replay a static-shape decode step with bucketed sizes, padded inputs and a counted eager fallback for misses.
- Measure replay against eager with warmup and interleaved medians, and attribute the gap to launch supply rather than faster execution.
Covers
Read
In this order, only the parts listed
0 of 4 done
- CUDA Graphs modes and the v1 dispatchervLLM CUDA Graphs design (v0.31.0)
CUDAGraphMode(NONE,PIECEWISE,FULL,FULL_DECODE_ONLY,FULL_AND_PIECEWISE),CudagraphDispatcherwithBatchDescriptor, and CUDA Graphs Compatibility of Attention Backends (theAttentionCGSupporttable and downgrade)Read the dispatch design, not the linked pull-request comments as results. Confirm
cudagraph_capture_sizesdefaults inCompilationConfigin your installed v0.31.0 source. - Full versus piecewise graphsUCSD CSE 291A (Spring 2026) - Guest lecture: Engineering vLLM
May 7, 2026 scribe notes: Sections 4.1 (CPU overhead share) and 4.5 (full versus piecewise graphs, the attention-boundary split, Table 1)
Treat the lecture's H100 percentages as its hardware result, not your target. Map its full/piecewise vocabulary onto v0.31.0 runtime modes; capture moved under
CudagraphDispatcher. - Decode-wrapper capture contractFlashInfer Documentation
FlashInfer documentation:
BatchDecodeWithPagedKVCacheWrapperconstructor (use_cuda_graph,backend),plan/runParameters and Notes (page read 2026-10-14, release 0.7.1)plancannot run inside a graph ortorch.compile; replay needs stable addresses and a planned shape. Read decode only. - Padding policy and overlapTensorRT-LLM Architecture Overview
Architecture Overview: Runtime Optimizations, CUDA Graph (padding to the nearest larger captured size) and Overlap Scheduler (page read 2026-10-14)
Read the padding-versus-eager tradeoff and the overlap pipeline. The 22% figure is the page's result on its models and hardware, not your target. Note the page date before treating a path as available.
Focus on
- A captured graph replays addresses, not values or shapes. New contents in the same static buffers replay correctly; a new batch size needs its bucket's capture or the eager path.
- The vLLM v1 dispatcher searches keys in the order
FULL, thenPIECEWISE, thenNONE. A missing key runs eager. Cascade attention never takes a full graph. - Most backends support full graphs only for uniform batches, so mixed batches downgrade automatically. Read the resolved runtime mode from logs, not the requested
cudagraph_mode. - Padding to a larger captured size executes wasted tokens to avoid a miss. Count padded rows as executed work and keep them out of goodput.
Common mistakes · 3
- Reusing a capture after the batch shape changed and reporting the outputs. A stale graph replays stale addresses; recapture or take the eager path.
- Reading the configured
cudagraph_modeas the executed mode. Downgrade by backend support is silent unless you check the dispatched runtime mode. - Counting padded tokens as served work. They cost execution time but complete no request.
Try it yourself
Closed-book, before checking anything
addmm plus in-place clamp) in torch.cuda.CUDAGraph after three warmup runs. Replay matches eager under torch.testing.assert_close, and new values written into the same buffers replay to the new expected outputs. Then list your engine's capture sizes, name which batch sizes hit, which pad and which miss to eager, and record interleaved eager/replay medians with the timed boundary stated.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Why do new values in the same buffers replay correctly while a new batch size cannot?Answer
Replay replays recorded addresses. Same-shape contents sit at the same addresses; a new shape needs different addresses and launches.
2.A batch misses every captured key. What runs?Answer
The eager path (the
NONEruntime mode). Count the miss; silent fallback hides a bucketing defect.3.The backend table allows FlashInfer full graphs only for single-token uniform decode. What happens to a requested
FULLmode on mixed batches?AnswerIt downgrades. Mixed batches cannot use full graphs with that backend, so they take piecewise or eager under the fallback policy.
4.Does graph replay make the matmul itself faster?Answer
No. It removes repeated launch supply. The recorded kernels run unchanged; the measured gap is reduced host overhead.
You're done when
Your capture replays identical results at captured shapes, misses fall back with no wrong outputs, and you can name the dispatched runtime mode for a uniform-decode and a mixed batch from the capture-size list.
Only when you can do this without notes.
Leads to
Notes
Resources · 4 for this unit, 10 more in the section
- 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.
- 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.
- 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.
Also in this 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.
- 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.
- 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.
- 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.