Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
State the KV cache memory formula and compute it for a 70B-class model with 80 layers, 64 KV heads, head_dim 128, sequence length 128k tokens, FP16, batch size 1. Show units. How does the result compare to the weights themselves?
KV cache bytes equal 2 times layers times KV heads times head_dim times sequence length times batch times dtype. At 128k context a 70B model's cache (~171 GB) exceeds its own weights.
Picture a hotel that must keep a guest file for every person who has ever checked in, on every floor of the building. Each new guest adds one file per floor, and you can never throw any away while they stay. With a few guests the filing cabinet is tiny. With a hundred thousand guests across eighty floors, the cabinets fill the entire basement, far more space than the hotel's own furniture takes up. The KV cache is that filing cabinet for a language model. Every token it has read leaves a note on every layer, and the notes pile up linearly as the conversation grows. Eventually the notes need more memory than the model weights, which is why long chats get expensive to serve.
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.
4 min: state the formula with all six factors plus the 2, compute ~171 GB, compare to ~140 GB weights, then name GQA, FP8 KV, and paged attention as the mitigations.
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.
Dropping the per-layer factor or the factor of 2 for K and V. Both errors put the estimate off by one or two orders of magnitude, which hides that the cache outgrows the weights.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.