Nucleus Sampling Candidates
Sort by probability and retain the smallest prefix reaching top-p mass. Return renormalized candidates, with original index breaking ties. No random draw is hidden in this lab.
probabilities = [0.6, 0.3]
mass = sum(probabilities)
print([p/mass for p in probabilities])From arithmetic to an experiment
Sort by probability and retain the smallest prefix reaching top-p mass. Return renormalized candidates, with original index breaking ties. No random draw is hidden in this lab. The trace reads the local variables of your function as it executes. Change one input or parameter, predict the result, then run the checks again. A small deterministic example makes arithmetic mistakes easier to locate than a long training run. State the shape and meaning of the returned value before coding. Keep preprocessing, evaluation and model updates in their proper order, and examine the boundary case in each check. The companion lab lets you replay intermediate values while the same algorithm runs.
Your task
nucleus(probabilities, p) returns (token_index, renormalized_probability) pairs. Nonnegative probabilities sum to one, 0 < p <= 1; sort descending, break ties by index, stop when cumulative mass reaches p (tolerance 1e-12).
The explanation opens here once every check passes.