Decoding Strategies & Sampling
Greedy, beam search, top-k, top-p (nucleus), temperature scaling, repetition penalty, contrastive decoding, and the theory of why sampling produces better text than search.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- Greedy Decoding
- Beam Search
- Temperature Scaling
- Top-k Sampling
- Top-p (Nucleus) Sampling
- Repetition Penalty
- Contrastive Decoding
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Analyze why maximization-based decoding produces degenerate text.
- Derive top-k and top-p truncation and their probability mass guarantees.
- Explain temperature as an entropy controller.
- Prove that beam search approximates MAP but not the human distribution.
- Describe contrastive decoding and its theoretical motivation.
Notation
- — next-token distribution
- — temperature
- — top-k cutoff
- — nucleus probability threshold
Core Intuition
Language models output a probability distribution over the next token. How we select from this distribution dramatically affects output quality. Counterintuitively, choosing the most probable token at each step (greedy) produces worse text than sampling from a truncated distribution. Human language is stochastic — always picking the "best" word creates repetitive, boring text.
Interactive: LLM Decoding Strategies
Next-token probability distribution (context: "The [???]")
Decoding strategy:
Low temperature → peaked distribution (confident). High temperature → flat distribution (creative/random).
Greedy Decoding
Problem: Produces degenerate text — repetitive loops, lack of diversity. The most probable sequence is often not the most natural (just as the most common word in English isn't what every sentence uses).
Beam Search
Maintain top- partial sequences, extend each by all tokens, keep top-:
Length normalization: Divide score by () to avoid favoring short sequences.
When useful: Tasks with a single correct answer (translation, summarization). NOT for open-ended generation.
Why it fails for generation: Beam search approximates the MAP sequence, but the MAP sequence of natural language is typically degenerate (short, repetitive) because probability mass is spread across many valid continuations.
Temperature Scaling
Sharpen or flatten the distribution before sampling:
- : Sharper (more deterministic, less diverse).
- : Original model distribution.
- : Flatter (more random, more diverse).
- : Greedy decoding.
- : Uniform distribution.
Entropy control: approximately — temperature linearly scales entropy.
Top-k Sampling
Only sample from the most probable tokens:
Problem: Fixed doesn't adapt to distribution shape. When the model is confident (peaked distribution), includes many low-probability tokens (adds noise). When uncertain (flat distribution), may exclude reasonable tokens.
Top-p (Nucleus) Sampling
Adaptive truncation: Include the smallest set of tokens whose cumulative probability exceeds :
Sample from with renormalized probabilities.
Advantage over top-k: Adapts to distribution shape:
- Peaked distribution → few tokens in nucleus (precise).
- Flat distribution → many tokens in nucleus (diverse).
Typical values: –.
Repetition Penalty
Penalize tokens that have already appeared in the generated text:
where penalizes repetition.
Frequency penalty (alternative): Subtract from the logit. Proportional to how often the token has appeared.
Presence penalty: Binary penalty for any token that has appeared at all.
Contrastive Decoding
Idea (Li et al., 2023): Use a small "amateur" model to identify and suppress generic/boring tokens:
Intuition: Tokens scored highly by both models are generic (common phrases). Tokens scored highly by the expert but not the amateur are more distinctive and interesting.
Implementation: Expert = large model, amateur = smaller model of same family.
Common Pitfalls
Pitfall 1. Using beam search for chatbots. Beam search produces the most probable response, which is often a generic non-answer ("I'm not sure"). Sampling produces diverse, specific responses.
Pitfall 2. Setting temperature too high for factual tasks. increases randomness — bad for math, coding, or factual QA where there's one correct answer.
Pitfall 3. Applying top-k with temperature together incorrectly. Temperature should be applied before top-k/top-p truncation (it changes the distribution shape).
Summary
- Greedy/beam search: Maximize probability → degenerate, repetitive text.
- Temperature: Controls entropy/randomness. Low for factual tasks, higher for creative.
- Top-k: Fixed number of candidates. Simple but not adaptive.
- Top-p (nucleus): Adaptive truncation based on cumulative probability. Standard choice.
- Repetition penalty: Prevents loops and repetition.
- Contrastive decoding: Expert minus amateur to encourage distinctive outputs.
Exercises
Exercise 1. For a vocabulary of 50K tokens with : compute the nucleus set for .
Exercise 2. Prove that as , temperature sampling converges to greedy decoding.
Exercise 3. Explain why the MAP sequence under an autoregressive model is often degenerate (short/repetitive), even when the model is well-calibrated.
Exercise 4. Design a decoding strategy that uses low temperature for factual claims and high temperature for creative elaboration within the same response.
Exercise 5. Compute the effective vocabulary size (number of tokens with probability) before and after temperature scaling with and .