KV Cache Layering and Reuse in Looped Decoding

    Looped decoding is not simply running the network again. In decoder-only autoregressive models, each layer’s attention reads historical keys and values. If the same layers are run for another time step while the cache strategy still stores a full per-round KV cache, storage and bandwidth grow roughly linearly with the number of rounds. This article examines two practical cache protocols: distance-based reuse with sparse far-history KV and sliding-window near-history KV, and a front-half-write/back-half-map design that keeps the global KV cache to a single copy. It also covers evaluation criteria, implementation constraints, pseudocode, and rollout guidance for balancing quality, peak memory, and per-step KV reads.

    KV Cache Layering and Reuse in Looped Decoding

    Looped execution is not the same as running the network again. In a decoder-only autoregressive model, attention at every layer must read the keys and values of historical tokens. If the same set of layers is run for another time step while the cache policy still stores a full set of keys and values for each round, storage and bandwidth grow roughly linearly with the number of rounds.

    Whether a looping scheme is practical depends less on the slogan and more on three questions: can historical information still be retrieved, are near-distance dependencies still precise, and can the inference system schedule the implementation stably?

    Where the problem comes from

    The cache semantics of a standard decoder are straightforward: every token before the current position has one key and one value, and later steps only append rather than recompute. Looping breaks this assumption. The second round does not continue generation on a longer sequence; it looks at the same context again using updated hidden states. If the second round still receives its own separate global cache, then:

    • GPU memory roughly doubles compared with a single round, and long contexts hit the capacity wall first;
    • the number of bytes read by attention increases, and compute may not become the bottleneck at the same time because memory bandwidth saturates first;
    • the alignment between the two caches—same token, different rounds—must be maintained by indices, which complicates scheduling and prefix caching.

    As a result, direct looping is rarely used as-is. In engineering practice, either the amount of KV that must remain resident is reduced, or the way later layers depend on the cache is changed.

    Path 1: distance-based reuse

    A common compromise is to split cache responsibility by distance instead of copying the entire history for every round.

    Far-distance information is more stable and is suitable for reusing sparse keys and values already computed in the first round. Far tokens often affect the current step through topic, entity, and long-range constraints, and the second round does not need to rewrite all their keys and values. Sparsification can be by block, by head, or by importance index, keeping only the subset that later queries will hit.

    Near-distance information changes more easily and is suitable for storing second-round local keys and values separately in a sliding window. Nearby tokens determine word choice, coreference, and syntactic continuity, and the hidden-state changes after looping are concentrated there. The window length should match the task’s local dependencies: too short and it loses the just-updated context; too long and the window itself degrades into a second global cache.

    A sparse index stitches the two stores together. A query first goes through the index, which decides whether far-distance reads reuse the cache and near-distance reads use the sliding window, rather than running dense attention twice over the full history. The index can be a static block table or can be updated by layer, but training and inference must use the same visibility rules; otherwise the gains from looping will be consumed by distribution shift.

    The cost of this path is engineering complexity:

    • the cache is no longer a single growing tensor but a “reuse region + sliding-window region + index”;
    • sparse patterns may differ across heads and layers, requiring padding or grouping during batching;
    • prefix reuse, speculative decoding, and continuous batching must all understand this hybrid layout, or duplicate computation and stale-block reads become likely.

    This path suits scenarios where a large decoder-only base model already exists and only a few layers are extracted for limited looping. The base model itself does not need a completely new topology; what changes is the cache protocol of the looped segment. The number of layers and rounds should be determined by the memory budget and the benefit curve, rather than assuming that a fixed depth is always optimal.

    Path 2: front-half write, back-half map

    Another approach structurally removes the need for the back half to store global keys and values. Split depth into two segments: the front half consumes the input and writes reusable keys and values; the back half still queries history, but its keys and values are not kept as a separate resident global cache. Instead, they are derived from front-half outputs through a fixed mapping. The mapping can be a linear projection, a head-shared low-rank transform, or a lightweight network tied to layer number, but its semantics should be stable: the historical representation read by the back half is a function of the front-half representation, not a copy from another independent attention round.

    The direct effect is that the global cache has only one primary copy. When the back half is looped or deepened, the added cost is mainly computation rather than an equal amount of KV storage. If near-distance information still needs higher resolution, the back half can keep only a very short local state instead of storing the entire history again.

    Compared with Path 1, the difference is whether “storing less” is an architectural constraint or an inference trick. An architectural constraint makes it easier to keep the training objective and inference layout consistent; a pure inference trick must additionally prove that sparse reuse has not changed the attention support set the model learned. Not storing global keys and values in the back half also means that it cannot freely rewrite the keys and values of distant tokens. If a task strongly depends on multi-round rewriting of long-range memory, this path will suffer; if the task mainly refines local predictions over a fixed context, it is cleaner.

    How to compare, not how to choose sides

    When comparing the two designs, use the same set of questions rather than drawing a single conclusion from parameter count or layer count.

    Consistency. Is the history seen by the second round or the back half consistent with the training-time mask? When reusing first-round sparse keys and values, it must be clear which positions are allowed to be hit by second-round queries. For a mapping scheme, it must be clear whether the mapping changes with token position and whether it introduces future information.

    Bandwidth and capacity. The number of global cache copies, sliding-window width, and sparse retention rate together determine peak memory. Reducing parameters without reducing KV reads can still leave long contexts bandwidth-bound. Compute volume and KV read/write bytes should be measured separately so that compute utilization does not hide memory problems.

    Source of accuracy. The benefit of looping usually comes from integrating the same context a second time, not from a longer cache. If far-distance information is completely frozen and the near-distance window is too narrow, the second round only circles within a small neighborhood, and additional depth will not translate into understanding. Conversely, if both rounds keep dense global caches, accuracy may improve, but system cost is often unacceptable.

    System integration. A hybrid cache must be expressible as blocks that the inference engine can recognize: which blocks are shared across requests, which blocks expire with rounds, and how to roll back when speculative decoding fails. A mapping scheme must ensure that mapping computation can be merged into existing layers rather than serially adding a full-history rewrite on the critical path.

    Observability. Before launch, record at least the cache hit range per layer, actual sliding-window length, sparse-block hit rate, and KV read volume per step. Without these metrics, it is impossible to distinguish “looping is ineffective” from “the cache policy has pruned away effective attention.”

    Implementation constraints

    The pseudocode below describes only the data flow and does not correspond to any specific framework interface.

    1. state = embed(tokens)
    2. # Front half: write the single global KV
    3. for layer in front_layers:
    4. state, kv_global = layer(state, kv_global)
    5. # Path A: looped segment
    6. kv_far = sparsify(kv_global) # far-distance reuse
    7. kv_near = empty_window() # near-distance second round
    8. for pass in range(num_loops):
    9. for layer in loop_layers:
    10. state = layer(state, kv_far, kv_near)
    11. kv_near = update_window(kv_near, state)
    12. # Path B: back half does not store a separate global KV
    13. for layer in back_layers:
    14. kv_mapped = project(kv_global) # mapped from front-half outputs
    15. state = layer(state, kv_mapped, local_state_only)

    The sparsify and project operations in the pseudocode must appear during training or be proven equivalent to the attention support used during training. Deleting blocks only at inference time can easily produce irreproducible quality fluctuations.

    Rollout guidance

    If the starting point is a mature decoder-only model and the goal is to use a small number of layers for limited rounds of refinement, evaluate distance-based reuse first: sparse far-distance, sliding-window near-distance, and unified visibility through an index. Get the cache layout and rollback semantics right before increasing the number of loop rounds. Rounds should have an upper bound, and marginal gains should be observed on a validation set to avoid stacking depth without purpose.

    If the structure can be adjusted and the goal is to control KV capacity for long contexts, prioritize a design where the front half writes centrally and the back half reads through a mapping. In this way the global cache remains a single copy, and the back half’s extra state serves only local dependencies. When multi-round rewriting of long-range memory is required, introduce a writable local cache instead of falling back to full duplication.

    Whichever path is chosen, unmeasured layer counts, round counts, or compression ratios should not be used as default configurations. A reasonable approach is to fix the task and context length, sweep sliding-window size, sparse retention rate, and loop rounds separately, and track quality, peak memory, and per-step read bytes at the same time. When quality gains are smaller than cache and bandwidth costs, stop adding rounds.

    The value of looped decoding is that the same context can be integrated multiple times; its risk is that the KV cache is amplified by rounds, and that sparsification and mapping change the history the model can actually see. Treating far-distance reuse, near-distance sliding windows, and cross-layer mapping as composable cache protocols rather than mutually exclusive product paths is what enables explainable choices among capacity, accuracy, and engineering complexity.