Inference

Beam search

Play beam search over a token tree built to trap greedy decoding. The most probable first word is not on the best sentence, and width two is enough to prove it.

Beam width 2
Run
Thecat0.50dog0.32fox0.18sat0.38ran0.34hid0.28sat0.14ran0.74hid0.12sat0.45ran0.33hid0.22calmly 0.38fast 0.34away 0.28calmly 0.38fast 0.34away 0.28calmly 0.38fast 0.34away 0.28calmly 0.45fast 0.33away 0.22calmly 0.10fast 0.78away 0.12calmly 0.45fast 0.33away 0.22calmly 0.45fast 0.33away 0.22calmly 0.45fast 0.33away 0.22calmly 0.45fast 0.33away 0.22
0 / 3
Level
1of 2
Live beams
0.00
Best log-prob

At each level, every live beam proposes its children, all candidates are ranked by cumulative log-probability, and the best 2 survive. The runner-up first word stays alive, which is the whole trick.

Greedy decoding answers a slightly wrong question. It picks the most probable next token, every step, but what you usually want is the most probable whole sequence, and those are not the same thing. A word can be the single best continuation right now while everything that follows it is mediocre.

This tree is built so that happens immediately. The first token "cat" has probability 0.5 against "dog" at 0.32, so greedy takes it, and everything under "cat" turns out flat and indecisive. Under "dog" hides a spine of high-probability continuations whose product beats anything "cat" can reach. Play the search at width 1 and watch it walk into the trap; widen to 2 and the runner-up stays alive just long enough for its subtree to win.

The mechanism is worth watching step by step. Every live beam proposes all of its children; the pool of candidates is ranked by cumulative log-probability, which is where a strong past can subsidise a weak present; and only the best k survive. The crossed-out boxes are real candidates that were considered and pruned, which is exactly what a finished-looking diagram of beam search hides. Widths three and four are on the slider too: on a tree this small they converge on the same answer as width two, which is itself a lesson in diminishing returns.

Two honest limits. Beam search is not exhaustive: even here, a width of 2 explores a fraction of the tree, and there exist trees that fool any fixed width. And in open-ended generation, maximising sequence probability is often not what you want at all: the highest-probability continuation of a chat prompt is repetitive and dull, which is why beam search rules translation and speech recognition, where there is a right answer, and sampling rules creative generation, where there is not.

The maths

The objective
y  =  argmaxytlogP(yty<t)\mathbf{y}^* \;=\; \arg\max_{\mathbf{y}} \sum_{t} \log P(y_t \mid y_{<t})

The best sequence maximises the SUM of log-probabilities, not each term separately. Greedy maximises term by term, which is exactly the difference this tree exploits.

One step of the search
Bt+1  =  top-k{(y,w):yBt,  wV}    by    logPB_{t+1} \;=\; \operatorname{top-}k \Big\{ (\mathbf{y}, w) : \mathbf{y} \in B_t,\; w \in V \Big\} \;\; \text{by} \;\; \log P

Every beam is extended by every token, and the pooled candidates are cut back to k. With k = 1 this is greedy; with k equal to the whole vocabulary raised to the depth, it is exhaustive search. Everything practical sits in between.

Why the trap works
log0.5+2log0.38  <  log0.32+log0.74+log0.78\log 0.5 + 2\log 0.38 \;<\; \log 0.32 + \log 0.74 + \log 0.78

The concrete numbers from this tree: a strong first step followed by weak ones loses to a weaker first step followed by strong ones. The right side is only reachable if something other than the argmax survives level one.

Related terms

More visualisations

Building with language models?

These explainers come out of the work. If you want the same thinking applied to your own system, that is what I do.

See how I can help