Open Interpretability Models Contributors Research Posts About

Generalization through Memorization: Nearest Neighbor Language Models

Source

Citation. Urvashi Khandelwal (Stanford University, work done while interning at Facebook AI Research), Omer Levy (FAIR), Dan Jurafsky (Stanford), Luke Zettlemoyer (FAIR) and Mike Lewis (FAIR). "Generalization through Memorization: Nearest Neighbor Language Models". arXiv:1911.00172, version 2 of 15 February 2020. Version 1 is of 1 November 2019. The arXiv comment says "ICLR 2020". We didn't check the proceedings. The code is at github.com/urvashik/knnlm. The text gives no funding statement and no disclosure.

Mismatch. Section 2 says the distance is squared L2 throughout. Section 3 says the runs other than Wikitext-103 use plain L2 on quantised keys. Table 4's caption claims a gain of "23 points", but the datastore's own gain in that table is 14.37. The 23 points is the gap to a model trained in the domain. Several figure and table numbers in the text point to the wrong figure.

Status. Planned. #5220 names it as the reference for arm H, a written side table beside a frozen model. The fix campaign is the maintainer's, so this page reports evidence and records questions only.

Read. On 2026-11-20 a web-fetch subagent read the abstract page and the HTML of version 2 for this page's author, a model (Claude Opus 5.5). It read the source's own text in full, in two fetches: Sections 1 to 8, Tables 1 to 9, Appendix A and the references. We didn't see any figure as an image. So the curves of λ against datastore size and of perplexity against k appear here only as the text describes them. We didn't read version 1.

Summary

Claim. Mix a frozen language model's next-token distribution with one from a nearest-neighbour search over its own hidden states. The model then improves. Retrieval over 3 billion tokens beats training on them.

Method.

Findings, as the source reports them.

Setting Result
Wikitext-103, test perplexity (Table 1, median of 3 seeds) base model 18.65, kNN-LM 16.12, with a continuous cache 15.79
Books, test (Table 2) base 11.89, kNN-LM 10.89
Wiki-100M model, Wiki-3B datastore, test (Table 3) 13.73, against 15.17 for a model trained on Wiki-3B
Wiki-3B model on Books, test (Table 4) 34.84 alone, 20.47 with a Books datastore, 11.89 for a model trained on Books
Key choice, Wikitext-103 dev (Table 5) last FFN input after layer norm 16.06. Before layer norm 17.06. Attention input after layer norm 16.76. Model output 17.07. No datastore 17.96
Cost (Section 3) about 2 hours on one CPU for the Wikitext-103 store. About 25 minutes for the validation set with 1,024 neighbours
Where it helps (Section 6) "examples where kNN-LM is most helpful typically contain rare patterns": names, facts and near-copies of training text

Where the claims go beyond the evidence

The authors name future work themselves: learning the similarity function, and making the datastore smaller.

Use in this repo