AI
8 min read

Stochastic Decoding Strategies in Large Language Models: Mathematical Foundations of Temperature Scaling, Top-k, Nucleus (Top-p), Typical Sampling, and Min-p

Autoregressive language models generate text sequentially by estimating a conditional probability distribution over a vocabulary at each decoding step. While model training optimizes cross-entropy loss over large corpora, the quality, diversity, and coherence of generated sequences depend directly on the decoding strategy used to select tokens from the output logits. Deterministic algorithms such as greedy search and beam search systematically fail in open-ended generation tasks, collapsing int

Stochastic Decoding Strategies in Large Language Models: Mathematical Foundations of Temperature Scaling, Top-k, Nucleus (Top-p), Typical Sampling, and Min-p

Autoregressive language models generate text sequentially by estimating a conditional probability distribution over a vocabulary at each decoding step. While model training optimizes cross-entropy loss over large corpora, the quality, diversity, and coherence of generated sequences depend directly on the decoding strategy used to select tokens from the output logits.

Deterministic algorithms such as greedy search and beam search systematically fail in open-ended generation tasks, collapsing into repetitive loops and dull phrasing. Conversely, unconstrained stochastic sampling from the raw distribution samples improbable tokens from the long tail of the vocabulary, causing severe semantic drift.

Modern inference engines rely on stochastic decoding algorithms that dynamically reshape, truncate, and normalize probability distributions before drawing samples. Understanding the mathematical mechanics of temperature scaling, top-k, nucleus (top-p), locally typical sampling, and min-p is essential for tuning inference pipelines across deterministic code generation, structured extraction, and open-ended reasoning.

The Autoregressive Decoding Formulation

At timestep tt, given a context sequence x<t=(x1,x2,…,xt−1)x_{<t} = (x_1, x_2, \dots, x_{t-1}), a transformer language model computes an unnormalized logit vector zt∈R∣V∣z_t \in \mathbb{R}^{|V|} over a vocabulary VV containing ∣V∣|V| discrete tokens:

zt=Wvocabhtz_t = W_{\text{vocab}} h_t

Here, ht∈Rdh_t \in \mathbb{R}^d represents the final hidden state of the transformer stack, and Wvocab∈R∣V∣×dW_{\text{vocab}} \in \mathbb{R}^{|V| \times d} denotes the output projection matrix (un-embedding layer).

The categorical distribution over candidate tokens is obtained via the standard softmax function:

P(xt=v∣x<t)=exp⁡(zt,v)∑v′∈Vexp⁡(zt,v′)P(x_t = v \mid x_{<t}) = \frac{\exp(z_{t, v})}{\sum_{v' \in V} \exp(z_{t, v'})}

In deterministic greedy decoding, the model selects the mode of the distribution at every step:

x^t=arg⁡max⁡v∈VP(xt=v∣x<t)\hat{x}_t = \arg\max_{v \in V} P(x_t = v \mid x_{<t})

As demonstrated by Holtzman et al. (2020), greedy search and beam search suffer from neural text degeneration. Because language models assign high probabilities to generic phrases and repetitive n-grams, maximizing token likelihood leads to degenerate loops and unnatural text statistics that deviate sharply from human writing. Human text exhibits localized dips in conditional probability where surprising or context-shifting words occur.

Sampling Truncation Mechanics

Temperature Scaling

Temperature scaling rescales logits by a strictly positive scalar parameter T>0T > 0 before computing the softmax distribution. Originally adapted from statistical mechanics and simulated annealing (Ackley et al., 1985), temperature controls the entropy of the resulting categorical distribution.

The temperature-adjusted probability distribution PT(v)P_T(v) is defined as:

PT(xt=v∣x<t)=exp⁡(zt,v/T)∑v′∈Vexp⁡(zt,v′/T)P_T(x_t = v \mid x_{<t}) = \frac{\exp(z_{t, v} / T)}{\sum_{v' \in V} \exp(z_{t, v'} / T)}

Limiting Behaviors

  1. As T→0+T \to 0^+, the distribution concentrates all probability mass onto the maximum logit value:

