Efficient Nearest Neighbor Language Models

Junxian He, Graham Neubig, Taylor Berg-Kirkpatrick

Abstract

$k$-nearest neighbor language models ($k$NN-LMs) achieve state-of-the-art performance by retrieving information from a massive external datastore, but they are often 10-100x slower than standard models. This paper introduces three "remedies"—adaptive retrieval, greedy datastore pruning, and PCA-based dimension reduction—that together provide up to a 6x speedup in inference while maintaining or even improving model accuracy.

Introduction

Modern Neural Language Models (NLMs) are typically parametric, meaning all their "knowledge" is stored in the weights of the neural network. While powerful, they struggle with rare patterns and adapting to new domains. Non-parametric models like the $k$NN-LM solve this by using an external "datastore" containing millions (or billions) of vector representations of training data. At test time, the model looks up the $k$ most similar examples in the datastore to help predict the next word.

The Catch: This retrieval step is incredibly expensive. For every single word the model generates, it must perform a complex vector search across a massive database. This makes $k$NN-LMs too slow for most real-world applications. The authors set out to answer: Do we really need to retrieve for every token? Can we make the datastore smaller? And can we make the vectors themselves simpler?

Methods

The authors propose three distinct techniques to tackle the efficiency bottleneck:

  1. Adaptive Retrieval: The authors observed that the base NLM is often already confident enough to make a correct prediction. They trained a "retrieval adaptor"—a lightweight MLP—to predict whether a retrieval is actually necessary for a given context. If the adaptor predicts the NLM is sufficient, the expensive search is skipped.
  2. Datastore Pruning (Greedy Merging): Datastores are highly redundant. The authors developed Greedy Merging (GM), an algorithm that iterates through the datastore and merges a record into its nearest neighbor if they share the same target word and are close in vector space. This reduces the number of entries without the performance loss seen in simpler methods like random pruning.
  3. Dimension Reduction (PCA): Context vectors are typically high-dimensional (e.g., 1024-D). The authors applied Principal Component Analysis (PCA) to compress these vectors. This reduces the memory footprint and speeds up the distance calculations during the search.
The kNN-LM architecture with the proposed Adaptive Retrieval and Datastore Pruning components.

Experimental Setup

The researchers evaluated their methods on two primary benchmarks:

  • WikiText-103: A standard large-scale language modeling dataset (103M training tokens).
  • Law-MT: A domain-adaptation setting where a model trained on general news text is tested on legal documents. This highlights the $k$NN-LM's ability to "learn" new domains just by swapping the datastore.

Key Insights and Results

  • Retrieval is often overkill: On WikiText-103, the $k$NN retrieval only improved the base model's prediction 39% of the time. This suggests that over 60% of retrieval operations are essentially wasted computation.
  • Local Merging > Global Clustering: Standard $k$-means clustering performs poorly because it forces points into centroids that might be far away. Greedy Merging works better because it only merges points that are already very close "neighbors," preserving the local structure of the vector space.
  • Speed vs. Performance: On WikiText-103, the combined approach achieved a 6.6x speedup (increasing from 277 to 1835 tokens/sec) while maintaining the same perplexity as the vanilla $k$NN-LM. In the Law-MT setting, the methods achieved a 5.4x speedup and actually improved the perplexity (from 12.64 down to 12.29).
Performance trade-offs (perplexity vs. speed) for individual and combined efficiency methods.

Conclusion and Future Work

By combining Greedy Merging, PCA, and Adaptive Retrieval, the authors significantly reduced the storage and computational requirements for $k$NN-LMs. While the adaptive retrieval method requires a small amount of training on a held-out dataset, it adds minimal overhead. Future work could explore co-designing the indexing strategy with these pruning and dimension reduction techniques to unlock further gains.

Summary updated

BibTeX
@inproceedings{he2021efficient,
  author = {Junxian He and Graham Neubig and Taylor Berg-Kirkpatrick},
  title = {{Efficient Nearest Neighbor Language Models}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2021},
  doi = {10.18653/v1/2021.emnlp-main.461},
}
Figure