New Tool Radar

AI Concepts

How to Choose Between Beam Search and Greedy Decoding

Use greedy when the output is short and the best next token is obvious, and beam search when the whole sequence matters more than each step.

Schematic with three steps: Try greedy first on short outputs; Try beam search on grounded tasks; Check the cost and the repetition.

Greedy search is the default decoding strategy: it selects the next most likely token at each step, and it works well for tasks with relatively short outputs where creativity is not a priority. Beam search keeps track of several generated sequences at each time step and, after a certain number of steps, selects the sequence with the highest overall probability, so it can look ahead and pick a sequence whose overall probability is higher even if its first tokens are less likely. Beam search is best suited for input-grounded tasks, like describing an image or speech recognition.

What you need

  • A generation task with a known good answer
  • The ability to set num_beams in the generation call
  • A note of how long the output will be

Step 1: Try greedy first on short outputs

Run the task with greedy decoding, which is num_beams 1. If the output is short and correct, beam search adds cost for no benefit.

Step 2: Try beam search on grounded tasks

Set num_beams to a value greater than 1, for example 2 or 4, on a task where the output must match the input, such as a caption or a transcription. Compare the result with greedy on the same input.

Step 3: Check the cost and the repetition

Beam search costs more compute per token, and greedy breaks down on longer sequences because it begins to repeat itself. Pick the method whose output is correct at an acceptable cost, and record the choice with the task.

Worked example: synthetic beam search versus greedy check

The table below is a synthetic example. It shows the expected result of comparing beam search and greedy; no text was generated.

Task Greedy result Beam search result
Short factual answer Correct Correct – slower
Image caption Misses a detail Correct – best overall sequence
Long story Repeats itself Less repetition
Decision Greedy for short answers Beam search for grounded tasks

Expected result (UNTESTED). greedy wins on short answers for speed, and beam search wins on the caption and the long story. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • Beam search with num_beams 1 is equivalent to greedy search, so set it above 1 to change anything.
  • You can use do_sample with beam search to sample at each step, but beam search still greedily prunes low-probability sequences between steps.
  • Early stopping controls when beam search stops; the default heuristic stops when it is very unlikely to find better candidates.
  • Beam search does not add creativity; for diverse or creative output, use sampling instead.

Sources

See all