MDR: Model-Specific Demonstration Retrieval at Inference Time for In-Context Learning

Huazheng Wang, Jinming Wu, Haifeng Sun, Zixuan Xia, Daixuan Cheng, Jingyu Wang, Qi Qi, Jianxin Liao

Introduction

In-Context Learning (ICL) has emerged as a cornerstone of Large Language Model (LLM) utility, allowing models to adapt to new tasks via a few examples provided in the prompt. Traditionally, these examples (demonstrations) are selected using "one-size-fits-all" retrievers that prioritize semantic similarity. However, this approach ignores a critical reality: LLMs possess unique internal biases shaped by their diverse architectures and training data. An example that clarifies a task for one model might confuse another.

This paper introduces Model-Specific Demonstration Retrieval (MDR), a novel framework designed to select demonstrations tailored to the specific preferences and biases of the inference model at runtime. By moving beyond simple similarity, MDR achieves significant performance gains, demonstrating that the "best" example is often model-dependent.

The MDR Framework

MDR operates as a two-stage inference-time framework that requires no additional training or human annotation.

Overview of the MDR framework, illustrating the two-stage process of initial retrieval and model-specific re-ranking.
  1. Initial Retrieval: A standard pre-trained dense retriever (e.g., UPRISE) identifies a candidate pool of $K'$ demonstrations (typically the top 20) based on semantic similarity to the input query.
  2. Model-Specific Re-ranking: The core of MDR involves evaluating these candidates against the specific inference LLM. Each candidate is scored based on a combination of its likelihood under the model and its robustness.
  3. Final Selection: The top $K$ examples (usually 3) with the highest combined scores are selected to form the final prompt for the inference model.

Metrics for Model Compatibility

The re-ranking stage utilizes a dual-metric approach to determine demonstration quality:

  • Loss: Measured as the per-token negative log-likelihood. A lower loss indicates that the demonstration is well-aligned with the model's internal distribution, suggesting better "comprehension."
  • Robustness ($\lambda_{max}$): To ensure the model isn't led astray by minor perturbations, MDR calculates the maximum eigenvalue of the Fisher Information Matrix (FIM) for each example in the model's feature space. A smaller value signifies that the example provides a stable, robust signal.

These metrics are combined using a tuning coefficient ($C$), allowing the system to balance raw comprehension with stability.

Performance Evaluation

The researchers conducted an extensive evaluation across 23 datasets spanning 11 domains, including both "seen" tasks (familiar to the base retriever) and "unseen" tasks. MDR was compared against several baselines: Random selection, BM25, SBERT, and the state-of-the-art UPRISE retriever.

Performance of MDR compared to baselines across different numbers of demonstrations (K) on WIC and SamSum datasets.

The results were striking. On unseen tasks, MDR showed remarkable generalization, achieving a 41.2% improvement in F1 score on the SamSum summarization dataset for LLaMA-7B compared to UPRISE. Even on seen tasks, MDR provided a consistent boost, such as a 34.98% absolute improvement over random selection on SQuADv1. As shown in the figures above, MDR consistently maintains a performance lead across different values of $K$, indicating its robustness as a retrieval strategy.

Key Insights

  • Similarity is Not Enough: The study confirms that the most semantically similar example is frequently not the most effective one for a given model. MDR often promotes examples with lower similarity scores that better match the model's internal preferences.
  • The Importance of Robustness: Incorporating the FIM-based robustness metric is crucial. It prevents the selection of "vulnerable" examples that might have low loss but are highly sensitive to noise, ensuring a more reliable ICL experience.
  • Addressing Retriever-Model Mismatch: The paper demonstrates that performance suffers when a retriever is optimized for one model but used with another. MDR effectively bridges this gap at inference time.

Conclusion and Future Work

MDR represents a significant step toward personalized, model-aware prompt engineering. By leveraging the inference model's own feedback, it unlocks latent performance without the need for costly retraining.

However, the method does introduce computational overhead due to the need for gradient and eigenvalue calculations at inference time. Furthermore, its reliance on gradient access limits its current applicability to open-source models rather than "black-box" APIs. Future research will likely focus on reducing this latency and developing gradient-free proxies for these model-specific metrics.

Summary updated

BibTeX
@inproceedings{wang2024mdr,
  author = {Huazheng Wang and Jinming Wu and Haifeng Sun and Zixuan Xia and Daixuan Cheng and Jingyu Wang and Qi Qi and Jianxin Liao},
  title = {{MDR: Model-Specific Demonstration Retrieval at Inference Time for In-Context Learning}},
  booktitle = {Conference of the North American Chapter of the Association for Computational Linguistics},
  year = {2024},
  doi = {10.18653/v1/2024.naacl-long.235},
}
Figure