More involved than it looks on the surface.
Start by clarifying the input format (logits shape, batch handling) and the expected output (single token id or per-sequence). Then outline a modular pipeline: apply temperature scaling, then top-k and/or top-p filtering, then sample or take argmax. Emphasize numerical stability and efficiency, and discuss trade-offs between strategies.
Pro tip: Mention that top-p filtering should be applied after sorting logits descending and computing cumulative probabilities, and that you must renormalize the filtered distribution before sampling. Also note that greedy is equivalent to temperature → 0 and can be handled as a special case.
Ask about input shape (batch vs single), whether strategies can be combined (e.g., top-k then top-p), and how to handle ties or empty filtered sets. Confirm output format (token id per sequence).
Divide logits by temperature (if temperature > 0) to control randomness. For greedy, skip scaling and take argmax directly.
Keep only the k highest logits, set others to -inf, and renormalize. Use torch.topk or equivalent for efficiency.
Sort logits descending, compute softmax probabilities, find the smallest set whose cumulative probability ≥ p, zero out the rest, and renormalize.
For greedy, return argmax. For stochastic, sample from the filtered distribution using torch.multinomial or equivalent. Return the token id.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.