KV Cache Reduction for Long Contexts: Architectural Approaches to Lower Cost

    This article examines model-architecture-level methods for reducing KV cache in long-context LLMs, including attention-head organization, cross-layer sharing, and overall architecture. It explains why long contexts are expensive, gives a formula and worked example for estimating KV cache size, compares MHA, GQA, and MQA, discusses cross-layer KV reuse and the relationship between MoE/Dense architectures and KV cache, and provides a selection checklist and evaluation metrics.

    Scope

    This article focuses only on model-architecture-level ways to reduce KV cache. It covers how attention heads are organized, cross-layer sharing, and overall architecture form. Inference-system optimizations such as quantization, paged memory management, and request scheduling are outside the scope.

    Why Long Contexts Are Expensive

    The cost of long contexts appears mainly in two places.

    The first is memory. During autoregressive inference, the model must store Key and Value vectors for every historical token. This cache is the KV cache. When context length doubles, KV cache size also doubles.

    The second is memory access and computation. During prefill, attention computation grows quadratically with context length. After entering the decode stage, each generated token requires roughly O(n) attention computation per step, but the full KV cache must be read from memory. In many scenarios, memory bandwidth rather than compute determines how many tokens can be produced per second.

    How to Estimate KV Cache

    With batch size 1, KV cache size can be estimated as follows:

    1. KV cache bytes ≈ 2 × L × n × (n_kv × d_head) × bytes per element

    Here, L is the number of layers, n is context length, n_kv is the number of KV heads, and d_head is the dimension of a single head. The coefficient 2 corresponds to the two caches for K and V. Bytes per element is 2 for FP16 or BF16 and 1 for FP8.

    For example, consider a model with 32 layers and 32 query heads. If it uses standard multi-head attention (MHA), then n_kv equals 32. With head dimension 128, context length 32,768 tokens, and FP16:

    • MHA (n_kv = 32): 2 × 32 × 32,768 × (32 × 128) × 2 ≈ 16 GiB
    • For the same model with n_kv changed to 8: the result is 4 GiB

    This estimate does not include batch expansion, beam search, fragmentation from paged management, or changes introduced by quantization. In actual deployment, each additional batch increases cache size proportionally.

    Route 1: Reduce the Number of KV Heads

    This type of method lets multiple Query Heads share the same set of KV Heads. The compression effect is independent of context length and represents a structural cost reduction.

    GQA (Grouped Query Attention) divides Query Heads into groups, with each group sharing one set of KV. The compression ratio is n_head / n_kv. In the example above, 32 Query Heads with 8 KV Heads gives a compression ratio of 4. GQA was proposed by Ainslie et al. in 2023 in the paper “GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints.” Most trained models can maintain reasonable quality under a GQA configuration, but the specific degradation depends on the training recipe and evaluation tasks and should be based on measured results.

    MQA (Multi-Query Attention) is a more aggressive form in which all Query Heads share one set of KV, meaning n_kv = 1. It provides the highest compression ratio, but the quality risk is usually greater. MQA first appeared in Shazeer’s 2019 paper “Fast Transformer Decoding: One Write-Head is All You Need.”

    The choice between GQA and MQA mainly depends on the balance between memory and quality. If memory is tight but quality requirements are high, GQA can be tried first, with the number of groups adjusted as needed.

    Route 2: Reuse KV Across Layers

    KV states in adjacent layers or specific layers have some similarity. The idea behind cross-layer attention is that some layers no longer compute and store KV independently but reuse results from other layers. If the reuse ratio is r, the layer factor in KV cache size decreases proportionally.

    Related research exists in academia, such as “Reducing Transformer Key-Value Cache Size with Cross-Layer Attention,” which can be searched on arXiv. Recently, some open-source models have adopted similar mechanisms in small models or edge architectures, but the specific number of reused layers and the benefits should be based on the corresponding paper or model card.

    Several points require attention when using this type of mechanism. The choice of reused layers affects quality, and similarity varies considerably across layers. The higher the reuse ratio, the more obvious the compression, and the greater the difficulty of training and fine-tuning. Cross-layer reuse usually must be adopted during the training stage; existing models generally cannot apply it directly.

    Route 3: Architecture Form and Its Relationship to KV Cache

    The main difference between MoE and Dense is in the feed-forward network. MoE reduces the parameters that actually participate in computation for each token through sparse activation. It reduces computation, not KV cache size. KV cache is determined by the configuration of the attention layers and has no direct relationship with whether MoE is enabled.

    In a Dense model, every token passes through all parameters, so computation cost is higher. However, its training and post-training pipelines are relatively straightforward, and it is more common in scenarios that require a high quality ceiling and can accept the inference cost.

    Therefore, when selecting a model, attention configuration and feed-forward structure should be considered separately. The former determines memory usage for long contexts, while the latter determines compute throughput and the training approach.

    Selection Checklist

    When reviewing a model card or configuration file, it is advisable to confirm the following in order:

    1. Whether the attention type is MHA, GQA, or MQA, and the corresponding number of KV Heads.
    2. The number of layers and head dimension, for use in the formula above.
    3. The data type used for the cache: FP16, BF16, or FP8.
    4. The target context length and concurrent batch size, to estimate memory usage.
    5. Whether structures such as cross-layer sharing or sliding windows exist, and the context range to which these structures apply.

    How to Evaluate the Effect

    Looking only at maximum context length is not enough to judge cost. It is advisable to measure the following metrics and note the test conditions:

    • KV cache bytes per token: reflects the memory usage of a single token.
    • Maximum usable batch: how many requests can be served simultaneously under fixed memory.
    • TTFT (time to first token) and TPOT (time per output token): these correspond to the response experience in the prefill and decode stages, respectively.

    During testing, input length, output length, hardware, and inference framework version need to be fixed; otherwise, different results cannot be compared directly.