Skip to content

Speculative decoding

Reading a large model from disk costs about the same whether a pass computes one token or several. Speculative decoding makes use of that: something cheap guesses the next few tokens, and one pass of the large model checks all of them at once. Every accepted guess is a token gained for free.

The output follows the large model's distribution exactly. With greedy sampling it is the same text as plain decoding, apart from near-ties within the quantization noise.

With a draft model

A small model of the same family drafts the tokens. It is kept fully in RAM, so drafting is fast:

nvserve run llama-3.1-70b.nvpack --draft llama-3.2-1b.nvpack --chat --mem-limit 8G \
    --prompt "Why is the sky blue?"
Target Draft Plain With draft Tokens per pass
Llama-3.1-70B Llama-3.2-1B 9.7 s/token 4.3 s/token 3.25
Llama-3.1-8B Llama-3.2-1B 1.4 s/token 0.64โ€“0.84 s/token 2.7
Qwen2.5-72B Qwen2.5-0.5B 11.0 s/token 5.5 s/token 1.96
  • Matching tokenizers. The draft must use the same tokenizer as the target, and NVserve checks this at startup. Llama-3.2-1B drafts for any Llama 3.x model. Qwen2.5-0.5B and 1.5B draft for Qwen2.5 models, even though the two pad their vocabularies to different sizes.
  • Memory. The draft costs its full size in RAM. Under --mem-limit, a draft that does not fit is dropped with a note, and generation continues without it.
  • Draft quality. A larger draft is accepted more often. Qwen2.5-0.5B is accepted only 24% of the time for the 72B, so Qwen2.5-1.5B should do better.

Without a draft model

--lookup proposes the continuation of an earlier occurrence of the latest few tokens. It costs nothing and helps when the output repeats the prompt: summaries, code edits, extraction.

nvserve run model.nvpack --lookup --prompt "..."

Draft length

--draft-tokens auto, the default, picks between 1 and 8 drafted tokens per pass. It measures pass times and acceptance as it goes and keeps adapting, also across server requests. A number fixes the length (--draft-tokens 4). Each drafted token adds CPU work to the verifying pass, so longer is not always better. In back-to-back runs on the 8B, auto was 20โ€“30% faster than a fixed 4.

In the server

nvserve serve llama-3.1-70b.nvpack --draft llama-3.2-1b.nvpack --mem-limit 4G

The server speculates while exactly one request is generating. Concurrent requests share plain passes instead, which already uses each pass for several tokens. Llama-3.1-70B under a 4 GiB cap generated at 4.2 s/token with the 1B draft, against 11.4 s/token plain, with identical text.

At the end of a run, run prints the acceptance rate, the tokens per verify pass and the average draft length:

speculate  14 verify passes, 29/96 drafts accepted (30%), 3.00 tokens per pass, k 6.9 on average (auto)