Algorithm Search⌘K

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 p(y∣x)=∏t=1∣y∣p(yt∣y<t,x)p(y \mid x) = \prod_{t=1}^{|y|} p(y_t \mid y_{<t}, x) over sequences that end with an end token </s>. Two questions are asked. The search question asks for the mode, arg⁡max⁡yp(y∣x)\arg\max_y p(y \mid x), which is what translation and speech recognition have usually wanted. The sampling question asks for a random yy drawn from p(⋅∣x)p(\cdot \mid x), 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 VV that contains </s>, a length limit nn, a context xx (a prompt, a source sentence or an audio signal, possibly empty), and a model that, given xx and a prefix y<ty_{<t}, returns the conditional probabilities p(w∣y<t,x)p(w \mid y_{<t}, x) for all w∈Vw \in V. The model is a black box; one call costs MM.

Outputs. The candidates are the sequences yy that end with their first </s> and have at most nn tokens, and the sequences of exactly nn tokens without </s>.

  • Mode (MAP decoding). Find arg⁡max⁡yp(y∣x)\arg\max_y p(y \mid x), or equivalently the largest ∑tlog⁡p(yt∣y<t,x)\sum_t \log p(y_t \mid y_{<t}, x).
  • Sampling. Output yy with probability p(y∣x)p(y \mid x), or with probability proportional to a changed distribution, such as one with a temperature or a truncated vocabulary.

Variants

  • k best. Find the kk most probable outputs.
  • Samples without replacement. Draw kk 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 kk outputs that are both probable and different from each other (diverse beam search).

Examples

Let V={a,b,</s>}V = \{a, b, \text{</s>}\} and n=2n = 2. The first step gives p(a)=0.6p(a) = 0.6 and p(b)=0.4p(b) = 0.4. After aa the model gives </s> 0.40.4, aa 0.30.3 and bb 0.30.3; after bb it gives </s> 0.90.9 and 0.050.05 to each of aa and bb. The output a</s> has probability 0.6×0.4=0.240.6 \times 0.4 = 0.24, and b</s> has 0.4×0.9=0.360.4 \times 0.9 = 0.36, while the outputs of two tokens without </s>, such as ab, have at most 0.6×0.3=0.180.6 \times 0.3 = 0.18. The mode is b</s>. Greedy decoding takes aa first, because 0.6>0.40.6 > 0.4, and returns a</s>. A beam of width 2 keeps both aa and bb and finds b</s>. Ancestral sampling returns b</s> with probability 0.360.36.

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 nn yes
greedy decoding mode nn no
beam search, width kk mode nkn k no
depth-first branch and bound mode up to ∣V∣n\vert V\vert^n yes
stochastic beam search kk samples without replacement nkn k yes
MBR over NN samples least risk nNn N, plus N2N^2 loss calls for the samples

Each call returns ∣V∣|V| probabilities, so beam search also does O(nk∣V∣)O(n k |V|) 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 nn.

For models with a finite state, the mode is a shortest path. For an n-gram model of order mm, the state is the last m−1m - 1 tokens, and a Viterbi pass over at most nn positions finds the most probable output in O(n∣V∣m)O(n |V|^m) time.

AlgorithmTimeSpacePropertiesCode
Contrastive searchO(n (k C + V + k (L + n) d)) worstO((L + n) d + V) extradeterministicnot exactonlineiterative1 language
Diverse beam searchO(n B (C + V)) worstO(n B + G V) extradeterministicnot exactofflineiterative1 language
Greedy decodingO(n (C + V)) worstO(n + V) extradeterministicnot exactonlineiterative1 language
Grid beam searchO(n k (c + 1) (M + V + m)) worstO(n k (c + 1) + V) extradeterministicnot exactofflineiterative1 language
Minimum Bayes risk decodingO(H N L) worstO(H + N) extradeterministicexactofflineiterative1 language
Nucleus samplingO(V log V) worstO(V) extrarandomizednot exactonlineiterative1 language
Speculative decodingO(n (P + k Q + k V)) worstO(k V) extrarandomizedexactonlineiterative1 language
Stochastic beam searchO(n k (M + V)) worstO(n k + k V) extrarandomizedexactofflineiterative1 language
Top-k samplingO(V + k log k) worstO(V) extrarandomizednot exactonlineiterative1 language
Typical samplingO(V log V) worstO(V) extrarandomizednot exactonlineiterative1 language

Bounds

  • The number of candidates is up to ∣V∣n|V|^n, so exhaustive search is exponential in nn.
  • 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 ∣V∣n|V|^n leaves.
  • The k best problem with k=1k = 1 is MAP decoding, and sampling kk outputs without replacement with k=1k = 1 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

  1. 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
  2. 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
  3. 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

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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a contract.yaml