The attention that caches a latent, not a head
A compressed KV latent must be projected into keys and values, and its position must be restored by a separate RoPE score.
What’s going wrong
Original LLM Architecture lab. This is a single-query, standard-library teaching reduction of Multi-head Latent Attention, not a reproduction of the DeepSeek kernel or a live model call.
MLA stores a low-rank latent for each token instead of a full key and value per head. At use time the latent is projected into a key and a value. Because the compression would destroy position information, MLA also keeps a small decoupled RoPE key. The starter projects the key but uses the raw latent as the value and ignores the RoPE score. Repair both omissions.
Read one JSON object with query, query_rope, latents, rope_keys, wk, wv and scale. query has d_h numbers; query_rope has d_r numbers; latents has N arrays of d_c numbers; rope_keys has N arrays of d_r numbers; wk and wv each have d_h arrays of d_c numbers. N is 0 to 8, d_h and d_c are 1 to 4, and d_r is 1 to 3. Every number is finite with at most three decimal places and scale is a finite number. 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 cached token i, compute key[i][j] = sum(wk[j][c] * latents[i][c] for c in range(d_c)) and value[i][j] = sum(wv[j][c] * latents[i][c] for c in range(d_c)). The content score is sum(query[j] * key[i][j] for j in range(d_h)); the position score is sum(query_rope[j] * rope_keys[i][j] for j in range(d_r)); the final score is scale * content + position. Apply a stable softmax over the final scores: subtract the maximum, exponentiate, divide by the sum. The output is the weighted sum of the projected values. An empty cache returns {"weights":[],"output":[0.0,...]} with d_h zeros. Print {"weights":[...],"output":[...]} in that order.
Expected behavior
Project each cached latent into a key and a value, add the decoupled RoPE score to the content score, and return a stable softmax plus the projected value mix.
Reproduction
Execution evidence
Simulated system · deterministic replay
Fixed latents, projections and RoPE keys. No model weights, attention kernel or GPU are used.
- 01Cache
Two tokens are stored as two-dimensional latents, not as per-head keys and values.
- 02Starter
The starter scores with the projected key but mixes the raw latent and drops the RoPE key.
- 03Reference
The value projection and the decoupled position score are both applied before the softmax.
- 04Result
A position-dominant case changes the selected token without changing the content score.
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.
{"query":[1,0],"query_rope":[1],"latents":[[1,0],[0,1]],"rope_keys":[[0],[5]],"wk":[[1,0],[0,1]],"wv":[[1,0],[0,1]],"scale":0.5}{"weights":[0.010987,0.989013],"output":[0.010987,0.989013]}{"query":[1,0],"query_rope":[0],"latents":[[1,2]],"rope_keys":[[0]],"wk":[[1,0],[0,1]],"wv":[[1,1],[1,-1]],"scale":1.0}{"weights":[1.0],"output":[3.0,-1.0]}{"query":[1,0],"query_rope":[1],"latents":[],"rope_keys":[],"wk":[[1,0],[0,1]],"wv":[[1,0],[0,1]],"scale":1.0}{"weights":[],"output":[0.0,0.0]}Before you begin
- Dot products, softmax and matrix-vector products
- The standard KV cache and why its size grows with context
- The idea of a low-rank bottleneck and why position is handled separately
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.