Papers
Topics
Authors
Recent
Search
2000 character limit reached

KV-Fold: One-Step KV-Cache Recurrence for Long-Context Inference

Published 12 May 2026 in cs.LG, cs.AI, and cs.CL | (2605.12471v1)

Abstract: We introduce KV-Fold, a simple, training-free long-context inference protocol that treats the key-value (KV) cache as the accumulator in a left fold over sequence chunks. At each step, the model processes the next chunk conditioned on the accumulated cache, appends the newly produced keys and values, and passes the enlarged cache forward; the same one-step update is applied repeatedly, analogous to foldl in functional programming. Building on the KV cache concatenation primitive introduced for latent multi-agent communication, we repurpose it as a chunk-to-chunk recurrence for long-context inference. When processing chunk t, the model attends to the KV cache carried from earlier chunks as a prefix, reusing its internal state across segments without modifying or retraining the model. Despite its simplicity, the induced recurrence is stable: per-step drift rises briefly and then saturates into a flat plateau that persists across deep chains. This plateau is insensitive to a 10,000x change in numerical precision, robust across chunk sizes, and consistent across model families. At the task level, KV-Fold preserves exact information over long distances. On a needle-in-a-haystack benchmark, it achieves 100% exact-match retrieval across 152 trials spanning contexts from 16K to 128K tokens and chain depths up to 511 on Llama-3.1-8B, while remaining within the memory limits of a single 40GB GPU. Compared to streaming methods, which trade fidelity for bounded memory, KV-Fold maintains long-range retrieval while operating as a sequence of tractable forward passes. Overall, our results show that frozen pretrained transformers already support a stable form of KV-cache recurrence, providing a practical route to long-context inference without architectural changes or training.

Summary

  • The paper introduces a training-free protocol that leverages a left fold of the KV cache to extend transformer context.
  • It demonstrates 100% exact-match retrieval across up to 128K tokens while maintaining stable negative log-likelihood performance.
  • KV-Fold scales inference on long sequences by accumulating non-compressed activations without fine-tuning or architectural changes.

KV-Fold: A Training-Free Recurrence Protocol for Scalable Long-Context Inference

Overview

The paper "KV-Fold: One-Step KV-Cache Recurrence for Long-Context Inference" (2605.12471) introduces a simple, training-free protocol for extending the inference context of pretrained transformers far beyond their native context windows. By treating the key–value (KV) cache as an accumulator in a left fold across sequence chunks, KV-Fold enables transformers to process arbitrarily long sequences as a series of manageable forward passes, maintaining state across chunks without fine-tuning, architectural modification, or insertion of special memory tokens. The method is evaluated thoroughly across multiple model families and settings, demonstrating robust and stable performance on both perplexity and task-level retrieval, with strong claims of exact information retention at deep chain depths.

Methodology

KV-Fold builds on the KV cache concatenation primitive, originally explored for latent multi-agent communication, and repurposes it as a form of chunkwise recurrence. Given a long input sequence, KV-Fold divides it into contiguous chunks. At each chunk tt, the model performs a forward pass:

  • It conditions on the KV-cache accumulated from previous chunks as a prefix.
  • It produces new keys and values for the current chunk, then appends them to the cache.
  • Continuous positional encodings are used across chunk boundaries to preserve spatial alignment.

Formally, this process constitutes a left fold over chunks, with the accumulated KV cache acting as the fold's state. Critically, the cache is neither compressed nor transformed—it grows linearly with sequence length, retaining a non-compressed representation of past activations for content-addressable retrieval.

Empirical Results

Stability and Drift: KV-Fold displays a brief rise in divergence from the full-attention regime at early chunk boundaries, after which the difference in negative log-likelihood (NLL) saturates and remains flat ("drift plateau") across deep chains. This behavior is consistent across an order-of-magnitude change in numerical precision, different chunk sizes, and diverse model families (including Llama-3.1-8B and Qwen2.5-7B). The plateau indicates a slightly shifted but stable attention regime, not an accumulation of numerical error.

