Speculative Decoding Math: Algorithms & Speedup Limits › Optimal Draft Length

Michael BrenndoerferJanuary 17, 20266 min read
Language AI HandbookAll chapters
Part 42 of 67Inference Optimization
Chapter 324Speculative Decoding Math: Algorithms & Speedup Limits › Optimal Draft Length
Reading section 6 of 11 · Optimal Draft Length · 6 minSection 6 of 11 · 6 min

Book contents

Section 6 of 11 · 6 min read

Speculative Decoding Math: Algorithms & Speedup Limits › Optimal Draft LengthOne section · Sections in this chapter6 / 11 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Choosing the draft length kk involves balancing the cost of generating more drafts against the diminishing probability of accepting them all. This optimization problem has both theoretical and practical importance. In theory, it determines the maximum achievable speedup for a given model pair and hardware configuration. In practice, it guides the single most important configuration choice when deploying speculative decoding.

The optimal draft length is not fixed. It depends on the acceptance probability α\alpha (which varies by task and context), the cost ratio cc (which depends on hardware and model sizes), and potentially on dynamic factors like recent acceptance rate history. Understanding these dependencies allows you to make good static choices and design adaptive strategies that respond to changing conditions.

The Optimization Problem

We want to find k∗k^* that maximizes speedup. Substituting our formula for E[N]\mathbb{E}[N], the optimization problem is:

k∗=arg⁡max⁡kE[Nk]1+k/c(maximize speedup)=arg⁡max⁡k(1−αk+1)/(1−α)1+k/c(substitute E[Nk])\begin{aligned} k^* &= \arg\max_k \frac{\mathbb{E}[N_k]}{1 + k/c} && \text{(maximize speedup)} \\ &= \arg\max_k \frac{(1 - \alpha^{k+1})/(1 - \alpha)}{1 + k/c} && \text{(substitute } \mathbb{E}[N_k] \text{)} \end{aligned}

where:

  • k∗k^*: optimal draft length
  • E[Nk]\mathbb{E}[N_k]: expected tokens generated with draft length kk
  • cc: cost ratio

Taking the derivative with respect to kk and setting to zero is analytically intractable because kk is an integer, but we can analyze the continuous relaxation and then round to find the optimal integer value. The continuous analysis reveals the structure of the problem and shows why an interior optimum always exists for finite cc.

Diminishing Returns

The expected tokens E[Nk]\mathbb{E}[N_k] grows sublinearly in kk. This can be seen from the geometric series formula:

E[Nk]=1−αk+11−α≈11−α as k→∞\mathbb{E}[N_k] = \frac{1 - \alpha^{k+1}}{1 - \alpha} \approx \frac{1}{1-\alpha} \text{ as } k \to \infty

where:

  • E[Nk]\mathbb{E}[N_k]: expected tokens for draft length kk
  • 11−α\frac{1}{1-\alpha}: asymptotic limit of expected tokens as kk grows without bound
  • α<1\alpha < 1: acceptance probability

The growth rate decreases exponentially because αk\alpha^k decays exponentially. Meanwhile, the cost 1+k/c1 + k/c grows linearly in kk. This tension creates an interior optimum. Intuitively, each additional draft token has a diminishing marginal benefit (it is only useful if all previous tokens were accepted, which becomes increasingly unlikely) but a constant marginal cost (one more draft model forward pass). At some point, the marginal cost exceeds the marginal benefit, and we should stop drafting.

Think of it as a classic diminishing returns problem. The first draft token you add is very valuable because there is a high probability it gets accepted (α\alpha). The second draft token is somewhat less valuable because it only contributes when the first was also accepted (α2\alpha^2 probability of usefulness). The tenth draft token only contributes when all nine preceding tokens were accepted (α10\alpha^{10} probability), which for α=0.8\alpha = 0.8 is only about 10.7%. At some point, the additional draft tokens contribute so rarely that running the draft model to generate them costs more than it saves.

Approximate Optimal kk

