This one has a lot of surface area and I didn't pace myself well.
Start by explaining the high-level architecture of a transformer decoder block and the role of KV caching in autoregressive generation. Then, walk through the shape transformations step-by-step for a single decoding step, highlighting how the cache is updated and used. Finally, discuss memory implications and propose a verification strategy comparing cached outputs to a naive full recompute.
Pro tip: Emphasize that KV caching trades memory for speed, and quantify the memory cost per layer and per token to show you understand the practical constraints. Also, mention that correctness verification should include both numerical equality (within tolerance) and performance benchmarking to ensure the cache actually speeds up generation.
Briefly describe the transformer decoder block components (self-attention, cross-attention if applicable, feed-forward) and how KV caching avoids recomputing keys and values for past tokens during autoregressive generation.
For a single decoding step, detail the input shapes (e.g., new token embedding: [batch, 1, d_model]), how queries, keys, and values are computed, and how the cache is concatenated along the sequence dimension. Show the resulting attention output shape and how it feeds into subsequent layers.
Compute the memory footprint of the KV cache: per layer, per token, it stores two tensors of shape [batch, num_heads, seq_len, head_dim]. Discuss scaling with sequence length, batch size, and model size, and mention techniques like quantization or paging to mitigate memory pressure.
Outline a test that runs a naive full recompute (without cache) and compares the outputs token-by-token with the cached version, ensuring numerical closeness (e.g., using torch.allclose). Also, suggest measuring latency and memory usage to validate the speedup and overhead.
Mention trade-offs such as increased memory usage versus reduced computation, and edge cases like handling the initial prompt (prefill) and cache initialization, as well as potential issues with variable sequence lengths in batched generation.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.