Causal linear attention with recurrent state
Implement normalized causal linear attention using prefix state.
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 lists of rows. q and k are already nonnegative feature maps of equal nonzero width; v has its own width. Maintain S = sum(k outer v) and z = sum(k) through the current token. Output q·S/(q·z), returning a zero row when the denominator is zero. Return output, state (S) and normalizer (z), using widths key_width and value_width even for an empty sequence.
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 normalized causal linear attention using prefix state.
Reproduction
{"q":[[1],[1]],"k":[[1],[2]],"v":[[2],[5]],"key_width":1,"value_width":1}{"output":[[2],[4]],"state":[[12]],"normalizer":[3]}{"q":[],"k":[],"v":[],"key_width":2,"value_width":3}{"output":[],"state":[[0,0,0],[0,0,0]],"normalizer":[0,0]}{"q":[[1],[1]],"k":[[0],[0]],"v":[[2],[5]],"key_width":1,"value_width":1}{"output":[[0],[0]],"state":[[0]],"normalizer":[0]}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.