Sliding-window causal attention
Implement a local causal attention head with a rolling KV window.
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 q, k and v are aligned nonempty rows for successive tokens. At position i, attend to keys j from max(0,i−window+1) through i, inclusive. Use scaled dot-product softmax and return output plus cache_lengths. window is a positive integer and includes the current token.
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 a local causal attention head with a rolling KV window.
Reproduction
{"q":[[1],[1],[1]],"k":[[10],[0],[1]],"v":[[20],[1],[2]],"window":2}{"output":[[20],[19.999137],[1.731059]],"cache_lengths":[1,2,2]}{"q":[[1],[1]],"k":[[10],[0]],"v":[[20],[1]],"window":4}{"output":[[20],[19.999137]],"cache_lengths":[1,2]}{"q":[[1],[1]],"k":[[1],[1]],"v":[[3],[9]],"window":1}{"output":[[3],[9]],"cache_lengths":[1,1]}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.