bugger.
All challenges/Pythonmediumllm-attention-sinks-window-v1
THE INVESTIGATION

The attention window with a place to dump probability

A sliding window bounds the cache, and a learned sink logit lets a head spend probability on no token at all.

LLM ArchitectureAttentionSliding windowAttention sinksgpt-oss

What’s going wrong

Original LLM Architecture lab. This is a single-layer, standard-library teaching reduction of the attention-sink and sliding-window pattern used by gpt-oss, not a reproduction of its kernel or a live model call.

A sliding-window attention layer keeps only the most recent keys and values. If every score is low, a normal softmax still has to put all of its mass on those keys. A learned sink logit adds one more term to the denominator; the sink has no value, so its probability is discarded. That gives a head a way to attend to nothing. The starter ignores both the sink and the window. Repair them.

Read one JSON object with queries, keys, values, sink_logits, window, value_dim and scale. queries has H arrays of d numbers; keys has N arrays of d numbers; values has N arrays of value_dim numbers; sink_logits has H numbers. H is 1 to 3, N is 0 to 8, d and value_dim are 1 to 4, window is an integer from 0 to 8, and scale is finite. 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 head, keep the last min(window, N) keys, in their original order. Compute score_i = scale * sum(query[j] * keys[i][j]) for those keys. Let top be the maximum of the sink logit and all kept scores. The denominator is exp(sink_logit - top) + sum(exp(score_i - top)). The sink weight is the sink term divided by the denominator; each key weight is its exponential divided by the denominator. The output is the sum of key_weight_i * values[i], with no value for the sink. If window is 0 or there are no keys, the output is value_dim zeros, the sink weight is 1.0 and the key weights are empty. Print {"outputs":[[...]],"sink_weights":[...],"key_weights":[[...]]}.

Expected behavior

Restrict attention to the most recent window, include the per-head sink logit in the softmax denominator, and return the value mix, sink weight and key weights without giving the sink a value.

Reproduction

Execution evidence

Simulated system · deterministic replay

Fixed queries, keys and per-head sink logits. No model weights, rolling cache or GPU are used.

  1. 01Scores
    Two keys score equally, but a sink logit of 10 is much larger.
  2. 02Starter
    The starter ignores the sink and puts all probability on the two keys.
  3. 03Reference
    Almost all probability goes to the sink, so the value mix is nearly zero.
  4. 04Window
    A second case keeps only the last two of three keys and changes the value mix.
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 Sink gives attention a no-op option
{"queries":[[1,0]],"keys":[[1,0],[1,0]],"values":[[2,0],[4,0]],"sink_logits":[10],"window":2,"value_dim":2,"scale":1.0}
{"outputs":[[0.00074,0.0]],"sink_weights":[0.999753],"key_weights":[[0.000123,0.000123]]}
02 Window keeps only the most recent keys
{"queries":[[1,0]],"keys":[[1,0],[1,0],[1,0]],"values":[[1,0],[2,0],[4,0]],"sink_logits":[-10],"window":2,"value_dim":2,"scale":1.0}
{"outputs":[[2.999975,0.0]],"sink_weights":[8e-06],"key_weights":[[0.499996,0.499996]]}
03 Sink can be negative and still take mass
{"queries":[[1,0]],"keys":[[0,1]],"values":[[5,0]],"sink_logits":[0],"window":1,"value_dim":2,"scale":1.0}
{"outputs":[[2.5,0.0]],"sink_weights":[0.5],"key_weights":[[0.5]]}

Before you begin

  • Dot products, softmax and stable exponentiation
  • How a KV cache grows and why a sliding window bounds it
  • The difference between a probability denominator and a weighted value sum

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.