# unbabel/comet architecture

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

- Interactive diagram: https://gitdiagram.com/unbabel/comet
- Repository: https://github.com/unbabel/comet

## Overview

##### Main components and layers
- CLI Interface: This is exposed via commands like comet-score, comet-compare, comet-mbr, and comet-train. The CLI layer handles user input (text files, model selection, flags) and forwards requests to the underlying Python API.
- Core Library (Package Structure): The core functionality is encapsulated in the “comet” Python package. Within this package:
- CLI Submodule (comet/cli): Contains scripts to trigger scoring, comparison, training, and MBR decoding.
- Encoders (comet/encoders): Implements various transformer-based encoders (BERT, MiniLM, ReMBERT, XLM-R, etc.) for preparing textual representations.
- Models (comet/models): Houses the evaluation models and metrics (including ranking, regression, and multitask approaches). Also contains utility functions (download, caching, pooling, metrics calculation) and components for error detection and explanation.
- Modules (comet/modules): Provides additional building blocks such as feedforward layers and layerwise attention modules that support the evaluation/pipeline.
- Configuration Files: YAML files in the “configs” directory (e.g., models, trainer, early stopping) guide training/hyperparameter selection and inference settings.
- Testing and Documentation: The “tests” directory (unit and integration tests) ensures robustness, and the “docs” directory provides installation, usage, and internal architectural details.
- External Dependencies & Integration: The toolkit integrates with external services such as Hugging Face Hub for model downloads, sacreBLEU for test sets, and xTower for natural language explanations.

##### Relationships and data flow
- The CLI tools serve as the entry point for users. They call functions within the comet library, which in turn invoke specific encoders and models.
- The encoders convert raw text into embeddings/data formats required for evaluation.
- The evaluation models (which have sub-divisions for regression, ranking, and multitask evaluation) then process these representations to produce scores, error spans, and overall system-level assessments.
- Configurations are loaded from YAML files to customize model training and scoring behavior.
- Test pipelines (via tests) validate the functionality while the docs offer user guidance and examples.
- External services (e.g., Hugging Face Hub) are linked to the model download and license-checking processes.

