Before you move a control in the demo, say what you expect to change.
Foundation reference
Decoding & Sampling: Temperature, Top-p & Inference-Time Control
Decoding turns a model's next-token distribution into a token sequence. Settings such as temperature, top-p and top-k change how each token is drawn, and so whether the same trained model produces repetitive or varied text.
Study guide
Where to start
Why is decoding more than formatting, given that it changes the distribution the output is drawn from?
How to study this pagePredict · Check · Generalize · Continue
Compare what the demo shows with the key equation and with the papers cited below.
Identify the part of the idea that survives a change of notation or model, so you can recognize it in another paper or system.
The newer notebook covers the same concept and has an interactive demo.
Why It Matters for Modern Models
- Decoding turns a model's next-token distribution into a token sequence. Settings such as temperature, top-p and top-k change how each token is drawn, and so whether the same trained model produces repetitive or varied text.
- Decoding controls the balance between determinism and diversity and affects failure modes such as repetition: Holtzman et al. (2020) found that maximization-based decoding such as beam search produces bland, repetitive text, which nucleus sampling reduces.
- Changing the decoding procedure needs no training: behavior can be adjusted without touching the weights.
- Sampling choices affect reliability: a low temperature reduces variation between runs, but does not prevent the most probable answer from being wrong.
- LLM sampling and diffusion guidance both expose a setting that trades diversity for fidelity to the prompt or condition.
Questions to Think About
- Why is "temperature controls creativity" an imprecise description, and what does temperature actually do to the softmax?
- How does top-p adapt to the entropy of each step's distribution, and why does that make it behave differently from a fixed top-k?
- How much of the repetition problem comes from the decoding procedure rather than from training, and which decoding settings reduce it?
- In diffusion models, why does very strong guidance degrade images instead of making them ever more faithful to the prompt?
Interactive Demo
Core Math
If you prefer intuition first, try the demo above, then come back for the math.
Training gives a next-token distribution . Decoding is the step that turns these probabilities into text.
1) Temperature reshapes the softmax
Given logits for tokens , temperature gives:
Lower sharpens the distribution and higher flattens it. As sampling approaches greedy decoding, which always picks the most likely token.
2) Top-k and nucleus (top-p) truncation
Top- sampling keeps only the most likely tokens (Fan et al., 2018). Nucleus sampling keeps a variable number: let be the smallest set of tokens, taken in order of decreasing probability, whose total probability is at least (Holtzman et al., 2020):
Then sample from the truncated, renormalized distribution:
Truncation therefore changes the distribution being sampled from, not just the surface form of the output. Locally typical sampling instead keeps tokens whose information content is close to the entropy of the distribution (Meister et al., 2023).
3) The same idea in diffusion: guidance
Classifier-free guidance in diffusion models also reshapes a distribution at sampling time, with a scale that trades diversity for fidelity to the conditioning (Ho & Salimans, 2021):
In both cases the weights stay fixed; only the sampling procedure changes.