Decoding: beam search, nucleus sampling and speculative decoding
The same model writes very different text depending on how tokens are chosen. Compare greedy and beam search with length penalties, nucleus sampling, and speculative decoding, which speeds generation up without changing what is generated.
What runs in CodeSplash
- LNG075Speculative decoding Runs as a notebook
- LNG076Nucleus / top-p sampling Runs as a notebook
- LNG077Beam search with length and coverage control Explained here
You will be able to
- Explain why beam search favors short outputs and how a length penalty corrects it
- Apply nucleus (top-p) sampling to a next-token distribution
- Describe speculative decoding's accept–reject rule and why it changes nothing about the output distribution
- Match a decoding strategy to a task
Search for the likeliest output
ReadGreedy decoding takes the single likeliest token at every step, and can paint itself into a corner. Beam searchBeam searchDecoding that keeps the B best partial sequences at every step; a length penalty counters its preference for short outputs. keeps the B best partial sequences by total log-probability, extends each, and keeps the best B again.
Total log-probability only falls as a sequence grows, so beam search prefers short outputs. A length penalty divides each score by a function of length; a common one is ((5 + length)/6)^α. In the reference program a 3-token output with log-probability −3 and a 6-token one with −4 score −2.52 and −2.78 after the penalty: still in favor of the short one, but by much less. Translation systems add a coverage penalty for source words never attended to.
DExplorable · Decoding: greedy, beam search and sampling
How does the way we pick tokens change the sentence a language model writes?
Method
Greedy takes the likeliest token each step; beam search keeps several partial sentences; sampling draws.
Beam search only: partial sentences kept at each step.
Length normalization
Total log-probability always favors shorter sentences; normalizing by length offsets that.
GNMT only: divide by ((5 + length) / 6)^α.
Sampling only: below 1 sharpens, above 1 flattens, as in llm-sampling.
Sampling only: keep the k likeliest tokens.
Sampling only: keep the likeliest tokens until they hold this much probability.
Tokens before the search stops.
“the data fits well today </s>”
Change the sample to draw another sentence from the same settings.
- Log-probability of the result
- −4.239
- Tokens
- 6
- Normalized score
- −4.239
- Gain over greedy (log-probability)
- −0.419
- Puzzle: Find a sentence more likely than greedy's.
- Puzzle: With beam search, make a sentence of five or more tokens win.
Sampling writes “the data fits well today </s>”: log-probability −4.239 over 6 tokens. Greedy's sentence has log-probability −3.820. The toy model's probabilities are fixed; only the way of choosing changes.
Sample from the head of the distribution
ReadFor open-ended text, the likeliest sequence is often dull and repetitive. Sampling adds variety, but sampling from the whole distribution sometimes picks absurd low-probability tokens. Nucleus samplingTop-p samplingKeeps only the most likely next tokens until they hold share p of the probability, then samples among those. keeps the smallest set of top tokens whose probabilities add up to at least p, renormalizes, and samples from that set.
In the reference program the next-token probabilities are 0.5, 0.25, 0.15 and 0.1, and p = 0.8. The first three tokens reach the threshold, so the last is dropped, and the kept tokens become 0.556, 0.278 and 0.167.
Move the dials
ExploreDExplorable · Temperature and top-p
What do the sampling settings on a language model actually change?
With these settings, how likely is the single most likely token?
Below 1 sharpens the distribution; above 1 flattens it.
Keep the likeliest tokens until they hold this much probability.
“The p-value tells you how extreme”
- With these settings
- Default (temperature 1, no cut-off)
Twelve sampled tokens: extreme · surprising · surprising · unusual · rare · rare · unusual · surprising · rare · extreme · surprising · likely
Commit your prediction above to see the result
Make your prediction to see the result.
Lower top-p until only one token survives: that is greedy decoding. Raise the temperature and watch the nucleus widen, because flatter probabilities need more tokens to reach the same p. Temperature reshapes the distribution; top-p cuts its tail.
Faster, with the same output: speculative decoding
ReadSpeculative decodingSpeculative decodingA small draft model proposes tokens and the large model accepts or resamples them so the output distribution is exactly the large model's. pairs the big target model with a small draft model. The draft proposes several tokens; the target checks them all in one pass. Each draft token is accepted with probability min(1, p/q), where p is the target's probability and q the draft's. At the first rejection, a token is sampled from the leftover distribution max(p − q, 0), renormalized.
In the reference program the acceptance probabilities are 1.00, 0.75 and 0.67, and accepted plus resampled tokens rebuild the target's distribution exactly: 0.5, 0.3, 0.2. Speed improves when the draft agrees often; the output is unchanged.
Which strategy for which job?
PredictPredict first
You extract a date field from thousands of invoices with a language model. Which decoding fits best?
Reveal
Greedy or beam search, deterministic. Extraction wants the single most likely answer, the same every time. Sampling suits brainstorming and drafts. Speculative decoding is a speed technique the runtime may use underneath either one.
Where it stands in CodeSplash
Read| Entry | In CodeSplash |
|---|---|
| Speculative decoding (LNG075) | Runs as a notebook; a concern of the runtime you connect |
| Nucleus sampling (LNG076) | Runs as a notebook; set in the runtime you connect |
| Beam search with length control (LNG077) | Explained here |
Decoding happens inside the language-model runtime you connect to CodeSplash's AI layer; its sampling settings belong to that runtime.
Check: decoding
CheckpointCheck yourself
Without a length penalty, why does beam search prefer short outputs?
Show the answer
Answer: a
Log-probabilities are negative and add up. Dividing by a length term puts long and short candidates on a fairer footing.
Check yourself
You widen the beam from 4 to 50 for open-ended story writing. What often happens?
Show the answer
Answer: b
Wider beams find higher-probability text, and for open-ended writing the highest-probability text is often dull. Search suits tasks with one right answer.
Check yourself
Next-token probabilities are 0.6, 0.2, 0.1, 0.05 and 0.05. With top-p = 0.85, which tokens are kept?
Show the answer
Answer: c
0.6 + 0.2 = 0.8 is below 0.85; adding 0.1 reaches 0.9. The smallest set that reaches p has three tokens.
Check yourself
What does nucleus sampling do that a fixed top-k doesn't?
Show the answer
Answer: b
The nucleus grows and shrinks with the shape of the distribution; top-k keeps k tokens regardless.
Check yourself
Does speculative decoding change what the target model generates?
Show the answer
Answer: b
Accepting with min(1, p/q) and resampling rejections from max(p − q, 0) recovers p exactly.
Check yourself
When does speculative decoding give little speed-up?
Show the answer
Answer: b
Each accepted draft token saves a step of the big model; frequent rejections waste the draft's work.
Recap
Recap- Beam search keeps the B best partial sequences; a length penalty counters its bias to short outputs.
- Nucleus sampling keeps the smallest set of tokens reaching probability p, adapting to the model's confidence.
- Speculative decoding drafts with a small model and checks with the big one; the output distribution is unchanged.
- Use search for one right answer, sampling for variety. Decoding runs inside the runtime you connect.
Concepts in this lesson