Zenaique

Name two recovery moves a serving stack can pull when KV memory runs short mid request.

Short answer·Medium·4.0 · 0·~3 min·Asked atCognizantDifyKrutrim·Relevant atAi4bharatAnthropicCerebrasDeepseek
Attempt it

A long context serving job is about to exceed its KV cache budget mid generation. The scheduler can either kill the request or degrade quality gracefully. Name two attention side strategies the runtime can apply, and explain the tradeoff each makes.

Free · 2 AI evals / day
TL;DR

Eviction (drop tokens, sliding window or H2O) and in-place quantization (FP16 to INT8 or INT4). Both degrade quality gracefully instead of OOM-killing the request.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Picture a bookshelf running out of room while you are still adding books. Two ways to make space without throwing the whole shelf out. First, toss old books you have not opened in a while, you lose some content but you keep reading. Second, photocopy each book into smaller print so each one takes half the space, you keep every book but the print is harder to read. A serving system's short-term memory works the same way: drop notes you probably will not need (call it eviction) or store each note using fewer digits per number (call it compression). Real systems usually do both before giving up and dropping the whole job.

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

KV cache memory is the single biggest serving-time constraint for long-context LLM inference. When the cache budget runs out mid-generation, the scheduler has a choice: kill the request and force a retry, or degrade gracefully. Two attention-side strategies make graceful degradation possible.

This deep dive walks through the memory equation, the two independent shrink axes, the canonical eviction and quantization policies, and how production serving stacks combine them in practice.

Mental model: KV memory is the product of entry count and bytes per entry. Eviction attacks the first; quantization attacks the second. Real stacks do both before they give up.

The KV memory equation

What the cache stores

For each generated token, every transformer layer caches its K and V tensors so future tokens can attend to them without recomputing. With Multi-Query or Grouped-Query Attention, fewer KV heads are stored than Q heads, but the per-token cost is otherwise straightforward.

The exact formula

bytes=2LHkvdhTb\text{bytes} = 2 \cdot L \cdot H_{kv} \cdot d_h \cdot T \cdot b

Where:

  • 2 = one tensor for K, one for V.
  • L = number of layers.
  • H_kv = number of KV heads (reduced by MQA or GQA).
  • d_h = head dimension (usually 128).
  • T = sequence length (the term that scales linearly with context).
  • b = bytes per element (2 for FP16, 1 for INT8, 0.5 for INT4).

Concrete example

For Llama-3-70B with 80 layers, 8 KV heads (GQA), d_h = 128, T = 8192, FP16:

2 * 80 * 8 * 128 * 8192 * 2 = 2.68 GB per request

Multiply by batch size and concurrent sequences and the cache dominates GPU memory before model weights do.

The two attackable axes

From the formula, only T (entry count) and b (bytes per entry) are runtime-modifiable. L, H_kv, and d_h are baked into model weights. Eviction shrinks T. Quantization shrinks b.

Eviction: shrink the entry count
Quantization: shrink the bytes per entry
Composing the strategies in production
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.
StrategyAxis attackedQuality tradeoffImplementation cost
Sliding windowEntry count (T)Drops oldest contextTrivial
H2OEntry count (T)Drops low-attention tokensPer-token running sum
StreamingLLMEntry count (T)Window + sink tokensTrivial extension of sliding
INT8 KVBytes per entry (b)Compounds across layersPer-channel scales
INT4 KVBytes per entry (b)Stronger precision lossPer-group scales (KIVI)

Real products, models, and research that use this idea.

  • vLLM with PagedAttention plus INT8 KV cache support combines block-level memory layout with quantization.
  • TensorRT-LLM ships INT8 and FP8 KV cache modes targeted at H100 and newer GPUs.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QWhy does pure sliding window degrade quality even when window is generous?
A

Models trained with full attention learn to route excess softmax probability to the first few tokens (attention sinks). Drop those sinks via sliding and the first kept token inherits the sink role, distorting its representation. StreamingLLM fixes this by keeping the sinks permanently.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Forgetting that long-context serving has two orthogonal shrink axes (entry count and bytes per entry) and naming only one. A senior answer mentions both.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • Two independent axes of KV memory: entry count and bytes per entry

  • Sliding window, H2O, and StreamingLLM eviction policies

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
Explain scaled dot product attention.
Short answer·Medium