Create your own
Lesson illustration

When Speculative Decoding Improves Performance

Welcome back. In the previous lesson, FlashAttention showed how an inference or training kernel can become faster by moving less data through high-bandwidth memory, even while doing essentially the same mathematical work. Speculative decoding applies the same performance instinct at a different level: during autoregressive decode, the target model often reloads enormous weights to produce just one token, leaving compute capacity underused.

This lesson develops a quantitative answer to a systems question: when does it pay to spend work on drafting and verifying several possible tokens rather than decoding one token at a time? You will calculate the expected tokens produced per target-model step, translate that into latency speedup, and adjust the ideal calculation for real verification and scheduling costs.


The basic bargain: extra compute for fewer serial target steps

Ordinary autoregressive decoding is strictly serial. To generate each new token, the target model reads its weights and the relevant KV cache, runs a forward pass, samples one token, and repeats. At low concurrency, this is commonly memory-bandwidth-bound: moving weights through the GPU memory hierarchy takes longer than using the available arithmetic units.

Speculative decoding inserts a cheap draft mechanism:

  1. A draft model proposes future tokens sequentially.
  2. The target model evaluates the proposed continuation in a single verification pass, producing target probabilities at every proposed position.
  3. The system accepts the initial run of draft tokens that passes the target’s acceptance rule.
  4. At the first rejection, later draft tokens are discarded and a corrected token is sampled from the target-derived distribution. If every draft token is accepted, the target can emit one additional “bonus” token.

Thus, one speculative iteration always emits at least one token and can emit up to tokens.

A small draft model proposes several continuation tokens, while the large target model verifies their positions together. When a proposal is rejected, the remaining draft suffix is discarded and generation resumes from a target-consistent token.

The crucial point is that verification is not free in terms of total GPU work. The target model evaluates several positions rather than one. It can nevertheless be close to the latency of a one-token decode step when the baseline is memory-bound: the target weights are fetched once and reused across more token positions, filling otherwise idle compute capacity.

For a visual explanation of this mechanism and why it is attractive specifically during memory-bound decode, watch the following two excerpts.

Speculative Decoding: The ONLY Video You Need to Speed Up Inference

“Speculative Decoding: The ONLY Video You Need to Speed Up Inference” by Cloud Codes gives a compact visual account of draft generation, parallel target verification, and the three quantities that determine the payoff.

Watch the core workflow, focusing on why scoring several positions can reuse one target-model weight pass and why rejection still guarantees progress. Then watch the cost tradeoff, which introduces draft quality, draft expense, and proposal length as coupled variables.

A caveat on “lossless” performance claims: with correct speculative sampling, the output distribution matches sampling directly from the target model under the same sampling transformations. It does not generally mean an independently seeded speculative request will generate the same literal sampled string as a baseline request. The guarantee comes from acceptance-rejection correction, not from treating a draft token as automatically correct whenever it matches a greedy choice.


Acceptance rate is the variable that turns guesses into useful work

Let be the probability that an individual proposed token is accepted, averaged over the workload. This is the draft acceptance rate.

For distribution-preserving speculative sampling, a proposed token drawn from the draft distribution is accepted with probability

where is the target distribution at that position. If the target assigns at least as much probability to the proposed token as the draft, it is accepted. Otherwise, it is accepted probabilistically. On rejection, sampling from a corrected residual distribution removes the draft’s bias.

So is not merely “the fraction of tokens whose greedy IDs match.” It measures the probability of acceptance under the actual sampling and rejection policy. It depends on:

  • the draft-target pair;
  • the prompt and output domain;
  • temperature, top-, top-, and other sampling transforms;
  • decoding position and context length;
  • the proposal method, such as a separate draft model, a draft head, or prompt lookup.

A coding-editing workload, for example, may have repeated or predictable text and yield much higher acceptance than open-ended creative writing. A single acceptance number reported by a model provider is therefore not enough for capacity planning.

Here is the mathematical foundation from the original speculative decoding paper.

