Macalinao StudioLearnAll enriched lessons ↗
High school path · 7 sectionsSwitch to college ↗

High school / LONGER LESSON 17

Speculative decoding: fast drafts, exact correction

Calculate acceptance, construct the rejection distribution, and separate exactness from speed.

Preparation: Probability and fractions; conditional distributions for college.

Plan 20–35 minutes with the activities; this is a study estimate, not a timed requirement. Both paths have complete narration. Your notes and progress are kept separately for each level.

Section 01 / 07

Drafting is a proposal

High-school path. Calculate acceptance, construct the rejection distribution, and separate exactness from speed.

Preparation: Probability and fractions; conditional distributions for college. Work through one section at a time. Predict the experiment result before changing its controls, then explain what the example leaves out.

A smaller model can propose several tokens while a larger target model checks them. The target does not simply approve anything that looks reasonable. A probability rule decides which proposals to accept and how to replace rejected ones. The goal of exact speculative sampling is to preserve the target distribution while reducing expensive sequential work. A fast draft is useful only when verification and correction are also efficient.

Worked step 1. Choose a prefix and obtain both p and q for that prefix.

Worked step 2. Sample a proposal from q.

Worked step 3. Use p and q together to decide acceptance and any correction.

Try it yourself. Does exact sampling mean every generated answer is factually correct?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. No. It means the output distribution matches the target sampler. The target itself can still make mistakes.

Section 02 / 07

Accept with a probability ratio

Suppose the target assigns a proposed token probability 0.3 while the draft assigns it 0.6. The draft proposes this token too often relative to the target. Accept it with probability one half. If the target instead assigns more probability than the draft, accept every time. The rule is the smaller of one and target probability divided by draft probability. It is not the product of the two probabilities.

Worked step 1. For p(x)=0.3 and q(x)=0.6, acceptance=0.5.

Worked step 2. For p(x)=0.6 and q(x)=0.3, acceptance=1.

Worked step 3. Accepted proposal mass is min(p(x),q(x)).

a(x)=min(1,p(x)/q(x))

Accept with the smaller of one and target probability divided by draft probability.

Try it yourself. If target and draft probabilities are equal and positive, what is acceptance?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. One. The draft is not overproducing that token relative to the target.

Section 03 / 07

Repair the missing probability mass

Rejecting a token is only half the job. You must choose a replacement from the parts the draft underrepresented. Use target probabilities [0.6,0.3,0.1] and draft probabilities [0.2,0.5,0.3]. The draft underrepresents only the first token. The positive difference is [0.4,0,0], so after rejection the replacement must be the first token in this example.

Worked step 1. Accepted mass is [0.2,0.3,0.1].

Worked step 2. Missing positive target mass is [0.4,0,0].

Worked step 3. Add the rejection replacement mass to obtain [0.6,0.3,0.1].

r(x)=max(p(x)−q(x),0) / Σᵧ max(p(y)−q(y),0)

Normalize the positive part of target probability minus draft probability.

Try it yourself. Why is drawing a fresh token directly from p after rejection generally wrong?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. Rejection already depends on the proposal. The accepted portion is not zero, so adding an unconditional target draw can distort the total distribution.

Section 04 / 07

Verify a chain without keeping its rejected suffix

For a block of proposed tokens, each probability depends on the preceding proposed prefix. Check tokens in order. Once one is rejected and replaced, the remaining draft suffix was built on the wrong prefix and cannot simply be kept. If every proposal is accepted, the target can produce an additional token. The details matter because text probabilities are conditional, not independent colored balls from one fixed bag.

Worked step 1. The draft proposes A, then B given A, then C given A and B.

Worked step 2. If B is rejected and replaced by D, C was proposed under an obsolete prefix.

Worked step 3. Continue from A,D, not A,D,C by default.

Try it yourself. Why can you not keep the suffix after replacing an earlier token?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. Its conditional distribution and cached state were computed for a different prefix. The later probabilities must be recomputed.

Section 05 / 07

Estimate accepted length without promising speed

Imagine every proposal has the same acceptance probability a, independently, just for a rough calculation. With four proposed tokens, the chance of reaching the first is a, the second a squared, and so on. Add those chances to estimate accepted draft length. This helps reason about the mechanism, but actual token acceptances depend on their prefixes. More accepted tokens do not automatically imply lower wall-clock time.

Worked step 1. With a=0.8 and k=4, expected accepted drafts are 0.8+0.64+0.512+0.4096=2.3616.

Worked step 2. Including the correction or bonus gives 3.3616 emitted tokens per idealized cycle.

Worked step 3. The time spent producing those tokens still has to be measured.

E[accepted]=Σᵢ₌₁ᵏ aⁱ; E[emitted]=1+Σᵢ₌₁ᵏ aⁱ

Sum the probabilities of reaching each accepted draft position; add one for the correction or bonus token.

Try it yourself. Can a slower draft make speculation slower even when many proposals are accepted?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. Yes. Draft and verification overhead can exceed the saved target steps. Acceptance rate alone is not a speedup measurement.

Section 06 / 07

Test the distribution, then test the system

Workbench shows the target, accepted proposal mass and repaired distribution side by side. Move the draft probability and check that the repaired result stays at the target. A wrong repair may still look plausible in a few samples, so compare the actual numbers. After checking the probability calculation, measure the whole system's speed under a fixed workload. Correctness and performance are separate tests.

Worked step 1. Check accepted mass plus rejection mass equals one.

Worked step 2. Check accepted token mass plus weighted residual equals p for every token.

Worked step 3. Only then compare wall-clock performance.

Try it yourself. What does a close-looking histogram from ten samples prove?

Pause the recording and write your reasoning before continuing. The worked solution follows.

Reveal worked solution

Worked solution. Very little. Small random samples fluctuate. Use algebraic invariants and enough controlled trials before drawing statistical conclusions.

Section 07 / 07

Mastery and transfer

Bring the pieces together. First answer the checkpoint without the worked solutions. Then explain the mechanism in your own words, using one numerical example from this lesson.

If p=0.3 and q=0.6, accept with probability… Option 1: 0.18. Option 2: 0.5. Option 3: 1.

Pause here and choose an answer before continuing. The checkpoint explanation follows.

Reveal worked solution

The correct choice is option 2: 0.5. Use min(1,p/q).

Transfer task. Change one assumption in the worked example. State your prediction, calculate or test it in Workbench, then explain whether the result supports your prediction.

Reveal worked solution

Self-check rubric. Name the changed assumption, keep the other inputs fixed, show at least one intermediate calculation, and distinguish what the toy example guarantees from what would require a trained model or a real measurement.

Sources and scope

Original worked examples and teaching text, grounded in these primary references. Numerical fixtures explain mechanisms; model quality and hardware performance require separate measurements. Both levels share key examples, with different explanations and checkpoints.

Reset this high school path?

This removes only this path’s notes, progress and experiments from this browser.