bugger.
All challenges/Pythonhardllm-kv-cache-v1
THE INVESTIGATION

Prefill and incremental KV-cache decoding

Implement cached decoding for one causal attention head.

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 past has keys and values of equal length; steps contains {q,k,v} rows in decode order, and value_width provides the output width. At each step append the new key and value, then attend over the entire cache (including that token), using stable scaled dot-product attention. Return output, keys and values in order. An empty step list returns the cache contents unchanged. All widths are consistent.

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

Implement cached decoding for one causal attention head.

Reproduction

01 Decode reads the old high-scoring key
{"past":{"keys":[[9]],"values":[[20]]},"steps":[{"q":[1],"k":[0],"v":[1]}],"value_width":1}
{"output":[[19.997656]],"keys":[[9],[0]],"values":[[20],[1]]}
02 No new tokens preserves the cache
{"past":{"keys":[[2]],"values":[[4]]},"steps":[],"value_width":1}
{"output":[],"keys":[[2]],"values":[[4]]}
03 Cold start uses the current value
{"past":{"keys":[],"values":[]},"steps":[{"q":[1],"k":[1],"v":[4]}],"value_width":1}
{"output":[[4]],"keys":[[1]],"values":[[4]]}

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.