arXiv:2211.17192v2 [cs.LG] 18 May 2023

Read Sections 2 and 3 of this arXiv paper for the original distribution-preserving algorithm and its ideal wall-time model. The goal is not to memorize the rejection-sampling proof, but to understand what the acceptance rate and draft-cost coefficient mean operationally.

In Section 2.1, “Overview” (p. 2), read the workflow summary and notice the lower bound of one token and upper bound of \gamma+1 tokens per target pass. In Section 3.1, “Number of Generated Tokens” (p. 3), follow the expectation argument. Focus on why each successive proposed token contributes only if all preceding proposals were accepted. Then read Section 3.3, “Walltime Improvement” (p. 3), especially the cost model. Section 3.4 explains why lower wall-clock latency can still mean more total arithmetic operations. Finally, read Section 3.5, “Choosing \gamma” (p. 4). Its opening discussion begins with the memory-traffic interpretation; then examine how the proposed-token count is selected numerically rather than assumed to be universally optimal.


Expected tokens per speculative iteration

Suppose the system proposes tokens and, as a planning approximation, each proposal is accepted independently with probability .

The first target-derived or recovery token is guaranteed. The first draft token contributes with probability , the second with probability , and so forth. Therefore the expected number of emitted tokens is

which is the finite geometric sum

for . If , then .

For example, with an acceptance rate of and draft tokens:

So a speculative iteration emits about tokens on average, rather than one. It does not emit four accepted draft tokens on average: later positions are conditional on all earlier positions being accepted.

This expectation explains an important diminishing-return effect. With , a fifth proposed token has only an probability of contributing, or about . A large proposal length is useful only when acceptance remains sufficiently high and verification cost remains controlled.


The ideal speedup formula

Define:

  • : time for one ordinary target-model decode step;
  • : time for one sequential draft-model token divided by ;
  • : number of draft tokens proposed per iteration;
  • : acceptance rate.

The original ideal model makes a deliberately strong assumption:

Target verification of proposed tokens takes approximately one baseline target decode-step time, .

This is plausible when a single-stream target decode is memory-bound and extra token positions mainly consume otherwise-idle compute. It is not automatically valid at high batch sizes or on a compute-saturated GPU.

Under that assumption, one speculative iteration costs

and produces tokens in expectation. Its speedup over ordinary decode is therefore

A value greater than indicates improvement. The corresponding expected inter-token latency is

rather than the baseline .

A worked ideal calculation

Assume:

  • baseline target decode time ;
  • draft cost ;
  • acceptance rate ;
  • proposal length .

We already calculated:

The speculative step time is

Therefore:

and

Compared with baseline, this is a substantial latency reduction. The speedup is not because the target did less conceptual work; it is because one costly weight-and-cache traversal supported multiple output tokens.


Why proposal length has an optimum

Increasing has two opposing effects:

  • It raises the maximum possible emitted tokens and increases .
  • It increases draft cost, and in a real system it may increase target verification cost, memory pressure, and scheduler overhead.

Using and under the ideal verification assumption:

Proposed tokens Expected output Normalized step cost Ideal speedup
22.4401.10
43.3621.20
84.3291.40
124.7251.60

The useful output rises with proposal length, but eventually too slowly to cover the additional draft cost. The best is therefore a workload- and deployment-specific integer chosen by measurement or a small numerical search.

For the special case :

Speculation improves performance exactly when

under the ideal verification assumption. This is a useful intuition, not a general production policy. It says that a one-token speculative attempt is worthwhile when the likelihood of accepting it exceeds the relative cost of generating it with the draft.

Also note that is a measured time ratio, not a parameter-count ratio. A small draft can still have a disappointing if it has poor GPU utilization, expensive synchronization, CPU-GPU transfer overhead, or runs on contended hardware.


Replace the ideal assumption with a production timing model

For serving decisions, measure end-to-end timings rather than assuming verification costs exactly one baseline step.

