NumPy allowed · plain Python earns 10 bonus points
Softmax(logits) → probability distribution
Dynamically adapt sampling vocabulary width using Holtzman et al. Nucleus (Top-P) filtering.
Implement top_p_filter(logits, p, filter_value=-float('inf')). Compute softmax probabilities. Sort token indices descending by probability (breaking ties by lower index). Accumulate probabilities until the cumulative sum is $ge p$. Retain this nucleus of tokens and mask all other tokens with filter_value. If $p le 0.0$ or $p > 1.0$, raise ValueError. At least one token (the top token) must always be retained.
top_p_filter(logits: list[float], p: float, filter_value: float = -float('inf')) -> list[float]