Language models, retrieval, prompting, and language adaptation · Reasoning, decoding, and discrete diffusion language · LNG075 · Algorithm
Speculative decoding
Accelerates autoregressive generation by letting a cheaper draft model propose tokens and a target model verify them in batches. Correct acceptance and correction rules can preserve the target sampling distribution.
In CodeSplash
No method yet, but the reference's example runs here as a notebook.
Do it in CodeSplash Open the reference's example as a Python or R notebook.
- Availability
- Runs as a notebook
- Code example
- Complete calculation
- Who ran it
- CodeSplash's own run
- CodeSplash's run
- The reference's example ran in Python and R here, and they agree.
- The reference's record
- Python: The chapter's authors report running it; there is no record for this entry alone. R: Reviewed but not run: R wasn't available to the reference's authors.
Purpose
Accelerates autoregressive generation by letting a cheaper draft model propose tokens and a target model verify them in batches. Correct acceptance and correction rules can preserve the target sampling distribution.
Question / objective
Can draft proposals accelerate target sampling without changing its intended distribution?
Architecture / computational structure
A cheap draft model proposes tokens and a target model verifies them with rejection correction. Both can be frozen; proposal length and scheduling are inference controls, while optional draft training is separate.
Assumptions
Incorrect residual handling or inconsistent temperature/top-p transformations biases output.
Formula / computational definition
For a draft token x~q, accept with probability min(1,p(x)/q(x)); after rejection, sample from normalized positive residual max(p-q,0). Multi-token algorithms also manage accepted prefixes and target verification carefully.
Parameters and notation
Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Draft model, proposal length, target/draft tokenization compatibility, batch size, sampling transforms, and hardware scheduling.
Fitting, fine-tuning, and adaptation
No target fine-tuning is required. If adapting a draft, train it to match the target's deployment domain/distribution and validate acceptance plus end-to-end latency. Preserve exact acceptance/correction rules and tokenization; a higher acceptance rate alone does not establish faster or distribution-correct generation.
How to use and calibrate
Verify distributional equivalence on small test problems, acceptance rate, tokens per second, latency distribution, and memory under realistic workloads. Use development data for settings and an independent test set for the final estimate. Report uncertainty across examples or seeds when the evaluation design supports it; model scores and self-assessments are not automatically calibrated probabilities of correctness.
Example application
Reduce generation latency while retaining a target model's sampling behavior.
Interpretation / pitfalls
Watch out
A poor draft or unfavorable hardware can eliminate speedups; exactness applies to the implemented distributions and algorithm, not arbitrary shortcut schemes. The code below isolates one stated component; successful execution does not validate model quality or reproduce the cited experiment.
Python and R code examples
Full implementation route: Use documented assisted-generation support when compatible, or implement the verified algorithm carefully. R generally bridges the target/draft inference backend. The native example calculates acceptance and residual probabilities for one token step; it is not a benchmark of real model speed.
Example scope: Exact one-step speculative acceptance and rejection residual on a finite vocabulary.
Dependencies: Python 3 standard library; base R; shared setup above. No model downloads or external services.
Validation: Python component executed in the combined example script; R counterpart supplied but not runtime-tested here. All numbers are synthetic.
Python and R
The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.
Complete calculation — Exact one-step speculative acceptance and rejection residual on a finite vocabulary.
Shared setup for this chapter
import math
def softmax(xs):
m = max(xs)
es = [math.exp(x-m) for x in xs]
return [e/sum(es) for e in es]
def dot(a, b):
return sum(x*y for x, y in zip(a, b))
def matvec(matrix, vector):
return [dot(row, vector) for row in matrix]
def meanvec(rows):
return [sum(row[j] for row in rows)/len(rows) for j in range(len(rows[0]))]
def sigmoid(x):
return 1/(1+math.exp(-x))
def nll(probabilities):
return -sum(math.log(p) for p in probabilities)
p,q = [0.5,0.3,0.2],[0.3,0.4,0.3]
accept = [min(1,a/b) for a,b in zip(p,q)]
residual = [max(a-b,0) for a,b in zip(p,q)]
residual = [x/sum(residual) for x in residual]
recovered = [q[i]*accept[i]+sum(q[j]*(1-accept[j]) for j in range(3))*residual[i] for i in range(3)]
assert max(abs(a-b) for a,b in zip(p,recovered)) < 1e-12
print(accept,residual,recovered)
Complete calculation — Exact one-step speculative acceptance and rejection residual on a finite vocabulary.
Shared setup for this chapter
softmax <- function(x) {
shifted <- exp(x-max(x))
shifted/sum(shifted)
}
p <- c(0.5,0.3,0.2); q <- c(0.3,0.4,0.3)
accept <- pmin(1,p/q)
residual <- pmax(p-q,0); residual <- residual/sum(residual)
recovered <- q*accept+sum(q*(1-accept))*residual
stopifnot(max(abs(p-recovered)) < 1e-12)
print(list(accept=accept,residual=residual,recovered=recovered))
Sources
- Source 1arxiv.org
From the CodeSplash reference, entry LNG075.