Sampling from the full distribution occasionally picks a token from the long tail of unlikely, incoherent options, and top-k sampling uses a fixed number of candidates whether the model is certain or unsure. Nucleus (top-p) sampling adapts: sort tokens by probability and keep the smallest set whose cumulative probability reaches p. When the model is confident the nucleus is a token or two, when it is unsure the nucleus is wide. The kept probabilities are renormalized so they sum to one and sampling proceeds from this truncated distribution.
Implement top_p_filter.
probs is a 1-D probability vector and 0 < p <= 1. Sort descending (stable, ties by lower index first).>= p. At least one token is always kept.0.probs.np.argsort(-probs, kind='stable') gives a stable descending order.
The first index where the cumulative sum reaches p (use np.searchsorted(cum, p - 1e-12) or compare with a small tolerance) marks the end of the nucleus.
No results yet
Run your code to check it against the examples, or submit to run every test.