bugger.
All challenges/Pythonhardllm-linear-recurrence-v1
THE INVESTIGATION

Causal linear attention with recurrent state

Implement normalized causal linear attention using prefix state.

LLM ArchitectureArchitecture builds

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

01 Two prefix tokens contribute with different weights
{"q":[[1],[1]],"k":[[1],[2]],"v":[[2],[5]],"key_width":1,"value_width":1}
{"output":[[2],[4]],"state":[[12]],"normalizer":[3]}
02 Empty prefix keeps zero state
{"q":[],"k":[],"v":[],"key_width":2,"value_width":3}
{"output":[],"state":[[0,0,0],[0,0,0]],"normalizer":[0,0]}
03 Zero keys keep every output zero
{"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.

    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.