---
title: 'OnlineSpec: Adaptive Speculative Decoding'
url: https://www.emergentmind.com/topics/onlinespec
type: topic
---

# OnlineSpec: Adaptive Speculative Decoding

Searching arXiv for the main paper and key related speculative decoding works.

Search query: 2603.12617

Search query: speculative decoding Leviathan 2023

Search query: Hydra speculative decoding

Search query: EAGLE speculative decoding

OnlineSpec is a unified framework for speculative decoding that systematically leverages interactive feedback to continuously evolve draft models during deployment. In speculative decoding, a small draft model rapidly proposes a block of $k$ tokens that are then verified in parallel by a larger target model; OnlineSpec treats the resulting verification signal as an online learning loop—draft commits, target verifies, feedback is observed, draft adapts—and formalizes how online learning performance, especially dynamic regret, controls acceptance length and acceleration. The framework develops algorithms based on optimistic online learning and online ensemble learning, and reports up to $24\%$ additional speedup across seven benchmarks and three foundation models [2603.12617].

## 1. Speculative decoding setting and the OnlineSpec problem

OnlineSpec is defined in the setting where the target predictive distribution at context $x$ is denoted by $p_v(\cdot \mid x)$ and the draft predictive distribution by $q_{\theta}(\cdot \mid x)$. At each step $t$, the draft proposes a block of $k$ tokens $x_1,\ldots,x_k$ via autoregression under $q_{\theta_t}$, while the target computes in parallel $p_v(x_i \mid x_{<i})$. The standard acceptance test accepts token $x_i$ if a uniform draw $r_i \le p_v(x_i\mid x_{<i}) / q_{\theta_t}(x_i\mid x_{<i})$, otherwise it rejects at position $i$. If $L_t$ is the number of consecutive draft tokens accepted at step $t$, then $L_t \in \{0,1,\ldots,k\}$ [2603.12617].

A central quantity is the per-token acceptance probability
$$
p_{\mathrm{acc},t}
=
\mathbb{E}_{x\sim q_{\theta_t}(\cdot\mid x)}
\left[\min\left\{1,\frac{p_v(x\mid x)}{q_{\theta_t}(x\mid x)}\right\}\right].
$$
Using $\min(a,b)=\tfrac12(a+b-|a-b|)$ and the fact that both distributions sum to $1$, the framework uses the identity
$$
p_{\mathrm{acc},t}=1-\mathrm{TV}(p_v(\cdot\mid x),q_{\theta_t}(\cdot\mid x)),
$$
where $\mathrm{TV}$ is total variation distance. Under mild i.i.d. assumptions across draft positions $i$ at step $t$, the expected acceptance length $\mathbb{E}[L_t]$ is a monotone function of $p_{\mathrm{acc},t}$. A common closed form for truncated geometric acceptance is
$$
\mathbb{E}[L_t]
=
\sum_{j=1}^k \Pr(L_t\ge j)
=
\sum_{j=1}^k p_{\mathrm{acc},t}^j
=
\frac{p_{\mathrm{acc},t}(1-p_{\mathrm{acc},t}^k)}{1-p_{\mathrm{acc},t}},
$$
up to an index shift variant used in prior work; all such forms increase with $p_{\mathrm{acc},t}$ and $k$.

The acceleration metric is expressed in wall-clock terms. Let $A$ be the time per target forward pass and $a$ the time per draft per token, with $\alpha=a/A \in (0,1)$. Generating a block costs $A+ak$, while baseline autoregressive generation costs $A$ per token. Over $T$ steps, if the total number of accepted tokens is $|x|=\sum_t L_t$, then the speedup is
$$
S
=
\frac{A\cdot \mathbb{E}[|x|]}{T(A+ak)}
=
\frac{\mathbb{E}[|x|]}{T(\alpha k+1)},
$$
with upper bound
$$
S \le \frac{k}{\alpha k+1}.
$$
This formulation makes long acceptance runs structurally necessary: short runs collapse speedup, whereas long runs are required to approach the maximal factor $k$.

## 2. Verification feedback as online learning

OnlineSpec’s key observation is that verification intrinsically produces, at no extra target calls, a rich feedback signal that pinpoints draft–target discrepancies. The framework casts each decoding step as an online round. At round $t$, the learner picks $\theta_t$ and induces $q_{\theta_t}(\cdot\mid\cdot)$; the environment reveals feedback via verification, which defines a loss $\ell_t(\theta)$ measuring $q_\theta$’s deviation from $p_v$ at the contexts encountered at round $t$; and the learner updates $\theta_{t+1}$ [2603.12617].

