Unit 11 of 12 · about 4 hours · implement
A deterministic distribution simulator and distributional equivalence tests
Exactness is a testable claim, not a citation. The simulator is also the harness the section mini-project reuses for rollback and acceptance checks.
You'll be able to
- Build a seeded speculative simulator with draft, target, acceptance and residual sampling as separate steps.
- Test output equivalence by exact enumeration on tiny vocabularies and by statistical agreement on larger ones.
- Diagnose biased output by contrasting the exact rule with the naive agree-with-target rule.
Covers
Read
In this order, only the parts listed
0 of 3 done
- Module 08 code lab: correctness then economicsLLM Inference: From scratch, one token at a time - Module 08
Code lab Part A (
speculative_stepwith the residual branch andnaive_stepfor contrast, 400,000-sample comparison) and Part B (speculate_gammawith the bonus token and the yield table) - Day 15: acceptance simulation rows100 Days of LLM Inference - Days 13, 14 and 15
The
speculative_decode_stepcell with its Perfect, Good, Medium, Poor and Random rows at 4 drafts, andsimulate_acceptance_rate: per-row yield as a comparison for your simulatorThe rows are labeled by mixing weight, not , and count no bonus token, so they follow with each row's mean ; match them with your bonus token switched off.
- Acceptance identity as test oracleFast Inference from Transformers via Speculative Decoding
Section 3.2, Corollary 3.6 (acceptance rate equals expected overlap): assert measured acceptance against summed minima on your test distributions
Focus on
- Seed everything and fix the distributions first. A deterministic simulator replays the same drafts, uniforms and residuals, so failures reproduce.
- Test at two levels: exact enumeration on vocabularies of 2 to 4 tokens where every path is checkable, and statistical agreement within noise on larger ones.
- Assert the identities, not just the samples: measured acceptance against , and residual selection against the normalized positive part of .
- Keep the naive rule in the suite as a negative control. It must fail visibly; a suite that passes a biased rule proves nothing.
Common mistakes · 3
- Testing only on aligned distributions. Near-identical draft and target accept almost everything and hide residual bugs; include hostile pairs with small overlap.
- Confusing sampling noise with bias. Size the trial count first: deviations near are noise, systematic gaps an order above it are bugs.
- Dividing by a zero draft probability. Exact sampling never drafts a token with , but a test can supply one; the TensorTonic contract accepts it when and rejects it otherwise, so branch before computing .
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.Draft [0] with , , uniform 0.9, residual uniform 0.5. What is emitted?Answer
Acceptance is , and 0.9 exceeds it, so reject. The residual is ; token 1 is emitted with nothing accepted before it.
2.Drafts [0, 1] with first-position acceptance 0.25 and uniform 0.9, residual uniform 0.4. What is returned, and what is the rejection index?Answer
The first proposal is rejected, so nothing is accepted. The residual crosses 0.4 at index 1, so token 1 is emitted with rejection index 0.
3.First proposal accepted, second rejected at residual uniform 0.75 with residual . Which tokens result?Answer
The first draft token, then token 2 from the residual cumulative crossing 0.75. One accepted, rejection at index 1.
4.Why does the suite need the naive rule as well as the exact rule?Answer
It is the negative control: plausible-looking biased output must fail the equivalence tests, proving the suite can catch the most likely implementation bug.
Practice
- TensorTonic: Perform Exact Speculative Sampling
Function
speculative_samplingwith draft tokens, draft and target probabilities and acceptance and residual random values; the three public cases reject at the first position twice and at the second once, checking the replacement each timeThis problem verifies proposals without the bonus token and returns accepted count with rejection index; state that contract difference in your solution before generalizing to the full loop.
You're done when
Your simulator passes exact enumeration on tiny vocabularies, matches the Day 15 yield rows within noise, fails the naive control, and clears the three public TensorTonic cases.
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.