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
Machine Learning
Machine Learning Engineer
Meta
November 29, 2026
Machine Learning Engineer
Onsite
Machine Learning
Medium

0

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
KV cache
LLM inference
grouped-query attention
PagedAttention
Llama
memory estimation
How to Approach This
  1. Understand the bias-variance trade-off. High training accuracy but low test accuracy signals overfitting.
  2. Choose evaluation metrics carefully based on the problem. Accuracy alone is often insufficient.
  3. Feature engineering is often more impactful than model selection.
  4. Know when to use tree-based models (tabular data) vs neural networks (unstructured data).
  5. 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 Prep
Sample 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...


Submit Your Answer
Markdown supported