< Will Bosler

Speculative Decoding ·

Large, intelligent models are slow to generate text. One of the most popular techniques for making them faster is speculative decoding.

0:02 / 0:05

At a high level, speculative decoding uses two models. The target model is the large, intelligent model whose output we want. The draft model is cheap, cheap in the sense of fast: it can produce significantly more tokens per second than the target. The draft model is used to reduce the total time the target takes to produce content for the user.

0:02 / 0:14

The process has three stages. In the drafting stage, the draft model proposes the next tokens. In the verification stage, the target model scores them. In the accept or reject stage, an algorithm based on the target model's distribution decides which of the drafted tokens to keep.

The approach becomes intuitive once we consider how large language models generate text. Inference is traditionally sequential. The models are autoregressive: each token is produced from the probabilities conditioned on the tokens that came before it. This process is slow, and we would rather not have the large model, which is expensive to run, perform it one token at a time.

0:02 / 0:12

The smaller draft model can perform this loop much faster. It produces k tokens, which requires k loops of its forward pass. The target model, the intelligent one, is then responsible only for verification. Verification can be done in parallel, since the target no longer has to predict the next tokens: the suggestions already exist. A single pass of the target model returns its probability distribution at every drafted position, from which we ask whether each drafted token matches what the target itself would have said.

This comparison forms the core of the accept or reject loop. If a drafted token matches the target's distribution, it is kept, and we gain the speed of the cheaper model. If it does not, it is discarded, and we take the token the target would have produced in its place.

0:04 / 0:14

Each pass of the target model therefore yields between 1 and k + 1 tokens. In the best case, every drafted token is accepted and the target adds one more of its own. In the worst case, the first draft is rejected, but we still gain the token the target produced during its pass. The cost of the draft model is amortised across the tokens it saves, and verification is fast. The target still generates from the distribution it wants, so we retain all of its intelligence, while the total time taken to produce the content is greatly reduced.

Consider a worked example with k = 4, continuing the context “The cat”.

0:15 / 0:20

The draft model proposes “sat”, “on”, “the” and “sofa”, one after another. The target then checks each against its own distribution. It agrees with the first three but not with “sofa”, which it replaces with its own “mat”. Three accepted tokens and one replacement give four tokens from a single pass of the target: “The cat sat on the mat”.

Speculative decoding pays off when the draft model reliably produces tokens that match the target's distribution. Much of our work does not require the full intelligence of the largest models, and smaller, cheaper models can often arrive at the same tokens. This is where the speed-up from speculative decoding is most valuable.