Research

KV caching cuts transformer complexity to linear

A new technical guide by Adrian Tam highlights how implementing KV caching during the transformer inference phase reduces computational complexity from quadratic to linear to optimize performance.

Machine Learning Mastery31 Jul 2026Research
Image: Machine Learning Mastery

On August 4, 2026, researcher Adrian Tam published an in-depth analysis detailing the critical operational shift that occurs when transitioning PyTorch transformer models from training to inference. While training is dominated by backward passes and large matrix multiplications, inference relies on autoregressive token generation. This process is split into a prefill phase, which processes the prompt, and a decode phase, which generates new tokens one by one. Without optimization, a naive decoding loop repeatedly recomputes hidden states, leading to an inefficient computational complexity of O(N^2) for a sequence of length N. For a prompt of length P and generated tokens G, naive generation scales at O(P^2G + PG^2 + G^3), but implementing a key-value (KV) cache reduces this complexity to O(P^2 + PG).

To demonstrate this, Tam designed a minimal transformer-like model with a vocabulary size of 128, a hidden size of 64, four attention heads, and two layers. In a typical execution using a prompt tensor like [[10, 20, 30, 40]] to generate eight new tokens, the model utilizes a KV cache to store the attention keys and values of previous tokens. This prevents the model from recomputing attention for the entire sequence at each step, shifting the per-token decode cost from O(N^2) to O(N). However, this computational savings comes at a steep memory cost. The memory footprint of the KV cache is calculated as two times the number of layers, batch size, sequence length, number of KV heads, head dimension, and bytes per element.

For practitioners, managing this memory is a major engineering hurdle. For instance, a model with 32 layers, 32 KV heads, a head dimension of 128, and BF16 cache values running a batch size of one with a sequence length of 4,096 requires exactly 1,073,741,824 bytes of memory just for the KV cache of a single request. To mitigate these demands, serving systems rely on advanced architectures. Tam points to foundational research such as Vaswani's original Transformer paper, Noam Shazeer's work on multi-query attention, Dao's FlashAttention, the Orca distributed serving system, and Kwon's PagedAttention. These methodologies help developers transition from basic PyTorch implementations to highly efficient, production-grade inference engines.

This is our own summary of reporting by Machine Learning Mastery

More in Research