Lune

EMNLP2025Top-tier venue

Linear-Time Demonstration Selection for In-Context Learning via Gradient Estimation

Ziniu Zhang, Zhenshuo Zhang, Dongyue Li, Lu Wang, Jennifer G. Dy, Hongyang R. Zhang

Abstract

This paper introduces an algorithm to select demonstration examples for in-context learning of a query set. Given a set of n examples, how can we quickly select k out of n to best serve as the conditioning for downstream inference? This problem has broad applications in prompt tuning and chain-of-thought reasoning. Since model weights remain fixed during in-context learning, previous work has sought to design methods based on the similarity of token embeddings. This work proposes a new approach based on gradients of the output taken in the input embedding space. Our approach estimates model outputs through a first-order approximation using the gradients. Then, we apply this estimation to multiple randomly sampled subsets. Finally, we aggregate the sampled subset outcomes to form an influence score for each demonstration, and select k most relevant examples. This procedure only requires precomputing model outputs and gradients once, resulting in a linear-time algorithm relative to model and training set sizes. Extensive experiments across various models and datasets validate the efficiency of our approach. We show that the gradient estimation procedure yields approximations of full inference with less than 1% error across six datasets. This allows us to scale up subset selection that would otherwise run full inference by up to 37.7× on models with up to 34 billion parameters, and outperform existing selection methods based on input embeddings by 11% on average.

Ask Lune

Know this paper in minutes, not an afternoon.

Lune can read every page of it and the top-tier papers that cite it, and gives you its claims, its evidence and its limits, each one cited to the paper it comes from. No skimming, no AI slop.

5 top-tier papers have cited it since. Know what they found before your reviewers do.

Questions to start from

Lune can draw on

the full paper · the top-tier papers that cite it · related work

Get the answer

Free to start. No credit card required.

Lineage

  • WinQ: Accelerating Quantization-Aware Training of Language Models Around Saddle Points cites Efficient Estimation of Kernel Surrogate Models for Task Attribution
  • WinQ: Accelerating Quantization-Aware Training of Language Models Around Saddle Points cites Scalable Multi-Objective and Meta Reinforcement Learning via Gradient Estimation
  • Efficient Estimation of Kernel Surrogate Models for Task Attribution cites Rethinking the Role of Demonstrations: What Makes In-Context Learning Work?
  • Efficient Estimation of Kernel Surrogate Models for Task Attribution cites Scalable Multi-Objective and Meta Reinforcement Learning via Gradient Estimation
  • Efficient Estimation of Kernel Surrogate Models for Task Attribution cites What Can Transformers Learn In-Context? A Case Study of Simple Function Classes
  • Rethinking the Role of Demonstrations: What Makes In-Context Learning Work? cites Language Models are Few-Shot Learners
  • Scalable Multi-Objective and Meta Reinforcement Learning via Gradient Estimation cites Gradient Surgery for Multi-Task Learning
  • What Can Transformers Learn In-Context? A Case Study of Simple Function Classes cites Language Models are Few-Shot Learners
Builds onCited byLeft to right by year; the more cited, the higher.

Cited by top-tier papers5

  1. Efficient Estimation of Kernel Surrogate Models for Task AttributionZhenshuo Zhang, Minxuan Duan, Hongyang R. ZhangICLR 2026 · 6 citations
  2. Scalable Multi-Objective and Meta Reinforcement Learning via Gradient EstimationZhenshuo Zhang, Minxuan Duan, Youran Ye, Hongyang R. ZhangAAAI 2026 · 3 citations
  3. Unsupervised Process-Aware Coreset Selection for In-Context LearningWei Zheng, Zijie Wang, Xin Li, Bin Gong et al.ICML 2026
  4. WinQ: Accelerating Quantization-Aware Training of Language Models Around Saddle PointsDongyue Li, Zechun Liu, Kai Yi, Zhenshuo Zhang et al.ICML 2026
  5. Efficiently Learning Branching Networks for Multitask Algorithmic ReasoningDongyue Li, Zhenshuo Zhang, Minxuan Duan, Edgar Dobriban et al.KDD 2026

Builds on18

  1. Language Models are Few-Shot LearnersTom B. Brown, Benjamin Mann, Nick Ryder, Melanie Subbiah et al.NeurIPS 2020 · 64,255 citations
  2. Gradient Surgery for Multi-Task LearningTianhe Yu, Saurabh Kumar, Abhishek Gupta, Sergey Levine et al.NeurIPS 2020 · 2,261 citations
  3. What Can Transformers Learn In-Context? A Case Study of Simple Function ClassesShivam Garg, Dimitris Tsipras, Percy Liang, Gregory ValiantNeurIPS 2022 · 883 citations
  4. Learning to Prompt for Continual LearningZifeng Wang, Zizhao Zhang, Chen-Yu Lee, Han Zhang et al.CVPR 2022 · 635 citations
  5. Rethinking the Role of Demonstrations: What Makes In-Context Learning Work?Sewon Min, Xinxi Lyu, Ari Holtzman, Mikel Artetxe et al.EMNLP 2022 · 634 citations

Related papers

  1. Selecting Demonstrations for Many-Shot In-Context Learning via Gradient MatchingJianfei Zhang, Bei Li, Jun Bai, Rumei Li et al.ACL Findings 2025
  2. In-Context Learning with Iterative Demonstration SelectionChengwei Qin, Aston Zhang, Chen Chen, Anirudh Dagar et al.EMNLP Findings 2024 · 19 citations
  3. Revisiting Demonstration Selection Strategies in In-Context LearningKeqin Peng, Liang Ding, Yancheng Yuan, Xuebo Liu et al.ACL 2024
  4. Representative Demonstration Selection for In-Context Learning with Two-Stage Determinantal Point ProcessZhao Yang, Yuanzhe Zhang, Dianbo Sui, Cao Liu et al.EMNLP 2023 · 3 citations
  5. Active Example Selection for In-Context LearningYiming Zhang, Shi Feng, Chenhao TanEMNLP 2022 · 84 citations
Dusk over the sea between two cliffs drawn in fine vertical lines