Across which axes is the KV cache stored, per layer, per head, per batch?
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
The KV cache spans all three axes: per layer, per KV head, and per batch request. Each combination stores its own K and V tensor over the sequence.
Imagine a multistory library where every floor (a transformer layer) has its own card catalog. On each floor, several librarians (the heads) keep separate drawers of cards. And every visitor (a request in the batch) brings their own set of bookmarks. To find a book, you need the right floor, the right librarian's drawer, and your personal bookmark stack. The KV cache works the same way: layers, heads, and requests are all separate axes, and any one of them being missed will make your memory math wrong by an order of magnitude.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
3m: the three independent axes (layer, KV head, batch), how MHA vs GQA vs MQA changes the head axis only, the per-token formula, and how prefix sharing partially compresses the batch axis.
Real products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Pretending the cache is per layer only, then sizing memory as if heads or batch share storage. Each axis is genuinely independent and contributes a multiplicative factor.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.