For large cc (fast draft model), we can derive an approximate optimal draft length. When c→∞c \to \infty, the optimal kk is infinite since there's no cost to drafting more tokens. For finite cc, the optimal kk satisfies the first-order condition from setting the derivative of the speedup function to zero. Treating kk as continuous and applying the quotient rule:

ddk(1−αk+11+k/c)=−αk+1ln⁡(α)⋅(1+k/c)−(1−αk+1)⋅(1/c)(1+k/c)2(quotient rule)=0(set derivative to zero)\begin{aligned} \frac{d}{dk} \left( \frac{1 - \alpha^{k+1}}{1 + k/c} \right) &= \frac{-\alpha^{k+1} \ln(\alpha) \cdot (1 + k/c) - (1 - \alpha^{k+1}) \cdot (1/c)}{(1 + k/c)^2} && \text{(quotient rule)} \\ &= 0 && \text{(set derivative to zero)} \end{aligned}

where:

  • α\alpha: acceptance probability
  • kk: draft length
  • cc: cost ratio
  • −αk+1ln⁡(α)-\alpha^{k+1} \ln(\alpha): derivative of the numerator term (1−αk+1)(1 - \alpha^{k+1}) (positive since ln⁡(α)<0\ln(\alpha) < 0 for α<1\alpha < 1)
  • 1/c1/c: derivative of the denominator term (1+k/c)(1 + k/c)
  • (1+k/c)2(1 + k/c)^2: squared denominator from the quotient rule

Setting the numerator to zero gives:

−αk+1ln⁡(α)=1−αk+1c(1+k/c)-\alpha^{k+1} \ln(\alpha) = \frac{1 - \alpha^{k+1}}{c(1 + k/c)}

where:

  • α\alpha: acceptance probability
  • kk: draft length
  • cc: cost ratio
  • −αk+1ln⁡(α)-\alpha^{k+1} \ln(\alpha): marginal gain from extending the draft by one position
  • Right side: normalized marginal cost of extension

This equation balances marginal benefit against marginal cost. The left side represents the expected additional tokens from extending the draft by one position (how often does the (k+1)(k+1)th token get accepted, and how much does that help). The right side represents the cost of that extension in terms of reduced speedup per existing token. While this equation cannot be solved in closed form, it can be solved numerically for any specific values of α\alpha and cc.

For typical values (α≈0.7\alpha \approx 0.7, c≈10c \approx 10), optimal kk ranges from 4 to 8 tokens. This explains why practical systems typically use draft lengths in this range. The formula predicts that using k=5k=5 is close to optimal for many common configurations, which is why it has become a common default setting in speculative decoding implementations.

Adaptive Draft Length

In practice, the optimal kk varies based on context. Different parts of a response have different acceptance rates: the beginning of a sentence is often more predictable than the middle, and common phrases have much higher acceptance rates than rare ones. Systems that use a fixed kk throughout generation leave performance on the table because they cannot adjust to these variations.

Practical adaptive strategies include:

  • Fixed kk: Simple to implement, works well when acceptance rates are stable. A good default for production systems where simplicity matters.
  • Adaptive kk: Adjust based on recent acceptance rates. If the last several iterations had high acceptance rates, increase kk; if rates were low, decrease kk. This can improve average throughput by 10-20% over fixed kk in practice.
  • Tree-based drafting: Generate multiple candidate continuations as a tree rather than a single sequence, increasing effective acceptance probability. When the draft model is uncertain about the best next token, it can propose several alternatives, and the target model accepts the best match. This approach trades increased memory usage for higher effective acceptance rates.

The adaptive approach monitors acceptance rates during generation and increases kk when rates are high (showing the draft model is performing well on the current content) or decreases kk when rates are low (showing more challenging content where longer drafts would be wasteful). Implementing this correctly requires careful bookkeeping to avoid introducing biases from the adaptation process itself, but when done correctly, it maintains the exact-sampling guarantee while improving average throughput.