Top-k & Top-p (Nucleus) Filtering

medium
Unit 3 · LLMs I0/2 to milestone

Before sampling the next token, LLM decoders usually filter the probability distribution.

Implement filter_probs(probs, top_k=None, top_p=None):

  1. If top_k is set, keep only the k most probable tokens.
  2. If top_p is set, keep the smallest set of most-probable tokens whose cumulative probability is ≥p\ge p.
  3. Zero out everything else and renormalise so the result sums to 1.

Apply top_k first, then top_p on the result. Return a NumPy array the same shape as probs.

Example

filter_probs(np.array([0.5, 0.3, 0.15, 0.05]), top_p=0.8)
# array([0.625, 0.375, 0.0, 0.0])
#llm#sampling#decoding3 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Top-k & Top-p (Nucleus) Filtering · TechGradify