Speculative Decoding and Speculative Sampling: Mathematical Foundations, Lossless Rejection Sampling, Draft Model Architectures, and Serving Economics

Large language models generate text autoregressively, predicting one token at a time by passing the full sequence through dozens of transformer layers. During inference at low batch sizes, this generation process is fundamentally memory-bandwidth bound rather than compute bound. Speculative decoding addresses this bottleneck by using a fast drafting mechanism to propose candidate token sequences and verifying them in parallel with the primary target model in a single forward pass. When configur

7 min
Speculative Decoding and Speculative Sampling: Mathematical Foundations, Lossless Rejection Sampling, Draft Model Architectures, and Serving Economics

Large language models generate text autoregressively, predicting one token at a time by passing the full sequence through dozens of transformer layers. During inference at low batch sizes, this generation process is fundamentally memory-bandwidth bound rather than compute bound. Speculative decoding addresses this bottleneck by using a fast drafting mechanism to propose candidate token sequences and verifying them in parallel with the primary target model in a single forward pass.

When configured with modified rejection sampling, speculative decoding is mathematically lossless: the sampled output matches the exact probability distribution of the target model while yielding 2x to 3.5x wall-clock latency reductions.

Speculative Decoding Rejection Sampling and Tree Verification Diagram

The Autoregressive Inference Bottleneck

Standard autoregressive transformer generation requires loading the complete set of model weights from high-bandwidth device memory (HBM) into SRAM or register files for every single generated token.

For a model with PP parameters executing at batch size 1 in 16-bit precision (FP16 or BF16), generating a single token requires transferring 2P2P bytes of memory. The arithmetic intensity of this single-token forward pass is:

Arithmetic Intensity = (2 * P FLOPs) / (2 * P Bytes) = 1 FLOP / Byte

Modern datacenter accelerators exhibit theoretical compute-to-bandwidth ratios between 100 and 250 FLOPs per byte. For example, an NVIDIA H100 SXM GPU provides 1,979 TFLOPs of dense FP16 tensor core compute alongside 3.35 TB/s of memory bandwidth, yielding a balance point of approximately 590 FLOPs per byte transferred. At 1 FLOP per byte, single-token generation leaves tensor cores idle for more than 98% of the execution cycle while waiting for weight retrieval from HBM.

Speculative decoding leverages the observation that scoring KK candidate tokens simultaneously requires virtually the same memory bandwidth and execution latency as scoring a single token. Because all KK tokens can be evaluated in parallel across matrix-matrix multiplication (GEMM) operations rather than sequential matrix-vector (GEMV) operations, the target model processes multiple positions in one forward pass without increasing memory transfers.

Mathematical Formulation and Lossless Rejection Sampling

Speculative decoding was independently formalized by Leviathan et al. (2022) at Google Research and Chen et al. (2023) at DeepMind. The core mechanism pairs a target model distribution P(xx<t)P(x \mid x_{<t}) with a fast draft distribution Q(xx<t)Q(x \mid x_{<t}).

The Sampling Protocol

Given a prompt prefix x<tx_{<t} and a lookahead speculation window of γ\gamma tokens:

  1. Draft Phase: The draft system autoregressively generates γ\gamma speculative tokens:

x_1, x_2, ..., x_\gamma ~ Q(x)

  1. Parallel Verification Phase: The target model executes a single parallel forward pass over the full draft sequence, computing true probability distributions P(xx<t+i1)P(x \mid x_{<t+i-1}) for all positions i{1,2,,γ+1}i \in \{1, 2, \dots, \gamma+1\}.
  2. Rejection Sampling Step: For each speculative token ii from 11 to γ\gamma:
  • Sample a uniform random variable riU(0,1)r_i \sim U(0, 1).
  • Evaluate the acceptance condition:

r_i <= min(1, P(x_i | x_{<i}) / Q(x_i | x_{<i}))

  • If accepted, the token xix_i is retained in the accepted prefix.
  • If rejected at index kγk \le \gamma, the rejection loop terminates. A corrected replacement token is sampled from the adjusted residual distribution:

P'(x) = max(0, P(x) - Q(x)) / \sum_y max(0, P(y) - Q(y))

  • All subsequent speculative tokens xk+1,,xγx_{k+1}, \dots, x_\gamma are discarded.
  1. Bonus Token Generation: If all γ\gamma draft tokens are accepted, an additional (γ+1)(\gamma+1)-th token is sampled directly from the target model distribution P(xxγ)P(x \mid x_{\le \gamma}) at zero incremental latency cost, since its logits were already computed in the parallel verification forward pass.

Proof of Distributional Equivalence

The rejection sampling scheme ensures that the generated sequence distribution under speculative decoding is identical to standard sampling from the target model.

