P

Speculative Decoding: Definition and Examples

Inference acceleration technique for large language models (LLMs) that uses a small fast model to generate candidate tokens, which are then verified in parallel by the main model, thereby reducing generation time without affecting response quality.

Full definition

Speculative Decoding is an optimization method designed to accelerate text generation by large language models. The principle is based on a simple observation: standard autoregressive generation, where each token is produced one by one, massively underutilizes GPU hardware because each step requires a full forward pass through the model. Speculative decoding bypasses this bottleneck by introducing a "draft then verify" mechanism.

Concretely, a smaller, faster auxiliary model (called the draft model) speculatively generates a sequence of candidate tokens. Then, the main model (the target model, larger and more accurate) verifies all these tokens in a single forward pass, in parallel. Accepted tokens are kept, and generation resumes from the first rejected token. This verification leverages the fact that evaluating multiple tokens simultaneously costs almost as much as evaluating a single token on a modern GPU.

The mathematical elegance of this approach lies in its guarantee: thanks to a carefully designed acceptance-rejection scheme, the final probability distribution is exactly identical to that of the target model alone. In other words, Speculative Decoding produces results that are strictly equivalent in quality, but 2 to 3 times faster. The gain depends on the acceptance rate, i.e., the ability of the small model to correctly predict what the large model would have generated.

This technique has become a standard in production systems of major AI providers. Variants exist, such as self-speculative decoding (where the model uses its own intermediate layers as a draft), Medusa (which adds parallel prediction heads), or tree-based speculative decoding, which explores multiple possible continuations simultaneously to maximize the acceptance rate.

Etymology

The term combines "speculative", borrowed from computer science where speculative execution refers to performing a calculation before knowing whether it will be necessary (as in modern processors), and "decoding", which refers to the process of generating text token by token in language models. The concept was formalized in 2022-2023 by teams from Google DeepMind (Leviathan et al.) and independently by Chen et al., drawing directly from branch prediction in CPUs.

Concrete examples

Deploying a chatbot in production requiring low latency

Configure the vLLM inference server with speculative decoding using a Llama 3 8B draft model to accelerate the main model Llama 3 70B, with a speculation length of 5 tokens.

Optimizing a code generation pipeline with an LLM

Enable speculative decoding to reduce the response time of my programming assistant. The source code has a restricted vocabulary, so the acceptance rate should be high.

Comparing inference acceleration strategies for an enterprise project

Compare the performance gains between INT8 quantization, speculative decoding, and continuous batching for our 70B parameter model deployed on 2 A100 GPUs.

Practical usage

In prompt engineering, Speculative Decoding does not directly affect prompt writing since it operates at the inference infrastructure level. However, understanding this technique allows better communication with infrastructure teams to optimize the latency of AI applications. When choosing an API provider or deploying an open-source model, checking whether speculative decoding is supported can cut response time by 2 to 3 times without any compromise on quality.

Related concepts

LLM InferenceAutoregressive generationModel quantizationKV Cache

FAQ

Does Speculative Decoding degrade the quality of the model's responses?
No, that is one of its main advantages. Thanks to a mathematical acceptance-rejection mechanism, the probability distribution of the generated tokens is rigorously identical to that of the target model used alone. The result is exactly the same, only the speed changes.
What speed improvement can be expected with Speculative Decoding?
The typical gain is between 2x and 3x in terms of tokens per second, but it strongly depends on the acceptance rate (how many draft model tokens are validated by the main model). For predictable tasks like code generation or translation, the gain can be higher. For very creative tasks, it will be more modest.
Is a specific auxiliary model required to use Speculative Decoding?
The draft model should ideally share the same tokenizer as the target model and be small enough to be very fast. For example, Llama 3 8B can be used as a draft for Llama 3 70B. Some variants like self-speculative decoding or Medusa eliminate the need for a separate model by reusing internal layers of the main model.

See also

How to use this prompt

  1. Copy the prompt with the button above.
  2. Paste it into ChatGPT, Claude or your favorite AI assistant.
  3. Replace the bracketed variables with your details, then refine the result.

About Prompt Guide

Prompt Guide is a free library of 2500+ ready-to-use prompts for ChatGPT, Claude and other AIs, with guides to learn prompting and tools to build and optimize your own prompts.

More definitions

Get new prompts every week

Join our newsletter.