Exercise 09 — Beam Search¶
Timebox: 45 minutes
Goal¶
Implement beam search decoding for a toy autoregressive scorer. Common at companies that hire from NLP roots.
Work in¶
beam_search.py
Tasks¶
- Take a
score_fn(prefix) -> dict[token, log_prob]so you can swap in a real model later. - Maintain
beam_widthpartial sequences with cumulative log-probs. - Expand each beam with all candidate tokens, keep the top
beam_widthby score. - Stop when all beams emit EOS or reach
max_len. - Return all completed sequences sorted by normalized score (length-normalize to avoid short-sequence bias).
Done when¶
- Hand-built
score_fncovers a small vocab and you can verify the top beam by inspection - Length normalization is implemented (mention
αin your README writeup) - You can articulate beam search vs greedy vs sampling tradeoffs
Stretch¶
- Diverse beam search (penalize beams that share tokens)
- Coverage penalty for sequence-to-sequence