For any candidate token xx, the probability of emission under speculative sampling Pspec(x)P_{\text{spec}}(x) is the sum of two mutually exclusive events: acceptance through the draft branch and recovery through the rejection branch:

P_spec(x) = Q(x) * min(1, P(x) / Q(x)) + (1 - \alpha) * P'(x)

Here, α\alpha denotes the total probability of draft acceptance:

\alpha = \sum_y Q(y) * min(1, P(y) / Q(y)) = \sum_y min(P(y), Q(y))

The recovery distribution P(x)P'(x) evaluates to:

P'(x) = (P(x) - min(P(x), Q(x))) / (1 - \alpha) = max(0, P(x) - Q(x)) / (1 - \alpha)

Substituting P(x)P'(x) and α\alpha back into the emission equation:

P_spec(x) = min(P(x), Q(x)) + (1 - \alpha) * [ (P(x) - min(P(x), Q(x))) / (1 - \alpha) ] P_spec(x) = min(P(x), Q(x)) + P(x) - min(P(x), Q(x)) P_spec(x) = P(x)

The resulting token distribution exactly equals the target model distribution P(x)P(x) across all vocabulary elements without approximation error or distributional drift.

Acceptance Rate and Latency Speedup Mechanics

The acceptance probability at each step is directly governed by the statistical proximity between draft and target distributions:

\alpha = 1 - D_TV(P, Q) = 1 - (1/2) \sum_x |P(x) - Q(x)|

where DTV(P,Q)D_{\text{TV}}(P, Q) represents the total variation distance between the two distributions.

Assuming independent and identically distributed acceptance across positions with average acceptance rate α\alpha, the expected number of accepted tokens E[N]\mathbb{E}[N] per verification round with lookahead γ\gamma is:

\mathbb{E}[N] = (1 - \alpha^{\gamma+1}) / (1 - \alpha)

If α=0.8\alpha = 0.8 and γ=5\gamma = 5, the expected token yield per target forward pass is 3.67 tokens. If α=0.9\alpha = 0.9, the expected yield rises to 4.69 tokens.

Wall-Clock Speedup Factor

Let ttargett_{\text{target}} be the execution time of one forward pass of the target model, and tdraftt_{\text{draft}} be the execution time of one forward pass of the draft model. Defining the relative cost ratio c=tdraft/ttargetc = t_{\text{draft}} / t_{\text{target}}, the theoretical wall-clock speedup SS is given by:

S = \mathbb{E}[N] / (\gamma * c + 1)

Achieving net wall-clock acceleration requires two conditions:

  1. High acceptance rate: The draft model must closely align with the target distribution, keeping α>0.65\alpha > 0.65.
  2. Low draft overhead: The per-token drafting cost cc must remain small, typically c0.05c \le 0.05 to 0.100.10.

If c=0.05c = 0.05, γ=5\gamma = 5, and α=0.8\alpha = 0.8, the expected speedup is:

S = 3.67 / (5 * 0.05 + 1) = 3.67 / 1.25 = 2.94x

Draft Architectures and Taxonomy

Deployments employ four primary architectural paradigms for draft generation:

1. Independent Small Draft Models

A smaller autoregressive model (such as a 1B to 7B parameter model drafting for a 70B parameter target) generates the draft sequence. Both models must share an identical tokenizer and vocabulary mapping to avoid token fragmentation misalignment.

  • Advantages: Completely independent architectures; no modifications to the target model.
  • Trade-offs: Consumes separate GPU memory for draft weights and KV-cache; cross-model distribution shifts can reduce α\alpha on out-of-distribution prompts.

2. Multi-Head Speculation (Medusa)

Introduced by Cai et al. (2024), Medusa eliminates the separate draft model by appending multiple parameter-efficient feedforward heads directly on top of the final transformer layer of the target model.

  • Head Architecture: Head kk predicts the token distribution at offset t+kt+k given the current hidden state hth_t.
  • Tree-Based Attention: Medusa generates a tree of candidate tokens across top-kk predictions from each head. A custom tree attention mask evaluates all candidate paths concurrently in a single forward verification pass.
  • Advantages: Zero additional model loading; simple single-engine deployment; no vocabulary alignment issues.

3. Feature-Level Autoregressive Drafting (EAGLE and EAGLE-2)

Proposed by Li et al. (2024), EAGLE generates speculative tokens by autoregressively predicting the target model's top-layer hidden feature representations rather than token distributions directly.

  • Mechanism: A lightweight single-layer transformer ingests the target model's second-to-top hidden state combined with shifted token embeddings, capturing contextual representations with lower semantic variance.
  • Performance: EAGLE achieves higher acceptance rates (α>0.85\alpha > 0.85 across standard benchmarks) than token-level drafting, delivering 2.5x to 3.5x speedups on instruction-tuned models.

