All posts

N-gram speculation

/AI/4 min read

Speculative decoding without a draft model: guess the next tokens by finding the last few tokens earlier in the context and copying what followed. On a code edit that is 4.59 tokens per step; in open chat it is 1.10 — and it never makes anything slower.

Speculative decoding speeds up generation by guessing several tokens ahead and having the large model check all the guesses in one pass. Checking is cheap — decode is limited by memory bandwidth, so verifying five tokens costs about the same as producing one. The question is always where the guesses come from.

The usual answer is a small draft model. N-gram speculation needs nothing of the kind. It takes the guesses from text that is already in the context.

The observation

A great deal of what a model writes, it has just read.

Ask it to fix a bug in a 200-line function and the reply is mostly the function, copied, with three lines changed. Ask it to answer from a document and it quotes names, figures and phrases from that document. Ask it to rewrite an email and most of the sentences survive. In all of these, the next few tokens are very often a continuation of something that appears verbatim earlier in the prompt.

The mechanism

At each step:

  1. Take the last n tokens generated — say the last 3.
  2. Search the context for an earlier occurrence of those 3 tokens.
  3. If found, propose the k tokens that followed it there as the draft.
  4. Run the large model once over the draft. Keep the longest prefix it agrees with, plus one token of its own.
  5. If nothing matched, decode one token normally.

The lookup is a string search over a few thousand tokens — microseconds. There is no second model to load, train, keep in memory, or keep in sync with the main one.

COPY WHAT FOLLOWED LAST TIME prompt … for item in order.items : total += item.price * qty output … for item in order.items : ? draft total += item.price * qty one forward pass checks all of it; the model edits only where it disagrees no match → one normal step, nothing lost
The draft is a lookup, not a model. It is only as good as the overlap between input and output.

How much it gains

If each drafted token is accepted with probability α, and k are drafted, the expected number of tokens produced per large-model step is:

(1 − α^(k+1)) / (1 − α)

acceptance αk = 4k = 8
0.31.431.43
0.51.942.00
0.72.773.20
0.94.106.13

At high acceptance, drafting longer pays — 6.13 tokens a step with 8 drafted. At low acceptance it is pointless: past the first couple of guesses, the chain has almost certainly broken.

But that formula only applies on steps where a match was found. The real gain depends on how often the context offers one:

workloadsteps with a matchacceptancetokens per step
editing code (k = 8)70%0.94.59
open-ended chat (k = 8)10%0.51.10

On a code edit, generation runs more than four times faster. In free conversation, 10% faster. The method is only as good as the overlap between what was read and what is written.

Why it is safe to leave on

Two properties make n-gram speculation an easy default.

It never changes the output. Verification accepts a drafted token only if it is the token the large model would have chosen anyway (or, with sampling, accepts in a way that preserves the model's distribution exactly). The text is the same text, produced sooner.

It rarely makes anything slower. When there is no match, no draft is made and the step is an ordinary step. When a draft is rejected at the first token, the step still produces one token — the large model's own — at the cost of a slightly longer forward pass, which in the bandwidth-bound regime is close to free.

The one case to watch is large batches. When a server is already processing many sequences per step, it is no longer purely bandwidth-bound, and verifying 8 extra tokens per sequence is real compute. Serving systems typically reduce k or switch speculation off as batch size grows.

Compared with a draft model

A trained draft model guesses well on any text, including text that has never appeared in the context. N-gram lookup guesses perfectly on repeated text and not at all on new text.

That makes them complementary rather than competing. Some systems try the n-gram lookup first and fall back to the draft model when it finds nothing — the lookup is so cheap that trying it costs nothing.

What to take away

N-gram speculation drafts tokens by finding the last few generated tokens earlier in the context and proposing what came after them. It needs no extra model, never changes the output, and costs almost nothing when it misses.

Its speed-up is set entirely by how much of the output is copied from the input: 4.59 tokens per step when editing code, 1.10 in open chat. For editing, summarising-with-quotes and retrieval-grounded answers, it is the cheapest speed-up available.