An LLM routing service samples one of three candidate reply types from logits with the snippet below, at temperature T = 0.5. Up to simulation noise, what does it print?
import numpy as np
rng = np.random.default_rng(42)
logits = np.array([2.0, 1.0, 0.0]) # scores of 3 candidate reply types
def sample(logits, T):
u = rng.random(logits.shape)
g = -np.log(-np.log(u)) # standard Gumbel noise
return np.argmax(logits / T + g)
draws = [sample(logits, T=0.5) for _ in range(200_000)]
print(np.bincount(draws, minlength=3) / len(draws))Show the answer and explanation
Correct answer: B, About [0.867 0.117 0.016], i.e. softmax(logits / T) with logits / T = (4, 2, 0): the argmax of logits plus Gumbel noise is an exact softmax sample, and a T below 1 sharpens it.
Explanation Monte Carlo & Simulation
Gumbel-max trick: if G₁, …, Gₖ are independent standard Gumbel variables (G = −log(−log U), U uniform), then argmaxᵢ (zᵢ + Gᵢ) equals i with probability exactly exp(zᵢ) / Σⱼ exp(zⱼ), i.e. softmax(z). It is an exact sampler, not an approximation, and needs no normalising constant.
The snippet uses z = logits / T = (4, 2, 0), so it samples from softmax(4, 2, 0) = (0.867, 0.117, 0.016); one run printed about [0.869 0.116 0.016]. Dividing by T < 1 widens the gaps relative to the noise, so the distribution sharpens towards greedy decoding; T > 1 flattens it, and argmax(z/T + G) is equivalent to argmax(z + T·G).
| T | Distribution sampled |
|---|---|
| 2 | softmax(1, 0.5, 0) = (0.506, 0.307, 0.186) |
| 1 | softmax(2, 1, 0) = (0.665, 0.245, 0.090) |
| 0.5 | softmax(4, 2, 0) = (0.867, 0.117, 0.016) |
| → 0 | always index 0 (greedy) |
Why the distractors fail: scaling the logits alone preserves the argmax, but the noise is not scaled, so the balance between signal and noise changes. Multiplying by T reverses the direction of the temperature effect. Standard Gumbel noise is unbounded, with mean ≈ 0.577 and SD π/√6 ≈ 1.28, so a lead of 2 is overturned regularly.
Interview tip: the Gumbel-max view explains why sampling from logits can be done with one argmax per draw, and adding Gumbel noise and taking the top-k indices draws k items without replacement (Gumbel-top-k).