4. Non-Parametric N-Gram and Prompt Lookup Decoding

For structured, repetitive, or retrieval-heavy generation (such as code editing, JSON extraction, and RAG continuations), candidates can be drawn directly from the input prompt or previously generated text via exact n-gram matching without any neural drafting model.

  • Mechanism: The engine searches for matching sequence prefixes in the prompt context and speculates the following KK tokens.
  • Overhead: Near-zero FLOP overhead; highly effective when outputs heavily quote input context.

System Implementation and KV-Cache Dynamics

Integrating speculative decoding into production inference engines (such as vLLM, TensorRT-LLM, and SGLang) requires dedicated memory management mechanics.

Rollback Management in Linear Speculation

When the target model rejects token kk during linear speculation, the KV-cache entries corresponding to positions k+1γk+1 \dots \gamma must be invalidated. In paged memory architectures, the engine truncates the logical sequence length and returns unused physical KV blocks to the free pool without reallocating active sequence memory.

Tree Attention Masking

When using tree speculation (e.g., Medusa or EAGLE), candidates branch into a directed tree of possible continuations. Instead of running separate verification passes for each branch, engines construct a 2D causal tree attention mask:

Candidate Tree:
       [Root: A]
       /        \
   [B1]          [B2]
   /  \          /  \
[C1]  [C2]    [C3]  [C4]

The tree attention mask permits token C1C_1 to attend only to its causal ancestors (Root AA and parent B1B_1), preventing cross-branch attention leakage while computing logits for all 7 candidate paths in one unified GEMM kernel.

Serving Economics and Batching Trade-Offs

Speculative decoding provides dramatic acceleration under specific serving regimes, but introduces distinct computational trade-offs:

The Batch Size Crossover

Speculative decoding is optimized for memory-bandwidth bound execution (batch sizes 1 through 8). As concurrent batch sizes increase beyond 32 or 64 sequences, aggregate matrix operations transition from memory-bound GEMVs to compute-bound GEMMs.

In compute-bound regimes:

  • Hardware tensor cores are already fully utilized.
  • Verifying γ\gamma tokens requires γ\gamma times more FLOPs, increasing target forward pass latency.
  • Draft model execution competes with the target model for tensor core compute and GPU memory bandwidth.
  • Net throughput (tokens per second per GPU) plateaus or degrades relative to non-speculative continuous batching.

Production Deployment Guidelines

  • Interactive Chat and Coding: Highly effective. At batch size 1 to 4, time-per-output-token (TPOT) drops by 50% to 70%, improving user-perceived responsiveness.
  • High-Throughput Offline Batching: Ineffective. Standard continuous batching without speculation achieves higher overall token throughput per dollar by dedicating all compute capacity to serving concurrent requests.
  • Agentic Loops and Reasoning Chains: Highly effective. Extended test-time reasoning traces and multi-step tool calls generate thousands of sequential tokens per request where single-stream latency is the primary operational bottleneck.

Sources

Written by

More to read

  • Group Relative Policy Optimization (GRPO): Mathematical Foundations, Critic-Free Advantage Estimation, Group Reward Normalization, and Reinforcement Learning with Verifiable Rewards

    Group Relative Policy Optimization (GRPO): Mathematical Foundations, Critic-Free Advantage Estimation, Group Reward Normalization, and Reinforcement Learning with Verifiable Rewards Reinforcement learning from human and verifiable feedback has become the central paradigm for unlocking complex reasoning, mathematical problem solving, and autonomous code synthesis in frontier large language models. While early post-training pipelines relied heavily on Proximal Policy Optimization (PPO) or offline

    1 min
  • OpenAI Tests Persistent Mode in Codex for Long-Running Autonomous AI Agents

    OpenAI is testing an execution profile termed "Persistent Mode" within its Codex agent codebase, designed to enable continuous, self-directed task execution without standard step-count timeouts or per-turn pauses. Code commits surfaced in the public repository of the Codex command-line interface indicate that the agent can proactively generate follow-up tasks, maintain state across development sessions, and continue working autonomously until explicitly halted by the user. Architecture and Re

    1 min
  • Federal Judge Overturns Pentagon Supply Chain Risk Blacklisting of Anthropic

    A federal court has permanently blocked the Department of Defense from enforcing a supply chain risk designation against artificial intelligence developer Anthropic, ruling that the Pentagon's blacklisting violated the First and Fifth Amendments of the U.S. Constitution. U.S. District Judge Rita Lin of the Northern District of California issued a 59-page decision overturning Defense Secretary Pete Hegseth's February 2026 classification. The ruling makes permanent an injunction granted earlier t

    1 min