Speculative Decoding

Overview

Speculative decoding is a method to speed up inference from large language models like GPT. At its core, we have a strong parent model like GPT3 and a weaker child model like GPT2. We use the weaker model to propose new tokens and the stronger parent model to verify and accept them. This leads to faster inference (i.e. a greater number of tokens generated per second).

To generate a new token, models like GPT generate a probability distribution $p(x)$ over the model’s vocabulary. We then sample a token $x \sim p(x)$ and append it to our sequence of tokens. This appended sequence is then sent through the model to generate another probability distribution to sample yet another token. This process continues, which is where these models get their autoregressive generation capabilities from.

Algorithm

To sample $x \sim p(x)$ we do the following:

For any distributions $p(x)$ and $q(x)$ and $x$ sampled this way, we have $x \sim p(x)$.

One may ask, how does this speed up our generation process? It seems we need both $p(x)$ and $q(x)$ to get our next token, which actually should increase our generation time, since we need to run forward pass through $M_q$ and $M_p$ now. This analysis is correct, generating one token at a time using this algorithm actually slows down generation. However, what if we look to generate the next $\gamma + 1$ tokens? This is what Algorithm 1 from [1] describes. Here is how to do it:

References

[1] Leviathan, Yaniv, Matan Kalman, and Yossi Matias. “Fast inference from transformers via speculative decoding.” International Conference on Machine Learning. PMLR, 2023.