beam search decoding

When a language model finishes a forward pass it does not hand you a word. It hands you a probability distribution over its entire vocabulary, and *decoding* is the policy you use to turn that distribution into the next token. The model is the same every time; the sampler is the dial you actually control at inference. Two people running the identical model can get a crisp deterministic answer or a wild creative riff purely by choosing different decoding settings.\n\n**Greedy decoding takes the single most likely token at every step.** It is fast, reproducible, and locally optimal, but it is also myopic: always grabbing the top token can walk the model into bland, repetitive, or degenerate loops because the globally best sentence sometimes starts with a locally second-best word.\n\n**Beam search widens the search by keeping the *k* most probable partial sequences alive at once**, extending all of them and pruning back to the top *k* each step. It reliably finds higher-probability full sequences and is the workhorse of machine translation and summarization, where there is roughly one correct answer. For open-ended generation it tends to produce safe, generic text and can collapse the beams onto near-duplicates.\n\n**Temperature reshapes the distribution before you sample from it** by dividing the logits by a scalar T inside the softmax. T below 1 sharpens the distribution and concentrates mass on the top tokens (more conservative); T above 1 flattens it and hands probability to the long tail (more diverse and more error-prone). T = 1 leaves the model's native distribution untouched, and T approaching 0 collapses back to greedy.\n\n**Top-k sampling truncates the candidate set to the k highest-probability tokens**, renormalizes, and samples from just those. It kills the long tail of absurd tokens, but a fixed k is a blunt instrument: when the model is confident, k is too generous, and when it is unsure, k is too stingy.\n\n**Top-p (nucleus) sampling truncates by cumulative probability mass instead of by count** — it keeps the smallest set of tokens whose probabilities sum to p (say 0.9) and samples from that. The candidate set breathes: it shrinks to a couple of tokens when the model is certain and expands to dozens when it is not, which is why top-p is the most widely used default for chat and creative generation. In practice teams stack a modest temperature with top-p and leave the rest alone.\n\n| Method | Determinism | Diversity | Best for | Failure mode |\n|---|---|---|---|---|\n| Greedy | Deterministic | None | Short factual answers, code | Repetition, blandness |\n| Beam search (k) | Deterministic | Low | Translation, summarization | Generic, near-duplicate beams |\n| Temperature (T) | Stochastic | Tunable | Global creativity knob | High T -> incoherence |\n| Top-k | Stochastic | Medium | Cutting the absurd tail | Fixed k mis-sizes the set |\n| Top-p / nucleus | Stochastic | Adaptive | Chat, open-ended text | Very high p -> drift |\n\n```svg\n\n \n Sampling — Turning Next-Token Probabilities into Text\n the model scores every token; the decoding strategy decides which one to actually emit — and how much risk to take\n\n \n Top-k (k = 3)\n \n keep a fixed number of\n candidates, renormalize, sample\n kept\n tail discarded\n\n \n Top-p / nucleus (p = 0.90)\n \n smallest set whose probs sum\n to p — count adapts to confidence\n the nucleus\n\n \n Temperature: softmax(z / T)\n \n \n \n \n T < 1 sharpens\n T = 1 raw\n T > 1 flattens\n divide logits by T before softmax:\n low = safe & sharp, high = diverse\n\n \n \n \n Greedy & beam (deterministic)\n Greedy takes the single most likely\n token every step — fast, but bland\n and repetitive. Beam keeps the top-B\n partial sequences and scores whole-\n sentence likelihood: good for\n translation, dull for open-ended\n generation.\n\n \n Temperature: the risk dial\n Divides the logits by T before the\n softmax. T→0 approaches greedy\n (sharp, safe); T = 1 is the model's\n raw distribution; T > 1 flattens it,\n raising surprise and diversity at the\n cost of coherence. The one knob\n most people actually tune.\n\n \n Top-k vs Top-p (truncation)\n Both chop off the unreliable tail\n before sampling. Top-k keeps a fixed\n count; top-p keeps a variable one —\n the smallest set covering probability\n p — so it widens when the model is\n unsure, narrows when confident.\n Nucleus + temperature is the default.\n\n```\n\nThe mistake most people make is treating decoding as an afterthought — a single "temperature" slider to nudge when output feels off. It is better understood as the interface between a fixed probabilistic model and the text you actually want. Greedy and beam search ask *what is most probable*; temperature, top-k, and top-p ask *how much of the model's uncertainty should I let through, and in what shape*. Read decoding through a shape-the-distribution lens rather than a pick-the-best-word lens, and every parameter stops being a magic number and becomes a deliberate statement about how much risk you want the model to take on each token.

Go deeper with CFSGPT

Get AI-powered deep-dives, save terms, and run advanced simulations — free account.

Create Free Account