Efficient k-Nearest-Neighbor Machine Translation with Dynamic Retrieval

Yan Gao, Zhiwei Cao, Zhongjian Miao, Baosong Yang, Shiyu Liu, Min Zhang, Jinsong Su

Abstract

$k$-Nearest-Neighbor Machine Translation ($k$NN-MT) significantly enhances translation quality by retrieving domain-specific examples from an external datastore during decoding. However, performing retrieval at every step introduces a massive computational bottleneck. We present $k$NN-MT-DR (Dynamic Retrieval), which utilizes a lightweight MLP classifier and a timestep-aware threshold to dynamically skip unnecessary retrievals. Our approach maintains the high translation quality of vanilla $k$NN-MT while substantially accelerating decoding speed, particularly on large-scale datastores.

Introduction

$k$NN-MT is a powerful technique for domain adaptation in Neural Machine Translation (NMT) without the need for retraining. By querying a massive external datastore of token pairs, the model can leverage specific context to improve accuracy. The primary drawback is latency: querying millions of entries at every generation step is computationally expensive.

Previous solutions, such as $k$NN-MT-AR, attempted to mitigate this by using the model's interpolation coefficient ($\lambda$) as a proxy for skipping lookups. However, this paper argues that $\lambda$ is mathematically ill-suited for binary skipping decisions. Furthermore, existing methods often use static thresholds, failing to account for the fact that a model's reliance on external retrieval naturally diminishes as it builds internal context throughout a sentence.

Methods: Dynamic Retrieval

To address the latency issue without sacrificing quality, $k$NN-MT-DR introduces two core innovations:

1. Dedicated Skipping Classifier

Instead of relying on $\lambda$, we train a lightweight, 2-layer MLP binary classifier to decide whether to skip retrieval at each timestep. To ensure minimal overhead, the classifier uses three scalar features:

  • Top-1 NMT probability: Reflects the base model's confidence.
  • L2 norm of the decoder representation: Acts as a proxy for translation difficulty.
  • Max cross-attention weight: Indicates certainty regarding source-token alignment.

2. Timestep-Aware Dynamic Threshold

We observed that the benefit of $k$NN retrieval is highest for the initial tokens of a sentence and decays as the decoding progresses.

Impact of kNN retrieval on translation quality (ΔBLEU) across different decoding timestep intervals.

To exploit this, we define a dynamic threshold that increases over time, allowing the model to skip retrievals more aggressively as the sentence length grows. The classifier is trained using Focal Loss to handle class imbalance and utilizes "smart labels" to avoid redundant lookups when the base NMT model is already correct.

Experimental Results

We evaluated $k$NN-MT-DR on standard multi-domain benchmarks, including German-English (covering Koran, IT, Medical, Law, and Subtitles) and Chinese-English datasets. Datastore sizes varied from 0.5M to 180.7M pairs.

Translation Quality

$k$NN-MT-DR achieves an average BLEU score of 41.83 across domains, representing a negligible 0.74 point drop from Vanilla $k$NN-MT while significantly outperforming previous adaptive methods like $k$NN-MT-AR.

Comparison of translation quality (BLEU) and retrieval efficiency across different models and domains.

Decoding Speed

Our method delivers massive speedups, especially on large datastores. On the Subtitles domain (180.7M pairs), the model achieves a decoding speed of 1002 tokens/sec, compared to 478 tokens/sec for the Vanilla model—a greater than 2x improvement at a batch size of 128. Additionally, $k$NN-MT-DR is highly compatible with existing datastore compression techniques, allowing for compounding efficiency gains.

Conclusion

$k$NN-MT-DR effectively resolves the efficiency-quality trade-off in $k$NN-MT. By treating retrieval skipping as a dedicated classification task and leveraging the temporal decay of retrieval utility, we achieve significant acceleration with minimal impact on translation accuracy. Future work will explore incorporating source-side information and extending the dynamic retrieval framework to multilingual scenarios and language modeling.

Summary updated

BibTeX
@inproceedings{gao2024efficient,
  author = {Yan Gao and Zhiwei Cao and Zhongjian Miao and Baosong Yang and Shiyu Liu and Min Zhang and Jinsong Su},
  title = {{Efficient k-Nearest-Neighbor Machine Translation with Dynamic Retrieval}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2024},
  doi = {10.18653/v1/2024.findings-acl.475},
}
Figure