Start by clarifying the input format (logits or probabilities) and whether to return indices or sampled values. Then implement top-K filtering by selecting the K highest probabilities, zeroing out the rest, renormalizing, and sampling using np.random.choice. Discuss trade-offs like handling ties, numerical stability, and efficiency for large vocabularies.
Pro tip: Mention that in practice, top-K is often applied to logits before softmax to avoid underflow, and that using np.argpartition is more efficient than full sorting for large K. Also, note that sampling should be done with replacement=False if returning multiple samples, but typically it's one sample.
Ask whether the input is a probability distribution or logits, and whether the output should be the sampled index or value. Confirm if K is a fixed integer and if sampling is with or without replacement.
Use np.argpartition to find the indices of the K largest probabilities (or logits) in O(n) time, then optionally sort those K for determinism. Alternatively, use np.argsort if simplicity is preferred.
Create a new array with only the top-K probabilities, set others to zero, and renormalize by dividing by the sum to ensure it sums to 1. If working with logits, apply softmax after filtering.
Use np.random.choice with the filtered probabilities to draw a sample. If returning multiple samples, specify replace=False and ensure K >= number of samples.
Address cases like K > number of non-zero probabilities, ties in probabilities, and numerical stability. Mention time complexity and memory considerations for large distributions.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.