Let:

  • : measured baseline target decode-step time;
  • : elapsed draft-proposal time;
  • : elapsed target verification time;
  • : rejection sampling, KV-cache bookkeeping, scheduling, and other overhead.

If these phases are serial, then:

If some phases overlap, use the measured end-to-end critical-path time directly rather than summing them.

The practical speedup is

Equivalently, normalize each cost by the baseline target step:

Then, for non-overlapped stages,

The direct break-even condition is:

This is the calculation to use in an architecture review. It forces the right question: do accepted tokens per speculative iteration exceed the normalized end-to-end cost of that iteration?

Revisiting the example with non-ideal verification

Keep and , so that . Now suppose measurements show:

The verifier is no longer “free”: it takes longer than a baseline decode step. The normalized speculative cost is

So the actual speedup is

Still useful, but materially below the ideal estimate.

Now consider a weaker draft with , under the same cost profile:

It still helps, but barely. If verification slows further because the GPU is compute-saturated, the speedup can fall below , meaning speculative decoding makes the service slower and potentially lowers aggregate throughput.


What changes verification cost in a real serving engine?

The vLLM-oriented workflow below makes the implementation responsibilities visible: a draft worker proposes, a target worker scores, and a rejection sampler decides what survives. Both draft and target KV-cache state must be kept consistent with the accepted prefix; rejected suffix state must not be retained as usable context.

This vLLM workflow separates proposal by a draft worker, scoring by the target worker, and acceptance or correction by a rejection sampler, while maintaining draft-model and target-model KV-cache state.

The ideal formula works best when verification exploits unused arithmetic capacity. In production, commonly rises when:

  • Baseline decode is already compute-bound. A large continuous batch may already use most tensor-core capacity, so verifying extra positions competes directly with useful work.
  • is too large. Long speculative spans add attention, activation, and bookkeeping work; the expected accepted length may not grow enough to justify it.
  • The model is a mixture of experts. Verification may invoke more routing, dispatch, and gather overhead for the additional positions.
  • Long-context or KV-heavy requests change the bottleneck. These can remain memory-bound even at substantial concurrency, but the KV-cache traffic and cache-management effects must be measured.
  • The draft method is poorly integrated. Separate-model drafts may impose memory residency and scheduling costs that a lightweight draft head, prompt lookup, or -gram proposer avoids.

For this reason, batch size is only a proxy. The real decision variable is whether the serving workload has headroom in compute while waiting on memory. Low-concurrency interactive chat often has that headroom; a dense-model offline batch workload may not.

A sound operational policy is dynamic:

  1. Measure acceptance rate and actual accepted tokens per speculative step by workload class.
  2. Measure , draft time, and end-to-end inter-token latency across concurrency levels.
  3. Select from a small candidate set, rather than assuming more proposals are better.
  4. Disable or reduce speculation when measured speedup is at or below , or when it violates a throughput or tail-latency objective.

The most informative metric is often mean emitted tokens per target verification, because it captures both acceptance behavior and the bonus-token effect. Acceptance rate remains essential for diagnosis, but the emitted-token metric enters the speedup equation directly.


Key takeaways

Speculative decoding turns unused compute capacity into lower decode latency by verifying several draft positions per target-model weight pass.

  • With acceptance rate and proposed tokens, expected emitted tokens per speculative iteration are
  • Under the ideal memory-bound assumption that target verification costs one ordinary target step and one draft token costs target steps, speedup is
  • In a real serving system, use measured timing:

Speculation helps only when expected useful tokens exceed the normalized cost of drafting, verification, and overhead.

  • High acceptance is valuable, but it is not sufficient by itself. Draft quality, draft cost, verifier slowdown, concurrency, model architecture, and workload shape all matter.
  • The largest gains generally occur in latency-sensitive, memory-bound decode. When the GPU is already compute-saturated, speculation can reduce throughput rather than improve it.

Next, the course moves from a single decoding optimization to the serving-system level: tracing a request through gateway, admission control, routing, scheduling, model execution, and token streaming.

Can't find a good explanation? Sign up and we'll make it for you

Sign up