Beam search

beam 3·O(k · B · D)All algorithms

AlgoLab runs no language model. Every probability here was written by hand into a twelve-token toy table, which you can read in full in the Data tab.

start, 0.00start0.00ka, -0.92ka-0.92mo, -1.20mo-1.20tu, -1.71tu-1.71ri, -2.12ri-2.12sel, -2.12sel-2.12nu, -2.19nu-2.19vex, -2.43vex-2.43ol, -2.53ol-2.53dra, -1.68dra-1.68pim, -2.81pim-2.81zeth, -3.51zeth-3.51qui, -3.73qui-3.73zeth, -1.94zeth-1.94vex, -4.24vex-4.24ol, -4.37ol-4.37nu, -4.71nu-4.71ri, -2.04ri-2.04zeth, -3.65zeth-3.65pim, -4.09pim-4.09qui, -4.34qui-4.34qui, -2.16qui-2.16dra, -4.46dra-4.46pim, -4.60pim-4.60ri, -4.93ri-4.93nu, -3.26nu-3.26ol, -3.43ol-3.43vex, -3.63vex-3.63qui, -3.78qui-3.78mo, -2.95mo-2.95ka, -3.42ka-3.42tu, -3.65tu-3.65sel, -3.94sel-3.94pim, -2.45pim-2.45zeth, -4.28zeth-4.28dra, -4.69dra-4.69ri, -5.16ri-5.16vex, -4.46vex-4.46sel, -4.53sel-4.53ol, -4.69ol-4.69ka, -4.97ka-4.97qui, -2.81qui-2.81zeth, -4.28zeth-4.28dra, -4.97dra-4.97ri, -5.26ri-5.26dra, -3.43dra-3.43pim, -4.56pim-4.56zeth, -5.26zeth-5.26qui, -5.48qui-5.48sel, -4.63sel-4.63nu, -4.70nu-4.70vex, -4.94vex-4.94ol, -5.03ol-5.03
Whole tree

Tap a candidate for its sequence and score; drag to pan, pinch or Ctrl+scroll to zoom.

  • Live beam

Nothing has been decoded yet. The beam holds one empty sequence, scoring 0.00.

Nothing has been decoded yet. The beam holds one empty sequence, scoring 0.00.
  1. 1Start with one empty sequence, scoring zero.
  2. 2Do the following once for each of the five token positions.
  3. 3Take each sequence the beam is still holding.
  4. 4Extend it by every token that can follow it, adding the log of that token's probability to the sequence's score.
  5. 5Rank all the candidates by score, best first, breaking ties by the order they were made.
  6. 6Keep the best k and throw the rest away.
  7. 7The best sequence left in the beam is the answer.
Sequences scored
0
Kept
0
Pruned
0

Complexity

BestO(k · B · D)
AverageO(k · B · D)
WorstO(k · B · D)
Extra spaceO(k · D)

Beam search

AlgoLab runs no language model. Every probability here was written by hand into a twelve-token toy table, which you can read in full in the Data tab.

Beam search picks a sequence one token at a time, but instead of committing to the single most likely token at each step it keeps the k best partial sequences — the beam — and extends all of them. Every extension is scored by adding the log-probability of the new token to the score of the sequence it extends, the candidates are ranked, and only the best k survive to the next step. After the last step the highest-scoring sequence in the beam is the answer.

When it is the right choice

It is the standard decoding strategy wherever a model produces a sequence and the whole sequence is what matters: machine translation, summarisation, speech recognition. It is worth the extra work exactly when a locally poor token opens onto a strong continuation, which greedy decoding can never reach because it has already thrown that branch away. It is the wrong choice when you want varied or surprising output — beam search is a search for the highest-probability sequence, and high-probability text is bland, repetitive text. Sampling methods exist for that reason, not as an inferior alternative.

How it works

Worth noticing

Where the numbers come from

The tool decodes over a table of next-token probabilities that a person typed in, held attools/beam-search/src/data/probability-tree.v1.json and shown in full in the tool’s Data tab. It has twelve nonsense tokens and thirteen rows — one for the start of a sequence and one for each token — with four possible successors each, every row summing to 1. Those thirteen rows describe a depth-five branching tree of 1,024 possible sequences, of which beam search ever builds at most sixty-nine.

Nothing about that is a limitation being hidden. Four probabilities can be read and checked; fifty thousand cannot. The reason this page can promise that its arithmetic is verifiable is the same reason it cannot promise to show you a real model.

Cost

Time
O(k · B · D)
Extra space
O(k · D)
Widest beam
4

k is the beam width, B the number of tokens that can follow any one context — four here — and D the number of decoding steps, five. The widest beam is four because a beam wider than B cannot prune anything at the first step: there are only four candidates to choose from, so every setting past four would differ in nothing you could see.

Worth comparing

All algorithms