← Questions

Add a KV cache to GPT inference

0
Applied IntuitionAI/ML designLast asked Sep 2025

An AI/ML design interview question asked in Applied Intuition's technical phone screen, last asked Sep 2025. It tests transformers, inference and kv cache.

You have a decoder-only Transformer (a small GPT) that generates text autoregressively by re-running the full sequence for every new token. Design and implement key–value caching for its inference: give the tensor shapes, an API that keeps per-layer caches across decoding steps and across a batch, handle cache growth and memory limits, and support both greedy decoding and beam search.

Requirements

  • Functional: a prefill step that processes the prompt and fills the cache; a decode step that takes one new token per sequence, computes only its query, key and value, attends over the cached keys and values plus its own, and appends to the cache; batched inputs of different lengths; greedy and beam search, including reordering the cache when beams are reselected; sequences that hit EOS while others in the batch continue.
  • Non-functional: output identical to the uncached model; memory bounded by a maximum context; a clear speedup over recomputation, measured.

Clarifying questions

  • Model size: layers, heads, head dimension, maximum context, precision (fp16 or bf16)?
  • Batch sizes, and are prompts padded or packed?
  • Is the cache preallocated to the maximum length or grown as needed?
  • What should happen when a sequence reaches the maximum context?
  • Must it run on GPU only, and is a framework (PyTorch) assumed?

What a strong answer covers

  • Shapes: per layer, K and V of (batch, heads, seq_len, head_dim); prefill writes seq_len = prompt_len; each decode step writes one position. Position ids (or rotary offsets) continue from the cache length.
  • An API such as KVCache.update(layer, k_new, v_new) -> (k_all, v_all) and a model forward(input_ids, cache, positions) that returns logits and the updated cache.
  • Attention masks for padded prompts and for finished sequences.
  • Beam search: reorder the cache along the batch dimension with the selected beam indices each step.
  • Memory: 2 * layers * batch * heads * seq_len * head_dim * bytes, worked through for the given model; preallocation versus growth and fragmentation.
  • Compute: per-token cost drops from attending over and projecting the whole prefix at each step to projecting one token, so generating T tokens goes from about O(T²) projection work to O(T); attention over the prefix is still linear per step.
  • Tests: cached and uncached logits match at every step, EOS in the middle of a batch, beam reordering, the maximum-length boundary.

Follow-ups

  • Memory runs out for long contexts. Compare a sliding window, paged blocks and quantizing the cache.
  • Serve many requests of different lengths at once (continuous batching).
  • Share the cache for a common prompt prefix across requests.

Where it's asked

What it tests

transformers · inference · kv cache

Your canvas
Draw your answer hereBoxes, arrows and notes, saved for you to come back to.Sign in to start drawing