bugger.
All challenges/Pythonmediumllm-sliding-window-v1
THE INVESTIGATION

Sliding-window causal attention

Implement a local causal attention head with a rolling KV window.

LLM ArchitectureTransformer components

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

01 Window evicts an attractive old key
{"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]}
02 Window wider than the prompt
{"q":[[1],[1]],"k":[[10],[0]],"v":[[20],[1]],"window":4}
{"output":[[20],[19.999137]],"cache_lengths":[1,2]}
03 Window of one selects the current value
{"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.

    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.