Interactive lab · attention machinery
This page is a working miniature of the KV cache inside DeepSeek and Kimi K2, written from scratch in vanilla JavaScript: real matrix multiplies, real RoPE, real low-rank compression, running on toy dimensions. Type a prompt, prefill it, then generate one token at a time and watch exactly what gets stored, and what never has to be.
The analogy: a normal cache is a notebook where every reader keeps a full photocopy of every page. The MLA cache keeps one compressed master copy per page, and each reader re-expands only the slice they need, only when they need it. Below, the master copy is 40 numbers per token per layer. Everything else on this page is the unsimplified version of that sentence.
Weights are seeded and deterministic, so every run of this page builds the identical cache. Press Tokenize + Prefill to start.
One row per layer, one cell per token. In MLA mode each cell holds only the compressed latent and the shared RoPE key: 40 numbers. Flip the toggle to see what plain multi-head attention would have to store for the same tokens: every head's full key and value, 160 numbers per layer. Tap any cell to read its actual stored numbers.
Prefill a prompt, then tap a cache cell to see the exact numbers stored in it.
Per token per layer: MLA stores latent 32 + RoPE key 8 = 40 numbers. MHA stores 4 heads x (key 24 + value 16) = 160 numbers. Two layers, so each new token adds exactly 80 numbers to the MLA cache and would add 320 to an MHA cache.
When a new token is generated, its queries need scores against every cached token. There are two ways to compute the same scores. The materialized way rebuilds each head's full keys from the latents first. The absorbed way folds the key up-projection into the query once, then scores directly against the stored latents, so the full keys never exist. This page computes both, every step, and compares them number by number.
No decode step yet. Prefill, then tap Generate next token.
Red bars: materialized attention weights. Olive bars: absorbed weights. If the algebra is right they are the same numbers, and the difference readout is floating point noise, on the order of one quadrillionth.
The toy above mirrors Kimi K2's architecture relation for relation, shrunk. Here is the real config, fetched from Kimi K2's published model files, and the cache arithmetic it produces.
| Hidden size | 7,168 |
| Layers | 61 |
| Attention heads | 64 |
| KV latent rank (the compressed thing stored) | 512 |
| RoPE key dimension (shared across heads) | 64 |
| Key per head, NoPE part + RoPE part | 128 + 64 = 192 |
| Value per head | 128 |
Symbols512 = KV latent rank, the compressed numbers stored per token per layer; 64 on the first line = shared RoPE key dimension, also stored; 61 = layers, each with its own cache; 2 bytes = size of one stored number at BF16; 64 on the second line = attention heads; 192 = key numbers per head, 128 NoPE content + 64 RoPE position; 128 = value numbers per head; B = bytes.
That is the whole reason MLA exists. At 128K tokens of context, one Kimi K2 conversation carries about 8.6 GiB of cache instead of about 305 GiB, and 305 GiB does not fit on the GPU next to a trillion parameter model. The compression is not a trick bolted on afterwards: the cache was never anything but the latent.
In plain attention, each token's hidden state h is projected into a key and a value per head, and all of them are stored. MLA inserts a bottleneck. One shared down-projection compresses h into a single latent vector c, and only c is cached. Each head's key and value are up-projections of that same c, rebuilt on demand.
Symbolst = the token's position in the sequence; i = attention head index; ht = token t's hidden state, the vector the layer receives (64 numbers in the toy, 7,168 in Kimi K2); WDKV = the one shared down-projection matrix that compresses ht; ctKV = the compressed latent for token t, the only content the cache stores; WiUK and WiUV = head i's up-projection matrices, rebuilding its key and value from the latent on demand; kt,iNoPE = head i's content key for token t (NoPE: this part carries no position information); vt,i = head i's value for token t; Rn = a vector of n real numbers.
Rebuilding sounds wasteful until you count memory traffic instead of arithmetic. During decode the bottleneck is reading the cache, not computing, and reading 40 numbers per token instead of 160 is the win. The rebuild math is small, dense, and stays on chip.
Position information is the awkward part. Rotary position embeddings rotate a vector by an angle set by its position, and a rotated key cannot be reconstructed from a shared latent without knowing the position at compression time. MLA's answer is to split the key. The content part (NoPE) comes from the latent. The position part is a single extra key, computed once per token, rotated by RoPE, cached alongside the latent, and shared by all heads.
Symbolst = the token's position; i = attention head index; ht = token t's hidden state; ktR = token t's shared RoPE key, cached beside the latent and used by every head; WKR = the matrix projecting ht into that key before rotation; RoPEt = rotary position embedding at position t, rotating each adjacent pair of numbers by an angle that grows with t; kt,i = head i's full key for token t; kt,iNoPE = its content part, rebuilt from the latent by the formula in card 1; [ a ; b ] = concatenation, stacking vector a on top of vector b into one longer vector.
The query gets the matching split: its NoPE part scores the latent content, its RoPE part scores the shared positional key. Eight extra numbers per token in the toy, 64 in Kimi K2, buy back everything RoPE needs.
The score a query gives a cached token is a dot product, and dot products associate. Expanding the key from the latent and then dotting with the query is the same number as first folding the expansion matrix into the query, then dotting with the latent directly. So decode does the fold once per query and scores against the 32 stored numbers, instead of expanding every cached token's keys on every step.
Symbolssj = the attention score the current token's query gives cached token j, before softmax and before the usual 1 over square root of key dimension scale, which multiplies both lines equally; j = a cached token's position; qNoPE = the query's content part (16 numbers in the toy, 128 in Kimi K2); qR = the query's RoPE part (8 toy, 64 real), rotated for the current position; WUK = one head's key up-projection matrix (the head index is dropped in this card because the identity holds head by head); cj = token j's stored latent, the same cKV from card 1 with its subscript written short; kjR = token j's stored shared RoPE key from card 2; transpose = the matrix flipped across its diagonal, which is what lets the fold land in latent space; · = dot product, multiply matching entries and add them up.
The same trick applies on the value side: the output projection absorbs the value up-projection, so values are also never fully rebuilt during decode. This lab materializes them anyway in the comparison view, because seeing both is the point of a lab.
Dimensions here are toy: hidden 64 instead of 7,168, 2 layers instead of 61, 4 heads instead of 64, latent 32 instead of 512. The weights are seeded pseudo-random numbers, never trained, so the generated text is gibberish and is labeled that way wherever it appears. What is real is the data flow: the same projections, the same low-rank bottleneck, the same shared RoPE key, the same absorbed scoring, the same cache growth of exactly latent plus RoPE per token per layer. A production engine adds paged memory management, batching, quantization, and kernels that fuse these steps; none of that changes what is stored, and what is stored is the subject of this page.