0%
LLM Inference and Serving: GPUs, Batching, and Cost
How Inference Works
Hardware and Memory
Serving Systems
Speculative Decoding
Speculative decoding speeds up generation by guessing several tokens ahead cheaply and then checking all the guesses with one pass of the large model. When the guesses are right, the large model produces several tokens for the cost of one step; when they are wrong, it falls back to its own token, so the output is exactly what it would have produced anyway. This lesson explains why speculation works, how verification preserves the output distribution, the main ways to produce guesses, how to estimate the speedup, and when speculation helps or hurts in production. This first section explains the core idea.
Decode wastes compute
Lesson five showed that a decode step at small batch sizes is memory bound: the GPU spends most of the step reading weights, while its arithmetic units sit mostly idle. Processing one token per sequence uses a tiny fraction of the available compute. Processing five tokens per sequence in the same step would cost almost the same time, since the weights are read once either way. The difficulty is that decode is sequential: each token depends on the one before it, so the model cannot simply generate five tokens at once. Speculative decoding finds a way to use that idle compute anyway.
Guess, then verify
The trick is to separate guessing from checking. A cheap method, such as a much smaller draft model, proposes the next few tokens, say four. The large target model then runs one forward pass over the current context plus all four guessed tokens. Because a transformer computes outputs for every position in parallel during a forward pass, this one pass yields the target model prediction at each of the five positions: after the context, after guess one, after guess two, and so on. The engine compares the guesses with these predictions and keeps the guesses up to the first disagreement, plus one token from the target at that point.
Why the output does not change
The key property of speculative decoding is that it is lossless when done correctly. With greedy decoding, the target model would have chosen its most likely token at each position; speculation keeps a guess only if it equals that choice, and at the first mismatch it uses the target choice. The resulting sequence is identical to what the target model alone would have produced. With sampling, a slightly more careful acceptance rule, covered in the next section, guarantees that the output tokens follow exactly the same probability distribution as sampling from the target alone. Quality is untouched; only speed changes.
Where the speedup comes from
If the draft guesses well, each target pass yields several tokens instead of one. If, on average, three tokens are accepted per pass plus the target token, the target runs a quarter as many passes as it would otherwise. Each verification pass costs slightly more than a normal decode step, because it processes five positions instead of one, and the draft model costs some time too. The net speedup depends on how often guesses are accepted, how many tokens are guessed, and how cheap the draft is relative to the target. Typical speedups in practice range from about 1.5 to 3 times for single user latency.
An analogy
Speculative decoding resembles speculative execution in CPUs, where the processor guesses the outcome of a branch and continues working ahead; if the guess was right, time is saved, and if wrong, the speculative work is discarded. It also resembles a junior writer drafting sentences that a senior editor approves in batches: the editor reads quickly, approves the correct beginning of the draft and writes the next word where the draft went wrong. The senior editor alone decides what gets published, so the result is the same as if they had written it all, only faster.
What speculation does not change
Speculation does not reduce the total compute needed to produce the output; it increases it slightly, because rejected guesses are wasted work and the draft model runs too. It trades extra compute, which is idle in memory bound decode, for fewer sequential steps. This is why it helps most when the GPU has spare compute and why its benefit shrinks when batches are large and the GPU is already busy, a theme of the final section.
A brief history
Speculative decoding was introduced in two papers in late 2022 and early 2023, one from Google and one from DeepMind, which independently described the draft and verify scheme with exact sampling guarantees. Since then, many variants have appeared: Medusa added extra prediction heads to the target model, EAGLE trained lightweight draft heads on the target hidden states, lookahead and n gram methods avoided drafts entirely, and some newer models are trained with multi token prediction heads that serve as built in drafters. Engines such as vLLM, SGLang and TensorRT LLM support several of these methods.
A step by step walk through
Consider a chat model answering a question about a database. The context so far ends with the words the index is stored. A small drafter, running in a fraction of a millisecond per token, proposes four continuations in a row: in, a, B, tree. The target model now runs one forward pass over the context with those four tokens appended. At the position after stored, the target would pick in, which matches. After in, it would pick a, which matches. After a, it would pick B, which matches. After B, the target prefers the token for a hyphen rather than tree, so the fourth guess is rejected and the hyphen is emitted instead. In one pass, the output grew by four tokens: in, a, B and the hyphen. Without speculation, those four tokens would have needed four full passes, each reading every weight of the target model from memory.
On the next pass, the drafter starts again from the new end of the output and proposes another four tokens. Some passes accept everything and earn a bonus token; others reject the very first guess and produce only the target token, exactly as plain decoding would. Over a whole response the average lands somewhere in between, and that average is what sets the speedup.
Why a small model can guess well
It may seem surprising that a model fifty times smaller agrees with a large model most of the time. The reason is that most tokens in real text are easy. After the words United States of, almost any language model predicts America. Closing brackets in code, the second half of a common word split into two tokens, punctuation, repeated variable names and phrases copied from the prompt are all predictable without deep reasoning. The large model earns its size on the hard tokens, such as the key fact in an answer or the right algorithm choice, and those are a minority. Speculation lets the cheap model handle the easy tokens while the large model still has the final say on every token, easy or hard.
The cost structure in one picture
It helps to think of each target pass as a fixed toll for reading the weights, plus a small extra charge per position processed. Plain decoding pays the full toll for every single token. Speculation pays the toll once for a group of tokens and adds the small per position charges plus the drafter cost. When the toll dominates, as in memory bound decode, sharing it across several tokens is a big saving. When the per position charge grows large, as in compute bound decode at high batch, sharing the toll no longer saves much. This toll model explains almost every practical observation about when speculation helps.
Common misconceptions
One misconception is that speculation makes the output a blend of the small and large models. It does not: the drafter only proposes, and every emitted token is either one the target agreed with or one the target produced. Another misconception is that speculation always helps. It helps when spare compute exists and acceptance is good; otherwise it can make things slower. A third is that speculation reduces cost. At the level of a single request it shortens wall clock time, but it does not reduce the floating point work, and at high load it can raise the cost per token. Keeping these distinctions clear avoids overpromising when proposing speculation to a team.
Memory bound decode leaves most arithmetic unused. Speculation spends that spare compute checking guesses, so each expensive weight read can produce several tokens.