KV cache
A KV cache stores attention keys and values from previous tokens so decoding need not recompute them for each new token. Its memory grows with workload and model architecture.
Before you start: Review Token cost first. Python-like examples use variables, lists and functions.
Follow the mechanism
Prefill
Compute keys and values for input tokens.
Start with an everyday explanation
Keep earlier calculations on a scratchpad instead of recalculating every line. More simultaneous students and longer worksheets need more scratch space.
See a concrete example
For 32 layers, 32 KV heads, head dimension 128, 4,096 tokens, batch 1 and 2-byte elements, cache size is 2,147,483,648 bytes = 2 GiB. Eight KV heads under the same assumptions use 0.5 GiB.
A claim to question
KV memory is not a universal 0.5 MB per token, and weight size alone cannot establish GPU fit.
Try before revealing the answer
Use the GPU calculator to change KV heads from 32 to 8 and batch from 1 to 4.
Show the worked answer
Heads divided by four reduce the simplified cache fourfold; batch multiplied by four restores the original cache total. Add weights and measured runtime overhead before judging capacity.
Now explain the idea to someone else using your own example. If you cannot explain a stage, revisit it before moving on.
Engineering details and trade-offs
A simplified dense cache byte count is 2 × layers × KV heads × head dimension × tokens × batch × bytes per element. The factor two covers keys and values.
Grouped-query and multi-query attention reduce KV heads. Runtime layouts, paging, quantization, tensor parallelism and other memory allocations affect actual GPU use.
Reason through the example
For 32 layers, 32 KV heads, head dimension 128, 4,096 tokens, batch 1 and 2-byte elements, cache size is 2,147,483,648 bytes = 2 GiB. Eight KV heads under the same assumptions use 0.5 GiB.
Implement and test
Use the GPU calculator to change KV heads from 32 to 8 and batch from 1 to 4.
Inspect the reasoning and expected result
Heads divided by four reduce the simplified cache fourfold; batch multiplied by four restores the original cache total. Add weights and measured runtime overhead before judging capacity.
What to measure
State the expected result before changing a control. Record your inputs, compare the observed result and explain one limit of this model. In a real implementation, test the failure case as well as the normal path.
Answer before reading the reference
How would you estimate whether a model fits a GPU?
A defensible answer
Estimate weight bytes, architecture-specific KV cache at target context and concurrency, and runtime overhead. Leave headroom, then measure peak memory and throughput on the actual implementation.
Prepare the follow-up
How do paged caches and batching change fragmentation and throughput?
Show follow-up reasoning
Paged caches can reduce allocation waste and support flexible scheduling; batching can improve utilization while increasing active cache demand and waiting time. Measure peak memory, fragmentation, throughput and time-to-first/token latency at realistic sequence lengths and concurrency.
Use the engineering explanation and worked task above to support your response. Identify which assumptions you would need to verify in production.
Choose a practice depth
- New to GenAI
- Define kv cache with the everyday example and name its key limitation.
- Applied experience
- Explain the example, implementation choice and a measured failure case. Use the exercise as a small design review.
- Senior or systems depth
- Answer the follow-up, compare alternatives and justify an evaluation or rollout plan with operational constraints.
Self-review your answer
0/4 checks selected. This is self-assessment, not an interview score.
Your explanation and revision notes
Loading local notes…