Skip to content

What is the difference between greedy decoding and beam search?

Beginner · 2 min read · LLM Fundamentals

Open in study mode to save this question and track progress

Short answer: Greedy decoding selects the highest probability single token at each step (O(1)O(1) breadth); Beam search maintains BB candidate sequences (beams) in parallel, selecting the sequence with the highest cumulative log-probability over time.

Detail

Autoregressive decoding generates text sequence Y=(y1,y2,…,yT)Y = (y_1, y_2, \dots, y_T) given prompt XX:

arg⁡max⁡YP(Y∣X)=∏t=1TP(yt∣y<t,X)\arg\max_Y P(Y|X) = \prod_{t=1}^T P(y_t | y_{<t}, X)

Decoding Comparison

  1. Greedy Decoding: Makes a locally optimal choice at step tt. If an early token leads to poor global sequence probabilities later, greedy decoding cannot backtrack or recover.
  2. Beam Search: Retains a beam width BB (e.g. B=4B=4). At each step, it expands all BB paths into B×VB \times V candidates and keeps the top BB cumulative log-probability sequences:

Score(Y)=1Tα∑t=1Tlog⁡P(yt∣y<t,X)\text{Score}(Y) = \frac{1}{T^\alpha} \sum_{t=1}^T \log P(y_t | y_{<t}, X)

Where TαT^\alpha normalizes for sequence length to avoid favoring short completions.

MetricGreedy DecodingBeam Search
Compute / MemoryLow (Single state pass)High (B×B \times forward passes & KV cache streams)
Primary Use CasesChat, code generation, real-time streamingTranslation, summarization, deterministic sequence parsing
Streaming CompatibilityHighLow (Must evaluate full beams before outputting)

Example

Python concept illustrating greedy vs beam candidate tracking:

python
# Greedy decoding step
next_token = logits.argmax(dim=-1)

# Beam search step (B=2 candidate sequences)
top2_probs, top2_indices = torch.topk(torch.softmax(logits, dim=-1), k=2)
beams = [
    {"seq": [idx.item()], "score": torch.log(prob).item()}
    for prob, idx in zip(top2_probs[0], top2_indices[0])
]
print("Top 2 initial beams:", beams)

Interview tips

  • Note that beam search can produce repetitive text in long-form generation, which is why sampling (T,p,kT, p, k) is preferred for chat models.
  • Explain why beam search is incompatible with real-time token streaming UIs.

⬅ Back to LLM Fundamentals · All topics