Skip to content

Prompt lookup decoding

At a glance

Prompt-lookup drafting: drafts are copied from earlier text that ended in the same n-gram.

Registered name Family Saves Lossless Trainable Configuration
lookup drafting latency yes no LookupConfig

Backends (default configuration): transformers: real · gguf: real · mlx: no · onnx: no · sglang: real · trtllm: untested · vllm: real

Prompt lookup decoding (Saxena, 2023) drafts by copying. At every cycle it takes the last n committed tokens of a row and searches for an earlier occurrence of them in the row's own tokens, prompt and output alike, for n = max_ngram, ..., min_ngram, longest first. The tokens that followed the match become the draft, and the model verifies them in one forward pass. No network runs while drafting, so a draft costs only the extra positions of the verification pass. A row without a match drafts nothing in that cycle and decodes one token, as plain decoding would. Verification keeps the longest prefix the model agrees with, which makes greedy decoding lossless. The technique has no parameters and needs no training or data. It helps most where the output repeats spans of the input, such as summarization, question answering over a document, code editing and multi-turn chat.

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

from economize.techniques.lookup import LookupConfig

model = economize.apply(base, LookupConfig(max_ngram=3, max_proposal=10),
                        decoding=economize.DecodeConfig(cycle_length=11))

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 decoder layers they ran

cycle_length=11 allows up to ten draft tokens per verification, and max_proposal=10 lets one match fill that budget. The output is the model's greedy output token for token. model.generate takes the usual transformers arguments and returns the same tokens as base.generate:

out = model.generate(ids, max_new_tokens=64)
print(tokenizer.decode(out[0, ids.shape[1]:]))

Decoding options

match chooses the occurrence that is copied. The default "earliest" copies after the first occurrence in the row, as the reference implementation, Hugging Face's prompt_lookup_num_tokens and vLLM's ngram method do. "latest" copies after the most recent occurrence, which tracks a repeated span that has changed since it first appeared. min_ngram raises the shortest suffix that may match, which trades fewer drafts for fewer rejected ones:

model.detach()
model = economize.apply(base, LookupConfig(max_ngram=4, min_ngram=2, match="latest"),
                        decoding=economize.DecodeConfig(cycle_length=11))
result = model.decode(ids[0], 64)

Sampling at a temperature above zero stays lossless with the rejection verifier. A copied token has a point-mass draft distribution, so it is accepted with the model's probability of that token:

sampling = economize.DecodeConfig(cycle_length=11, verifier="rejection")
sampled = model.generate(ids, max_new_tokens=64, do_sample=True, temperature=0.7, decoding=sampling)

Combining with other techniques

Prompt lookup drafts nothing when the context has no match. The hybrid technique fills those cycles with a second drafter: lookup first, and the fallback for the rows that found no match. Here the fallback is an untrained EAGLE-3 head (train it as in EAGLE-3 before use), and min_confidence stops a fallback chain once its confidence drops below 0.3:

from economize.techniques.eagle3 import Eagle3Config
from economize.techniques.hybrid import HybridConfig

model.detach()
model = economize.apply(base, HybridConfig(fallback=Eagle3Config(), max_ngram=3, max_proposal=7),
                        decoding=economize.DecodeConfig(cycle_length=8, min_confidence=0.3))
result = model.decode(ids[0], 64)

Lookup also composes with techniques that are not drafters, such as KV-cache quantization or weight quantization of the model. economize.compatible checks a pair before you build it:

from economize.techniques.kv_quant.kivi import KIVIConfig

print(economize.compatible(LookupConfig(), KIVIConfig(bits=4)))  # True

model.detach()
model = economize.apply(base, KIVIConfig(bits=4), LookupConfig(max_proposal=10),
                        decoding=economize.DecodeConfig(cycle_length=11))
result = model.decode(ids[0], 64)

Serving

vLLM serves prompt lookup with its own ngram method, and SGLang with its n-gram speculative decoding. The export writes the engine arguments and leaves the model weights alone:

from economize.backends.vllm import serving

model.detach()
model = economize.apply(base, LookupConfig(max_ngram=3))
plan = serving.to_vllm(model, "out/qwen-lookup", num_speculative_tokens=10)
llm = plan.llm()

Configuration

LookupConfig dataclass

LookupConfig(max_ngram: int = 3, min_ngram: int = 1, max_proposal: int | None = None, match: MatchRule = 'earliest')

Hyperparameters of prompt-lookup drafting (Saxena, 2023; vLLM's "ngram" method).

Every cycle, the last n committed tokens of a row are searched in all its earlier tokens (prompt and output), for n = max_ngram, ..., min_ngram, longest first. The tokens that followed the earliest (or, with match="latest", the most recent) earlier occurrence become the draft. A row without a match drafts nothing in that cycle (the exact pass then decodes one token, as plain decoding would).

Attributes:

Name Type Description
max_ngram int

Longest suffix that is matched.

min_ngram int

Shortest suffix that is matched.

max_proposal int | None

Most tokens one match proposes (default: the cycle's budget, cycle_length - 1).

match MatchRule

Which occurrence is copied: "earliest" (Saxena's prompt lookup, Hugging Face and vLLM) or "latest" (the most recent occurrence).

References