Temperature, top-k and nucleus sampling
Sample a token using an explicit uniform draw.
What’s going wrong
Original LLM Architecture lab. Implement the missing behavior in solve(data) and keep the supplied JSON interface. Use Python’s standard library.
Input logits is nonempty, temperature > 0, top_k is null or 1 through vocabulary size, and 0 < top_p ≤ 1. Order by score descending and ID ascending. Apply temperature and top-k, normalize, then keep the smallest prefix whose probability mass reaches top_p (top_p=1 keeps all top-k candidates). Renormalize and select the first token whose cumulative probability strictly exceeds draw in [0,1), with the last token as a rounding fallback. Return token, kept and probabilities in the kept order.
Small, well-formed JSON inputs and standard-library Python arithmetic. Keep full precision during computation. The supplied output wrapper rounds results to six decimal places.
Expected behavior
Sample a token using an explicit uniform draw.
Reproduction
{"logits":[2,1,0],"temperature":1,"top_k":3,"top_p":1,"draw":0.85}{"token":1,"kept":[0,1,2],"probabilities":[0.665241,0.244728,0.090031]}{"logits":[1,3,2],"temperature":1,"top_k":1,"top_p":1,"draw":0.999}{"token":1,"kept":[1],"probabilities":[1]}{"logits":[2,1,0],"temperature":1,"top_k":null,"top_p":0.8,"draw":0.95}{"token":1,"kept":[0,1],"probabilities":[0.731059,0.268941]}Before you begin
- Python functions, lists and dictionaries
Need a nudge?
Reveal one investigation hint at a time. Try each clue before opening the next.
Read the report, then sign in to start your investigation.