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

Deterministic distribution simulatorDistributional equivalence tests
1

Read

In this order, only the parts listed

0 of 3 done

  1. Module 08 code lab: correctness then economicsLLM Inference: From scratch, one token at a time - Module 08

    Code lab Part A (speculative_step with the residual branch and naive_step for contrast, 400,000-sample comparison) and Part B (speculate_gamma with the bonus token and the yield table)

  2. Day 15: acceptance simulation rows100 Days of LLM Inference - Days 13, 14 and 15

    The speculative_decode_step cell with its Perfect, Good, Medium, Poor and Random rows at 4 drafts, and simulate_acceptance_rate: per-row yield as a comparison for your simulator

    The 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.

  3. 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

2

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 .
3

Try it yourself

Closed-book, before checking anything

From a blank file, implement one-token speculative sampling and show on , that 400,000 samples match the target within about 0.002 with acceptance near 0.80, while the naive rule deviates clearly.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 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. 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. 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. 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.

5

Practice

  • TensorTonic: Perform Exact Speculative Sampling

    Function speculative_sampling with 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 time

    This 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

Also in this section