bugger.
All challenges/Pythonmediumllm-sampling-v1
THE INVESTIGATION

Temperature, top-k and nucleus sampling

Sample a token using an explicit uniform draw.

LLM ArchitectureLLM inference

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

01 A uniform draw selects a nonmaximum token
{"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]}
02 Top-k of one has only the greedy token
{"logits":[1,3,2],"temperature":1,"top_k":1,"top_p":1,"draw":0.999}
{"token":1,"kept":[1],"probabilities":[1]}
03 Nucleus keeps the token that crosses the threshold
{"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.

    The bug is intentional. Run the starter code to see it fail, then work toward a fix. Difficulty is provisional.
    Read-only preview
    Ready when you are
    Every fix starts with a failing test.

    Read the report, then sign in to start your investigation.