lim⁡T→0+PT(xt=v)={1if v=arg⁡max⁡u∈Vzt,u0otherwise\lim_{T \to 0^+} P_T(x_t = v) = \begin{cases} 1 & \text{if } v = \arg\max_{u \in V} z_{t, u} \\ 0 & \text{otherwise} \end{cases}

This recovers deterministic greedy argmax selection.

  1. At T=1.0T = 1.0, the sampling distribution matches the raw model output probabilities.
  2. As T→∞T \to \infty, the logits become negligible relative to TT, causing the distribution to converge to a discrete uniform distribution:

lim⁡T→∞PT(xt=v)=1∣V∣∀v∈V\lim_{T \to \infty} P_T(x_t = v) = \frac{1}{|V|} \quad \forall v \in V

Entropy Dynamics

The Shannon entropy H(PT)H(P_T) of the distribution is given by:

H(PT)=−∑v∈VPT(v)log⁡PT(v)H(P_T) = -\sum_{v \in V} P_T(v) \log P_T(v)

Differentiating H(PT)H(P_T) with respect to TT yields dH(PT)dT≥0\frac{d H(P_T)}{d T} \ge 0, establishing that entropy increases monotonically with temperature.

While temperature scales the spread of probabilities, it operates uniformly across all tokens. Increasing TT flattens the distribution and inflates the probability of low-confidence tail tokens, often generating incoherent tokens unless combined with truncation filters.

Fixed-Rank Truncation: Top-k Sampling

Top-k sampling, introduced for language models by Fan et al. (2018), restricts candidate tokens to the kk most probable items in vocabulary VV.

Let the vocabulary tokens be sorted by descending probability at timestep tt:

P(xt=v(1))≥P(xt=v(2))≥⋯≥P(xt=v(∣V∣))P(x_t = v_{(1)}) \ge P(x_t = v_{(2)}) \ge \dots \ge P(x_t = v_{(|V|)})

The top-k candidate set V(k)⊂VV^{(k)} \subset V is defined as:

V(k)={v(1),v(2),…,v(k)}V^{(k)} = \{ v_{(1)}, v_{(2)}, \dots, v_{(k)} \}

The truncated distribution Ptop-k(v)P_{\text{top-}k}(v) sets all probabilities outside V(k)V^{(k)} to zero and renormalizes the remaining mass:

Ptop-k(xt=v)={P(xt=v)∑u∈V(k)P(xt=u)if v∈V(k)0otherwiseP_{\text{top-}k}(x_t = v) = \begin{cases} \frac{P(x_t = v)}{\sum_{u \in V^{(k)}} P(x_t = u)} & \text{if } v \in V^{(k)} \\ 0 & \text{otherwise} \end{cases}

Structural Failure Modes

Top-k sampling enforces a static rank threshold regardless of the underlying distribution geometry:

  • High-Confidence (Peaked) Contexts: When the model predicts a specific grammatical continuation (for example, a closing parenthesis or common idiom where P(v(1))=0.98P(v_{(1)}) = 0.98), top-k with k=50k=50 still admits 49 highly improbable tokens into the candidate set.
  • Low-Confidence (Flat) Contexts: When the model evaluates dozens of equally plausible synonyms (for example, in creative descriptions where 200 candidates each carry P(v)≈0.004P(v) \approx 0.004), top-k with k=50k=50 arbitrarily cuts off 150 valid options.

Cumulative Probability Truncation: Nucleus (Top-p) Sampling

To address the rigidity of fixed rank thresholds, Holtzman et al. (2020) introduced nucleus sampling (top-p sampling). Nucleus sampling defines the truncation boundary dynamically by accumulating probability mass until reaching a predefined threshold p∈(0,1]p \in (0, 1].

The nucleus candidate set V(p)⊂VV^{(p)} \subset V is the smallest subset of top-ranked tokens whose cumulative probability exceeds pp:

V(p)={v(1),v(2),…,v(m)}V^{(p)} = \{ v_{(1)}, v_{(2)}, \dots, v_{(m)} \}

where mm is the minimal index satisfying:

∑i=1mP(xt=v(i))≥p\sum_{i=1}^m P(x_t = v_{(i)}) \ge p

The truncated and renormalized distribution is:

Ptop-p(xt=v)={P(xt=v)∑u∈V(p)P(xt=u)if v∈V(p)0otherwiseP_{\text{top-}p}(x_t = v) = \begin{cases} \frac{P(x_t = v)}{\sum_{u \in V^{(p)}} P(x_t = u)} & \text{if } v \in V^{(p)} \\ 0 & \text{otherwise} \end{cases}

Dynamic Candidate Pool Sizing

The size of the candidate pool ∣Vt(p)∣|V^{(p)}_t| adjusts automatically based on the conditional entropy of the distribution:

  • When the model is confident (P(v(1))>pP(v_{(1)}) > p), the nucleus collapses to a single token (∣V(p)∣=1|V^{(p)}| = 1), acting deterministically.
  • When uncertainty is high, the nucleus expands across hundreds or thousands of tokens, preserving lexical diversity.

Tail Inflation Under High Temperature

When high temperatures (T>1.0T > 1.0) are applied prior to top-p truncation, individual token probabilities flatten. If the top token's probability drops to 0.02, accumulating p=0.90p = 0.90 requires including hundreds of low-probability tail tokens, permitting unvetted low-quality generations.

Information-Theoretic Decoding

Information-Theoretic Truncation: Locally Typical Sampling

Locally typical sampling, formulated by Meister et al. (2022), approaches token selection through an information-theoretic framework known as the Expected Information Hypothesis.

In human communication, speakers balance information transfer across utterances, maintaining a relatively uniform rate of information rather than selecting either overly obvious or overly obscure words.

The information content (surprisal) of a candidate token v∈Vv \in V is:

h(v)=−log⁡2P(xt=v∣x<t)h(v) = -\log_2 P(x_t = v \mid x_{<t})

The expected information of the next token corresponds to the conditional Shannon entropy H(Xt∣x<t)H(X_t \mid x_{<t}):

H(Xt∣x<t)=∑v∈VP(xt=v∣x<t)⋅h(v)=−∑v∈VP(xt=v∣x<t)log⁡2P(xt=v∣x<t)H(X_t \mid x_{<t}) = \sum_{v \in V} P(x_t = v \mid x_{<t}) \cdot h(v) = -\sum_{v \in V} P(x_t = v \mid x_{<t}) \log_2 P(x_t = v \mid x_{<t})

Locally typical sampling constructs a candidate set V(typ)⊂VV^{(\text{typ})} \subset V by selecting tokens whose surprisal is closest to the conditional entropy:

V(typ)={v∈V∣∣h(v)−H(Xt∣x<t)∣≤ϵ}V^{(\text{typ})} = \{ v \in V \mid |h(v) - H(X_t \mid x_{<t})| \le \epsilon \}

Tokens are sorted by their absolute deviation from the expected information:

Δ(v)=∣−log⁡2P(xt=v∣x<t)−H(Xt∣x<t)∣\Delta(v) = |-\log_2 P(x_t = v \mid x_{<t}) - H(X_t \mid x_{<t})|

Tokens with minimal Δ(v)\Delta(v) are accumulated until their cumulative probability mass reaches threshold pp:

∑v∈V(typ)P(xt=v∣x<t)≥p\sum_{v \in V^{(\text{typ})}} P(x_t = v \mid x_{<t}) \ge p

By favoring tokens whose information density matches the local context entropy, typical sampling suppresses both repetitive boilerplate tokens (which have near-zero surprisal) and nonsensical tail tokens (which have extreme surprisal).

Relative Probability Truncation: Min-p Sampling

Min-p sampling, proposed by Menet et al. (2024), introduces an adaptive truncation threshold scaled directly against the highest-probability token in the distribution.

Rather than defining an absolute cumulative mass pp or rank kk, min-p defines a base scaling factor pbase∈[0,1]p_{\text{base}} \in [0, 1]. The minimum probability threshold pthresholdp_{\text{threshold}} at step tt is calculated as:

pthreshold=pbase×max⁡v∈VP(xt=v∣x<t)p_{\text{threshold}} = p_{\text{base}} \times \max_{v \in V} P(x_t = v \mid x_{<t})

Let vtop=arg⁡max⁡v∈VP(xt=v∣x<t)v_{\text{top}} = \arg\max_{v \in V} P(x_t = v \mid x_{<t}) denote the top candidate. The candidate set V(min-p)V^{(\text{min-}p)} retains only tokens whose probability is at least pbasep_{\text{base}} times that of vtopv_{\text{top}}:

V(min-p)={v∈V∣P(xt=v∣x<t)≥pbase⋅P(xt=vtop∣x<t)}V^{(\text{min-}p)} = \{ v \in V \mid P(x_t = v \mid x_{<t}) \ge p_{\text{base}} \cdot P(x_t = v_{\text{top}} \mid x_{<t}) \}

The truncated distribution is renormalized:

Pmin-p(xt=v)={P(xt=v)∑u∈V(min-p)P(xt=u)if v∈V(min-p)0otherwiseP_{\text{min-}p}(x_t = v) = \begin{cases} \frac{P(x_t = v)}{\sum_{u \in V^{(\text{min-}p)}} P(x_t = u)} & \text{if } v \in V^{(\text{min-}p)} \\ 0 & \text{otherwise} \end{cases}

Behavioral Advantages

Min-p adapts dynamically across confidence regimes:

  • Confident Contexts: If P(vtop)=0.80P(v_{\text{top}}) = 0.80 and pbase=0.10p_{\text{base}} = 0.10, the threshold is 0.080.08. All tokens below 8% probability are immediately pruned, preventing low-likelihood noise.
  • Uncertain Contexts: If P(vtop)=0.10P(v_{\text{top}}) = 0.10 and pbase=0.10p_{\text{base}} = 0.10, the threshold becomes 0.010.01. Any token with at least 1% probability remains eligible.

Because the cutoff scales proportionally with the top candidate, min-p maintains stability even at high temperatures (T=1.2T = 1.2 to 1.51.5). It allows creative exploration among competing top candidates while rigorously excluding tail tokens.

Perplexity-Controlled Decoding: Mirostat

Mirostat, developed by Basu et al. (2021), treats text generation as a closed-loop control problem. Standard samplers often drift over long generations into either:

  1. The Boredom Trap: Perplexity collapses, and the model enters repetitive loops.
  2. The Confusion Trap: Perplexity escalates, and the model produces incoherent outputs.

Mirostat regulates the generation rate of surprise to match a target surprisal τ\tau.

At each step tt, the algorithm observes the surprise of the sampled token xtx_t:

s(xt)=−log⁡2P(xt∣x<t)s(x_t) = -\log_2 P(x_t \mid x_{<t})

The control error ete_t is:

et=s(xt)−τe_t = s(x_t) - \tau

A feedback parameter μt\mu_t is updated via gradient descent:

μt+1=μt−ηet\mu_{t+1} = \mu_t - \eta e_t

where η\eta is the learning rate.

The truncation parameter (such as dynamic top-k or top-p threshold) is then derived as a function of μt+1\mu_{t+1} using estimated Zipfian power-law parameters of the vocabulary distribution. By dynamically widening or narrowing candidate pools in response to observed surprisal, Mirostat enforces stable cross-entropy across thousands of generated tokens.

Decoding Strategies in Production

Each decoding strategy optimizes for distinct trade-offs between computational overhead, vocabulary coverage, and sample coherence:

  • Greedy Search (T=0T = 0): Deterministic mode selection. Ideal for code compilation, mathematical reasoning, and schema-constrained JSON extraction where reproducibility is paramount.
  • Temperature + Top-p: General standard for conversational assistants. Standard settings (T=0.7,p=0.9T = 0.7, p = 0.9) provide balance, though prone to degradation if temperature is pushed past 1.01.0.
  • Min-p Sampling: Recommended for creative writing, narrative generation, and high-temperature exploration (T≥1.0,pbase=0.05–0.10T \ge 1.0, p_{\text{base}} = 0.05\text{--}0.10). Prunes low-confidence tail tokens without restricting multi-candidate exploration.
  • Locally Typical Sampling: Well-suited for stylized narrative and essay generation where uniform information density improves prose cadence.
  • Mirostat: Preferred for long-form synthetic book generation, long-horizon narrative planning, and autonomous agents prone to degradation over extended context horizons.

Sources

AILLMEducationExplainers