A natural full-information loss is the cross-entropy of $q_\theta$ against the target,
$$
\ell_t(\theta)
=
-
\mathbb{E}_{x\sim p_v(\cdot\mid x_t)}
[\log q_\theta(x\mid x_t)],
$$
where $x_t$ is the relevant context distribution induced by verification at round $t$. If $\theta_t^*$ is an oracle per-round comparator minimizing $\ell_t$, then
$$
\ell_t(\theta_t)-\ell_t(\theta_t^*)
=
\mathrm{KL}(p_v(\cdot\mid x_t)\,\|\,q_{\theta_t}(\cdot\mid x_t)).
$$
By Pinsker’s inequality, $\mathrm{TV}^2 \le \mathrm{KL}/2$, so the framework obtains
$$
p_{\mathrm{acc},t}
=
1-\mathrm{TV}(\cdot)
\ge
1-\sqrt{\mathrm{KL}_t/2}.
$$
Lowering per-round $\mathrm{KL}$ therefore directly raises $p_{\mathrm{acc},t}$ and $\mathbb{E}[L_t]$.

The online objective is dynamic regret,
$$
R_T^{\mathrm{dyn}}
=
\sum_{t=1}^T \ell_t(\theta_t)
-
\sum_{t=1}^T \ell_t(\theta_t^*).
$$
Lower dynamic regret means that the learner tracks the best time-varying draft $\theta_t^*$ for each round. OnlineSpec then connects this quantity to acceptance and speed: as dynamic regret decreases, aggregate acceptance length increases, and acceleration improves. This suggests that speculative decoding is not merely a verification procedure but a deployment-time adaptation process in which verification feedback is already the supervision signal needed for continual draft improvement.

## 3. Algorithms and operational loop

OnlineSpec develops two principal algorithmic families. The first is optimistic online learning, denoted Opt. Optimism injects predictive hints about upcoming gradients to adapt faster when gradients are predictable. If $g_t=\nabla \ell_t(\theta_t)$ is obtained from verification at round $t$, then a simple hint is $h_{t+1}=g_t$; more generally, one may use an exponential moving average of past gradients, or task-specific predictors. Two equivalent update forms are described. Optimistic Mirror Descent uses
$$
\tilde{\theta}_{t+1}
=
\arg\min_{\theta}
\{\langle h_{t+1},\theta\rangle + D_R(\theta,\theta_t)\},
$$
$$
\theta_{t+1}
=
\arg\min_{\theta}
\{\langle g_t,\theta\rangle + D_R(\theta,\tilde{\theta}_{t+1})\},
$$
where $D_R$ is a Bregman divergence for a $1$-strongly convex regularizer $R$. The Euclidean projected optimistic step is
$$
\tilde{\theta}_{t+1}=\Pi_W(\theta_t-\eta h_{t+1}),
\qquad
\theta_{t+1}=\Pi_W(\tilde{\theta}_{t+1}-\eta g_t).
$$
If hints are accurate, with small $\sum_t \|h_{t+1}-g_t\|^2$, regret scales with the hint error rather than $T$, leading to better dependence on non-stationarity and longer acceptance runs [2603.12617].

The second family is online ensemble learning, denoted Ens. Ensembles hedge against unknown and time-varying environments. The framework maintains $M$ draft models $\{d_i\}$ with parameters $\{\theta_t^{(i)}\}$ and diverse step sizes or inductive biases. Each base updates via online gradient descent,
$$
\theta_{t+1}^{(i)}
=
\Pi_W(\theta_t^{(i)}-\eta_i g_t^{(i)}).
$$
Mixture weights $w_t\in\Delta^M$ are updated by Hedge or Exponentiated Gradient,
$$
w_{t+1,i}\propto w_{t,i}\exp(-\eta_{\mathrm{meta}}\ell_t(\theta_t^{(i)})),
$$
followed by normalization. The ensemble can be used as a mixture draft,
$$
q_{\mathrm{mix}}(\cdot\mid x)=\sum_i w_{t,i} q_{\theta_t^{(i)}}(\cdot\mid x),
$$
or through multi-branch drafting, where a small number of candidates from top-weighted drafters are sampled, batch-verified with the target, and the longest accepted branch is selected. The latter is described as compatible with Medusa/Hydra/EAGLE-style parallel trees.

