bugger.
All challenges/Pythonmediumllm-kda-gated-delta-v1
THE INVESTIGATION

The linear attention that forgets one association at a time

A fixed-size state keeps every old write, so a repeated key accumulates instead of replacing its value.

LLM ArchitectureAttentionLinear attentionKimi Delta AttentionState

What’s going wrong

Original LLM Architecture lab. This is a single-head, standard-library teaching reduction of Kimi Delta Attention, not a reproduction of the fused kernel or a live model call.

A linear-attention layer compresses the past into a fixed-size state instead of caching every key and value. The starter applies decay and then adds each new outer product directly. That is plain gated linear attention: a repeated key accumulates another value instead of correcting the value the state already predicts. Repair the recurrence so each write uses the delta rule.

Read one JSON object with queries, keys, values, decays, betas and state. queries and keys contain T arrays of d numbers; values contains T arrays of dv numbers; decays contains T arrays of d numbers in [0,1]; betas contains T numbers in [0,1]; state contains d arrays of dv numbers. T is 0 to 12, d and dv are 1 to 4, and every number is finite with at most three decimal places. Inputs satisfy the stated bounds. Use only the Python standard library and print one JSON object. Round every returned number with round(value, 6) and print 0.0 for any value that rounds to zero. Inputs satisfy this contract.

For each step t, decay first: S[i][j] = decays[t][i] * S[i][j]. Then read the prediction prediction[j] = sum(S[i][j] * keys[t][i] for i in range(d)). The error is error[j] = values[t][j] - prediction[j]. Write S[i][j] = S[i][j] + betas[t] * keys[t][i] * error[j]. The output is sum(S[i][j] * queries[t][i] for i in range(d)) for each value channel j. Preserve the order of these operations. An empty sequence returns {"outputs":[]}. Print {"outputs":[[...],...]} with one array per step.

Expected behavior

Apply the gated delta rule so a repeated key corrects the state prediction instead of adding a second copy, while preserving per-channel decay and the fixed-size state.

Reproduction

Execution evidence

Simulated system · deterministic replay

Fixed two-step recurrence. No model weights, kernel or GPU are used.

  1. 01Step 0
    Zero state; key [1,0] writes value [2,3] at full strength.
  2. 02Step 1
    The same key writes [5,7]. The delta rule replaces the old association; plain linear attention would add to it.
  3. 03Starter output
    The starter returns [2,3] then [6,8.5] because it never subtracts the prediction.
  4. 04Reference output
    The corrected recurrence returns [2,3] then [5,7].
Explain your repair before submitting

Which observation contradicts the intended behavior? Where does the code break that contract? Explain why your change fixes the cause and which new regression case would detect it.

This evidence describes the starter. Use Run to observe your current code; Submit checks additional regressions. No live model calls are made.

01 KDA overwrites a repeated key instead of accumulating it
{"queries":[[1,0],[1,0]],"keys":[[1,0],[1,0]],"values":[[2,3],[5,7]],"decays":[[0.5,0.5],[0.5,0.5]],"betas":[1.0,1.0],"state":[[0,0],[0,0]]}
{"outputs":[[2.0,3.0],[5.0,7.0]]}
02 KDA leaves an empty sequence empty
{"queries":[],"keys":[],"values":[],"decays":[],"betas":[],"state":[[1,2],[3,4]]}
{"outputs":[]}
03 KDA applies a per-channel decay
{"queries":[[1,1]],"keys":[[1,1]],"values":[[2,1]],"decays":[[0.5,0.25]],"betas":[0.5],"state":[[2,0],[0,4]]}
{"outputs":[[2.0,1.0]]}

Before you begin

  • Python lists, nested loops and matrix-vector products
  • The difference between a growing KV cache and a fixed-size recurrent state
  • The idea of a prediction error used as a write signal

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.