Unit 6 of 11 · about 5 hours · explain, measure
Quantized dispatch, speculative verification and structured-decoding gaps
These three paths change what the engine executes every step. Each wins only inside its acceptance, error and schema regime.
You'll be able to
- Explain quantized dispatch, speculative acceptance and structured-mask costs with their batch-shape effects.
- Measure one quantized, one speculative and one guided path against exact-match and quality gates.
- Name the batch shape or schema where each path stops paying.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Quantization, dequant fusion and constrained decodingAI Systems Performance Engineering - Chapters 13, 15 and 16
Chapter 16, Quantization Approaches for Real-Time Inference: Reducing Precision from FP16 to FP8 and FP4; Weight-Only Quantization (GPTQ, AWQ); Fusing Quantization-Dequantization Steps into the Execution Graph; Chapter 15, Constrained Decoding Performance Implications
- Verification cost modelFast Inference from Transformers via Speculative Decoding
Sections 2 to 3, draft-verify-accept loop, acceptance rule, resampling distribution and walltime-versus-operations analysis. The paper names the target and the draft distribution; this unit and LeetGPU 87 swap the letters, so its rule reads with the draft
- Mask reuse and weight-only quantization labsAI Systems Performance Engineering companion code - Chapters 12 to 16
GuidedDecodingBenchmark(defined inguided_decoding_common.py) withreuse_gpu_mask=Falseinbaseline_guided_decoding.pyversusreuse_gpu_mask=Trueinoptimized_guided_decoding.py;ch16/baseline_awq_gptq_smoothquant.pyversusch16/optimized_awq_gptq_smoothquant_awq.py
Focus on
- Quantized dispatch changes scales, granularity and accumulation, not just storage. Per-token scales dequantize as ; verify error against task tolerance, not just kernel error.
- Speculative verification accepts draft token with probability , where is the draft probability and the target probability. Rejection resamples from the normalized positive part of and ends that sequence's accepted run.
- Structured decoding pays for masks and host interaction every step. A GPU-resident reused mask removes the per-step rebuild; measure its cost per schema, not as a constant.
Common mistakes · 3
- Reading a high acceptance rate as a speedup. Verification adds a full batched forward; low acceptance wastes it.
- Comparing quantized and dense paths by kernel time alone. Scale, granularity and accumulation change error; gate by task quality.
- Treating structured decoding as free once correct. Mask builds and host round trips scale with schema complexity and batch width.
Try it yourself
Closed-book, before checking anything
[1, 2, 0] with the given draft and target probabilities and uniform samples [0.50, 0.70, 0.30, 0.90]. Accept position 0 (, ), reject position 1 (, ), resample token 3 from normalized [0.667, 0, 0, 0.333], and emit [1, 3, 0, 0].Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.What does lossless speculative decoding guarantee?Answer
It preserves the target distribution up to floating-point limits: accepted drafts follow it and resamples come from the adjusted difference. Greedy equality tests check the implementation, not the proof.
2.Why does speculative decoding depend on batch shape?Answer
Batch width changes arithmetic intensity and memory traffic per step. Verification adds a batched target forward that pays only when acceptance is high enough at that width.
3.Where does guided-decoding overhead come from?Answer
A per-step CPU mask rebuild plus host interaction. Reusing a GPU-resident mask removes the rebuild; the book reports that complex grammars or small batches can still cost double-digit percent.
Practice
- LeetGPU 87 Speculative Decoding Verification
Challenge 87 in
challenges/medium/87_speculative_decoding_verification/challenge.py: implementsolveand pass the accept-all, reject-first and mixed functional tests plus theB = 64,T = 8,V = 32768performance testRun it on LeetGPU or locally against
reference_implfromchallenge.pyas in the CUDA primitives section. Keep correctness tests beyond the hosted score. - LeetGPU 96 INT8 KV Cache Attention
Challenge 96 in
challenges/medium/96_int8_kv_cache_attention/challenge.py: implementsolvewith per-token dequantization and1 / sqrt(head_dim)scaling; check thenum_heads = 1,seq_len = 3,head_dim = 4example giving[5.00, 6.00, 7.00, 8.00]Run it on LeetGPU or locally against
reference_implfromchallenge.pyas in the CUDA primitives section. Test adversarial scales and sequence tails locally.
You're done when
You report measured acceptance length, quantized error and mask overhead on one batch shape plus the regime where each path loses.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 11 more in the section
- AI Systems Performance Engineering - Chapters 13, 15 and 16Chris Fregly · primary
Read Chapter 13 on profiling and tuning PyTorch, Chapter 15 on inference parallelism, decoding and routing, and Chapter 16 on profiling and debugging inference. Keep Chapters 17 to 19 for GPU-9 and depth.
- AI Systems Performance Engineering companion code - Chapters 12 to 16Chris Fregly · required
Use compatible examples from
code/ch12throughcode/ch16. Trace whether each optimized path changes a kernel, graph, compiler choice, scheduler or library dispatch, then integrate one bounded custom path into a realistic operator or small endpoint. - Fast Inference from Transformers via Speculative DecodingPMLR · support
Read Sections 2 to 3 for the draft-verify-accept loop, the acceptance rule and the walltime-versus-operations analysis. Use it for the verification cost model behind the engine guides.
Also in this section
- AI Performance Engineering - Inference EnginesWafer · reference
A curated index. Use Section 4 only to find the introducing paper and implementation for the mechanism your measurement points to.
- PagedAttention and vLLMvLLM Authors · required
Study block-based key-value allocation, sharing and scheduling interaction. Trace logical token positions to physical blocks in one engine run and quantify fragmentation and memory-capacity effects.
- SGLangSGLang Authors · support
Read the runtime and RadixAttention design, then inspect the repository path for one kernel or prefix-reuse interaction. Use it as an alternative architecture in the integration defense.
- LeetGPU - Inference Kernel SetAlphaGPU · required
Choose three bottleneck-matched challenges from 74 GPT-2 Block, 83 Fused Residual Add RMS Norm, 84 SwiGLU MLP Block, 87 Speculative Decoding Verification, 93 Llama Transformer Block and 96 INT8 KV Cache Attention. Integrate one result locally rather than stopping at the hosted submission.
- FlashInferFlashInfer Project · support
Use the repository as the serving-kernel baseline and interface reference. Reconstruct workspace planning, layouts, dispatch and stream behavior for one selected operation before comparing your path.
- GPU Microbenchmark and Kernel Suite - IntegrationRoadmap-Final · support
Complete the real tensor or model-operation integration, dispatch and fallback requirements. Report compilation and invocation overhead, workload frequency, correctness or quality and the end-to-end no-benefit regime.
- PyTorch 2.14 ProfilerPyTorch · reference
Read the Overview example plus the API reference for
torch.profiler.profile(activities,schedule,on_trace_ready,record_shapes,with_stack),torch.profiler.record_functionandexport_chrome_trace. Use it for exact profiler and NVTX-range call contracts missing from the book. - Orca (OSDI 2022)USENIX · support
Read Sections 3 to 4 for iteration-level scheduling and selective batching, and Section 6 for the engine microbenchmark and end-to-end latency-throughput method. Use it as the continuous-batching mechanism behind the book summary.
- Sarathi-ServearXiv · support
Read Sections 3 to 4 for chunked-prefill stall-free batching and token-budget selection, and Section 5 for capacity and overhead method. Use it as the chunked-prefill mechanism behind the book summary.
- vLLM Documentation - Automatic Prefix CachingvLLM Project · reference
Read Automatic Prefix Caching (vLLM v0.31.0) for hash-based block reuse, full-block caching, reverse-order freeing and the multimodal hashing example. Use it for engine behavior the papers do not version.
- Transformer Inference ArithmeticKipply · reference
Read kv cache, capacity, latency calculations, batch sizes and flops counting for per-token KV bytes, weight-capacity arithmetic and memory-bound versus compute-bound latency floors. Use it to derive endpoint bounds before measuring.