Skip to content
mentoor

Topic 3 of 5 · 4 min read

Speeding generation with speculative decoding

Goal: Configure speculative decoding with a matching draft model and judge from a benchmark whether it helped.

Speculative decoding makes generation faster without changing the output: a small, fast model guesses several tokens ahead, and the large model checks all the guesses in one pass. When the guesses are right, you get several tokens for the price of one large-model step.

Check yourself · 1 of 3

In speculative decoding, what does the large target model do with the draft model's tokens?

Choose one answer

Why it matters

Generation is limited by reading the large model's weights once per token. Verifying five proposed tokens in a single pass costs about as much as producing one, because the pass is mostly waiting on memory. So if most guesses are accepted, speed goes up. The original paper shows the output distribution is the same as running the large model alone, so you trade memory for speed, not quality.

The parts

  • Target model: the big model whose answers you want.
  • Draft model: a much smaller model that proposes tokens. It must share the target's tokenizer and vocabulary, usually because it comes from the same family.
  • Acceptance rate: the share of proposed tokens the target agrees with. This number decides everything.
  • Other forms: some models carry extra prediction heads, and runtimes such as vLLM support several methods besides a separate draft model.

How to configure it

In llama.cpp's server, add a draft model and cap the draft length:

llama-server -m big-Q4_K_M.gguf -md small-Q4_K_M.gguf \
  -ngl 99 -ngld 99 --draft-max 16 --draft-min 1

Check that it helped with a before-and-after benchmark on your own prompts, same settings except the draft model.

When it helps and when it does not

It helps on predictable text: code, JSON, boilerplate, summaries that copy phrases. It helps less on creative writing at high temperature, where the target often disagrees. It also costs memory, because the draft model and its own cache must fit next to the target. If that pushes layers off the GPU, the net result is slower.

Before and after

Weak: pair a 70B target with a draft from another family because it is small, and report "speculative decoding made it slower." The mismatched tokenizer means proposals are rejected or the pairing fails.

Better: use a small model from the same family, benchmark with and without it on a code task, and keep it only if generation speed rises.

What goes wrong

  • Mismatched draft. Different vocabulary means little or no acceptance.
  • Too big a draft. It spends the time it was meant to save.
  • Memory squeeze. The draft pushes the target off the GPU.
  • Testing on the wrong text. A gain on code does not carry to creative writing.

Examples

A matched pair

llama-server -m big-Q4_K_M.gguf -md small-Q4_K_M.gguf -ngl 99 -ngld 99 --draft-max 16

Both models are from one family, so they share a vocabulary. -ngld puts the draft on the GPU too. Run the same code-generation prompt with and without -md and compare tg speed.

Reading a disappointing result

A team adds a draft model for a story-writing assistant at high temperature. Generation speed drops from 30 to 26 tokens per second. Acceptance is low because sampled creative text is hard to predict, and the draft's memory forced two target layers onto the CPU. Remove it, or restrict it to structured outputs.

Key points

  • A draft model proposes tokens and the target verifies them in one pass, so output quality is unchanged.
  • The draft must share the target's tokenizer, so use a smaller model of the same family.
  • The acceptance rate decides the speed-up; predictable text such as code gets higher acceptance.
  • The draft model and its cache use memory that can push target layers off the GPU.
  • Judge it by benchmarking with and without the draft on your own prompts.

Common mistakes

  • Choosing a draft model from a different family, so its tokens do not match the target's vocabulary.
  • Using a draft that is too large, so drafting costs as much as it saves.
  • Ignoring the extra memory of the draft model and its cache.
  • Judging the speed-up on one prompt type and applying it to all workloads.

Check yourself · 2 of 3

Why must a draft model usually come from the same family as the target?

Choose one answer

Check yourself · 3 of 3

You add a draft model and generation drops from 28 to 21 tokens per second. The logs show the target now has 6 layers on the CPU. What is the best next step?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic