Estimate KV Cache Memory for Serving Llama and Reduce It
Last updated: November 29, 2026
Quick Overview
Estimate how much GPU memory the key-value cache needs when serving a 7B Llama-style model at long context, and explain the techniques that reduce it.
Meta
November 29, 20260
0
0 solved
Estimate how much GPU memory the key-value cache needs when serving a 7B Llama-style model at long context, and explain the techniques that reduce it.
Serving Llama models in Meta AI across WhatsApp, Messenger, and Instagram makes inference cost a real concern. The interviewer wants back-of-envelope memory math and a clear view of why KV cache, not weights, limits batch size at long context.
What the Interviewer Expects
- Explain what the KV cache stores and why it speeds up decoding
- Compute bytes per token from layers, heads, head dimension, and precision
- Scale the estimate to context length and batch size
- Explain grouped-query attention and its saving
- Mention paged attention, quantized cache, and sliding windows
Key Topics to Cover
How to Approach This
- Understand the bias-variance trade-off. High training accuracy but low test accuracy signals overfitting.
- Choose evaluation metrics carefully based on the problem. Accuracy alone is often insufficient.
- Feature engineering is often more impactful than model selection.
- Know when to use tree-based models (tabular data) vs neural networks (unstructured data).
- Handle class imbalance with SMOTE, class weights, or appropriate loss functions.
Possible Follow-up Questions
- How does the cache change the prefill versus decode cost profile?
- What quality risk comes with int8 or int4 KV cache?
- How would you handle many users sharing the same long system prompt?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Explore ML Interview PrepSample Answer
Clarify
Assume a Llama 2 7B style model: 32 layers, hidden size 4096 split into 32 heads of dimension 128, fp16 everywhere, context up to 4096 tokens. Weights alone are about 13.5 GB in fp16....
Approach
During autoregressive decoding each new token attends to all previous tokens. Without a cache we would recompute K and V for the whole prefix every step, which is quadratic total work. The cache store...