{"about":{"site":"https://codewithpapers.app","non_affiliation":"Code with Papers and Syntology are not affiliated with, endorsed by, or sponsored by Papers with Code, Meta, or the pwc-archive mirror.","licence":"CC BY-SA 4.0","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","attribution":"https://codewithpapers.app/attribution","modified":"archive material modified by Syntology; see the attribution page"},"url":"/paper/nearest-neighbor-machine-translation","title":"Nearest Neighbor Machine Translation","arxiv_id":"2010.00710","date":"2020-10-01","proceeding":"ICLR 2021 1","authors":["Urvashi Khandelwal","Angela Fan","Dan Jurafsky","Luke Zettlemoyer","Mike Lewis"],"abstract":"We introduce $k$-nearest-neighbor machine translation ($k$NN-MT), which predicts tokens with a nearest neighbor classifier over a large datastore of cached examples, using representations from a neural translation model for similarity search. This approach requires no additional training and scales to give the decoder direct access to billions of examples at test time, resulting in a highly expressive model that consistently improves performance across many settings. Simply adding nearest neighbor search improves a state-of-the-art German-English translation model by 1.5 BLEU. $k$NN-MT allows a single model to be adapted to diverse domains by using a domain-specific datastore, improving results by an average of 9.2 BLEU over zero-shot transfer, and achieving new state-of-the-art results -- without training on these domains. A massively multilingual model can also be specialized for particular language pairs, with improvements of 3 BLEU for translating from English into German and Chinese. Qualitatively, $k$NN-MT is easily interpretable; it combines source and target context to retrieve highly relevant examples.","url_abs":"https://arxiv.org/abs/2010.00710v2","url_pdf":"https://arxiv.org/pdf/2010.00710v2.pdf","source":{"archive":"pwc-archive (Hugging Face), CC BY-SA 4.0","snapshot":"2025-07-28","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","row_kind":"abstracts"},"code_links":[{"paper_slug":"nearest-neighbor-machine-translation","repo_url":"https://github.com/urvashik/knnlm","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":0,"framework":"pytorch","reach":null},{"paper_slug":"nearest-neighbor-machine-translation","repo_url":"https://github.com/hubreb/imitkd_ast","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"pytorch","reach":null},{"paper_slug":"nearest-neighbor-machine-translation","repo_url":"https://github.com/neulab/knn-transformers","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"pytorch","reach":null},{"paper_slug":"nearest-neighbor-machine-translation","repo_url":"https://github.com/urvashik/knnmt","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"pytorch","reach":null},{"paper_slug":"nearest-neighbor-machine-translation","repo_url":"https://github.com/cordercorder/knn-models","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":0,"framework":"pytorch","reach":{"status":"ok","spdx":"Apache-2.0"}}],"tasks":[{"task_slug":"decoder","task_name":"Decoder"},{"task_slug":"machine-translation","task_name":"Machine Translation"},{"task_slug":"translation","task_name":"Translation"}],"methods":[],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"atlas_url":"https://app.syntology.ai/?focus=2010.00710","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2010.00710"}},"developers":"https://syntology.ai/developers","read_at":"2026-09-24T18:15:14+00:00","read_at_is":"when the build read Syntology's graph, not when any sample ran","claim":"Per-sample execution status on synthesized fixtures; not a correctness claim about the paper. Samples come from repositories linked to the paper, official or community; repo_kind says which.","repos":[{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/hubreb/imitkd_ast","reach":null},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/urvashik/knnmt","reach":null},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/neulab/knn-transformers","reach":null},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/cordercorder/knn-models","reach":{"status":"ok","spdx":"Apache-2.0"}},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/urvashik/knnlm","reach":null}],"summary":{"ran":1,"ran_draft_wrong":3,"unverified":6},"by_repo_kind":{"official":{"samples":1,"ran":0,"repositories":1},"listed":{"samples":7,"ran":2,"repositories":3}},"repo_kind_vocabulary":{"official":"The archive marks this repository official for the paper","named_in_paper":"The archive records that the paper mentions this repository; it is not marked official","listed":"In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper","found_in_text":"Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted","community":"Not in the archive's code links for this paper; a community repository Syntology harvested"},"n_pointer_only_for_licence":4,"samples":[{"code_sha256_prefix":"41c932a1879b6eca","entry":"ActivationCapturer","repo":"neulab/knn-transformers","repo_kind":"listed","path":"knnlm.py","file_url":"https://github.com/neulab/knn-transformers/blob/HEAD/knnlm.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"41c932a1879b6eca"}},{"code_sha256_prefix":"05520f6527f04bde","entry":"get_dstore_path","repo":null,"repo_kind":null,"path":null,"file_url":null,"link_basis":"identical_code_first_harvested_elsewhere","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"OUTPUT_MISDECLARED","metamorphic_tier":"deterministic","behaviour_fingerprint":true,"licence":null,"inline_ok":false,"mcp_get_code":{"code_sha256":"05520f6527f04bde"}},{"code_sha256_prefix":"918cf1d7f24a77f5","entry":"get_index_path","repo":null,"repo_kind":null,"path":null,"file_url":null,"link_basis":"identical_code_first_harvested_elsewhere","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"OUTPUT_MISDECLARED","metamorphic_tier":"deterministic","behaviour_fingerprint":true,"licence":null,"inline_ok":false,"mcp_get_code":{"code_sha256":"918cf1d7f24a77f5"}},{"code_sha256_prefix":"88ce019380f10478","entry":"get_symbols_to_strip_from_output","repo":"urvashik/knnmt","repo_kind":"listed","path":"fairseq_cli/generate.py","file_url":"https://github.com/urvashik/knnmt/blob/HEAD/fairseq_cli/generate.py","link_basis":"first_harvest_node","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"OUTPUT_MISDECLARED","metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"88ce019380f10478"}},{"code_sha256_prefix":"4f270d89f02eecd7","entry":"DIST","repo":"neulab/knn-transformers","repo_kind":"listed","path":"knnlm.py","file_url":"https://github.com/neulab/knn-transformers/blob/HEAD/knnlm.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"4f270d89f02eecd7"}},{"code_sha256_prefix":"bf7d9cb24001654f","entry":"KEY_TYPE","repo":"neulab/knn-transformers","repo_kind":"listed","path":"knnlm.py","file_url":"https://github.com/neulab/knn-transformers/blob/HEAD/knnlm.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"bf7d9cb24001654f"}},{"code_sha256_prefix":"0e7bf9fbf9dd8617","entry":"KNNWrapper","repo":"neulab/knn-transformers","repo_kind":"listed","path":"knnlm.py","file_url":"https://github.com/neulab/knn-transformers/blob/HEAD/knnlm.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"0e7bf9fbf9dd8617"}},{"code_sha256_prefix":"22d23f04ff2d14de","entry":"KNN_Dstore","repo":"urvashik/knnlm","repo_kind":"official","path":"fairseq/knnlm.py","file_url":"https://github.com/urvashik/knnlm/blob/HEAD/fairseq/knnlm.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"22d23f04ff2d14de"}},{"code_sha256_prefix":"d1ecba343e9e4ddb","entry":"KNN_Dstore","repo":"urvashik/knnmt","repo_kind":"listed","path":"fairseq/knnlm.py","file_url":"https://github.com/urvashik/knnmt/blob/HEAD/fairseq/knnlm.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"NONE","inline_ok":false,"mcp_get_code":{"code_sha256":"d1ecba343e9e4ddb"}},{"code_sha256_prefix":"dc707b421046950e","entry":"KNN_Dstore","repo":"hubreb/imitkd_ast","repo_kind":"listed","path":"fairseq/fairseq/knnlm.py","file_url":"https://github.com/hubreb/imitkd_ast/blob/HEAD/fairseq/fairseq/knnlm.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"dc707b421046950e"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}