Exact Information Retention: On needle-in-a-haystack benchmarks, KV-Fold achieves 100% exact-match retrieval across all tested context lengths (up to 128K tokens) and chain depths (up to 511), even on memory-constrained hardware. Multi-needle retrieval experiments further corroborate that multiple distinct facts are preserved without interference through deep recurrences.

Memory and Compute Trade-Off: KV-Fold operates with linear growth of memory relative to context length, as the cache accumulates all past activations. While this is substantially more than memory-bounded streaming solutions, it enables full content-based retrieval over arbitrary distances. For instance, at 128K tokens (Llama-3.1-8B), KV-Fold operates within 40GB hardware constraints where full attention would require impractically large memory (>1TB). Total wall-clock time is also within practical bounds, and per-chunk computation scales linearly with cache size.

Robustness: The protocol’s efficacy is invariant to numerics (e.g., bfloat16 vs. fp32), chunk size, and model architecture. Even under quantization noise, retrieval rates remain high (int8 quantization: 93% at maximum tested depths).

Contrast with Streaming Methods: In direct comparison, streaming inference protocols like StreamingLLM offer bounded memory usage and faster per-chunk runtimes but fail to retrieve information once it exits the fixed-size sliding window. KV-Fold, by contrast, always preserves retrieval fidelity at the cost of linear memory, making explicit the trade-off along the memory-retrieval frontier.

Theoretical and Practical Implications

Structural Shift Interpretation: The empirical drift plateau suggests that chaining induces a transition to a new, self-consistent attention regime—a putative fixed point of the recurrence induced by chunkwise KV-passing. This regime differs slightly but stably from the single-pass regime. Importantly, the non-compressed cache and content-based addressing ensure continued retrievability of early information.

Practical Scalability: The approach presents a tractable inference-time strategy for scaling transformers to contexts that would be intractable via standard full-attention mechanisms, enabling exact retrieval applications in domains such as codebase analysis, medical record processing, preference modeling over prolonged interactions, and log analysis for critical operations.

Benchmarks:

Context Length (tokens) Chain Depth Retrieval Accuracy Peak GPU Mem (GB) Wall-Clock (s)
32K 127 100% 21.00 13.2
64K 255 100% 25.86 44.9
96K 383 100% 30.72 96.9
128K 511 100% 35.57 171.3

All retrieval benchmarks perform at ceiling; see above for details.

Implications for Future Research

KV-Fold demonstrates that pretrained transformers are already capable of stable, scalable long-context inference given appropriate state-passing protocols, obviating the need for retraining for many use cases. Future directions suggested in the paper include:

  • Hybridization with Position Extrapolation: Composing KV-Fold with methods for extending or re-anchoring positional encodings (e.g., YaRN, Positional Interpolation, LongRoPE) could push context lengths even further.
  • Compression for Memory Efficiency: Integrating learned or algorithmic compression schemes for the growing KV cache without sacrificing retrieval capability would enable constant-memory inference.
  • Fine-tuning for Drift Correction: Light-weight fine-tuning could be investigated to minimize the initial drift plateau and bring multi-step KV-Fold inference even closer to the full-attention regime.

These directions point toward models that are both hardware-efficient and information-preserving for arbitrarily long contexts—a critical property for next-generation sequence models.

Conclusion

KV-Fold (2605.12471) establishes a principled, training-free protocol for extending the usable context of pretrained transformers to the limits of hardware memory, offering exact information retrieval across enormous contexts with empirically bounded and stable performance drift. The method addresses a critical scalability bottleneck in transformer deployments and highlights unexplored capabilities latent in standard model architectures. Future work on cache compression and position-extension is likely to yield further operational advances towards infinite-context, information-robust sequence modeling.

Paper to Video (Beta)

No one has generated a video about this paper yet.

Whiteboard

No one has generated a whiteboard explanation for this paper yet.

Open Problems

We haven't generated a list of open problems mentioned in this paper yet.

Collections

Sign up for free to add this paper to one or more collections.