# njunlp/knn-box architecture

> Architecture diagram of the GitHub repository njunlp/knn-box, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/njunlp/knn-box
- Repository: https://github.com/njunlp/knn-box

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Main Pipeline
    A["Input Data"]:::nmt
    B["Data Preprocessing"]:::nmt
    N["NMT Model (fairseq)"]:::nmt
    D["Datastore Module"]:::knn
    R["Retriever Module"]:::knn
    C["Combiner Module"]:::knn
    O["Final Translation Output"]:::nmt

    A --> B
    B --> N
    B --> D
    D --> R
    N --> C
    R --> C
    C --> O

    %% Configuration influences key components
    config["Configuration Management"]:::config
    config --> B
    config --> N
    config --> D
    config --> R
    config --> C

    %% External Dependencies
    subgraph "External Dependencies"
        exDep1["faiss"]:::ext
        exDep2["PyTorch"]:::ext
    end
    N --> exDep2
    D --> exDep1

    %% Visualization
    vis["Visualization Component"]:::knn
    C -->|"monitors"| vis

    %% Experimental Setups
    subgraph "Experimental Setups"
        E1["Examples"]:::exp
        E2["Scripts"]:::exp
    end

    %% Testing and Documentation
    subgraph "Testing & Documentation"
        T1["Testing"]:::test
        T2["Documentation"]:::test
    end

    %% Click Events
    click N "https://github.com/njunlp/knn-box/tree/master/fairseq/"
    click D "https://github.com/njunlp/knn-box/tree/master/knnbox/datastore/"
    click R "https://github.com/njunlp/knn-box/tree/master/knnbox/retriever/"
    click C "https://github.com/njunlp/knn-box/tree/master/knnbox/combiner/"
    click config "https://github.com/njunlp/knn-box/tree/master/config/"
    click E1 "https://github.com/njunlp/knn-box/tree/master/examples/"
    click E2 "https://github.com/njunlp/knn-box/tree/master/knnbox-scripts/"
    click vis "https://github.com/njunlp/knn-box/tree/master/knnbox-scripts/vanilla-knn-mt-visual/"
    click T1 "https://github.com/njunlp/knn-box/tree/master/tests/"
    click T2 "https://github.com/njunlp/knn-box/tree/master/docs/"

    %% Styles
    classDef nmt fill:#add8e6,stroke:#000,stroke-width:2px;
    classDef knn fill:#90ee90,stroke:#000,stroke-width:2px;
    classDef config fill:#ffdead,stroke:#000,stroke-width:2px;
    classDef exp fill:#d8bfd8,stroke:#000,stroke-width:2px;
    classDef test fill:#d3d3d3,stroke:#000,stroke-width:2px;
    classDef ext fill:#f08080,stroke:#000,stroke-width:2px;
```
