In-Context Former: Lightning-fast Compressing Context for Large Language Model

Xiangfeng Wang, Zaiyi Chen, Tong Xu, Zheyong Xie, Yongyi He, Enhong Chen

Abstract

Large Language Models (LLMs) face significant computational overhead when processing long contexts due to the quadratic complexity of self-attention. We present In-Context Former (IC-Former), a lightweight, standalone compression model that utilizes a cross-attention mechanism to condense long documents into compact "soft prompts." IC-Former achieves up to 112x faster compression than existing LLM-based encoders while maintaining over 90% of the original model's performance and reducing memory usage by 88%.

Introduction: The Context Window Tax

As Large Language Models are increasingly tasked with processing massive datasets—ranging from entire books to extensive chat histories—they encounter a fundamental bottleneck: the "context window tax." The standard Transformer self-attention mechanism operates with quadratic time complexity ($O(n^2)$), meaning that as input length doubles, the computational cost quadruples.

Overview of the IC-Former pipeline showing how long context is compressed into soft prompts for an LLM.

While existing solutions attempt to compress context into summary tokens, many rely on the LLM itself as the encoder. This approach inherits the LLM's heavy overhead, making real-time applications computationally expensive. IC-Former addresses this by introducing a separate, lightweight architecture designed specifically for rapid context compression.

The IC-Former Architecture

IC-Former is a standalone model (~607M parameters) that acts as a pre-processor for a target LLM. Its design departs from traditional self-attention in several key ways:

  • Causal Cross-Attention: Instead of every token attending to every other token, IC-Former uses a small set of learnable "digest tokens" as queries to extract information from contextual word embeddings. This shifts complexity from quadratic to linear ($O(kn)$), where $k$ is the fixed number of digest tokens.
  • Rotary Positional Embeddings (RoPE): The model integrates RoPE to allow digest tokens to perceive the relative positions of the text they are processing, ensuring the preservation of sequential information.
  • Independent Integration: As a separate model rather than a modification (like LoRA), it can be used to pre-process text before it ever reaches the frozen LLM.
Detailed architecture of IC-Former, highlighting the attention mask, causal cross-attention mechanism, and the generation of digest vectors.

Training Methodology

The authors employ a two-stage training process to ensure both information density and reasoning utility:

  1. Reconstruction Pre-training: The model is trained to compress text such that the LLM can perfectly reconstruct the original input from the resulting compressed tokens.
  2. Instruction Fine-tuning: The model is further refined on question-answering tasks using the PwC (Prompt-with-Context) dataset to ensure the "soft prompts" are optimized for downstream reasoning.

Key Insights and Performance

Analysis of the model's internal mechanics reveals a sophisticated division of labor. Visualization of attention maps shows a "backslash" pattern, indicating that digest tokens naturally learn to focus on small, sequential neighborhoods of text. Furthermore, the model exhibits a semantic hierarchy: initial layers focus on grammar and punctuation, while deeper layers aggregate high-level semantic content like nouns and verbs.

Experimental Results

  • Speed: IC-Former is 68x to 112x faster at compressing context than current baselines like ICAE.
  • Efficiency: It requires only 1/32 of the FLOPs and achieves over 88% memory savings during compression.
  • Retention: Despite the massive reduction in compute, the model retains over 90% of the performance of the uncompressed baseline across ROUGE and BLEU metrics.

Conclusion

IC-Former demonstrates that context compression does not need to be as computationally expensive as the LLM inference itself. By utilizing a lightweight cross-attention architecture, it provides a path toward real-time processing of massive contexts without the traditional quadratic penalty. Future work will explore scaling this approach to even larger models and further closing the performance gap on highly detail-sensitive tasks.

Summary updated

BibTeX
@inproceedings{wang2024context,
  author = {Xiangfeng Wang and Zaiyi Chen and Tong Xu and Zheyong Xie and Yongyi He and Enhong Chen},
  title = {{In-Context Former: Lightning-fast Compressing Context for Large Language Model}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  doi = {10.18653/v1/2024.findings-emnlp.138},
}
Figure