KatInterview

LLM Inference Memory and Phase Splitting: KV Cache and PD Disaggregation

Machine LearningModel answer
MediumModel answerMachine Learningllm-servingkv-cacheinference

Last updated

Explain why transformer decoders cache keys and values, size that cache for a given model, and reason about running prefill and decode on separate GPU pools.

You are discussing how to serve a large decoder-only language model efficiently. Answer the two parts below the way you would in a technical conversation: explain the mechanism, put numbers on it, and discuss trade-offs. State your assumptions first, since the answers depend on them.

Assumptions to settle up front

  • Model shape: number of layers, query heads versus key/value heads (full multi-head attention or grouped-query attention), head dimension, numeric precision.
  • Workload: typical prompt length, output length, and number of concurrent requests.
  • Latency goals: time to first token, time per output token, or total throughput.
  • Hardware: GPU type and the interconnect between GPUs that would serve each phase.

Part 1: The KV cache

Describe what is stored during autoregressive generation, why this speeds up decoding, and how much memory it uses. Derive the cache size per token and per request for a model you specify. Explain what that size implies for batch size, maximum context length and GPU memory management as requests grow.

Cover:

  • What is cached per layer and per head, and what is not cached
  • The compute saved per step versus the memory spent
  • A per-token formula with a worked example
  • Consequences for batching and memory management

Part 2: Prefill-decode disaggregation

Describe the two phases of inference and how their resource profiles differ. Then explain disaggregation, where prefill and decode run on separate groups of GPUs: why do it, what does it cost, and when is it not worth it?

Cover:

  • Compute-bound prefill versus bandwidth-bound decode, and which latency metric each drives
  • Interference when both phases share GPUs
  • The cost of moving state between pools and how to reduce or hide it
  • When colocating both phases is the better choice

Hints

Hint 1

Think about which tensors in attention depend only on earlier tokens and therefore never change once computed. Queries are only needed for the current token.

Hint 2

For the size formula, count two tensors (K and V) per layer, each with (number of KV heads x head dimension) values per token, multiplied by bytes per value. Then check how grouped-query attention changes the KV head count.

Hint 3

For disaggregation, compare arithmetic intensity: prefill processes many tokens per weight read, decode processes one token per sequence per weight read. Then ask what has to travel from the prefill pool to the decode pool, and how large it is relative to the network bandwidth.