Skip to main content
QUICK REVIEW

[Paper Review] $k$NN Prompting: Beyond-Context Learning with Calibration-Free Nearest Neighbor Inference

Benfeng Xu, Quan Wang|arXiv (Cornell University)|Mar 24, 2023
Domain Adaptation and Few-Shot LearningComputer Science12 citations
TL;DR

The paper introduces kNN Prompting, a calibration-free, gradient-free method that uses a nearest-neighbor approach over LM distributions to leverage unlimited training data beyond context length, yielding strong few-shot and fully supervised performance across multiple LLMs.

ABSTRACT

In-Context Learning (ICL), which formulates target tasks as prompt completion conditioned on in-context demonstrations, has become the prevailing utilization of LLMs. In this paper, we first disclose an actual predicament for this typical usage that it can not scale up with training data due to context length restriction. Besides, existing works have shown that ICL also suffers from various biases and requires delicate calibration treatment. To address both challenges, we advocate a simple and effective solution, $k$NN Prompting, which first queries LLM with training data for distributed representations, then predicts test instances by simply referring to nearest neighbors. We conduct comprehensive experiments to demonstrate its two-fold superiority: 1) Calibration-Free: $k$NN Prompting does not directly align LLM output distribution with task-specific label space, instead leverages such distribution to align test and training instances. It significantly outperforms state-of-the-art calibration-based methods under comparable few-shot scenario. 2) Beyond-Context: $k$NN Prompting can further scale up effectively with as many training data as are available, continually bringing substantial improvements. The scaling trend holds across 10 orders of magnitude ranging from 2 shots to 1024 shots as well as different LLMs scales ranging from 0.8B to 30B. It successfully bridges data scaling into model scaling, and brings new potentials for the gradient-free paradigm of LLM deployment. Code is publicly available.

Motivation & Objective

  • Address the context-length limitation of in-context learning (ICL) by reusing training data beyond the prompt length.
  • Eliminate reliance on calibration techniques by leveraging LM distributions for matching rather than direct label-token alignment.
  • Enable scalable data utility by decoupling training data usage from prompt length, enabling learning with larger datasets.
  • Investigate robustness, split strategies, and the effectiveness of anchor-based representations across diverse datasets and model scales.

Proposed method

  • Split training data into a demonstration set D and an anchor set A.
  • Query the LLM with prompts containing D and each anchor x^a to obtain a full LM distribution p(v|P,θ) and cache it as key k_i with label y^a as value in a datastore.
  • At inference, compute p(v|P_test,θ) for the test instance and retrieve k-nearest neighbors in the datastore using KL divergence D_KL(p_test || k_i).
  • Predict y by aggregating the labels of the k nearest neighbors via a majority-like voting: argmax_y sum_{i in NN^k(p_test, K)} 1(y_i^a = y).
  • The framework is calibration-free because it avoids aligning LM outputs with a fixed task-specific label space, and it supports beyond-context learning by using the anchor set to utilize unlimited data.
  • No training is required; the method is designed to work with existing LLMs and training data.

Experimental results

Research questions

  • RQ1Can calibration-free nearest-neighbor inference improve performance relative to calibration-based ICL methods under few-shot settings?
  • RQ2How does kNN Prompting scale when more training data are available beyond the context length, across different model sizes and tasks?
  • RQ3What is the impact of the demonstration-anchor split on performance, and how robust is the method to varying training splits?
  • RQ4Does leveraging full LM distributions (vs. only label words) for retrieval improve or degrade accuracy and robustness?

Key findings

  • Outperforms state-of-the-art calibration-based methods under strict few-shot settings (up to +7.07 on some tasks).
  • Demonstrates strong beyond-context scaling, improving steadily as training data grows from 2 to 1024 shots and across LLMs from 0.8B to 30B parameters.
  • With as few as 32 shots, achieves +13.58 average gain at its peak (0.8B model) and up to +18.84 in fully supervised settings.
  • Shows robustness improvements over vanilla ICL, with lower output instability and reduced standard deviation across seeds.
  • Outperforms ICL Ensemble and standard fine-tuning at various data scales, and remains competitive across model sizes including 0.8B–30B.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.