The greedy part was fine, argmax at each step until you hit EOS, straightforward.
Start by clarifying the interface: the model returns deterministic 1D logits, so both decoding functions will repeatedly call forward, append the selected token to the input, and stop when EOS is generated. For greedy, simply take argmax at each step; for top-k sampling, apply softmax to the top-k logits, renormalize, sample, and append. Emphasize correctness, efficiency, and handling edge cases like EOS as the first token.
Pro tip: Mention that you would use torch.topk to efficiently get the top-k logits and indices, then apply softmax only to those values and renormalize—this avoids computing softmax over the full vocabulary and is more efficient. Also, note that you would detach or convert logits to probabilities appropriately to avoid gradient tracking during inference.
Confirm that the model's forward method takes a sequence of token IDs and returns a 1D tensor of logits over the fixed vocabulary. The decoding loop should stop when the EOS token is generated, and EOS should not be included in the output sequence.
Initialize the input with a start token (or empty), then loop: get logits, select argmax token, append to output, and break if it's EOS. Return the generated sequence.
At each step, get logits, select the top-k values and indices using torch.topk. Apply softmax to these top-k logits to get probabilities, renormalize (softmax already sums to 1 over top-k), then sample a token from this distribution using torch.multinomial. Append the sampled token and break if it's EOS.
Consider cases where k is larger than vocab size, or when EOS is not in the top-k (sampling might never stop—though with EOS in vocab it's possible). Also, ensure no gradient computation by using torch.no_grad() and detach tensors as needed.
Mention that greedy is deterministic and fast but can be repetitive; top-k sampling adds diversity but may require tuning k. Both are autoregressive and stop at EOS.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.