SuffixDecoding¶
At a glance
SuffixDecoding: drafts from a suffix structure over earlier responses and the row's own tokens.
| Registered name | Family | Saves | Lossless | Trainable | Configuration |
|---|---|---|---|---|---|
suffix_decoding |
drafting | latency | yes | no | SuffixDecodingConfig |
Backends (default configuration): transformers: real · gguf: no · mlx: no · onnx: no · sglang: no · trtllm: no · vllm: real
SuffixDecoding (Oliaro et al., 2025) drafts from suffix structures over earlier text, without a draft model. Every
cycle it searches two of them: a global one over a datastore of earlier responses and a local one over the row's own
prompt and output. For each match length m of the context's suffix, the matched node is expanded by the empirical
probability of each continuation, count(child) / count(node), multiplied along the path. A token enters the draft
only while that probability is at least min_token_prob, and a match of m tokens drafts at most
max_spec_factor * m + max_spec_offset tokens, so long matches draft long continuations and short matches draft
little. The draft with the highest score (the sum of its token probabilities) wins. The model verifies it in one
forward pass and keeps the longest prefix it agrees with, so greedy decoding is lossless. The method has no
parameters and needs no training. It suits workloads that repeat themselves across requests, such as agents that
call the same tools or rewrite the same code.
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)
Training data is a flat file of uint32 token ids, the format of nanoGPT-style pretraining shards, and calibration
data is a [N, S] tensor of token ids. Both are made here from GSM8K:
import numpy as np
from datasets import load_dataset
texts = [f"{row['question']}\n{row['answer']}" for row in load_dataset("openai/gsm8k", "main", split="train")]
tokens = np.concatenate([tokenizer(t + tokenizer.eos_token).input_ids for t in texts]).astype(np.uint32)
tokens[: len(tokens) * 9 // 10].tofile("train.bin")
tokens[len(tokens) * 9 // 10 :].tofile("val.bin")
calibration = torch.from_numpy(tokens[: 128 * 2048].astype(np.int64)).view(128, 2048)
Quick start¶
Without a datastore, SuffixDecoding matches only the row's own prompt and output:
from economize.techniques.suffix_decoding import SuffixDecodingConfig
model = economize.apply(base, SuffixDecodingConfig(), decoding=economize.DecodeConfig(cycle_length=17))
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
cycle_length=17 lets a cycle draft up to 16 tokens, and the adaptive length decides how many it uses. The output
is the model's greedy output token for token, and model.generate returns the same tokens as base.generate:
out = model.generate(ids, max_new_tokens=64)
Building a datastore¶
The global structure is a SuffixIndex folder over token records, the same format the REST drafter reads. Here the
GSM8K training examples other than the prompt's own stand in for earlier responses:
from economize.techniques.retrieval import SuffixIndex
records = [tokenizer(t + tokenizer.eos_token).input_ids for t in texts[1:]]
SuffixIndex.build(records, depth=64).save("responses_store")
model.detach()
model = economize.apply(base, SuffixDecodingConfig(datastore="responses_store"),
decoding=economize.DecodeConfig(cycle_length=17))
result = model.decode(ids[0], 64)
A match plus its continuation in the datastore never exceeds the index's depth, so build it with a depth of at
least max_depth (64 by default). The command-line builder indexes a token file, with --spans for an [R, 2] array of record boundaries:
python -m economize.techniques.retrieval build responses_store --tokens train.bin --dtype uint32 --depth 64
Decoding options¶
The defaults are those of the reference implementation, Arctic Inference: depth 64, max_spec_factor=1.0,
max_spec_offset=0.0, min_token_prob=0.1 and a single path. tree=True drafts a tree instead, expanding the most
probable frontier node first up to max_spec_tokens nodes, which the model verifies in one pass with tree
attention. local=False matches only the datastore:
model.detach()
model = economize.apply(
base,
SuffixDecodingConfig(datastore="responses_store", tree=True, max_spec_tokens=8, max_spec_factor=2.0,
min_token_prob=0.2),
decoding=economize.DecodeConfig(cycle_length=9),
)
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 tree by rejection sampling needs a selector that gives the sampling distribution
of every node, such as economize.TopPSampler:
sampling = economize.DecodeConfig(cycle_length=9, 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)
Combining with other techniques¶
SuffixDecoding is a drafter, so it does not combine with a second drafter. It composes with techniques that change the cache or the model, such as KV-cache quantization:
from economize.techniques.kv_quant.kivi import KIVIConfig
model.detach()
model = economize.apply(base, KIVIConfig(bits=4), SuffixDecodingConfig(datastore="responses_store"),
decoding=economize.DecodeConfig(cycle_length=17))
result = model.decode(ids[0], 64)
Serving¶
Without a datastore, vLLM serves the technique with its own suffix method, which needs the arctic-inference
package. With a datastore, the export uses economize's custom proposer over it. vLLM verifies a single chain, so a
tree draft is served as its best path:
from economize.backends.vllm import serving
model.detach()
model = economize.apply(base, SuffixDecodingConfig())
plan = serving.to_vllm(model, "out/qwen-suffix", num_speculative_tokens=16)
llm = plan.llm()
Configuration¶
SuffixDecodingConfig
dataclass
¶
SuffixDecodingConfig(datastore: str | None = None, local: bool = True, max_depth: int = 64, max_spec_factor: float = 1.0, max_spec_offset: float = 0.0, min_token_prob: float = 0.1, tree: bool = False, max_spec_tokens: int | None = None)
Hyperparameters of SuffixDecoding (Oliaro et al., NeurIPS 2025, "SuffixDecoding: Extreme Speculative Decoding for Emerging AI Applications"; the reference implementation is Arctic Inference's suffix cache).
Two suffix structures are searched every cycle: a global one over a datastore of earlier responses (a
:class:~economize.techniques.retrieval.SuffixIndex folder) and a local one over the row's own prompt and
output. For every match length m = 1, 2, ... of the context's suffix (at most max_depth - 1, stopping
at the first length without an occurrence), the matched node is expanded by the empirical continuation
probability count(child) / count(node), multiplied along the path. Path mode follows the most frequent
child; tree mode expands the most probable frontier node first. A token enters the draft only while its
probability is at least min_token_prob, and a match of m tokens drafts at most
min(max_spec_tokens, floor(max_spec_factor * m + max_spec_offset)) tokens (the adaptive speculation length).
The draft with the largest score (the sum of its token probabilities; ties go to the longer match) wins, and the
local draft wins a tie with the global one.
The defaults are Arctic Inference's: depth 64, factor 1, offset 0, minimum probability 0.1, path mode.
Attributes:
| Name | Type | Description |
|---|---|---|
datastore |
str | None
|
Folder of the global suffix index ( |
local |
bool
|
Also match the row's own prompt and output. |
max_depth |
int
|
Longest suffix the structures hold: match plus continuation never exceed it. |
max_spec_factor |
float
|
Draft tokens per matched token. |
max_spec_offset |
float
|
Draft tokens added to the adaptive length. |
min_token_prob |
float
|
Smallest probability of a drafted token. |
tree |
bool
|
Draft a tree (with a verifier that verifies trees); otherwise a chain. |
max_spec_tokens |
int | None
|
Most draft tokens (tree nodes in tree mode). |
References¶
- Oliaro, G., Jia, Z., Campos, D., & Qiao, A. (2025). SuffixDecoding: Extreme speculative decoding for emerging AI applications. In Advances in Neural Information Processing Systems. https://doi.org/10.48550/arXiv.2411.04975