Greedy and beam search decoding for a language model
I was asked this
No reports yet. Sign in to say where you were asked it.
You are writing the decoding step for a language model. Decoding turns the model's per-step scores into a sequence of token ids.
Part 1: greedy decoding
Implement greedy_decode(scores: list[list[float]]) -> list[int].
-
scores[i][j]is the score of tokenjat time stepi. Each step is independent of the others. - Return one token index per step: the token with the highest score at that step.
- If two or more tokens tie for the highest score, choose the smaller index.
- Scores can be negative. An empty
scoresreturns[].
Example: scores = [[2.0, 5.0, 1.0], [3.0, 3.0, -1.0]] returns [1, 0] (step 1 is a tie, so index 0 wins).
Part 2: beam search
In a real model the scores at a step depend on the tokens already chosen, so the best token now can lead to a worse sequence. Implement:
beam_search(step_fn, beam_width: int, max_len: int, eos: int | None = None) -> tuple[list[int], float]
-
step_fn(prefix: tuple[int, ...])returns a list ofVlog-probabilities (all<= 0) for the next token, given the tokens chosen so far. - The score of a sequence is the sum of the log-probabilities of its tokens.
- Keep at most
beam_widthpartial sequences at each step. Return the highest-scoring sequence found and its score. - A sequence that emits
eosis complete (eosis included in it) and is not extended further. Stop aftermax_lentokens or when no partial sequence remains. - Break score ties by choosing the lexicographically smaller sequence.
- Raise
ValueErrorifbeam_width < 1ormax_len < 0.
Example (probabilities shown; step_fn returns their logs, V = 2, max_len = 2):
| prefix | P(token 0) | P(token 1) |
|---|---|---|
() |
0.6 | 0.4 |
(0,) |
0.5 | 0.5 |
(1,) |
0.9 | 0.1 |
With beam_width = 1 (greedy) the result is [0, 0] with probability 0.30. With beam_width = 2 the result is [1, 0] with probability 0.36, score log(0.36).
Constraints
-
1 <= V <= 50,000,1 <= beam_width <= 32,0 <= max_len <= 512. - State the time and space complexity of each function. Calls to
step_fnare expensive; say how many your solution makes.
Reconstructed from the reported question; details may differ from what interviewers ask.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to submit your code; every attempt is kept here.


