Estimate KV Cache Memory and Why Grouped-Query Attention Shrinks It

Last updated: November 29, 2026

Quick Overview

Compute how much accelerator memory the key-value cache needs when serving a decoder model at a given batch size and context length, and explain how multi-query and grouped-query attention change that number and model quality.

Google
Machine Learning
Machine Learning Engineer
Google
November 29, 2026
Machine Learning Engineer
Onsite
Machine Learning
Medium

0

0

0 solved


Compute how much accelerator memory the key-value cache needs when serving a decoder model at a given batch size and context length, and explain how multi-query and grouped-query attention change that number and model quality.

Serving Gemini in products like Search AI Overviews and Workspace is memory-bound during decoding, so Google interviewers probe whether candidates can size the KV cache and connect it to batch size, latency, and cost. Strong answers move from the formula to the architectural lever and its quality trade-off.

What the Interviewer Expects
  • Writes the KV cache formula including the factor of 2 for keys and values
  • Plugs in realistic numbers and gets the units right
  • Explains why decoding is memory-bandwidth bound
  • Describes multi-query and grouped-query attention and the cache reduction factor
  • Discusses the quality cost and uptraining from a multi-head checkpoint
  • Mentions paging, quantizing the cache, or sliding windows as other levers
Key Topics to Cover
KV cache
Grouped-query attention
Multi-query attention
Memory bandwidth
LLM serving
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 paged attention reduce fragmentation in the cache?
  • What happens to the arithmetic intensity of decoding as batch size grows?
  • Would you quantize the KV cache to int8, and how would you validate it?
  • How does a sliding-window layer pattern change the memory curve for 1M-token contexts?
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 32-layer decoder, 32 query heads of dimension 128 (d=4096d = 4096), bf16 storage (2 bytes), context of 8192 tokens, and a serving batch of 16 concurrent sequences. I am asked for cache memory, no...

Approach

Every layer stores a key and a value vector per KV head per token: bytes =2⋅L⋅hkv⋅dh⋅nctx⋅B⋅b= 2 \cdot L \cdot h_{kv} \cdot d_h \cdot n_{ctx} \cdot B \cdot b. In standard multi-head attention hkv=hqh_{kv} = h_q. Multi-quer...


Submit Your Answer
Markdown supported