Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Explainers/Inference

Speculative decoding explained: making AI answer faster

A small model drafts tokens and the big model checks them in one pass, giving 2x to 3x faster output with exactly the same probabilities.

Derpx0621 Sept · 6 min read
Contents
The idea in one sentenceHow it worksStep 1: why one run can check many tokensStep 2: draft, then verifyStep 3: the accept or reject ruleStep 4: how many tokens per roundStep 5: the speed-upWhat the paper foundWhat to be careful aboutSources
0% read · 6 min leftBack to top ↑
On this page0%
Contents
The idea in one sentenceHow it worksStep 1: why one run can check many tokensStep 2: draft, then verifyStep 3: the accept or reject ruleStep 4: how many tokens per roundStep 5: the speed-upWhat the paper foundWhat to be careful aboutSources

A large language model writes one token (a word or word piece) at a time, and each token needs a full run of the model. For a big model this is slow, and the runs cannot be done in parallel because each token depends on the one before it. This paper shows how to get several tokens from one run of the big model, while the output stays exactly what the big model would have written alone.

Paper

Fast Inference from Transformers via Speculative Decoding. Yaniv Leviathan, Matan Kalman, Yossi Matias (Google Research). ICML 2023. arXiv:2211.17192. A team at DeepMind, Chen et al., published the same idea independently: arXiv:2302.01318.

The idea in one sentence

A small, fast draft model guesses the next few tokens, and the big target model checks all of the guesses in a single run, keeping the ones that pass a fair test.

Many tokens are easy, so a small model often guesses them right. The test is built so that the kept tokens follow exactly the same probabilities as the big model alone. Nothing is retrained and the big model is not changed.

How it works

Step 1: why one run can check many tokens

Writing a token means reading all the model's weights out of memory. The paper notes that this is often limited by memory speed, not by arithmetic, so the chip has spare calculating power. Checking five positions in one run costs about the same time as one position. The paper uses this spare power.

Two words used below. A distribution is a list of probabilities for every possible next token. Autoregressive means each token is chosen using all the tokens before it.

Step 2: draft, then verify

Draft model
Target model
Accept or reject
Output

The draft model writes γ guessed tokens one by one (γ is a small number such as 4)

Step 1 of 4
One round. Each round gives between 1 and γ+1 tokens.

So a round always gives at least one token and at most γ+1\gamma + 1γ+1.

Step 3: the accept or reject rule

Call the target's probability for a token p(x)p(x)p(x) and the draft's q(x)q(x)q(x). (Chen et al. write it the other way round, with qqq for the target. The rule is the same.) The draft proposes a token xxx drawn from qqq. Then:

  • If p(x)≥q(x)p(x) \ge q(x)p(x)≥q(x), accept it.
  • Otherwise accept it with probability p(x)/q(x)p(x)/q(x)p(x)/q(x).

In one line, accept with probability min⁡(1,p(x)/q(x))\min(1, p(x)/q(x))min(1,p(x)/q(x)).

If it is rejected, draw a replacement from the leftover distribution:

p′(x)=max⁡(0, p(x)−q(x))∑x′max⁡(0, p(x′)−q(x′))p'(x) = \frac{\max(0,\, p(x) - q(x))}{\sum_{x'} \max(0,\, p(x') - q(x'))}p′(x)=∑x′​max(0,p(x′)−q(x′))max(0,p(x)−q(x))​

This is where the target likes a token more than the draft does, scaled so the probabilities add to 1.

Toy example (our numbers, not from the paper). Three possible tokens A, B, C.

TokenTarget pppDraft qqqAccept chance min⁡(1,p/q)\min(1, p/q)min(1,p/q)Chance draft proposes it and it is accepted: min⁡(p,q)\min(p,q)min(p,q)
A0.60.310.3
B0.30.50.60.3
C0.10.20.50.1

The chance that a proposal is accepted is 0.3+0.3+0.1=0.70.3 + 0.3 + 0.1 = 0.70.3+0.3+0.1=0.7. So a proposal is rejected with probability 0.30.30.3. Leftover distribution: max⁡(0,p−q)\max(0, p - q)max(0,p−q) is 0.30.30.3 for A and 000 for B and C. After scaling, A has probability 1. So after a rejection the token is always A.

Final probability of each token:

  • A: 0.3+0.3×1=0.60.3 + 0.3 \times 1 = 0.60.3+0.3×1=0.6
  • B: 0.3+0.3×0=0.30.3 + 0.3 \times 0 = 0.30.3+0.3×0=0.3
  • C: 0.1+0.3×0=0.10.1 + 0.3 \times 0 = 0.10.1+0.3×0=0.1

That is exactly ppp. The draft model's quirks are removed by the leftover step. This is why the method does not change the output distribution.

Step 4: how many tokens per round

The acceptance rate α\alphaα is the average chance that a draft token is accepted. In the toy above, α=0.7\alpha = 0.7α=0.7. The paper also shows α=1−E[DLK(p,q)]\alpha = 1 - \mathbb{E}[D_{LK}(p, q)]α=1−E[DLK​(p,q)], where DLKD_{LK}DLK​ measures how far apart the two distributions are, so a closer draft gives a higher α\alphaα.

If each guess is accepted with the same chance α\alphaα, the expected number of tokens per round is (paper, Equation 1):

E[tokens]=1−αγ+11−α\mathbb{E}[\text{tokens}] = \frac{1 - \alpha^{\gamma+1}}{1 - \alpha}E[tokens]=1−α1−αγ+1​

