Model and Inference EngineeringTransformer and model mechanicsPrincipal
What does an MLA cache store, and how do you account for its separate positional key?
By Amit Kumar4 min readWorked answer includedReviewed 28 Sept 2026
The question
Interview question
For a DeepSeek-V2-style MLA decoder, assume a 512-element compressed content latent and 64-element decoupled positional key per token per attention layer, 60 layers and bf16 cache storage. Explain why both pieces are needed, derive the memory for 32,768 tokens, and distinguish this compressed decode representation from expanded attention workspace and other backends.
Draft your approach first
Take a few minutes before you read the answer. Name the constraint that matters most, the failure you are designing for and how you would know it worked.
Reveal the worked answerWorked answer
In multi-head latent attention, the content part of each token's key and value can be represented by a smaller shared latent vector. Instead of keeping full per-head K and V for every past token, the server keeps that compressed content state. The query and output projections can be arranged to work with the latent without reconstructing every full key and value during decode. DeepSeek-V2 introduces this design, and vLLM's MLA backend notes describe the cached latent and separate positional key in its implementation.
Position is the complication. Rotary position encoding applies a position-dependent transformation to query and key components. A cached content latent alone does not carry the required position-specific key term in the form this architecture uses. DeepSeek's decoupled RoPE keeps a smaller positional key component alongside the content latent. The query compares content through its compressed path and position through this separate rotated path. So "one latent vector" is a useful shorthand, but the actual cached entry includes positional state as well.
I would make a candidate derive what is stored per token in this checkpoint and serving backend: latent width, positional-key width, number of layers, dtype and any extra metadata. Then compare with ordinary MHA or GQA KV bytes. A claim like "MLA reduces the entire KV cache by exactly X percent" cannot be copied across models with different dimensions. It also does not mean attention compute, weight storage or prefill cost fall by the same fraction.
If we remove the positional key to save a little more, we are changing the trained attention function. A test with repeated content at two positions is a good probe: can the query still distinguish where the matching token appeared? Check output parity between the reference implementation and compressed-cache decode, especially after a long prefix. Grouped query attention cuts KV memory. Why might prefill still be expensive? asks why GQA's smaller KV does not erase prefill cost. This question shows how MLA reduces per-token cached content while preserving a separate path for positional information.
Here the content score can be written as a query multiplied by a key-up-projection and the cached latent. Because that up-projection is a fixed learned matrix, it can be absorbed into the query computation. A position-dependent rotation inserted into that product is not the same fixed matrix for every cached token. Decoupled rotary position embedding, or RoPE, therefore keeps a separate positional score path. The cached positional key is already transformed for its source position in the referenced decode path. It is shared across heads, rather than being another full K/V pair for every head.
For a concrete budget, DeepSeek-V2’s configuration specifies latent width 512, positional width 64 and 60 layers. With the bf16 storage assumed in this question, one layer needs (512 + 64) × 2 = 1,152 bytes per cached token. Across 60 layers that is 69,120 bytes, or 67.5 KiB. For 32,768 tokens, the raw compressed-cache tensors need 2,264,924,160 bytes, or 2.109375 GiB. The positional component contributes 7.5 KiB per token across the model. Including it increases a latent-only estimate by 64/512, or 12.5 percent. Omitting it understates the complete compressed entry by 64/576, or about 11.1 percent.
These are raw tensor bytes for an unsharded logical cache. They exclude block rounding, allocator bookkeeping, quantization scales, temporary buffers and request metadata. Do not divide them by tensor-parallel degree without checking whether that implementation replicates the compressed state. Prefill may reconstruct per-head K/V into temporary workspace, so its peak memory can exceed the persistent compressed-cache budget. The versioned vLLM backend cited above explicitly separates cached latent/positional state from expanded prefill work.
A correctness test compares attention scores and output logits with a reference path at several positions, including repeated content at different offsets. Check the checkpoint dimensions, stored dtype and rotation convention before changing the layout. Quantizing this cache or replacing its backend changes the memory calculation and may change numerical error. Report the verified representation and context length with any savings claim instead of treating MLA as one universal byte ratio.
Related questions
Keep practising model and inference engineering
Follow another question in this area, or search the complete library.
Get my essays on AI engineering and distributed systems by email. Subscribe to ArchCrux