Cross-Layer KV Cache Reuse: LLM Architecture and Inference Optimization

    Cross-layer key-value (KV) cache reuse reduces the number of independent historical states stored by a large language model. This article explains how it works, why it requires architectural and training support, and how it interacts with grouped-query attention, local attention, cache quantization, and mixture-of-experts designs. It also outlines how to evaluate memory savings, inference performance, output quality, and cache-management correctness under real workloads.

    1. LLM Architecture Is Shifting Toward Resource Efficiency

    Parameter count and answer quality are not enough to evaluate a large language model. Production deployment introduces additional engineering questions: How much accelerator memory does each request require? Can the model handle long inputs reliably? How does latency change as concurrency increases? Can it run on resource-constrained devices?

    These questions are shifting architectural priorities from increasing model capacity to improving useful output per unit of resources. The key-value (KV) cache is an important part of this shift. It avoids recomputing historical information during autoregressive generation, but its storage requirements grow with context length.

    Cross-layer KV cache reuse allows some attention layers to share previously generated keys and values instead of maintaining a complete, independent cache for every layer. Understanding this mechanism helps explain the architectural trade-offs behind long-context inference, lightweight deployment, and high-concurrency serving.

    Cross-layer reuse is not simply a cache setting. It changes information flow and parameter organization inside the model, so it generally needs to be considered during both architecture design and training.

    2. Why the KV Cache Becomes a Bottleneck

    What computation does caching eliminate?

    For each new token, an autoregressive language model computes attention over the preceding context. Attention typically uses three representations:

    • Queries identify relevant information.
    • Keys support relevance matching.
    • Values provide the content to be aggregated.

    Under causal attention, where later tokens do not change earlier representations, previously computed keys and values can be stored. When generating the next token, the model computes representations for the new position and reads the historical cache.

    The KV cache therefore eliminates repeated computation of historical keys and values. It does not eliminate the new query’s access to historical information. As context grows, cache reads and attention computation can still become major costs.

    Estimating cache capacity

    Assuming uniform layer configurations, storage based on actual sequence lengths, and no additional overhead, KV cache memory can be approximated as:

    1. M ≈ 2 × L × B × T × Hkv × D × S

    Where:

    • L is the number of attention layers maintaining independent caches.
    • B is the batch size or number of concurrent sequences.
    • T is the cached length of each sequence.
    • Hkv is the number of key-value heads.
    • D is the dimension of each key-value head.
    • S is the number of bytes stored per element.
    • The factor of 2 accounts for both keys and values.

    This formula highlights an important point: even when model weights remain unchanged, longer contexts and higher concurrency continue to increase cache requirements.

    Actual serving memory must also accommodate model weights, temporary computation buffers, allocation granularity, cache-management metadata, and other runtime overhead. Theoretical cache capacity is therefore a starting point for budgeting, not a direct measure of how many requests a device can support.

    3. Cross-Layer Reuse Reduces Independently Stored Historical States

    From per-layer caches to shared caches

    In a conventional design, each attention layer typically generates keys and values from its own input representations and maintains an independent cache. Because layers process different representations, these caches are not naturally identical copies.

    Cross-layer reuse reorganizes this relationship. Multiple layers can form a sharing group: a designated source layer generates the keys and values, while other layers retain their own query computations but read the group’s shared KV cache.

    Conceptually, the process is:

    1. The source layer generates keys and values
    2. → New positions are written to the shared cache
    3. → Subsequent reuse layers generate their own queries
    4. → Each reuse layer computes attention using the shared keys and values

    If a model originally stores separate caches for L layers and is redesigned to maintain only G independent cache groups, the ideal cache-tensor capacity ratio is approximately:

    1. Shared cache capacity / Original cache capacity ≈ G / L

    This assumes all other configurations remain unchanged. The ratio describes only the cache tensors themselves. It does not mean total memory, inference latency, or deployment cost will fall by the same proportion.

    Why existing caches cannot simply be removed

    Different layers often perform different representation transformations. Keys and values generated by one layer may not be suitable for direct use by another. Forcing layers to share them can disrupt the learned relationship between queries and key-value representations, shifting attention distributions.

    Reliable implementations therefore generally require training around the sharing structure or sufficient adaptation training to learn the new information flow. For an existing model that was not designed for cross-layer sharing, changing an inference configuration alone cannot be assumed to provide lossless compression.

    This distinguishes an architectural optimization from runtime memory management. Memory management primarily changes where and how data is stored; architectural sharing also changes which intermediate representations the model relies on.

    Matching dimensions is not the same as matching meaning

    At a minimum, an implementation must verify that:

    • Query and shared-key dimensions satisfy attention computation requirements.
    • The mapping between query heads and key-value heads is consistent.
    • Positional representations are generated and applied compatibly.
    • Causal masks and accessible context ranges are correct.
    • All relevant layers see consistent cache lengths during incremental generation.

    These checks establish computational validity. Whether the shared representations remain sufficient for the model’s tasks must be established through training and quality evaluation.

    4. Benefits and Limits

    Lower cache storage and write requirements

    When multiple layers reuse the same key-value state, fewer independent historical tensors need to be stored. Where the architecture allows it, reuse layers also no longer need their own key and value projections, reducing the associated computation and writes.

    This matters particularly on resource-constrained devices. Fitting model weights into memory does not guarantee stable operation after a long conversation. Controlling cache growth can support longer contexts or leave more memory available for other application components.

    Attention reads do not automatically disappear

    Reuse layers generally have different queries and still need to perform their own attention computations. Multiple layers reading the same cache does not mean one read can automatically serve all their computations.

    Actual speedups depend on cache layout, compute kernels, device bandwidth, scheduling across sharing layers, and the runtime’s ability to exploit data locality.

    A smaller cache is a relatively direct structural benefit. Faster generation must be demonstrated through measurement.

    Sharing can constrain layer-specific representations

    Independent per-layer keys and values allow different depths to maintain different views of information. Sharing reduces that freedom. Broader sharing can offer larger savings, but it may also impose stronger constraints on representation capacity.

    How to form sharing groups, which layers should remain independent, and which layers should reuse cached states are experimental design choices. There is no fixed optimum for every model and task.

    5. How Cross-Layer Reuse Complements Other Optimizations

    Grouped-query attention optimizes a different dimension

    Grouped-query attention lets multiple query heads share a smaller number of key-value heads, primarily reducing the number of KV heads within each layer. Cross-layer reuse reduces the number of layers or groups storing independent KV states.

    In the cache formula, grouped-query attention reduces Hkv, while cross-layer reuse reduces the number of independent cache groups. The two can be combined when their structures are compatible. However, the combined quality and performance must be evaluated again; the capacity formula alone cannot predict the final benefit.

    Local attention limits history rather than cache copies

    Local attention restricts how much historical context each position can access, reducing attention computation and cache-management pressure. Cross-layer reuse does not necessarily change the visible context length.

    The distinction is straightforward:

    • Local attention limits how much history a layer can access.
    • Cross-layer reuse reduces how many independent sets of historical states are stored.

    For tasks that require retrieving information across long distances, a local window can impose a direct constraint. Cross-layer sharing may preserve a longer access range, but the model’s ability to use distant information still requires validation.

    Low-precision caches reduce bytes per element

    Low-precision cache storage reduces memory by using fewer bytes per element. Cross-layer reuse reduces the number of independent states. These approaches can be combined, but their effects on numerical error should not be treated as simply additive.

    When multiple layers use one cache, errors in that cache may affect several stages of computation. Evaluation should therefore cover long-text retrieval, numerical processing, structured output, and multi-turn conversations—not just general question answering.

    Mixture-of-experts designs address a separate resource problem

    Sparse mixture-of-experts architectures typically place multiple experts in the feed-forward components and activate only a subset for each token. This separates total parameter count from the amount of computation activated for a given token.

    It does not automatically solve the attention-cache problem. A model that activates only some experts can still need substantial historical KV storage.

    Mixture-of-experts designs also introduce routing, load-balancing, and cross-device communication considerations. Dense architectures generally have a more direct computation path, but whether they are easier to optimize or cheaper to deploy depends on model scale, training choices, and deployment conditions.

    6. Evaluating Cross-Layer Reuse for Production

    Establish three resource budgets

    Before deployment, estimate these separately:

    • Weight budget: Memory required by model parameters in their storage format.
    • Cache budget: Memory determined by context length, concurrency, and cache structure.
    • Execution budget: Temporary tensors, computation workspaces, and runtime overhead.

    Reporting weight size alone understates production requirements. Testing only short requests can also hide the memory pressure that appears with longer contexts.

    Measure prefill and decoding separately

    During prefill, the model processes the prompt and builds its cache. During subsequent token-by-token decoding, it repeatedly reads historical states and writes new ones.

    Cross-layer sharing may affect these phases differently. At a minimum, testing should record:

    • Time to first token.
    • Latency of subsequent tokens.
    • Request throughput.
    • Peak accelerator memory usage.

    These metrics should be measured across different input lengths, output lengths, and concurrency levels.

    Include long-range dependencies in quality evaluation

    Beyond standard knowledge-based question answering, useful tests include:

    • Retrieving key facts from different positions in a long document.
    • Retaining and combining several conditions scattered across the input.
    • Following early instructions after a lengthy conversation.
    • Producing output that satisfies fixed schemas and field requirements.
    • Distinguishing similar entities and numerical values in long materials.

    These tasks are more likely to expose information confusion caused by representation sharing. Examine the distribution of failure cases rather than relying only on a single average score.

    7. Implementation Checks That Matter

    Shared caches make state management more sensitive. Each cache must be explicitly associated with a request and sharing group to prevent incorrect reuse across requests.

    Incremental generation also requires correct execution ordering. The computation responsible for generating keys and values must finish before dependent layers read the corresponding position. Shared state for each new token must be written according to the architecture, without appending the same position multiple times merely because several layers reuse it.

    Batch serving, request cancellation, and generation rollback require correct handling of valid lengths, position indices, and memory release. If caches are managed through shared references, reclamation must avoid both premature release and lingering allocations.

    Engineering validation can proceed in two stages:

    1. Compare full-sequence computation with incremental cached computation for the same architecture.
    2. Verify that the optimized execution implementation preserves the expected behavior.

    This separates quality changes introduced by the architecture from errors in the cache implementation.

    8. Optimize for End-to-End Efficiency

    Cross-layer KV cache reuse illustrates an important direction in model architecture: identify intermediate states that can be shared, then use less storage to support ongoing inference. It is neither a lossless switch that leaves the model unchanged nor a complete solution to long-context performance challenges.

    For developers, the priority is understanding its relationship with attention computation, positional representations, and incremental state management. For organizations deploying models, the priority is validating quality, latency, throughput, and cost together under real workloads.

    A successful architectural improvement should deliver reproducible, end-to-end gains: more useful work from limited memory, bandwidth, and compute while meeting the required quality standards.