Memorize Step by Step: Efficient Long-Context Prefilling with Incremental Memory and Decremental Chunk

Zhiyuan Zeng, Qipeng Guo, Xiaoran Liu, Zhangyue Yin, Wentao Shu, Mianqiu Huang, Bo Wang, Yunhua Zhou, Linlin Li, Qun Liu, Xipeng Qiu

Abstract

Large Language Models (LLMs) face significant computational and memory bottlenecks during the prefilling stage of long-context processing. Standard chunked prefilling often utilizes inefficient fixed-size memory buffers that waste resources on early tokens. We present a novel approach using Incremental Memory (IM) and Decremental Chunking (IMDC) to optimize this process. By starting with a small memory buffer that grows as processing progresses, and simultaneously shrinking input chunks to maintain stable GPU memory usage, we achieve a 1.45x speedup and a 23.3% reduction in peak VRAM consumption without compromising performance on long-context benchmarks.

Introduction

The ability of LLMs to process contexts exceeding a million tokens has opened new frontiers in AI. However, the "prefilling" stage—where the model processes the initial prompt—remains a massive bottleneck due to the quadratic complexity of attention mechanisms. Standard "chunked prefilling" attempts to mitigate this by processing the input in segments and maintaining a fixed-size Key-Value (KV) cache "memory." This paper identifies a fundamental inefficiency in fixed-size memory: early tokens are rarely referenced later in the sequence, meaning large initial buffers waste computational cycles and memory.

Methodology: IM and IMDC

The proposed system introduces two core techniques to optimize the prefilling pipeline:

  1. Incremental Memory (IM): Instead of a static buffer, the memory size starts small and grows (e.g., linearly or via a square root function) as the model processes subsequent chunks. This aligns resource allocation with the model's actual attention patterns.
  2. Decremental Chunk (IMDC): To ensure the total KV cache length (current chunk + previous memory) does not exceed GPU capacity, input chunk sizes are decreased as the memory buffer expands. This maintains a constant memory footprint and prevents Out-of-Memory (OOM) errors.
  3. Adaptive Layer Strategy: The authors also developed an adaptive growth strategy that allocates more memory to specific layers that demonstrate a higher "retention ratio" of information.
Comparison of memory management strategies: Fixed-Size Memory vs. the proposed Incremental Memory and Decremental Chunking.

Empirical Motivation

Analysis of attention scores reveals that memory from early chunks has a significantly lower impact on final outputs compared to later chunks. Furthermore, the distribution of retained tokens shows a heavy concentration toward the end of the sequence. These findings justify the "start small" philosophy of Incremental Memory, as maintaining a large buffer for early, low-impact tokens is inefficient.

Empirical analysis of (a) average attention scores and (b) memory distribution across chunks, showing that early memory is less critical.

Results and Discussion

Evaluations on benchmarks like LongBench, RedPajama, and Needle-in-a-Haystack demonstrate that IMDC maintains high accuracy and low perplexity while significantly improving efficiency:

  • Efficiency Gains: The approach achieved a 1.45x speedup on NVIDIA A800 GPUs and reduced peak memory usage by 23.3% compared to fixed-size methods.
  • Scalability: The authors successfully processed sequences of 1 million tokens on a single GPU, whereas standard full attention typically crashes at much lower thresholds.
  • Retrieval Performance: While IM shows slight degradation for information at the very beginning of a sequence (0% depth) due to the small initial buffer, it outperforms fixed-size memory for information placed later in the document.

Conclusion

Incremental Memory and Decremental Chunking provide a robust framework for scaling LLM prefilling. By dynamically adjusting memory and chunk sizes, researchers can process massive contexts more efficiently, paving the way for "infinite" prefilling on single-enterprise GPUs. Future work aims to bridge the gap between these compression techniques and optimized kernels like Flash Attention for even greater performance gains.

Summary updated

BibTeX
@inproceedings{zeng2024memorize,
  author = {Zhiyuan Zeng and Qipeng Guo and Xiaoran Liu and Zhangyue Yin and Wentao Shu and Mianqiu Huang and Bo Wang and Yunhua Zhou and Linlin Li and Qun Liu and Xipeng Qiu},
  title = {{Memorize Step by Step: Efficient Long-Context Prefilling with Incremental Memory and Decremental Chunk}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  doi = {10.18653/v1/2024.emnlp-main.1169},
}
Figure