Prefill and incremental KV-cache decoding
Implement cached decoding for one causal attention head.
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
{"past":{"keys":[[9]],"values":[[20]]},"steps":[{"q":[1],"k":[0],"v":[1]}],"value_width":1}{"output":[[19.997656]],"keys":[[9],[0]],"values":[[20],[1]]}{"past":{"keys":[[2]],"values":[[4]]},"steps":[],"value_width":1}{"output":[],"keys":[[2]],"values":[[4]]}{"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.
Read the report, then sign in to start your investigation.