Min-p Sampling

medium
Unit 19 · Streaming0/3 to milestone

Top-p keeps a fixed amount of probability mass, which lets junk tokens in when the model is confident and cuts good ones when it's unsure. Min-p adapts: keep only tokens whose probability is at least min_p times the top token's probability.

Implement min_p_filter(logits, min_p=0.1, temperature=1.0) returning a renormalised probability array:

  1. Apply temperature, then a numerically stable softmax.
  2. Zero every probability below min_p * max(probs).
  3. Renormalise to sum to 1.

min_p=0 keeps everything. The top token is always kept.

#sampling#decoding#llm2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Min-p Sampling · TechGradify