KV Cache

What is KV Cache?

KV cache (key-value cache) is a memory optimization used during inference in Transformer-based AI models, particularly large language models (LLMs). It stores the key (K) and value (V) tensors calculated by attention layers for previously processed tokens. When the model generates subsequent tokens, it reuses these tensors instead of recomputing them, reducing redundant computation and accelerating autoregressive generation.

For AI hardware, KV cache is important because its footprint grows with factors including sequence length, model architecture, batch size or request concurrency, and numerical precision. As a result, KV cache can become a significant consumer of accelerator memory and an important consideration for HBM capacity and bandwidth.

How KV Cache Works

Transformer attention generates query (Q), key (K), and value (V) representations. During autoregressive inference, previously calculated keys and values remain useful as each new token is generated.

The process typically works like this:

  1. Process the initial tokens. The model computes Q, K, and V representations through its attention layers.
  2. Cache K and V. Keys and values associated with processed tokens are retained in accelerator memory.
  3. Generate the next token. The model computes representations for the new token and attends to the previously cached K and V data.
  4. Update the cache. The new token’s K and V tensors are added for use during subsequent decoding.
  5. Repeat. The cache grows as generation continues until the sequence ends or the serving system releases or reuses the allocated memory.
 

This exchanges increased memory consumption for lower computational overhead during token-by-token decoding.

KV Cache Key Features and Benefits

  • Reduces redundant computation: Keys and values for previously processed tokens do not need to be recalculated during every decoding step.
  • Accelerates autoregressive inference: Reusing cached attention data makes token generation more computationally efficient.
  • Supports long-context processing: Previously generated attention state can remain available as the sequence grows, although longer contexts increase memory requirements.
  • Creates a capacity-bandwidth trade-off: The optimization reduces computation but requires space to retain K/V tensors and sufficient memory bandwidth to access them.
  • Affects inference concurrency: When many requests are served simultaneously, their KV caches compete for available accelerator-memory capacity.
 

Enabling Technologies

KV cache depends on both software and hardware. At the software level, inference engines can use techniques such as paged KV-cache management, cache reuse, quantization, and KV cache offload to improve memory utilization.

At the hardware level, technologies such as HBM, memory controllers, GPUs and AI accelerators, DDR DRAM, PCI Express, and CXL can form parts of the broader memory hierarchy supporting inference. HBM is particularly relevant for accelerator-local KV cache because it provides the high memory bandwidth required by data-intensive AI workloads.

KV cache is also distinct from a conventional processor cache. CPU and GPU hardware caches automatically retain frequently accessed data, whereas an LLM KV cache is explicitly managed as part of Transformer inference and contains previously computed attention keys and values.

KV Cache and Rambus Technologies

Rambus does not provide the KV-cache algorithm itself, but its memory and interface technologies address the infrastructure underneath AI inference. HBM memory controller IP can connect AI accelerators and other high-performance SoCs to HBM, while DDR5 memory interface chips support server memory systems that can form another tier in the AI memory hierarchy. High-speed PCIe and CXL technologies are also relevant where AI architectures move data among accelerators, processors, and expanded memory resources.

Rambus logo