The practical loop is explicit. At each round, the current draft autoregressively samples up to $k$ tokens. A single target forward then yields $p_v(x_i\mid x_{<i})$ for $i=1,\ldots,k$ in batched form. Consecutive tokens are accepted until the first failure; if rejection occurs at position $j$, the next token is resampled from the target as in standard speculative decoding. From the verification pass, the target logits are already available, so the framework computes a per-round loss and gradients without extra target calls. The loss can be token-level cross-entropy, a KL divergence estimate via logit matching, or, for reasoning, a preference-based or DPO loss that replaces token-level supervision with pairwise step preference. The learner then performs OGD, optimistic, or ensemble updates and proceeds to the next block.

## 4. Acceptance–regret theory and acceleration bounds

The theoretical program of OnlineSpec is to make the acceptance–regret link explicit under standard online convex optimization conditions: a bounded domain, bounded gradients, full-information feedback from verification, and the i.i.d.-across-positions simplification used in prior speculative-decoding analyses [2603.12617].

The framework’s key identities are:
$$
p_{\mathrm{acc},t}
=
\mathbb{E}\!\left[\min\left\{1,\frac{p_v}{q_{\theta_t}}\right\}\right]
=
1-\mathrm{TV}(p_v,q_{\theta_t}),
$$
$$
\ell_t(\theta_t)-\ell_t(\theta_t^*)
=
\mathrm{KL}(p_v\|q_{\theta_t}),
$$
and
$$
\mathrm{TV}(p_v,q_{\theta_t})\le \sqrt{\mathrm{KL}/2}.
$$
Hence
$$
p_{\mathrm{acc},t}\ge 1-\sqrt{\mathrm{KL}/2},
$$
and $\mathbb{E}[L_t]$ increases as $\mathrm{KL}$ decreases. Substituting this lower bound into the truncated geometric formula and aggregating across rounds yields a lower bound of the form
$$
\mathbb{E}[|x|]
=
\sum_{t=1}^T \mathbb{E}[L_t]
\ge
f(k,T)-g(k,T)\cdot \Phi(R_T^{\mathrm{dyn}}),
$$
for explicit $f$, $g$, and $\Phi$. Intuitively, as $R_T^{\mathrm{dyn}}/T \to 0$, the aggregate acceptance length approaches the ideal $kT$.

The acceleration theorem states that
$$
S=\frac{\mathbb{E}[|x|]}{T(\alpha k+1)},
\qquad
S\le \frac{k}{\alpha k+1}.
$$
Moreover, as the dynamic regret $R_T^{\mathrm{dyn}}$ decreases sublinearly, $R_T^{\mathrm{dyn}}=o(T)$, the acceptance length $\mathbb{E}[|x|]$ increases, and $S$ improves. The paper then provides algorithm-specific regret and speedup translations, with bounds holding up to logarithmic factors and constants suppressed.

For online gradient descent with $\eta=\Theta(1/\sqrt{T})$,
$$
R_T^{\mathrm{dyn}}
\le
O(\sqrt{T}(1+P_T)),
$$
where
$$
P_T=\sum_{t=1}^{T-1}\|\theta_{t+1}^*-\theta_t^*\|
$$
is the path-length of the moving comparator sequence. The resulting speedup lower bound is
$$
S
\ge
c\cdot (1-1/k)\cdot T^{1/4}
\big/
[(\alpha k+1)(1+P_T)^{1/4}].
$$

For optimistic online learning with hint error
$$
\delta_T=\sum_t \|h_{t+1}-\nabla \ell_t(\theta_t)\|^2,
$$
the regret bound becomes
$$
R_T^{\mathrm{dyn}}
\le
O(\sqrt{1+\delta_T}\cdot (1+P_T)),
$$
and the speedup lower bound becomes
$$
S
\ge
c\cdot (1-1/k)\cdot T^{1/4}
\big/
[(\alpha k+1)(1+\delta_T)^{1/4}(1+P_T)^{1/4}].
$$
Accurate hints therefore tighten regret and yield higher $S$.

For the online ensemble method, with $N=O(\log T)$ base drafters with geometrically spaced step sizes and a Hedge meta-learner,
$$
R_T^{\mathrm{dyn}}
\le
O(\sqrt{T}(1+P_T)),
$$
with a similar speedup lower bound and strong adaptivity when $P_T$ is large. The qualitative message is precise: stability, good hints, or ensembles reduce dynamic regret; smaller dynamic regret raises acceptance probability, accepted length, and thus speedup.

## 5. Implementation, compute profile, and deployment behavior

