10.1 — Greedy decoding and beam search
Goal: Define greedy and beam-search decoding from the autoregressive factorisation; derive their compute and memory profiles; explain why beam search hurts open-ended generation (the 'beam search curse') and why LLMs have almost entirely abandoned it outside machine translation.
1. Why this session exists
Chapter 9 closed the alignment loop: with SFT, RLHF or DPO, GRPO, and inference-time selection, we have a policy that gives us, for every prefix , a next-token distribution
Chapter 10 opens the question of how to use that distribution at deployment time: given the per-token distribution, how do we produce a complete sequence?
There are two families. Deterministic search picks tokens by some rule — argmax (greedy) or beam-restricted argmax (beam search). Stochastic sampling draws from a (transformed) distribution — covered in 10.2.
This session does the deterministic family carefully. The headline conclusion is uncomfortable: beam search, the workhorse of pre-Transformer NMT, hurts open-ended LLM generation. Understanding why sets up everything else in this chapter.[1]
2. The autoregressive decoding problem
Recall the autoregressive language model factorisation (5.3):
Decoding is the optimisation problem
This search space has size — for and , roughly candidate sequences. Exact optimisation is hopeless. Every practical decoder is an approximation.
Two design axes:
- Greedy vs. lookahead. At each step, commit to the token that looks best locally, or maintain alternatives and re-evaluate after seeing more context.
- Deterministic vs. stochastic. Pick by argmax, or sample from a distribution.
Greedy and beam search are both deterministic; greedy is no-lookahead, beam search is bounded-lookahead.
3. Greedy decoding
The simplest decoder: at each step, take the token of maximum probability.
Greedy(x):
y_1 = argmax_v π_θ(v | x)
for t = 2, ..., T_max:
y_t = argmax_v π_θ(v | x, y_{<t})
if y_t = <EOS>: break
return y_{1:t}
3.1 Cost
One model forward per token, tokens total. With the KV cache (6.14), each step costs FLOPs and reuses cached keys/values for the prefix.
where is the parameter count (the factor is the standard "one fwd ≈ 2N FLOPs per token" rule from 7.3). The term is attention against the cached prefix.
3.2 What greedy gets wrong
Greedy maximises the local conditional at each step, but this is not the joint argmax. Two failure modes.
Pathological loops. If the model assigns high probability to a token that, once emitted, makes itself most probable again, the decoder enters a loop ("the the the …" or "I think I think I think …"). This is endemic in undertrained models and in regions of context space the model hasn't seen.
Premature commitment. A high-probability token at step may force the model into a low-probability region later. The classic toy:
Greedy picks . But suppose and . Then
Greedy's sequence has joint probability ; the better sequence has but greedy never explores it.
This is the motivation for lookahead — beam search.
4. Beam search
Maintain a set of partial hypotheses (the "beam") and at each step expand all of them, keeping only the top- by joint log-prob.
BeamSearch(x, k):
B_0 = { (∅, 0.0) }
for t = 1, ..., T_max:
candidates = []
for (y, s) in B_{t-1}:
if y ends in <EOS>: candidates.append((y, s)); continue
for v in V:
candidates.append((y ⊕ v, s + log π_θ(v | x, y)))
B_t = top-k candidates by score
if all B_t end in <EOS>: break
return argmax B_T by score
4.1 Cost
Each step expands live hypotheses against next tokens, then prunes to . Forward passes: per step, steps, so total. KV cache must be maintained per beam (each hypothesis has its own continuation):
So beam is greedy in both axes. Typical – for NMT; beyond ~ the marginal quality gain flattens for almost all tasks.
4.2 Length normalisation
The naive score accumulates a negative number per step (since ). Longer sequences always score worse. Beam search prefers short sequences pathologically.
The standard fix is length-normalised scoring:
with . is the per-token log-likelihood; smaller penalises length less. NMT systems typically use .
4.3 Coverage penalties
NMT systems also add a coverage term — a reward for translating each source token at least once, computed from attention weights. This is specific to encoder-decoder MT and doesn't transfer to decoder-only LLMs (no source to cover).
5. The beam search curse
In Sequence-to-Sequence MT, beam- with – reliably outperforms greedy by 1–2 BLEU. In open-ended generation — story continuation, dialogue, instruction-following — beam search underperforms greedy and dramatically underperforms stochastic sampling.
This counterintuitive result is the "beam search curse". Holtzman et al. (2019[2], arXiv:1904.09751) documented it: as increases, generated text becomes more repetitive, less diverse, and lower quality (by human evaluation) — even though the joint log-likelihood goes up.
5.1 Diagnosis: argmax of a calibrated LM is degenerate
The next-token distribution of a well-trained LM has heavy tail: many plausible continuations have small but nonzero probability. The argmax sequence — the single most likely string — is highly atypical of the distribution. It is short, generic, and repetitive, because those properties accumulate small probability advantages step after step.
Formally, consider a unigram model with , , etc. The argmax sequence is "the the the the the…". Probability: . The typical sequence under the model is something like "The cat sat on the mat", which has lower per-step probability but is far more plausible.
This is the typical set phenomenon from information theory: for sources with entropy , almost all probability mass is on sequences of approximate per-token log-prob , which is not the argmax. Beam search drives towards the mode of ; sampling stays in the typical set.
5.2 The plot from Holtzman et al.
Beam- generated text exhibits degenerate repetition: after some tokens, the same phrase begins to repeat verbatim. The probability of the repeated phrase actually increases with each repetition — the model becomes more confident in repetition the more it repeats — so beam search locks in.
Empirically, the curse is sharpest for on open-ended generation. Even greedy () exhibits some repetition; the cure is to introduce stochasticity (Cap 10.2) or repetition penalties (post-hoc score adjustments that downweight tokens recently emitted).
6. When beam search is still right
Beam search isn't dead. Three settings where it works:
- Machine translation. Source dictates target. Output length is bounded by source length; faithfulness matters more than diversity. Beam-4 with length-normalised scoring remains state of the art for NMT, including modern neural systems.
- Constrained generation. Tasks where the output must match a schema (JSON, SQL, code with syntactic constraints) benefit from beam search combined with a constraint-aware scoring function that prunes invalid prefixes.
- Short, factual responses. "Yes/no" answers, single-word completions, classification framed as generation. The output is short enough that the typical-set vs. mode distinction barely applies.
For everything else — open-ended dialogue, instruction-following, code, creative writing — sampling-based decoding (Cap 10.2) wins.
7. Diverse beam search and group beam search
A line of work tried to fix beam search by forcing diversity across beams.
Diverse Beam Search (Vijayakumar et al., 2016, arXiv:1610.02424) partitions the beams into groups and applies a dissimilarity penalty between groups:
In practice this is rarely used today; the moment you accept some diversity, plain sampling with temperature/top-p is simpler and equally effective.
8. Implementation notes
A few details that matter for a correct implementation:
- EOS handling. A hypothesis ending in
<EOS>is finished; it should not be expanded further, but its score still competes with live hypotheses in the top- selection. Otherwise long, fluent-but-pointless beams crowd out short, complete ones. - KV cache management. Each beam needs its own KV cache. When beam is pruned, its cache memory is freed; when beam branches into two top- continuations (rare but possible), the cache must be cloned. Production beam implementations therefore maintain a tree of caches with reference counting.
- Batching. Beam search batches naturally: the beams form a batch of size , and tokens within a batch are independent. This is a free speedup on a GPU — relevant for NMT serving but not for the LLM case where (greedy) dominates.
9. What we keep going forward
Three things.
- Greedy decoding is the simplest baseline: argmax at each step, forwards total, fast and deterministic, but vulnerable to loops and premature commitment.
- Beam search maintains hypotheses with lookahead; costs greedy in compute and memory; needs length normalisation; remains the right choice for machine translation and constrained generation.
- The beam search curse: on open-ended generation, the argmax of a calibrated LM is degenerate (repetitive, generic), because most probability mass sits in the typical set, not at the mode. Sampling — Cap 10.2 — is the cure.
The rest of this chapter takes the sampling-based decoding for granted and turns to engineering: KV-cache details (10.3), speculative decoding (10.4), FlashAttention (10.5), quantisation (10.6), test-time scaling (10.7), continuous batching (10.8), agentic decoding (10.9). With these, an aligned policy from Chapter 9 becomes a deployable LLM.
(a) State the autoregressive decoding optimisation problem. Why is exact optimisation infeasible? Give the search-space size for and .
(b) Write greedy decoding in pseudocode. Derive the cost expression — what does each term represent?
(c) Construct a 2-token toy example where greedy decoding picks the lower-joint-probability sequence (different from the example in §3.2).
(d) State the length-normalised beam-search score . Why does help in NMT? What does mean operationally?
(e) State the beam search curse. Give the information-theoretic intuition (typical set vs. mode) in one paragraph.
(f) Beam search remains standard for which two task types? Explain why those tasks dodge the curse.
References
- [1]
Jurafsky, D., & Martin, J. H. (2025, draft 3rd ed.). Speech and Language Processing. Ch 9 covers greedy/beam decoding (10.1) and modern sampling (10.2). stanford.edu/~jurafsky/slp3
↩ - [2]
Holtzman, A., Buys, J., Du, L., Forbes, M., & Choi, Y. (2019). "The curious case of neural text degeneration". arXiv:1904.09751 — Beam search curse + nucleus (top-) sampling (10.1, 10.2).
↩ - [3]
Wu, Y., Schuster, M., Chen, Z., et al. (2016). "Google's neural machine translation system: Bridging the gap between human and machine translation". arXiv:1609.08144 — Length-normalisation in beam search (10.1).
↩