Problemintro
Autoregressive decoding
Also called decoding from a language model.
Given a model that gives the next token's distribution from a prefix, find the most probable output or draw a sample, one token at a time. Sampling is easy; the exact mode is hard.
Autoregressive decoding is the problem of producing an output sequence from a model that gives, for every prefix, a distribution over the next token. The model defines over sequences that end with an end token </s>. Two questions are asked. The search question asks for the mode, , which is what translation and speech recognition have usually wanted. The sampling question asks for a random drawn from , or from a changed version of it, which is what open text generation usually wants. Sampling is easy: ancestral sampling draws one token at a time and costs one model call per token. The exact mode is hard: for recurrent networks the general problem is undecidable, and NP-complete for outputs of polynomial length (Chen et al. 2018), so practical systems use greedy decoding or beam search, which can miss the mode.
In plain words
A language model writes text one piece at a time. At each step it says how likely each possible next piece is, given what is already written. Decoding is the choice of the pieces. You can take a random piece by these chances at each step, which gives varied text and is cheap. Or you can try to find the single most likely complete text. That is much harder, because the number of possible texts grows very fast with the length, and the best first piece does not always lead to the best text.
Definition
Input. A vocabulary that contains </s>, a length limit , a context (a prompt, a source sentence or an audio signal, possibly empty), and a model that, given and a prefix , returns the conditional probabilities for all . The model is a black box; one call costs .
Outputs. The candidates are the sequences that end with their first </s> and have at most tokens, and the sequences of exactly tokens without </s>.
- Mode (MAP decoding). Find , or equivalently the largest .
- Sampling. Output with probability , or with probability proportional to a changed distribution, such as one with a temperature or a truncated vocabulary.
Variants
- k best. Find the most probable outputs.
- Samples without replacement. Draw distinct outputs, with the law of sampling one at a time without replacement; stochastic beam search does this exactly.
- Constrained. Find the best output that contains given phrases (grid beam search) or that a grammar accepts (grammar constrained decoding).
- Least risk. Find the output of least expected loss (minimum Bayes risk decoding). With the 0-1 loss this is the mode.
- Normalized scores. Find the output of best score divided by a function of the length (length normalization).
- Diverse sets. Find outputs that are both probable and different from each other (diverse beam search).
Examples
Let and . The first step gives and . After the model gives </s> , and ; after it gives </s> and to each of and . The output a</s> has probability , and b</s> has , while the outputs of two tokens without </s>, such as ab, have at most . The mode is b</s>. Greedy decoding takes first, because , and returns a</s>. A beam of width 2 keeps both and and finds b</s>. Ancestral sampling returns b</s> with probability .
In real translation models the mode can be very bad: Stahlberg and Byrne 2019 ran exact search on a Transformer model for English to German and measured that the most probable translation was the empty one for more than half of the test sentences (see inadequacy of the mode).
Known algorithms
| method | question | cost in model calls | exact? |
|---|---|---|---|
| ancestral sampling | sample | yes | |
| greedy decoding | mode | no | |
| beam search, width | mode | no | |
| depth-first branch and bound | mode | up to | yes |
| stochastic beam search | samples without replacement | yes | |
| MBR over samples | least risk | , plus loss calls | for the samples |
Each call returns probabilities, so beam search also does work to score the extensions and a top-k selection over them per step. Speculative decoding samples exactly with fewer calls of the large model, and a key-value cache makes each call of a transformer cost time linear in the prefix length, not quadratic.
Exact search uses depth-first branch and bound. The log probability of a prefix never increases when a token is added, so a prefix whose log probability is already below that of the best complete output found so far can be pruned. Stahlberg and Byrne 2019 used this to find the exact mode of a neural translation model on a test set. The worst case stays exponential in .
For models with a finite state, the mode is a shortest path. For an n-gram model of order , the state is the last tokens, and a Viterbi pass over at most positions finds the most probable output in time.
| Algorithm | Time | Space | Properties | Code |
|---|---|---|---|---|
| Contrastive search | O(n (k C + V + k (L + n) d)) worst | O((L + n) d + V) extra | deterministicnot exactonlineiterative | 1 language |
| Diverse beam search | O(n B (C + V)) worst | O(n B + G V) extra | deterministicnot exactofflineiterative | 1 language |
| Greedy decoding | O(n (C + V)) worst | O(n + V) extra | deterministicnot exactonlineiterative | 1 language |
| Grid beam search | O(n k (c + 1) (M + V + m)) worst | O(n k (c + 1) + V) extra | deterministicnot exactofflineiterative | 1 language |
| Minimum Bayes risk decoding | O(H N L) worst | O(H + N) extra | deterministicexactofflineiterative | 1 language |
| Nucleus sampling | O(V log V) worst | O(V) extra | randomizednot exactonlineiterative | 1 language |
| Speculative decoding | O(n (P + k Q + k V)) worst | O(k V) extra | randomizedexactonlineiterative | 1 language |
| Stochastic beam search | O(n k (M + V)) worst | O(n k + k V) extra | randomizedexactofflineiterative | 1 language |
| Top-k sampling | O(V + k log k) worst | O(V) extra | randomizednot exactonlineiterative | 1 language |
| Typical sampling | O(V log V) worst | O(V) extra | randomizednot exactonlineiterative | 1 language |
Bounds
- The number of candidates is up to , so exhaustive search is exponential in .
- For recurrent networks, finding the highest weighted string is undecidable in general, and NP-complete when the length is bounded by a polynomial (Chen et al. 2018). These are theorems about the worst case over networks.
- Even for a hidden Markov model, the most probable output string, a sum over all state paths, is NP-hard to find (Lyngsø and Pedersen 2002), while the most probable state path is easy (HMM decoding).
- Ancestral sampling uses one model call per output token. Speculative decoding keeps the exact law and replaces some calls of the large model with calls of a cheaper draft model.
Reductions
- With the 0-1 loss and the full distribution, MBR decoding is MAP decoding.
- For an HMM, finding the most probable state path is HMM decoding; a neural decoder whose state is the whole prefix makes the state path and the output the same, so its mode is a most probable path in a tree of leaves.
- The k best problem with is MAP decoding, and sampling outputs without replacement with is ancestral sampling.
Applications
Every text generator built on a language model or an encoder-decoder model solves this problem: machine translation, speech recognition with attention or transducer models, summarization, dialogue and code generation. Translation and speech recognition mostly use beam search for the mode; open text generation mostly samples, with temperature, top-k or nucleus sampling.
References
- Felix Stahlberg and Bill Byrne, On NMT search errors and model errors, cat got your tongue?, EMNLP-IJCNLP 2019, pages 3356 to 3362, exact search for the most probable translation by depth-first search with pruning, and the measured share of sentences whose most probable translation is empty
- Yining Chen, Sorcha Gilroy, Andreas Maletti, Jonathan May and Kevin Knight, Recurrent neural networks as weighted language recognizers, NAACL 2018, pages 2261 to 2271, the complexity of finding the highest weighted string of a recurrent network, undecidable in general and NP-complete for strings of polynomial length
- Rune B. Lyngsø and Christian N. S. Pedersen, The consensus string problem and the complexity of comparing hidden Markov models, Journal of Computer and System Sciences 65(3), 2002, pages 545 to 569, the hardness of finding the most probable string emitted by a hidden Markov model
Where this leads
- Diverse beam search
- Exposure bias
- Grammar-constrained decoding
- Greedy decoding
- Grid beam search
- Inadequacy of the mode
- Length normalization
- Minimum Bayes risk decoding
- Nucleus sampling
- Repetition penalty
- Scheduled sampling
- Speculative decoding
- Stochastic beam search
- Temperature sampling
- Top-k sampling
- Typical sampling
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag RQ9MXS Edit this page History Report an error Markdown