Macalinao StudioLearnAll enriched lessons ↗
College path · 7 sectionsSwitch to high school ↗

College / 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

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

Speculative sampling uses a draft distribution q and target distribution p at the same prefix. Exactness refers to reproducing the target sampling distribution under the algorithm's assumptions, not matching a particular random seed or proving factual accuracy. Multi-token verification can exploit parallel computation of target conditionals. Greedy speculative variants and heuristic acceptance procedures should not be conflated with exact stochastic sampling.

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

For a sampled token x with q(x)>0, accept with min(1,p(x)/q(x)). The accepted mass for each token is q(x) times this factor, equal to min(p(x),q(x)). This identity is the key to the correction proof. A token with zero draft probability is never proposed by that draft, but can still receive mass through the residual distribution after rejection.

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

The residual distribution is proportional to max(p−q,0), normalized by the total positive difference. For p=[0.6,0.3,0.1] and q=[0.2,0.5,0.3], accepted mass is [0.2,0.3,0.1], totaling 0.6. Rejection mass is 0.4 and the residual is [1,0,0]. Adding accepted and replacement mass recovers p exactly. If p=q, rejection probability is zero and no residual draw is needed.

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

Verify proposals left to right using target and draft conditionals at the same accepted prefix. Stop at the first rejection, draw its residual correction, and discard the remaining draft suffix. If all k proposals are accepted, draw an extra target token from the final conditional. Cache rollback and position bookkeeping must match this prefix logic. Reusing rejected suffix state can break correctness even when the scalar acceptance equation is implemented properly.

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

Under an idealized constant conditional acceptance a, expected accepted draft count is a+a²+…+a^k. Expected emitted tokens including a correction or bonus is 1+a+…+a^k. This simplification is not an independence claim about real model traces. A latency model must include draft cost, target verification cost, memory traffic, scheduling and cache operations. Benchmark the complete algorithm against the same target sampling policy.

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

Use small distributions to verify nonnegative residuals, normalization and p recovery algebraically before empirical sampling. Include p=q, highly mismatched proposals and zero-probability edge cases. Then test conditional block handling, cache rollback and the all-accepted bonus. Finally measure latency and throughput under fixed sampling settings. Our finite three-token distribution is an exact local demonstration, not an implementation of a production transformer sampler.

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.

After rejection, exact correction samples from… Option 1: p directly. Option 2: Normalized positive part of p−q. Option 3: q again.

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

Reveal worked solution

The correct choice is option 2: Normalized positive part of p−q. The residual repairs the mass missing from accepted proposals.

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 college path?

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