Why do Nearest Neighbor Language Models Work?
Frank F. Xu, Uri Alon, Graham Neubig
Abstract
Language models (LMs) compute the probability of a text by sequentially computing a representation of an already-seen context and using this representation to predict the next word. Currently, most LMs calculate these representations through a neural network consuming the immediate previous context. However recently, retrieval-augmented LMs have shown to improve over standard neural LMs, by accessing information retrieved from a large datastore, in addition to their standard, parametric, next-word prediction. In this paper, we set out to understand why retrieval-augmented language models, and specifically why k-nearest neighbor language models (kNN-LMs) perform better than standard parametric LMs, even when the k-nearest neighbor component retrieves examples from the same training set that the LM was originally trained on. To this end, we perform a careful analysis of the various dimensions over which kNN-LM diverges from standard LMs, and investigate these dimensions one by one. Empirically, we identify three main reasons why kNN-LM performs better than standard LMs: using a different input representation for predicting the next tokens, approximate kNN search, and the importance of softmax temperature for the kNN distribution. Further, we incorporate these insights into the model architecture or the training procedure of the standard parametric LM, improving its results without the need for an explicit retrieval component. The code is available at https://github.com/frankxu2004/knnlm-why .
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.
10 top-tier papers have cited it since. Know what they found before your reviewers do.
Lune can draw on
the full paper · the top-tier papers that cite it · related work
Free to start. No credit card required.
Lineage
- CAGRA: Highly Parallel Graph Construction and Approximate Nearest Neighbor Search for GPUs cites Generalization through Memorization: Nearest Neighbor Language Models
- kNN-LM Does Not Improve Open-ended Text Generation cites Improving Language Models by Retrieving from Trillions of Tokens
- kNN-LM Does Not Improve Open-ended Text Generation cites Generalization through Memorization: Nearest Neighbor Language Models
- kNN-LM Does Not Improve Open-ended Text Generation cites Nearest Neighbor Machine Translation
- kNN-LM Does Not Improve Open-ended Text Generation cites The Curious Case of Neural Text Degeneration
- Improving Language Models by Retrieving from Trillions of Tokens cites Generalization through Memorization: Nearest Neighbor Language Models
- Improving Language Models by Retrieving from Trillions of Tokens cites Language Models are Few-Shot Learners
- Memory Decoder: A Pretrained, Plug-and-Play Memory for Large Language Models cites Generalization through Memorization: Nearest Neighbor Language Models
- Language Models are Few-Shot Learners cites The Curious Case of Neural Text Degeneration
- Nearest Neighbor Machine Translation cites Generalization through Memorization: Nearest Neighbor Language Models
- RAGO: Systematic Performance Optimization for Retrieval-Augmented Generation Serving cites Improving Language Models by Retrieving from Trillions of Tokens
- RAGO: Systematic Performance Optimization for Retrieval-Augmented Generation Serving cites Generalization through Memorization: Nearest Neighbor Language Models
- RAGO: Systematic Performance Optimization for Retrieval-Augmented Generation Serving cites Language Models are Few-Shot Learners
- RAGO: Systematic Performance Optimization for Retrieval-Augmented Generation Serving cites Nearest Neighbor Machine Translation
- FT2Ra: A Fine-Tuning-Inspired Approach to Retrieval-Augmented Code Completion cites Improving Language Models by Retrieving from Trillions of Tokens
- FT2Ra: A Fine-Tuning-Inspired Approach to Retrieval-Augmented Code Completion cites Generalization through Memorization: Nearest Neighbor Language Models
Cited by top-tier papers10
- CAGRA: Highly Parallel Graph Construction and Approximate Nearest Neighbor Search for GPUsHiroyuki Ootomo, Akira Naruse, Corey Nolet, Ray Wang et al.ICDE 2024 · 59 citations
- RAGO: Systematic Performance Optimization for Retrieval-Augmented Generation ServingWenqi Jiang, Suvinay Subramanian, Cat Graves, Gustavo Alonso et al.ISCA 2025 · 16 citations
- Memory Decoder: A Pretrained, Plug-and-Play Memory for Large Language ModelsJiaqi Cao, Jiarui Wang, Rubin Wei, Qipeng Guo et al.NeurIPS 2025 · 14 citations
- FT2Ra: A Fine-Tuning-Inspired Approach to Retrieval-Augmented Code CompletionQi Guo, Xiaohong Li, Xiaofei Xie, Shangqing Liu et al.ISSTA 2024 · 14 citations
- kNN-LM Does Not Improve Open-ended Text GenerationShufan Wang, Yixiao Song, Andrew Drozdov, Aparna Garimella et al.EMNLP 2023 · 3 citations
Builds on11
- Language Models are Few-Shot LearnersTom B. Brown, Benjamin Mann, Nick Ryder, Melanie Subbiah et al.NeurIPS 2020 · 64,255 citations
- The Curious Case of Neural Text DegenerationAri Holtzman, Jan Buys, Li Du, Maxwell Forbes et al.ICLR 2020 · 4,112 citations
- Improving Language Models by Retrieving from Trillions of TokensSebastian Borgeaud, Arthur Mensch, Jordan Hoffmann, Trevor Cai et al.ICML 2022 · 1,629 citations
- Generalization through Memorization: Nearest Neighbor Language ModelsUrvashi Khandelwal, Omer Levy, Dan Jurafsky, Luke Zettlemoyer et al.ICLR 2020 · 1,038 citations
- Nearest Neighbor Machine TranslationUrvashi Khandelwal, Angela Fan, Dan Jurafsky, Luke Zettlemoyer et al.ICLR 2021 · 323 citations
Related papers
- You can't pick your neighbors, or can you? When and How to Rely on Retrieval in the kNN-LMAndrew Drozdov, Shufan Wang, Razieh Rahimi, Andrew McCallum et al.EMNLP Findings 2022 · 10 citations
- Adaptation Approaches for Nearest Neighbor Language ModelsRishabh Bhardwaj, George Polovets, Monica SunkaraACL Findings 2023 · 7 citations
- Nearest Neighbor Zero-Shot InferenceWeijia Shi, Julian Michael, Suchin Gururangan, Luke ZettlemoyerEMNLP 2022 · 26 citations
- Efficient Nearest Neighbor Language ModelsJunxian He, Graham Neubig, Taylor Berg-KirkpatrickEMNLP 2021
- Neuro-Symbolic Language Modeling with Automaton-augmented RetrievalUri Alon, Frank F. Xu, Junxian He, Sudipta Sengupta et al.ICML 2022 · 79 citations
