Greedy, top-k and top-p decoding for a language model

0
Remix

You get a toy language model with a small vocabulary whose forward pass returns next-token logits for the tokens so far. Using NumPy, write the generation loop and three ways to pick the next token:

  1. Greedy: always the most likely token.
  2. Top-k: sample from the k most likely tokens after renormalizing.
  3. Top-p (nucleus): sample from the smallest set of tokens whose probabilities add up to at least p.

Then explain what temperature does, when you would pick each method, and the edge cases (ties, p close to 0 or 1, k larger than the vocabulary). Some interviewers open with questions on how a transformer produces those logits.

CohereMistral AICodingLast asked Sep 2026
No answer yet. Write the first one; the best answer gets endorsed.