[Paper Review] Why do Nearest Neighbor Language Models Work?
This paper investigates why k-Nearest Neighbor Language Models (kNN-LMs) improve perplexity even when retrieving from the same training data as the base language model. It identifies three key factors: ensembling representations from different transformer layers (55% of gain), using approximate nearest neighbor search (2.6% gain), and tuning the softmax temperature (up to 3.7% gain), which together explain kNN-LM's success beyond memorization or overfitting.
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.
Motivation & Objective
- To understand why kNN-LMs improve performance even when retrieving from the same training data used to train the base LM.
- To isolate and evaluate the specific design choices in kNN-LMs that contribute to their performance gains.
- To determine whether the improvement stems from retrieval, architectural ensembling, or other factors such as regularization or label smoothing.
- To develop a generalized formulation of kNN-LM to enable systematic ablation of design components.
- To demonstrate that the performance gains can be replicated in standard parametric LMs without explicit retrieval, improving efficiency.
Proposed method
- Propose a generalized formulation of kNN-LM that unifies the non-parametric component with the parametric LM’s softmax and embedding layers.
- Perform ablation studies by systematically modifying components: input representation for similarity computation, use of approximate vs. exact kNN search, and softmax temperature in the kNN component.
- Train and compare variants of the base LM using different input representations (e.g., attention layer output vs. feedforward layer output) to isolate ensembling effects.
- Replace the kNN retrieval with a differentiable matrix operation to improve inference efficiency while preserving performance.
- Test alternative training objectives and label types (e.g., soft labels) to rule out alternative explanations for kNN-LM’s success.
Experimental results
Research questions
- RQ1Why does kNN-LM reduce perplexity even when retrieving from the same training data used to train the base LM?
- RQ2Is the performance gain due to retrieval, architectural ensembling, or regularization from approximate search?
- RQ3Does the softmax temperature in the kNN component significantly affect model performance, and is it optimal in the original kNN-LM setup?
- RQ4Can the performance gains of kNN-LM be replicated in a standard parametric LM without retrieval, by modifying input representations or training procedures?
- RQ5What role does approximate nearest neighbor search play in generalization, and why might it outperform exact search?
Key findings
- Ensembling representations from different layers of the transformer (e.g., attention and feedforward outputs) accounts for 55% of the perplexity improvement in kNN-LM, equivalent to a 6.5% relative reduction in perplexity.
- Using approximate nearest neighbor search instead of exact search leads to a 2.6% relative perplexity improvement, suggesting a regularization effect from reduced precision.
- Tuning the softmax temperature in the kNN component can yield up to a 3.7% relative perplexity improvement, indicating that temperature is a critical but often overlooked hyperparameter.
- The kNN-LM’s success is orthogonal to ensembling with different base models, indicating the improvement is not due to model averaging.
- The performance gains are not due to memorization, overfitting, sparsification, stolen probabilities, or soft labeling, as these factors were ruled out through controlled ablation experiments.
- A differentiable matrix operation can replace kNN search while preserving over 6.5% relative perplexity improvement, enabling efficient inference without retrieval.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.