WRITE · TRACE · UNDERSTAND

Nucleus Sampling Candidates

Sort by probability and retain the smallest prefix reaching top-p mass.

Your experiment

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).

Python runs in your browser. Your draft stays on this device.

What to look for

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.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.