Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Choosing the next word
A language model doesn’t write text directly: at each step it gives a chance to every possible next word. Decoding is the rule for choosing. Always taking the top word sounds safe but leads to dull, repeating text. Picking at random by the chances is lively but sometimes picks a silly word. In practice we sample but first cut off the unlikely words, and use a ‘temperature’ knob to make choices bolder or safer. The demo lets you turn these knobs.
For tasks with one right answer (extraction, code, classification), use greedy or low temperature; for open-ended writing, sample with top-p or min-p truncation.
- 1. Greedy decoding picks…
- 2. Higher temperature makes output…
- 3. Top-p keeps…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Greedy: argmax at each step. Beam search: keep the B best partial sequences by total log-probability; length-normalise scores so that short outputs are not favoured.
- Temperature T: p ∝ exp(logit/T); T < 1 sharpens, T > 1 flattens, T → 0 is greedy.
- Top-k: keep the k most likely tokens. Fixed k is too many when the model is confident and too few when it is not.
- Top-p (nucleus): keep the smallest set with total probability ≥ p; adapts to the distribution’s shape.
- Min-p: keep tokens with probability ≥ p_min × the top token’s probability; scales the cutoff with confidence and stays robust at high temperature.
- Repetition and frequency penalties: lower the logits of tokens already generated.
- Degeneration: maximisation-based decoding produces repetition loops in open-ended generation (Holtzman et al., 2020), because high-probability text is not human-like text.
- 1. Beam search keeps…
- 2. Min-p's threshold is relative to…
- 3. Maximisation decoding in open-ended text tends to…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Decoding in code
Exercise 1.
- Apply temperature to a distribution.
- Implement top-k and top-p truncation.
- Implement min-p truncation.
Practice.
- Hugging Face
generatesupportsdo_sample,temperature,top_k,top_p,min_p,num_beams,repetition_penalty. - Provider APIs expose temperature and top-p; set one, not both aggressively.
- Defaults: chat assistants often use T ≈ 0.7 with top-p 0.9–0.95; reasoning models use their recommended settings (often T 0.6).
- For deterministic evaluation use greedy decoding and fixed seeds, and report the settings.
out = model.generate(**tok(prompt, return_tensors="pt"),
do_sample=True, temperature=0.7, top_p=0.9, max_new_tokens=200)
out = model.generate(**inputs, do_sample=True, temperature=1.2, min_p=0.1) # creative, still coherent
out = model.generate(**inputs, num_beams=4, length_penalty=1.0) # e.g. translation- 1. Hugging Face argument for nucleus sampling:
- 2. For deterministic evaluation use…
- 3. Beam search suits…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Why the tail is unreliable. A model trained by maximum likelihood must give some probability to every token to avoid infinite loss, and smoothing or limited data spreads probability over continuations that never occur. Each one is unlikely, but together they can hold several percent of the mass. Over hundreds of steps, plain sampling almost surely draws some of them, and one bad token can derail the rest of the text.
Why maximisation repeats. Once a phrase appears, the model (and any n-gram-like statistics it has learned) makes repeating it more likely, which makes greedy or beam search repeat it again: a positive feedback loop. Human text is not the most probable sequence; its per-token likelihood sits near the source’s entropy, not at the maximum.
Truncation trade-off. Truncation removes the tail but reduces diversity. Min-p’s cutoff scales with the top probability: when the model is confident it keeps few tokens; when it is uncertain it keeps many. This lets high temperatures add diversity among plausible tokens without admitting the tail.
- 1. Human text's per-token likelihood is near…
- 2. Repetition loops are a…
- 3. Truncation trades…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.How do decoding methods trade off coherence, error rate and diversity?
History.
- A. Fan, M. Lewis and Y. Dauphin, “Hierarchical neural story generation” (2018): top-k sampling.
- A. Holtzman et al., “The curious case of neural text degeneration” (ICLR 2020): nucleus sampling.
- M. Nguyen et al., “Turning up the heat: min-p sampling” (ICLR 2025).
Exercise 2.
- Setup: the trigram source of F1-2 (50 tokens). A Kneser–Ney model trained on 30 000 sampled tokens generates 100 tokens after each of 40 random prompts.
- Metrics:
- error rate: fraction of generated tokens the true source gives probability below 0.001;
- true-source NLL per token (the source’s own text sits at about 1.63);
- distinct-4: unique 4-grams / all 4-grams;
- loop fraction.
| method | error rate | true NLL | distinct-4 |
|---|---|---|---|
| true source text | 0.0018 | 1.634 | 0.610 |
| greedy | 0 | 1.081 | 0.015 |
| beam search (4) | 0 | 0.985 | 0.020 |
| sampling T = 1 | 0.0262 | 1.807 | 0.609 |
| sampling T = 0.7 | 0.0063 | 1.439 | 0.436 |
| top-k 10 | 0.0077 | 1.661 | 0.571 |
| top-p 0.9 | 0.0057 | 1.501 | 0.456 |
| min-p 0.1 | 0.0032 | 1.470 | 0.462 |
| sampling T = 1.5 | 0.1710 | 2.932 | 0.845 |
| min-p 0.1 at T = 1.5 | 0.0147 | 1.886 | 0.658 |
- Maximisation degenerates: greedy and beam search never made an error but looped (greedy looped in every sequence). Their NLL is far below the source’s: too probable to be natural.
- Plain sampling matches diversity but makes 14× more errors than the source’s own rate.
- Truncation cuts errors 3–8× at some cost in diversity; T = 0.7, top-p 0.9 and min-p 0.1 were similar, with min-p lowest.
- Min-p rescues high temperature: at T = 1.5 it cut errors from 17% to 1.5% while keeping distinct-4 above plain sampling. Seeds 1 and 2 gave the same pattern.
- Caveat: a trigram model’s tail errors are larger than a modern LLM’s, so the gaps are exaggerated, but the direction matches large-model studies.
Decoding in AI, robotics and aerospace
- Assistants: temperature and top-p settings shape tone and reliability; code and extraction use greedy or low temperature.
- Robotics: VLA models that output action tokens typically decode greedily for repeatability.
- Safety-critical text: procedures and reports generated with deterministic decoding are reproducible and auditable.
References
- A. Holtzman, J. Buys, L. Du, M. Forbes and Y. Choi (2020). “The curious case of neural text degeneration.” ICLR.
- A. Fan, M. Lewis and Y. Dauphin (2018). “Hierarchical neural story generation.” ACL.
- M. N. Nguyen et al. (2025). “Turning up the heat: min-p sampling for creative and coherent LLM outputs.” ICLR.
- Y. Wu et al. (2016). “Google’s neural machine translation system.” arXiv:1609.08144 (length normalisation).