How njunlp/knn-box works
Main components
• kNN Modules: The project splits kNN logic into three main modules:
– Datastore: Responsible for storing translation knowledge in key-value pairs (e.g., see the “knnbox/datastore” folder).
– Retriever: Implements algorithms for fetching nearest neighbors from the datastore (refer to “knnbox/retriever”).
– Combiner: Integrates retrieval results with the base NMT prediction (see “knnbox/combiner”).
• Configuration: A large number of configuration files under the “config” directory define parameters for models, optimizers, scheduling, tasks, and more.
• Examples and Scripts: The “examples” folder contains multiple subdirectories demonstrating various experimental setups. The “knnbox-scripts” folder provides bash scripts for running datastore construction, inference, and other utilities.
• Visualization Component: As documented in the README, there is a visualization service (found in “knnbox-scripts/vanilla-knn-mt-visual”) to display the translation process interactively.
• Testing/Documentation: The “tests” and “docs” directories facilitate automated testing and technical documentation, respectively.
Architectural Patterns and Principles
• Modular Design: Emphasize the separation into discrete modules (datastore, retriever, combiner), which lets researchers swap in new algorithms or modify behaviors.
• Integration with Fairseq: Mark the link between the standard fairseq modules (languages, tasks, models) and the kNN augmentation logic.
• Pipeline Nature: Highlight the two-step process described in the README (first building the datastore, then invoking inference with retrieval), with clear directional data flow.
• Use of “config” Driven Design: Show that many components are parameterized through YAML configuration files, meaning that behavior can be altered without changing the core code.