412. LLM sampling: turn logits into probabilities without overflowing
You are writing the sampling step of an LLM serving stack. For each next token the model returns one raw score (a logit) per vocabulary entry, and the product exposes a temperature setting. Softmax with temperature turns the scores into probabilities that add up to 1:
p_i = exp(z_i / T) / sum_j exp(z_j / T)
T below 1 makes the top token more likely, and T above 1 flattens the odds.
Write softmax(logits, temperature=1.0) that returns a 1-D NumPy array of probabilities, the same length as logits.
What real traffic throws at it:
- Large logits. exp(1000) overflows to inf, and inf / inf is nan. Divide by the temperature first, then subtract the largest scaled value before np.exp. The answer is unchanged and nothing overflows. Low temperatures make this worse: 1000 / 0.5 is 2000.
- Masked tokens. Servers often block banned or invalid tokens by setting their logit to float('-inf'). Those tokens must get probability exactly 0, and the rest must still sum to 1. At least one logit is always finite.
- Inputs can be Python lists or NumPy arrays. Convert with np.asarray(logits, dtype=float), and do not change the caller's array in place.
- temperature > 0.
def softmax(logits, temperature=1.0)Softmax: how one logit moves every probability
Pick a class, then slide its logit or drag across the chart. The other probabilities shrink to make room because they always sum to 1. Temperature divides every logit before the exponent.
- entropy (nats)
- 0.847
- top class
- cat
Example 1
Input: softmax([2.0, 1.0, 0.1])
Output: about [0.659, 0.242, 0.099]
Example 2
Input: softmax([1.0, 2.0], temperature=0.5)
Output: about [0.119, 0.881]: halving T doubles the gap between the logits
Example 3
Input: softmax([-inf, 1.0, 2.0])
Output: about [0.0, 0.269, 0.731]
Constraints
- Python 3 with NumPy. Code runs in your browser.
- Your code must pass all 9 tests to be accepted.
Hint 1
Divide by temperature first, then subtract z.max() before np.exp.
Hint 2
Divide by the sum at the end.
Understand the idea
Softmax turns raw scores into probabilities: bigger score, bigger share, and all shares add up to 1. Think of splitting a cake by votes, where each vote is first made exponential. Temperature T is applied first by dividing every logit by T. Small T exaggerates gaps, large T shrinks them. The overflow problem: exp(1000) is inf in floating point. Subtracting the largest logit from every logit fixes this, because softmax does not change when you shift all inputs by the same number. Worked example, logits [2, 1, 0], T = 1: subtract max 2 to get [0, -1, -2], exponentiate to [1, 0.368, 0.135], sum is 1.503, so probabilities are about [0.665, 0.245, 0.090]. With T = 2 the scaled logits are [1, 0.5, 0] and the result flattens to about [0.506, 0.307, 0.186].
z = logits / T, z = z - max(z), p = exp(z) / sum(exp(z))
Every LLM sampler uses exactly this before drawing the next token.
Topics
Companies4
- Anthropic a candidate reported this question Anthropic: One-layer transformer in NumPy (CircleCoder)
- Apple a candidate reported this question Apple Machine Learning Engineer Interview | Screna AI
- DeepMind the topic appears in interview reports AI Engineering Interview Questions Company Wise (Outcome School)
- OpenAI a candidate reported this question OpenAI Interview Process & Questions (2026) (TrueInterview)
From public interview reports and guides. Not affiliated with or endorsed by these companies.