Beam Search › Hypothesis Expansion and Pruning

Michael BrenndoerferMay 22, 20253 min read
Language AI HandbookAll chapters
Part 12 of 67Sequence-to-Sequence
Chapter 85Beam Search › Hypothesis Expansion and Pruning
Reading section 6 of 17 · Hypothesis Expansion and Pruning · 3 minSection 6 of 17 · 3 min

Book contents

Section 6 of 17 · 3 min read

Beam Search › Hypothesis Expansion and PruningOne section · Sections in this chapter6 / 17 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

The core mechanics of beam search involve two repeated operations: expansion and pruning.

Expansion

At each step, every hypothesis in the beam is extended by every possible token. If the beam has kk hypotheses and the vocabulary has ∣V∣|V| tokens, expansion produces k×∣V∣k \times |V| candidates. For k=5k = 5 and ∣V∣=30,000|V| = 30{,}000, that is 150,000 candidates per step.

In modern implementations, this is done efficiently using batched matrix operations. The kk partial sequences are fed through the decoder in a single batch, producing a k×∣V∣k \times |V| score matrix. The scores are added to the current hypothesis scores to get candidate scores.

One important subtlety is that the decoder has state. For RNN-based models, the state is the hidden vector; for transformer-based models, the state is the key-value cache. When you batch the kk hypotheses together, you need to maintain separate states for each hypothesis. If two hypotheses share a common prefix (which is common early in decoding, when the beam is initialized from a single start token), you can share their cached keys and values to save memory. As the hypotheses diverge, the caches diverge too.

Pruning

From the k×∣V∣k \times |V| candidates, pruning selects the top kk by score (accounting for length normalization if used). This is a partial sort, which is more efficient than a full sort of 150,000 elements. Libraries like NumPy and PyTorch provide highly optimized topk operations that perform this partial sort in O(Nlog⁡k)O(N \log k) time rather than O(Nlog⁡N)O(N \log N) for a full sort, where N=k×∣V∣N = k \times |V|.

When a candidate ends with EOS, it is moved from the beam to the completed hypotheses list. This is important because once a hypothesis completes, it should not continue accumulating log-probability terms. If a completed hypothesis stayed in the beam, its score would keep growing more negative with each additional step (since log-probabilities are negative), even though the sequence is done. Removing completed hypotheses from the beam ensures they are scored at their natural endpoint.

The beam continues with the remaining hypotheses until either the beam is full of EOS-completing sequences, the maximum length is reached, or the beam is empty. In some implementations, the search terminates early when the best completed hypothesis has a higher score than the best remaining hypothesis in the beam, since no future extension can improve upon it.

Tie-Breaking and Minimum Length

Several practical considerations modify the basic algorithm. A minimum output length can be enforced by setting the probability of EOS to zero until a minimum length is reached. This prevents degenerate outputs where the model immediately predicts EOS. Without this guard, models that have learned to assign high probability to EOS from short sequences (a training artifact) will produce one- or two-token translations.

Similarly, certain repeated phrases can be suppressed using n-gram blocking: if a bigram or trigram has already appeared in a hypothesis, its continuation tokens are penalized or zeroed out. This is particularly effective for summarization, where models sometimes fall into repetition loops, generating the same phrase multiple times. N-gram blocking was popularized by the FAIRSEQ library and is now a standard option in most sequence generation frameworks.

Another common modification is early stopping: once the first completed hypothesis is produced, the search continues for a fixed number of additional steps (rather than until the beam is exhausted). This balances thoroughness with efficiency in production deployments.