# dequanwang/tent architecture

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

- Interactive diagram: https://gitdiagram.com/dequanwang/tent
- Repository: https://github.com/dequanwang/tent

## Overview

##### Main components of the system
- Experiment Runner: The central script (cifar10c.py) that manages the experiment process by reading command-line configurations.
- Configuration Management: The configuration files within the “cfgs” directory (source.yaml, norm.yaml, tent.yaml) along with conf.py, which likely handle experiment parameters and settings using tools like yacs.
- Adaptation Modules: Three key modules are implemented—tent.py for the entropy minimization-based adaptation, norm.py for test-time normalization updates, and the baseline/source implementation directly in cifar10c.py.
- Underlying Framework: PyTorch is the main dependency, and the design is built around leveraging pre-trained models and datasets (e.g., CIFAR-10-C via RobustBench).
- External Dependencies: The project depends on several libraries (e.g., PyTorch, RobustBench, yacs) and optionally interacts with tools for experiment tracking (as referenced by the wandb report).

##### Establish the relationships and interactions
- Data Flow: Inputs (images from CIFAR-10-C, possibly adversarial examples) enter through the experiment runner, which loads and configures a PyTorch model.
- Method Selection: Based on the provided configuration file (source, norm, or tent), the system dynamically selects the adaptation method.
- Adaptation Process: The chosen module (tent.py for entropy minimization adaptation or norm.py for normalization updates) processes the input data in an online fashion—making one update per batch to adapt the model.
- Output: The final predictions are produced while also updating model parameters online. Results are compared using performance metrics (error percentages across various corruption types).

##### Architectural patterns and design principles
- Modularity: Different adaptation strategies (source baseline, normalization, and Tent) are isolated into separate modules/files, which encourages extensibility and clear separation of concerns.
- Configuration-Driven Behavior: With multiple YAML config files, the experiments become highly configurable and demonstrate an inversion of control design—parameters for the experiments are externalized.
- Pipeline Organization: The design emphasizes an experimental pipeline—from data input, through model configuration and adaptation, to final output and evaluation.
- Dependency Management: The system relies on standard Python tools and libraries, ensuring that the adaptation process is reproducible and extensible.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Input Layer
    Data["CIFAR-10-C Data"]:::data

    %% Experiment Runner
    Runner["Experiment Runner"]:::core

    %% Configuration Management Subgraph
    subgraph "Configuration Management"
        Config["Config Script (conf.py)"]:::config
        SourceYAML["source.yaml"]:::config
        NormYAML["norm.yaml"]:::config
        TentYAML["tent.yaml"]:::config
    end

    %% Adaptation Modules Subgraph
    subgraph "Adaptation Modules"
        Baseline["Baseline/Source Adaptation"]:::adaptation
        NormAdapt["Test-Time Normalization"]:::adaptation
        TentAdapt["Tent Adaptation"]:::adaptation
    end

    %% PyTorch Model and Output
    Model["PyTorch Model"]:::model
    Output["Output/Prediction & Metrics"]:::core

    %% External Dependencies Subgraph
    subgraph "External Dependencies"
        PT["PyTorch"]:::external
        RB["RobustBench"]:::external
        Yacs["yacs"]:::external
        Wandb["Weights&Biases"]:::external
    end

    %% Data Flow Arrows
    Data -->|"Input"| Runner
    Runner -->|"LoadConfig"| Config
    Runner -->|"SelectMethod"| Baseline
    Runner -->|"SelectMethod"| NormAdapt
    Runner -->|"SelectMethod"| TentAdapt
    Config -->|"ParamsFrom"| SourceYAML
    Config -->|"ParamsFrom"| NormYAML
    Config -->|"ParamsFrom"| TentYAML
    Baseline -->|"UpdateModel"| Model
    NormAdapt -->|"PerBatchUpdate"| Model
    TentAdapt -->|"PerBatchUpdate"| Model
    Model -->|"PredictAndEval"| Output

    %% External Dependencies Connections
    PT --> Model
    RB --> Model
    Yacs --> Config
    Wandb --> Runner

    %% Styles
    classDef core fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef config fill:#ABEBC6,stroke:#1E8449,stroke-width:2px;
    classDef adaptation fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef external fill:#FAD7A0,stroke:#D35400,stroke-width:2px;
    classDef model fill:#D7BDE2,stroke:#6C3483,stroke-width:2px;
    classDef data fill:#F5CBA7,stroke:#CA6F1E,stroke-width:2px;

    %% Click Events
    click Runner "https://github.com/dequanwang/tent/blob/master/cifar10c.py"
    click Config "https://github.com/dequanwang/tent/blob/master/conf.py"
    click SourceYAML "https://github.com/dequanwang/tent/blob/master/cfgs/source.yaml"
    click NormYAML "https://github.com/dequanwang/tent/blob/master/cfgs/norm.yaml"
    click TentYAML "https://github.com/dequanwang/tent/blob/master/cfgs/tent.yaml"
    click NormAdapt "https://github.com/dequanwang/tent/blob/master/norm.py"
    click TentAdapt "https://github.com/dequanwang/tent/blob/master/tent.py"
    click Baseline "https://github.com/dequanwang/tent/blob/master/cifar10c.py"
```