##### Architectural Patterns and Design Principles
- Modular Design: Each functional unit (CLI, encoders, models, modules) is isolated and can be maintained independently.
- Separation of Concerns: The responsibilities are clearly divided between user interfaces, business logic (model inference and training), configuration management, and error analysis.
- Extensibility: The structure allows for the integration of additional transformer models and evaluation techniques as new submodules or encoder implementations without disrupting existing functionality.
- Reusability: Common utility functions (like caching, download utilities, and pooling) are centralized, reducing code duplication.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Top-level User Interface
    U["User"]:::external

    subgraph "CLI Interface"
        CLI1["Comet Score (score.py)"]:::cli
        CLI2["Comet Compare (compare.py)"]:::cli
        CLI3["Comet MBR (mbr.py)"]:::cli
        CLI4["Comet Train (train.py)"]:::cli
    end

    %% Controller / API Layer
    CTRL["comet.cli Controller"]:::core

    %% Core Processing Layer
    subgraph "Core Processing"
        %% Encoders Subgraph
        subgraph "Encoders"
            ENC1["BERT Encoder"]:::encoder
            ENC2["MiniLM Encoder"]:::encoder
            ENC3["ReMBERT Encoder"]:::encoder
            ENC4["XLM-R Encoder"]:::encoder
            ENC5["XLM-R XL Encoder"]:::encoder
        end

        %% Evaluation Models Subgraph
        subgraph "Evaluation Models"
            MOD1["Regression Evaluation"]:::model
            MOD2["Referenceless Evaluation"]:::model
            MOD3["Ranking Evaluation"]:::model
            MOD4["Multitask Evaluation"]:::model
            MOD5["Model Utilities"]:::model
        end

        %% Modules Subgraph (Additional Building Blocks)
        subgraph "Modules"
            M1["Feedforward Module"]:::module
            M2["Attention Module"]:::module
        end
    end

    %% Configuration, Testing and Documentation
    CF["Configuration Files (YAML)"]:::config
    EXT["External Dependencies\n(HuggingFace, sacreBLEU, xTower)"]:::external
    TEST["Testing Suite"]:::test
    DOCS["Documentation"]:::docs

    %% Data Flow
    U -->|"initiates"| CLI1
    U -->|"initiates"| CLI2
    U -->|"initiates"| CLI3
    U -->|"initiates"| CLI4

    CLI1 -->|"calls"| CTRL
    CLI2 -->|"calls"| CTRL
    CLI3 -->|"calls"| CTRL
    CLI4 -->|"calls"| CTRL

    CTRL -->|"processes"| ENC1
    CTRL -->|"processes"| ENC2
    CTRL -->|"processes"| ENC3
    CTRL -->|"processes"| ENC4
    CTRL -->|"processes"| ENC5

    ENC1 -->|"encodes"| MOD1
    ENC1 -->|"encodes"| MOD2
    ENC2 -->|"encodes"| MOD1
    ENC2 -->|"encodes"| MOD2
    ENC3 -->|"encodes"| MOD3
    ENC4 -->|"encodes"| MOD4
    ENC5 -->|"encodes"| MOD4

    MOD1 -->|"evaluates"| M1
    MOD2 -->|"evaluates"| M1
    MOD3 -->|"evaluates"| M2
    MOD4 -->|"evaluates"| M2
    MOD5 ---|"supports"| MOD1

    CF -->|"configures"| CTRL
    CF -->|"configures"| MOD5

    EXT -->|"provides"| MOD5

    CTRL ---|"validated by"| TEST
    DOCS ---|"guides"| U

    %% Click Events for CLI Interface
    click CLI1 "https://github.com/unbabel/comet/blob/master/comet/cli/score.py"
    click CLI2 "https://github.com/unbabel/comet/blob/master/comet/cli/compare.py"
    click CLI3 "https://github.com/unbabel/comet/blob/master/comet/cli/mbr.py"
    click CLI4 "https://github.com/unbabel/comet/blob/master/comet/cli/train.py"

    %% Click Events for Encoders
    click ENC1 "https://github.com/unbabel/comet/blob/master/comet/encoders/bert.py"
    click ENC2 "https://github.com/unbabel/comet/blob/master/comet/encoders/minilm.py"
    click ENC3 "https://github.com/unbabel/comet/blob/master/comet/encoders/rembert.py"
    click ENC4 "https://github.com/unbabel/comet/blob/master/comet/encoders/xlmr.py"
    click ENC5 "https://github.com/unbabel/comet/blob/master/comet/encoders/xlmr_xl.py"
    
    %% Click Events for Evaluation Models - Regression & Referenceless
    click MOD1 "https://github.com/unbabel/comet/blob/master/comet/models/regression/regression_metric.py"
    click MOD2 "https://github.com/unbabel/comet/blob/master/comet/models/regression/referenceless.py"
    %% Click Event for Ranking Evaluation
    click MOD3 "https://github.com/unbabel/comet/blob/master/comet/models/ranking/ranking_metric.py"
    %% Click Events for Multitask Evaluation
    click MOD4 "https://github.com/unbabel/comet/blob/master/comet/models/multitask/unified_metric.py"
    %% Click Events for Additional Model Utilities (use representative file)
    click MOD5 "https://github.com/unbabel/comet/blob/master/comet/models/base.py"
    
    %% Click Events for Modules
    click M1 "https://github.com/unbabel/comet/blob/master/comet/modules/feedforward.py"
    click M2 "https://github.com/unbabel/comet/blob/master/comet/modules/layerwise_attention.py"
    
    %% Click Events for Configuration, Testing and Documentation
    click CF "https://github.com/unbabel/comet/tree/master/configs"
    click TEST "https://github.com/unbabel/comet/tree/master/tests"
    click DOCS "https://github.com/unbabel/comet/tree/master/docs"

    %% Styles & Classes
    classDef cli fill:#FFCC00,stroke:#000,stroke-width:2px;
    classDef core fill:#CCCCFF,stroke:#000,stroke-width:2px;
    classDef encoder fill:#C0E0FF,stroke:#000,stroke-width:2px;
    classDef model fill:#C0FFC0,stroke:#000,stroke-width:2px;
    classDef module fill:#FFE0B2,stroke:#000,stroke-width:2px;
    classDef config fill:#FFD1DC,stroke:#000,stroke-width:2px;
    classDef external fill:#E0E0E0,stroke:#000,stroke-width:2px;
    classDef test fill:#F5A9A9,stroke:#000,stroke-width:2px;
    classDef docs fill:#D2B4DE,stroke:#000,stroke-width:2px;
```
