Unifying Demonstration Selection and Compression for In-Context Learning

Wenjie Li, Jun Gao · DOI

Abstract

In-Context Learning (ICL) allows Large Language Models (LLMs) to perform tasks by prepending a few examples to a prompt. However, this approach is often limited by context window constraints and the poor quality of external example retrievers. We present UniICL, a framework that unifies demonstration compression, selection, and generation into a single pipeline. By distilling demonstrations into "Memory Tokens" and caching them in a "Demonstration Bank," UniICL enables LLMs to handle up to 64 demonstrations on a single consumer GPU while simultaneously improving retrieval accuracy.

The Bottlenecks of In-Context Learning

Despite its power, ICL faces two primary challenges that hinder its scalability and effectiveness:

  1. The Context Crunch: Increasing the number of demonstrations to improve performance leads to an explosion in prompt length. This results in high memory requirements and slow inference, often exceeding the hardware limits of standard GPUs.
  2. The Selection Gap: Most ICL systems rely on "off-the-shelf" retrievers (like BM25 or S-BERT). These tools often select examples based on surface-level lexical similarity rather than logical relevance, which can mislead the LLM.
Comparison of UniICL against standard virtual token and retrieval methods.

As shown in the figure above, traditional methods often fail on nuanced tasks like linguistic acceptability, whereas UniICL's unified approach correctly identifies unacceptable sentence structures by leveraging deeper semantic understanding.

The UniICL Framework

UniICL augments a frozen LLM backbone (such as Vicuna-7B) with a lightweight 17M parameter adapter. The system operates through several key technical components:

  • Memory Slots [M]: Learnable embeddings are attached to demonstrations. The LLM processes these and distills the demonstration's essence into Memory Tokens.
  • Unified Selection: Instead of external tools, UniICL uses the cosine similarity between the Memory Tokens of the query and candidate demonstrations. This ensures the selection process is aligned with the LLM's internal data perception.
  • Demonstration Bank (DB): Because demonstrations are compressed independently, their Memory Tokens can be cached. This avoids redundant computation and significantly boosts throughput.
The UniICL pipeline featuring the Demonstration Bank and Adapter-based compression.

Two-Phase Training

The model is trained in two distinct phases. In Phase 1, it learns to compress information and generate text based on Memory Tokens. In Phase 2, a contrastive learning strategy called "Perplexity (PPL) Mining" is employed. The system identifies which demonstrations actually help the model generate correct answers (by lowering PPL) and trains the selector to prioritize these "helpful" examples over those that are merely lexically similar.

Key Results and Insights

UniICL was evaluated across a diverse range of tasks, including summarization (XSum), reasoning (CICERO), and linguistic understanding (CoLA, MMLU).

  • Massive Scalability: While standard models often crash due to memory limits at 8-shot ICL on a 24GB GPU, UniICL successfully scales to 64-shot ICL within the same footprint.
  • Performance Gains: UniICL consistently outperformed state-of-the-art compression methods like AutoCompressor and LLMLingua. For instance, it achieved a +2.8 Rouge-1 gain on Arxiv summarization tasks.
  • Information Density: Experiments revealed that a 12x compression ratio often performed better than lower ratios, suggesting that the compression process effectively filters out noise and concentrates the semantic signal.
  • Zero-Shot Retrieval: Even without being a dedicated retrieval model, UniICL’s internal representations proved highly effective for passage ranking on MS MARCO, outperforming traditional BM25 baselines.

Conclusion and Future Work

UniICL effectively bridges the gap between demonstration selection and compression, offering a scalable solution for high-performance ICL on limited hardware. Future research will explore the framework's interaction with complex prompting strategies like Chain-of-Thought (CoT) and its scalability to significantly larger models (e.g., 70B+ parameters).

Summary updated

BibTeX
@inproceedings{li2025unifying,
  author = {Wenjie Li and Jun Gao},
  title = {{Unifying Demonstration Selection and Compression for In-Context Learning}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
}
Figure