Inference & Decoding Strategies
What the model says: sampling, search, and their knobs
Greedy decoding always picks the argmax token; sampling with temperature, top-k and top-p trades determinism for diversity. Beam search and length penalties tune long-form quality. Decoding choices are product decisions.
What you will learn
- Explain greedy, beam search and sampling and their differences
- Tune temperature, top-k, top-p for a use case
- Explain KV-cache decoding and why latency is proportional to output length
What is this?
At each step the LM outputs a probability over the vocabulary; the decoding strategy decides which token becomes the output. Greedy takes the argmax every time — deterministic but formulaic. Beam search keeps the top-k candidate sequences, trading compute for coherence. Sampling draws from the (possibly re-weighted) distribution; temperature sharpens or flattens it, top-k keeps the k most likely tokens, top-p keeps the smallest set covering probability mass p. Autoregressive inference with a KV-cache makes each new token O(1) rather than O(sequence).
Why do you need it?
- Repetition and blandness come from greedy/argmax — the mode is boring.
- Diversity (top-p + temperature) is what makes creative writing and code comments human.
- Determinism matters for tests, parsing and latency-sensitive decision paths.
- Understanding decoding = predictable latency, cost and output quality per product.
Mental model
Choosing restaurant dishes for a perfect dinner
Decoding strategies
Greedy = always pick your favorite dish again. Beam = consider the top 3 ten-course menus and pick the best whole menu. Sampling = roll a die weighted by how much you like each dish, with temperature as your adventurousness slider.
How it works
- greedy: out_t = argmax P(vocab | prefix) — deterministic, fastest, least diverse.
- beam search (k): track the k most probable full sequences, extend each, keep best k, return highest-scoring — coherent but costly and often formal.
- temperature: divide logits by T before softmax; T→0 is greedy, T≈1 is raw, T>1 flattens to randomness.
- top-k: restrict sampling to top-k logits then re-normalize; top-p: smallest set containing cumulative probability p.
- KV cache: cache the K/V tensors of all past tokens so each new token only recomputes its own attention — the reason per-token latency flattens.
A distribution, after temperature and truncation filters.
In production
- Deterministic extraction/parsing: temperature 0 or low with strict formats.
- Creative drafting: temperature 0.8-1.0 with top-p 0.9.
- Code assistance: top-p sampling with repetition penalty for fewer loops-forever bugs.
Best practices
- Use temperature + top-p together as one dial (nucleus sampling); don't tune in isolation.
- For JSON/structured output, prefer temperature 0 plus schema validation over hoping sampling complies.
- Add a repetition/coverage penalty for open-ended long-form; keep it small (1.0-1.15).
- Seed the RNG per request so you can replay and debug any sampled output.
Common mistakes
- Blasting temperature 2.0 to 'fix boredom' and getting gibberish.
- Using beam search for creative text where repetition creeps into sentences.
- Ignoring that greedy can lock into a loop; sampling breaks it.
- Forgetting the KV cache and measuring O(n²) latency that flash-decoding then contradicts.
Common issues
Repetitive loops in long outputsGreedy/beam mode collapsed onto a template
Switch to top-p (0.9) sampling, add a mild repetition penalty, or raise temperature.
Structured output frequently invalid JSONSampling explores token tails that break grammar
Use temperature 0 + JSON mode/schema, or validated decoding.
Answers vary run to run in productionSampling active on a task that should be deterministic
Set temperature 0 for the task, fix seed, and log the config for audit.
Debugging tips
- Log temperature/top-p/top-k naive values with every completion so you can replay failures.
- Probe with one prompt across seeds: high variance = sampling settings or prompt underspecification.
- Watch output-length histograms — a spike near max_tokens usually means truncation, not natural endings.
- Compare greedy vs sampled on your eval set; if greedy wins, sampling probably hurts your task.
Interview checkpoint
Hands-on lab
- Simulate greedy, temperature and top-p sampling on a toy distribution
- Measure diversity and determinism across strategies
- See why temperature/top-p modulate output tails
Compare Decoding Strategies
30 min · intermediate
Quick quiz
Lowering temperature toward 0 makes sampling:
Beam search guarantees the most creative, diverse output.