NumPy allowed · plain Python earns 10 bonus points
Softmax(logits) → probability distribution
Filter vocabulary logits to the top-K highest scoring tokens before sampling.
Implement top_k_filter(logits, k, filter_value=-float('inf')). Retain only the $k$ tokens with the highest logits. Replace all other logits with filter_value. If multiple tokens tie at the cutoff, prioritize lower token indices. If $k ge ext{len}(logits)$, return an unmasked copy of logits. If $k le 0$, raise ValueError.
top_k_filter(logits: list[float], k: int, filter_value: float = -float('inf')) -> list[float]