Check with α=0.7\alpha = 0.7α=0.7, γ=2\gamma = 2γ=2: this is 1+0.7+0.49=2.191 + 0.7 + 0.49 = 2.191+0.7+0.49=2.19 tokens per big-model run.

Step 5: the speed-up

Let ccc be the cost of one draft run divided by the cost of one target run. One round costs γc+1\gamma c + 1γc+1 target-runs. The paper's Theorem 3.8 gives the expected speed-up in time:

1−αγ+1(1−α)(γc+1)\frac{1 - \alpha^{\gamma+1}}{(1 - \alpha)(\gamma c + 1)}(1−α)(γc+1)1−αγ+1​

Toy example, c=0.1c = 0.1c=0.1, α=0.8\alpha = 0.8α=0.8, γ=4\gamma = 4γ=4: tokens per round =(1−0.85)/0.2=3.36= (1 - 0.8^5)/0.2 = 3.36=(1−0.85)/0.2=3.36. Cost =4×0.1+1=1.4= 4 \times 0.1 + 1 = 1.4=4×0.1+1=1.4. Speed-up =3.36/1.4≈2.4= 3.36 / 1.4 \approx 2.4=3.36/1.4≈2.4.

α = 0.82.4x
α = 0.61.65x
α = 0.41.18x
Toy numbers from the formula above, with γ = 4 and c = 0.1. Not measured.

Guessing further is not always better. The last guess only counts if all earlier guesses passed, but its cost is paid every round. With the same α=0.8\alpha = 0.8α=0.8 and c=0.1c = 0.1c=0.1, γ=8\gamma = 8γ=8 gives about 2.402.402.40, the same as γ=4\gamma = 4γ=4.

What the paper found

Leviathan et al. tested T5-XXL (11 billion parameters) on a TPU-v4 with batch size 1, against the standard T5X code. The draft was a smaller T5 model. They report 2x to 3x faster with identical outputs.

TaskDraftTempγ\gammaγα\alphaαSpeed-up
English to GermanT5-small070.753.4x
English to GermanT5-small170.622.6x
English to GermanT5-large070.821.7x
Summarising (CNN/DM)T5-small050.653.1x
Summarising (CNN/DM)T5-small150.532.3x

Temperature 0 means always pick the most likely token. Temperature 1 means sample normally. The smallest draft was the fastest overall because its low cost ccc outweighed its lower α\alphaα.

Chen et al. tested Chinchilla (70 billion parameters) with a 4 billion parameter draft and K=4K = 4K=4 guesses.

BenchmarkSamplingTime per token, normalTime per token, speculativeSpeed-up
XSumNucleus14.1 ms7.52 ms1.92x
XSumGreedy14.1 ms7.00 ms2.01x
HumanEval (code)Nucleus14.1 ms5.73 ms2.46x

Scores stayed the same, for example XSum ROUGE-2 of 0.157 and 0.156 with greedy sampling. These numbers show real speed-ups on two model families. They do not mean every task gets the same speed-up. The code task did best, which the authors link to code having many repeated pieces.

What to be careful about

  • It trades extra arithmetic for speed. The paper says it is not useful when there is no spare compute. Its Table 1 (with c=0c = 0c=0) shows α=0.8\alpha = 0.8α=0.8, γ=5\gamma = 5γ=5 gives 3.69×3.69\times3.69× speed but 1.63×1.63\times1.63× the arithmetic.
  • "Identical" means identical in distribution. Individual samples differ from run to run, as with normal sampling. Chen et al. add that this holds up to the rounding of the hardware numbers.
  • α\alphaα depends on the task and the draft. Leviathan et al. measured α\alphaα from 0.03 for a unigram draft against a small GPT-like model up to 0.89 for a 6M-parameter draft. Tiny drafts that ignore context give little.
  • The draft must use the same tokens as the target. The rule compares both models' probability for the same token.
  • The formula assumes every guess has the same chance of passing. Real texts vary, so measure α\alphaα on your own data.
  • A good draft is easy to get wrong. Chen et al. report that a small model served on many chips loses time to communication, so they trained a wide draft with only 8 layers (1.8 ms per token against 14.1 ms for Chinchilla).
Key takeaways
  • Generating a token is limited by memory reads, so checking several tokens in one run is almost free.
  • A draft model guesses, the target verifies all guesses at once, and the first rejected guess is replaced using the leftover distribution.
  • The accept rule min⁡(1,p/q)\min(1, p/q)min(1,p/q) plus the leftover step gives exactly the target's distribution.
  • Expected tokens per round is (1−αγ+1)/(1−α)(1 - \alpha^{\gamma+1})/(1 - \alpha)(1−αγ+1)/(1−α), and the speed-up divides this by γc+1\gamma c + 1γc+1.

Sources

  • Leviathan, Kalman, Matias. Fast Inference from Transformers via Speculative Decoding. ICML 2023.
  • Chen, Borgeaud, Irving, Lespiau, Sifre, Jumper. Accelerating Large Language Model Decoding with Speculative Sampling. 2023.
ShareXLinkedInEmail
Written by Derpx06A developer who builds AI search tools and the code around them. I explain what I got wrong first, so you can skip it. Spotted a mistake? Tell me — corrections get credited.
Foundations · 6 minHow do AI tokenizers work? (Why AI can't count the R's in strawberry)Inference · 7 minWhat is quantization? Running AI models in 4 bitsLLM Systems · 7 minWhy do AI chatbots make things up?Distributed Systems · 7 minHow does Raft work? Getting computers to agree
Start here
01Why do AI chatbots make things up?02How does Raft work? Getting computers to agree03Mamba explained: an alternative to Transformers