Top-p, Top-k & min-p (Sampling Parameters)
write the sampler yourself and the knobs stop being magic
AI engineeringAI engineering / inference & decoding
The entire sampler, 8 lines
Every "creativity" knob you've ever set lives in this function. Nothing else.
def sample(logits, temp=1.0, top_k=0, top_p=1.0):
logits = logits / temp # 1 warp
if top_k:
kth = logits.topk(top_k).values[-1]
logits[logits < kth] = -float("inf") # 2 hard cap
p = logits.softmax(-1) # 3 to probabilities
p, idx = p.sort(descending=True) # 4
p[p.cumsum(-1) - p > top_p] = 0 # 5 nucleus cut
return idx[torch.multinomial(p / p.sum(), 1)] # 6 renormalize + draw
What each line actually teaches
- Line 1 —
tempdivides logits, before any probability exists. temp<1 sharpens, temp>1 flattens. temp→0 isargmax. - Line 2 —
top_kkeeps a fixed count. Blunt: k=40 is too wide when the model is certain, too narrow when it's genuinely torn. - Line 5 —
top_pkeeps the smallest set whose probability sums to p. The- pis the exclusive cumsum, so the token that crosses the line stays in. This is the detail most reimplementations get wrong. - Line 6 — survivors are renormalized. So top_p=0.9 does not mean "10% of the time you get garbage." The tail is deleted, not down-weighted.
The trap: order of operations
Temperature runs on line 1. Truncation runs on lines 2 and 5. So top_p never sees the model's real distribution — it sees whatever temperature already did to it.
Next token after "The capital of France is":
temp=1.0 Paris .90 Lyon .04 Nice .02 ...
top_p=.9 → nucleus = {Paris} 1 candidate
temp=2.0 Paris .45 Lyon .19 Nice .13 ...
top_p=.9 → nucleus = 7 candidates "Lyon" is now live
Same top_p=0.9. You did not widen the nucleus — temperature did, and top_p obediently let it through. This is why temperature=1.4, top_p=0.9 still produces nonsense and people conclude top_p "doesn't work."
min-p: the threshold that scales itself
min_p keeps tokens with p >= min_p * p_max — relative to the top token, not to a running sum.
min_p=0.1, Paris .90 → cutoff .090 → {Paris} confident: tail chopped
min_p=0.1, flat .12 → cutoff .012 → ~20 tokens unsure: room to breathe
It tightens exactly where the model is sure and loosens where it isn't, which is the behaviour people wanted from top_p. Supported in llama.cpp, vLLM and Ollama; not in the OpenAI/Anthropic HTTP APIs, so on hosted models you're tuning temp + top_p.
Two more real ones
- Don't stack three knobs. Pick one truncator.
top_k=40+top_p=0.9+min_p=0.05→ whichever is strictest silently wins, and your "tuning" is noise. - temp=0 ignores top_p entirely. Filing a bug that "top_p had no effect" at temp 0 is a classic. Line 1 already collapsed the distribution.
Your exercise
Take a local model (Ollama/llama.cpp). Prompt: "Name a colour:" and generate 20 completions at each setting:
temp=0.8, top_p=0.95temp=1.6, top_p=0.95temp=1.6, min_p=0.1
Count distinct answers in each. Run 2 should spray junk; run 3 should stay plausible at the same temperature. That gap is the whole lesson.