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

Alireza Nadali, Patrick Cooper, Ashutosh Trivedi, Alvaro VelasquezarXiv 2026

TL;DR

KV-Fold is a simple, training-free protocol that turns frozen, pretrained transformers into recurrent models for long-context inference. By treating the key-value (KV) cache as an accumulator in a functional "left fold" over sequence chunks, it bypasses the massive memory footprint of full attention while maintaining 100% exact information retrieval up to a 128K token context window on a single 40GB GPU.

What Problem Does This Paper Address?

While modern large language models (LLMs) boast massive context windows, processing long sequences in a single monolithic forward pass is incredibly expensive. The memory cost of standard attention scales quadratically with sequence length, quickly bottlenecking hardware.

To cope with this, existing approaches either compress the KV cache lossily, use sliding windows that discard old tokens entirely (like streaming methods), or require expensive retraining to introduce recurrent architectures. However, in tasks where exact recall matters—such as analyzing long codebases, legal documents, or medical records—discarding or compressing past tokens leads to unacceptable retrieval failures. The authors ask a fundamental question: Can a pretrained transformer carry its internal state across successive chunks, turning long-context inference into a stable recurrence without any modifications or retraining?

Methods

The authors introduce KV-Fold, an operational shift inspired by functional programming (foldl). Instead of processing a long sequence all at once, the sequence is divided into manageable chunks of size $C$.

Figure 2: KV-Fold. Queries in chunk t attend to the KV cache from earlier chunks (amber) as a prefix; chunk t’s KV states are then appended and carried forward. No compression or additional parameters are introduced.

When processing chunk $t$:

  1. The model attends to the accumulated KV cache from all previous chunks ($0$ to $t-1$) as a prefix.
  2. It generates new keys and values for the current chunk.
  3. It appends these new keys and values to the cache, passing the enlarged, uncompressed cache forward to chunk $t+1$.

Crucially, the model parameters remain completely unchanged, no special tokens are introduced, and positional embedding (RoPE) rotations are kept continuous across chunk boundaries to maintain sequence alignment.

Datasets

The method was evaluated using two primary setups:

  • PG-19 Validation Split: A benchmark of long-form prose used to evaluate language modeling performance, perplexity, and per-step drift.
  • Needle-in-a-Haystack (NIAH): The standard benchmark for long-context retrieval, testing both single-fact and multi-fact (up to $K=8$ independent key-value pairs) retrieval across contexts ranging from 16K to 128K tokens.

Key Insights and Ideas

  • The Drift Plateau: One might expect that processing text chunk-by-chunk would cause errors to compound over time. Surprisingly, the authors discovered that the "drift" (the difference in next-token likelihood between KV-Fold and full attention) does not grow linearly with chain depth. Instead, it rises briefly during the first few chunks and then stabilizes into a flat plateau.
Figure 3: Drift saturates; advantage persists. Per-step drift (blue) and recurrence advantage (green) versus chain depth on Qwen2.5-7B-Instruct (T = 16,384, C = 256). Drift saturates at ∼0.04 nats by depth 7 and remains flat through depth 63, while recurrence advantage stays positive throughout.
  • Structural, Not Numerical: This stability is a structural property of transformer attention dynamics, not a numerical artifact. Increasing numerical precision by 10,000× (moving from bfloat16 to fp32) only reduced the plateau drift by a tiny 2.8%.
  • A New Stable Attention Regime: The recurrence effectively shifts the frozen model into a slightly altered but highly stable attention regime that preserves content-based addressing capabilities over deep chains.

Key Results

  • Perfect Retrieval at Scale: On Llama-3.1-8B, KV-Fold achieved 100% exact-match retrieval across 152 trials spanning context lengths up to 128K tokens and chain depths up to 511.
  • Hardware Feasibility: At a 128K context window on a single 40GB A100 GPU, a standard full-attention pass is completely infeasible because its attention-scores matrix alone would require ~1 TB of memory. KV-Fold successfully ran within the hardware budget, peaking at 35.57 GB of GPU memory.
  • KV-Fold vs. StreamingLLM: While streaming methods like StreamingLLM maintain a constant memory footprint and run faster, they suffer from a total loss of memory once information exits their sliding window. KV-Fold trades linear memory growth for perfect long-range recall.
Comparison of retrieval accuracy and memory scaling between KV-Fold and StreamingLLM.

Limitations and Future Work

Despite its operational advantages, KV-Fold does not reduce the total memory required to store the final KV cache; the cache still grows linearly ($O(T)$) with the sequence length. As the cache size increases, per-step latency also scales linearly.

The authors suggest several promising directions for future work:

  1. Lightweight Fine-Tuning: Modest fine-tuning could potentially eliminate the residual drift plateau entirely.
  2. Context Extension: Combining KV-Fold with position-extrapolation methods (like YaRN or LongRoPE) could allow models to push well beyond their native context limits.
  3. Intelligent Compression: Developing techniques to compress the accumulated KV state without losing content-based addressability could pave the way toward true constant-memory, long-context inference.

Summary updated

BibTeX
@misc{nadali2026kv,
  author = {Alireza Nadali and Patrick Cooper and Ashutosh Trivedi and Alvaro Velasquez},
  title = {{KV-Fold: One-Step KV-Cache Recurrence for Long-Context Inference}},
  year = {2026},
  eprint = {2605.12471},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2605.12471},
}
Figure