OnlineSpec is designed so that online adaptation does not change the basic speculative decoding systems picture. Per round, the target cost is one forward pass with $k$ parallel positions, with cost $A$. Drafting costs $ak$, where $a\ll A$ and $\alpha=a/A$. Gradients are computed from target logits already available from verification, so the online update requires no extra target calls. Draft updates are small backprop steps on the draft parameters, or on head-only adapters, and are typically amortized asynchronously on separate devices. The latency-critical path remains dominated by $A+ak$ [2603.12617].

The implementation details are correspondingly lightweight. Token-level cross-entropy or KL is computed from the target logits produced during verification, with no target modification or extra queries. For reasoning tasks, the paper uses a DPO-style loss
$$
\ell_t(\theta)
=
-\log \sigma\!\left(\beta\,[L_\theta(y^w\mid x)-L_\theta(y^\ell\mid x)]\right),
$$
where $(x,y^w,y^\ell)$ is a preferred/dispreferred pair from semantic verification and $L_\theta$ is log-likelihood ratio against a reference policy. For Online-LR, AdamW with small learning rate is used; for EAGLE-style heads, Adam; for Hydra heads, SGD with momentum. Gradient clipping and mixed precision, specifically bf16, are used, with chunked streaming evaluation such as $T\approx 40$–$80$ and periodic updates.

The ensemble implementation maintains $N\in\{3,\ldots,O(\log T)\}$ base drafters with geometrically spaced $\eta$, updates meta-weights from per-round losses, and combines them by mixture or branch selection. Base learners train in parallel. On the systems side, KV cache reuse is unchanged; a single target pass is retained per block; updates run asynchronously on spare GPUs; and periodic parameter synchronization preserves inference-time efficiency. The experiments use $4\times$A800 80GB, large CPU memory, and FlashAttention for efficient attention.

A plausible implication is that OnlineSpec’s deployment profile is intentionally conservative: it does not require extra target evaluation, does not alter the target model, and places most adaptation overhead on small draft-side optimization. This is why the framework is presented as deployment-friendly rather than as a retraining-heavy alternative to speculative decoding.

## 6. Empirical results, scope, and limitations

The empirical study spans seven datasets covering math reasoning, code generation, and finance question answering: GSM8K, MATH, the math subset of MMLU, CodeSearch-Python, Spider, MBPP, and Alpaca-finance. The target models are Vicuna-7B, Llama-2-7B-Chat, and Qwen3-8B. Across these settings, OnlineSpec instantiations consistently beat static speculative decoding and naive online baselines such as OSD, with speedups up to $24\%$ wall-clock over prior SOTA and clear gains in average accepted length [2603.12617].

The reported task-specific instantiations are meant to show compatibility with existing speculative systems. Opt-Hydra improves over Hydra and OSD-Hydra across tasks. Ens-EAGLE and Ens-EAGLE-3 improve over EAGLE and OSD-EAGLE or OSD-EAGLE-3. Online-LR, which uses DPO-style online updates for lookahead reasoning, outperforms offline LR and naive OSD-LR. These results are paired with ablations on learning rates, hint quality, and online window sizes. Fixed $\eta$ exhibits the expected tradeoff between slow and unstable behavior and does not match Opt-Hydra or Ens-EAGLE, which is presented as validation of optimism and ensembles. Better gradient predictability lowers $\delta_T$ and improves speed. Longer online adaptation raises speed monotonically, matching the theory that $S$ improves as regret per round shrinks.

The framework also identifies several limitations. Aggressive online updates can overfit recent feedback and shorten acceptance later in the same sequence; learning-rate control and optimism or ensemble smoothing mitigate but do not eliminate this. Per-deployment adaptation may forget older domains under cross-sequence distribution shift, making meta-learning and replay buffers promising. Feeding target logits into online training raises privacy considerations; secure logging and on-device learning are described as options. Compatibility with beam search and top-$k$/top-$p$ sampling is good in principle, but acceptance formulas and per-token independence approximations should be revisited under heavy sampling truncation. Server batching may couple different users’ online loops, so per-tenant adapters or weight partitioning can help. The paper further points to combining optimism and ensembles as an open engineering problem, and to bandit extensions when only partial feedback is available, such as acceptance length but not full logits.

In this formulation, OnlineSpec is neither a new decoding criterion nor a replacement for speculative decoding’s lossless verification semantics. It is a theory-backed adaptive layer on top of the draft–verify architecture: verification provides free, informative feedback about draft–target discrepancy; online learning converts that feedback into draft evolution; and smaller dynamic regret is translated into higher acceptance probability, longer accepted prefixes, and improved acceleration.

Source: https://www.emergentmind.com/topics/onlinespec