bugger.
All challenges/Pythonmediumllm-sft-loss-v1
THE INVESTIGATION

Response-only supervised fine-tuning loss

Compute response-only next-token cross-entropy across a batch.

LLM ArchitecturePost-training

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.

Each row in batch contains tokens, response (Boolean mark per token), and logits with one row per token position. At position i, score target tokens[i+1] only if response[i+1] is true. Return loss (mean over eligible target tokens), count and per_example (sum of eligible losses for each row). Return zero loss for no eligible targets.

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

Compute response-only next-token cross-entropy across a batch.

Reproduction

01 Last prompt position predicts the first response token
{"batch":[{"tokens":[0,1,1],"response":[false,true,true],"logits":[[2,0],[1,4],[0,99]]}]}
{"loss":1.087758,"count":2,"per_example":[2.175515]}
02 Prompt-only sample
{"batch":[{"tokens":[0,1],"response":[false,false],"logits":[[1,0],[0,0]]}]}
{"loss":0,"count":0,"per_example":[0]}
03 Unequal response lengths use one token denominator
{"batch":[{"tokens":[0,1],"response":[false,true],"logits":[[1,3],[0,0]]},{"tokens":[0,1,0],"response":[false,true,true],"logits":[[1,3],[0,2],[0,0]]}]}
{"loss":0.793595,"count":3,"per_example":[0.126928,2.253856]}

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.