Unit 7 of 12 · about 5 hours · derive, implement
Draft and target roles, exact acceptance and the correction distribution
The acceptance rule is what makes speculation free performance instead of a quality trade. A wrong rule still produces plausible text, so only the proof protects you.
You'll be able to
- State the draft and target roles and why verification parallelizes while drafting does not.
- Derive the acceptance probability and the residual distribution, and prove the output matches the target.
- Implement exact speculative sampling on tiny distributions and confirm the empirical distribution equals the target.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Acceptance, correction and the exactness proofFast Inference from Transformers via Speculative Decoding
Sections 2.1 to 2.3 (overview, standardized and speculative sampling), Algorithm 1 SpeculativeDecodingStep, and Sections 3.1 and 3.2 with Theorem 3.5 and Corollary 3.6 (acceptance rate as expected overlap)
Work the proof yourself before reading the appendix. Skip the distributed and cluster sections.
- Module 08: the acceptance rule and its proofLLM Inference: From scratch, one token at a time - Module 08
Concept 02 The acceptance rule, and the proof that nothing changes: the min(1, p/q) rule, the residual formula and the two-case P(emit z) = p(z) derivation, with the naive-rule bias note
The Concept 02 caveat and the matching Checkpoint question say that sampling parameters applied to the target only lose exactness. They do not: the proof holds for any the draft actually sampled from, so the output follows the processed target and only acceptance drops. Test it in your simulator.
- Day 15: the three-step loop in code100 Days of LLM Inference - Days 13, 14 and 15
Notebook
day15/speculative-decoding.ipynb: Concept Overview with the accept formula, and thespeculative_decode_stepandsimulate_acceptance_ratecells
Focus on
- The draft proposes tokens autoregressively with distributions ; the target scores all of them in one causal forward pass, yielding at every position. Verification is parallel because causality makes one pass cover every prefix.
- Accept draft token with probability . On rejection, sample from the residual normalized by its sum, which exactly equals the rejection probability.
- The acceptance rate is , or one minus the total variation distance. It depends only on the two distributions, never on the draft implementation.
- A bad draft costs speed only. The output distribution is the target either way; the naive agree-with-target rule instead biases output toward the draft and looks fine while doing it.
Common mistakes · 3
- Sampling the replacement from instead of the residual. That double-counts agreed mass and biases output; the residual is the mass wanted that undersupplied.
- Building the ratio from a the draft did not sample from, such as raw logits when the draft used temperature. Exactness needs the actual draft distribution; processing only the target stays exact but cuts acceptance.
- Reading acceptance as a property of the draft model. It is a property of the pair on a workload: same draft, new domain or higher temperature, new .
Try it yourself
Closed-book, before checking anything
Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.For the Reproduce pair, what are the accepted masses, the rejection probability and the residual?Answer
Accepted masses , rejection , residual , and .
2.Why does sampling the replacement from the residual, not from the target, preserve the distribution?Answer
The normalizer of the residual equals the rejection probability, so accepted mass plus residual mass sums to in both the and cases.
3.A run accepts 80 percent of draft tokens. What does that number predict, and what does it not predict?Answer
It estimates , hence expected yield per verification. It does not predict wall-clock speedup, which also needs draft cost, batch effects and rollback overhead.
4.The target applies top-p 0.9 and the draft samples its full distribution. Is the output still exact?Answer
Yes, if the ratio uses the distribution the draft actually sampled from: the proof holds for any . Acceptance drops, since drafts outside the top-p set are always rejected.
Practice
- 100 Days of LLM Inference Day 15: acceptance simulation
Cells
speculative_decode_stepandsimulate_acceptance_rate: reproduce the Perfect, Good, Medium, Poor and Random rows and confirm simulated yield matches the formula within sampling noiseThe rows are labeled by mixing weight, not , and the step returns no bonus token: compare with using each row's measured mean . Keep this simulation separate from engine evidence.
You're done when
You prove exactness without notes, work the four-token arithmetic by hand, and your simulator matches the target distribution to sampling noise on arbitrary pairs.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 10 more in the section
- Fast Inference from Transformers via Speculative DecodingDeepMind · required
Derive acceptance and correction probabilities and implement the exact categorical simulator. Prove distributional equality on tiny distributions before adding a model or engine.
- 100 Days of LLM Inference - Days 13, 14 and 15Elizabeth Thomas · support
Use the numeric-format, quantization-algorithm and speculative-decoding notebooks as compact preparation. Keep their synthetic demos separate from production-engine and hardware evidence.
- LLM Inference: From scratch, one token at a time - Module 08Tushar Amrit · support
Work the Speculative Decoding module's acceptance arithmetic and checkpoint before the speedup-model unit.
Also in this section
- Hands-On LLM Serving and Optimization - Quantization (Chapter 6) and Speculative Decoding (Chapter 7)Chi Wang and Peiheng Hu (O'Reilly) · primary
Read "Model Compression" in Chapter 6 for quantization and "Speculative Decoding" in Chapter 7, with its hands-on example. For each method write its correctness contract, the engine support it needs and a predicted winning and losing workload before running it.
- MIT 6.5940 TinyML and Efficient Deep Learning Computing - Quantization LecturesMIT Han Lab · required
Use Lectures 5 and 6 for quantization fundamentals and Lecture 13, Efficient LLM Deployment, for SmoothQuant and AWQ with TinyChat. Implement a small affine quantizer and an outlier experiment before using a pre-quantized checkpoint.
- SmoothQuant: Accurate and Efficient Post-Training Quantization for Large Language ModelsMIT and NVIDIA · required
Study activation outliers and the offline smoothing transformation. Reproduce the algebra on one linear layer and compare calibration-sensitive quality with an unmodified reference.
- AWQ: Activation-Aware Weight Quantization for LLM Compression and AccelerationMIT Han Lab · required
Focus on salient-weight protection, group scaling and the distinction between simulated quality and real kernel speed. Compare one supported AWQ path with the same unquantized workload.
- AI Systems Performance Engineering - Chapters 15 and 16, speculation and quantizationChris Fregly (O'Reilly) · support
Read "Speculative Decoding and Parallel Token Generation Techniques" in Chapter 15 (draft models, EAGLE, self-speculation, Medusa) and "Quantization Approaches for Real-Time Inference" in Chapter 16.
- vLLM Speculative Decoding and Quantization (v0.31.0)vLLM Project · required
Use the speculative decoding page for the supported proposers (draft model, n-gram, EAGLE, MTP) and its losslessness notes, and the quantization page for the hardware support table. Check every method against your GPU's compute capability before planning a run.
- AI Systems Performance Engineering companion code - Chapters 16, 18 and 19 labsChris Fregly · required
Use Chapter 16 AWQ, GPTQ and SmoothQuant pairs, Chapter 18 speculation sweeps and Chapter 19 dynamic quantized cache. Run one family at a time, require quality or numerical parity, and regenerate results on available hardware.
- TensorTonic Language Modeling from Scratch - Perform Exact Speculative SamplingTensorTonic · support
Solve after the acceptance-rule unit, then check your implementation's output distribution against the target by exact enumeration on a tiny vocabulary.
- AI Performance Engineering - Quantization and Speculative DecodingWafer · reference
A curated index. Use Section 4 only to find a quantization or speculation paper the other entries lack.
- Inference Engineering - Section 5.1, QuantizationPhilip Kiely (Baseten Books) · support
Read 5.1.1 Number Formats for the format table and Precision/Type/Scale-factor with Dynamic range/Granularity, 5.1.2 for PTQ versus QAT and the what-to-quantize decisions, and 5.1.3 for the perplexity, benchmark and custom-eval quality checks.