Lookahead decoding¶
At a glance
Lookahead decoding: n-grams from a Jacobi window of the model itself, verified in the same exact pass.
| Registered name | Family | Saves | Lossless | Trainable | Configuration |
|---|---|---|---|---|---|
lookahead |
drafting | latency | yes | no | LookaheadConfig |
Backends (default configuration): transformers: real · gguf: no · mlx: no · onnx: no · sglang: no · trtllm: no · vllm: real
Lookahead decoding (Fu et al., 2024) drafts without a second network. Every cycle runs one exact pass of the model
over two branches that do not attend to each other. The lookahead branch holds a 2D Jacobi window of window
columns and ngram - 1 levels of the model's own guesses for future tokens. The pass refines the window by one
Jacobi iteration, and every column then yields an n-gram of ngram tokens for an n-gram pool. The verification
branch holds the pool's n-grams that start with the last committed token, merged into one tree, and the verifier
keeps the longest path the model agrees with. Window tokens are never accepted themselves, so greedy decoding is
lossless. The method trades extra positions in each forward pass for fewer passes. It has no parameters and needs
no training or data.
Setup¶
Every tutorial starts from the same model and prompt:
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
import economize
device = "cuda" if torch.cuda.is_available() else "cpu"
base = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B", dtype=torch.bfloat16, device_map=device)
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B")
ids = tokenizer("Natalia sold clips to 48 of her friends in April, and then she sold half as many clips in May. "
"How many clips did Natalia sell altogether?", return_tensors="pt").input_ids.to(device)
Quick start¶
An n-gram proposes ngram - 1 tokens, so cycle_length=ngram lets one cycle accept all of them:
from economize.techniques.lookahead import LookaheadConfig
model = economize.apply(base, LookaheadConfig(window=7, ngram=5), decoding=economize.DecodeConfig(cycle_length=5))
result = model.decode(ids[0], 64)
print(tokenizer.decode(result.tokens))
print(result.mean_accepted) # accepted drafts per verification
print(result.cost_summary()) # model calls and the positions they ran
The cost summary counts every node of the exact pass, the window included, since that is the compute the method spends. The output is the model's greedy output token for token.
Decoding options¶
The window size is the main setting. A larger window and ngram make more and longer guesses per pass but widen
every pass. The paper's settings on A100 GPUs were window=15, ngram=5 for 7B models, window=10 for 13B models
and window=7 for 34B models, each with guesses equal to window. guesses caps the number of n-grams the pool
keeps per first token and the verification branch checks. pool_from_prompt=True also fills the pool with every
n-gram of the prompt and the output so far, as the official implementation's POOL_FROM_PROMPT option does, which
helps when the output repeats the input:
model.detach()
model = economize.apply(base, LookaheadConfig(window=15, ngram=5, guesses=15, pool_from_prompt=True),
decoding=economize.DecodeConfig(cycle_length=5))
result = model.decode(ids[0], 64)
The drafts are deterministic, so the rejection verifier treats them as point masses and stays lossless at a
temperature above zero. Verifying a draft tree by rejection sampling needs a selector that gives the sampling
distribution of every node, such as economize.TopPSampler, so sampled runs go through model.decode:
sampling = economize.DecodeConfig(cycle_length=5, verifier="rejection")
sampler = economize.TopPSampler(temperature=0.7, top_p=0.9, seed=0, device="cuda")
sampled = model.decode(ids[0], 64, decoding=sampling, selector=sampler)
Lookahead decoding needs a verifier that accepts trees (greedy, rejection or typical).
Saving and loading¶
The technique has no weights, so a saved model holds only its configuration:
model.save_pretrained("lookahead_model")
model.detach()
model = economize.load(base, "lookahead_model", decoding=economize.DecodeConfig(cycle_length=5))
Combining with other techniques¶
Lookahead decoding is a drafter, so it does not combine with a second drafter. It composes with techniques that change the model or its cache, such as weight quantization:
from economize.techniques.quant import QuantConfig
model.detach()
model = economize.apply(
AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B", dtype=torch.bfloat16, device_map="cuda"),
QuantConfig(format="int8"),
LookaheadConfig(window=7, ngram=5),
decoding=economize.DecodeConfig(cycle_length=5),
)
result = model.decode(ids[0], 64)
Serving¶
vLLM has no Jacobi window. With pool_from_prompt=True the export serves the pool built from the committed tokens
through vLLM's n-gram proposer, which drops the lookahead branch. economize.backend_support reports what each
runtime runs:
print(economize.backend_support(LookaheadConfig(pool_from_prompt=True)))
from economize.backends.vllm import serving
model.detach()
model = economize.apply(base, LookaheadConfig(window=7, ngram=5, pool_from_prompt=True))
plan = serving.to_vllm(model, "out/qwen-lookahead", num_speculative_tokens=4)
llm = plan.llm()
Configuration¶
LookaheadConfig
dataclass
¶
LookaheadConfig(window: int = 15, ngram: int = 5, guesses: int | None = None, pool_from_prompt: bool = False, seed: int = 0)
Hyperparameters of lookahead decoding (Fu et al., 2024).
Every cycle runs one exact pass over two branches that do not see each other. The lookahead branch holds a
2D Jacobi window of window columns and ngram - 1 levels; the pass refines it by one Jacobi step and every
column yields an ngram-gram for the n-gram pool. The verification branch holds the pool's n-grams that start
with the last committed token (at most guesses of them), merged into one tree and verified as usual. The
paper's settings on A100 GPUs (Table 4, with guesses == window):
========= ========== ========= ===========
model window ngram guesses
========= ========== ========= ===========
7B 15 5 15
13B 10 5 10
34B 7 5 7
========= ========== ========= ===========
An n-gram proposes ngram - 1 tokens, so DecodeConfig(cycle_length=ngram) lets a cycle accept all of
them; a shorter cycle verifies their prefixes.
Attributes:
| Name | Type | Description |
|---|---|---|
window |
int
|
Columns |
ngram |
int
|
Length |
guesses |
int | None
|
Most n-grams the pool keeps per first token, the most recently made ones ( |
pool_from_prompt |
bool
|
Also fill the pool with every n-gram of the committed tokens (prompt and output), as the
official implementation's |
seed |
int
|
Seed of the random initial window (its tokens are drawn from the prompt, as in the official code). |
References¶
- Fu, Y., Bailis, P., Stoica, I., & Zhang, H. (2024). Break the sequential dependency of LLM inference using lookahead decoding. In International Conference on Machine Learning. https://doi.org/10.48550/arXiv.2402.02057