Softmax, temperature, top-k and top-p

50 tokens·T = 1·k = 5·p = 0.9All algorithms

AlgoLab runs no language model. These fifty tokens and their logits were written by hand, following the rule named in the Preset tab — nothing here was computed by a model. A real model chooses from a vocabulary of tens of thousands of tokens, and every logit comes from a forward pass through billions of learned weights, not from a rule a person wrote down.

These are the 50 authored logits for the preset “One clear leader”: Token 0 gets logit 9. Tokens 1 to 9 count down from 8 to 0. Tokens 10 to 49 are a flat tail at -1. The largest is “the” at 9.

These are the 50 authored logits for the preset “One clear leader”: Token 0 gets logit 9. Tokens 1 to 9 count down from 8 to 0. Tokens 10 to 49 are a flat tail at -1. The largest is “the” at 9.
  1. 1Start with one logit per token, authored for this preset.
  2. 2Divide every logit by the temperature.
  3. 3Turn the scaled logits into probabilities that add up to one.
  4. 4Rank every token by probability and mark the k highest to keep.
  5. 5Zero every other token and rescale the kept ones to add up to one again.
  6. 6Rank every token by probability and keep the smallest group whose total reaches p.
  7. 7Zero every other token and rescale the kept ones to add up to one again.
  8. 8Finished: softmax, top-k and top-p each produced their own distribution from the same logits.

What temperature does to this preset

The largest probability and the entropy, computed at each offered temperature, for the preset on screen now.
TLargest probabilityEntropy (bits)
0.586.47%0.6614 bits
0.776.03%1.0453 bits
163.14%1.5179 bits
1.547.54%2.2838 bits
235.79%3.1386 bits

Dividing every logit by a number greater than one cannot raise the largest probability, and dividing by a number less than one cannot lower it — a property of the arithmetic, checked over 50,000 random rows, not only over this preset.

Probabilities computed
0
Rankings built
0
Tokens cut
0
Distributions renormalised
0

Complexity

BestO(n log n)
AverageO(n log n)
WorstO(n log n)
Extra spaceO(n)

Softmax, temperature, top-k and top-p

AlgoLab runs no language model. These fifty tokens and their logits were written by hand, following the rule named in the Preset tab — nothing here was computed by a model. A real model chooses from a vocabulary of tens of thousands of tokens, and every logit comes from a forward pass through billions of learned weights, not from a rule a person wrote down.

softmaxᵢ(x) = exp(xᵢ) / Σⱼ exp(xⱼ)

Three tools a language model uses to turn a row of scores into a choice. Softmax turns fifty authored logits into probabilities that add up to one. Temperature divides every logit by a constant before softmax, which changes how concentrated or spread out the resulting probabilities are. Top-k keeps only the k highest-probability tokens and rescales their probabilities to add up to one again; top-p keeps the smallest group of highest-probability tokens whose probabilities already add up to at least p, then does the same rescaling.

Why it matters, and what it costs

This is the last step of choosing a token at generation time, after a model has already produced one score per vocabulary entry. Plain softmax at temperature 1 is the baseline. Lowering the temperature makes the probabilities more concentrated on the already-largest scores; raising it spreads them out — both are measured on this page, not asserted. Top-k and top-p exist because sampling from the full, barely-above-zero tail of a large vocabulary occasionally draws a token that fits poorly; both cut that tail off, in different ways, before a token is drawn.

How it works

Worth noticing

Where the numbers come from

Every logit on this page was written by a person, following a simple rule stated for each of the three presets. They are held attools/sampling/src/data/vocabulary.v1.json and shown in full in the tool’s Vocabulary tab, alongside the rule that produced them. Nothing is derived from what the token names mean — the fifty names are an arbitrary label set, picked for readability, and swapping any one of them would not change a single number here.

What temperature does

Dividing every logit by a number greater than one cannot raise the largest probability, and dividing by a number less than one cannot lower it — a property of the arithmetic, checked over 50,000 randomly generated rows, true of any logits at all. On the preset with one clear leader, the largest probability moves from 86.47% at T = 0.5 to63.14% at T = 1 to 35.79% at T = 2. On the near-uniform preset, across the same three temperatures it moves only from4.31% to 2.95% to 2.43% — because temperature changes the *shape* of a distribution, and there is very little shape to change when the logits already sit close together. Both are measured intools/sampling/test/sampling.test.ts, not asserted.

What top-k and top-p do, and where they disagree

Top-k keeps a fixed number of the highest-probability tokens; top-p keeps however many are needed to reach a cumulative probability. On the preset with a tied pair at the top, that difference is visible rather than theoretical: the two leading tokens are exactly tied at45.42% each. Keeping only the single highest-probability token (top-1) keeps one of them and drops the other, even though nothing distinguishes them — whichever is listed first in the vocabulary survives. Top-p at p = 0.5 keeps both, because one tied token alone falls just short of 50% and the rule is to keep the smallest group that reaches the target, not the smallest number of tokens.

What this does not show

This page does not draw a token. It shows the probabilities a model would draw from, at every stage, as numbers; the actual draw is a single weighted random choice over whichever distribution is on screen, and nothing about that step is specific to language models. Top-k and top-p are shown independently here, both applied to the same temperature-scaled distribution — not chained into one specific production pipeline, since more than one real serving stack composes them differently. And a real model's vocabulary and weights are far larger than anything on this page: tens of thousands of tokens, scored by a forward pass through billions of learned parameters, not by a rule a person wrote down.

Cost

Time
O(n log n)
Extra space
O(n)
Steps recorded
8, always

n is the vocabulary size. Softmax itself is O(n); ranking the vocabulary for top-k or top-p is O(n log n), which is the term that dominates.

Unlike every Track A tool here, this one has no ceiling and no input to refuse. The vocabulary size is fixed by the data file, so the step count — eight, always — is a constant: the preset changes which fifty numbers are used, and temperature, k and p each change a value inside the same eight-step pipeline, never its shape. What is validated instead is the data file itself, at load, because that is the part a person could edit.

Source

Holtzman et al., “The Curious Case of Neural Text Degeneration” (2020) introduces nucleus (top-p) sampling. Radford et al., “Language Models are Unsupervised Multitask Learners” (2019) uses a 50,257-token vocabulary, cited here for the scale of a real one. Brown et al., “Language Models are Few-Shot Learners” (2020) reports a 175-billion-parameter model, cited for the scale of a real forward pass.

Worth comparing

All algorithms