# facebookresearch/mae architecture

> Architecture diagram of the GitHub repository facebookresearch/mae, 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/facebookresearch/mae
- Repository: https://github.com/facebookresearch/mae

## Overview

##### Project Type and Purpose
- This project is a machine learning research implementation of a Masked Autoencoder in PyTorch. It focuses on both the pre-training and fine-tuning pipelines for vision transformers (ViT) and autoencoder models.
- The repository includes multiple execution paths: pre-training, fine-tuning, and even a linear probing evaluation (linprobe), as well as an interactive visualization demo in a Colab notebook.

##### File Structure
- Main script files (main_finetune.py, main_pretrain.py, main_linprobe.py) are entry points for the different training phases.
- Engine scripts (engine_finetune.py, engine_pretrain.py) encapsulate the core training logic – they probably handle model initialization, forward passes, loss computation, and optimization.
- Model definition files (models_mae.py and models_vit.py) define the architecture of the autoencoder and vision transformers.
- The util directory (with files like datasets.py, crop.py, lr_decay.py, lr_sched.py, misc.py, pos_embed.py, etc.) holds utility functions for data processing, learning rate management, and other helper tasks.
- The submitit_* files (submitit_finetune.py, submitit_pretrain.py, etc.) indicate integration with a job scheduling system (Submitit) for distributed or scheduled training.
- The demo folder contains a Jupyter notebook (mae_visualize.ipynb) for interactive visualization of model behavior.

##### Extracting Insights from the README
- The README confirms that the project is a re-implementation of “Masked Autoencoders Are Scalable Vision Learners” using PyTorch+GPU.
- It highlights two main functionality areas: pre-training code and fine-tuning code (with available pre-trained checkpoints).
- There’s a separate section for the visualization demo, suggesting an emphasis on both model performance and interpretability.
- The README also briefly mentions dependencies (e.g., PyTorch, timm library) that are crucial in the system’s stack.

##### Main Components and Their Relationships
a. Input / Data Pipelines:
- Data ingestion and augmentation are handled in util/datasets.py and util/crop.py.
- Additional utility components (lr_decay, lr_sched, misc, pos_embed) support training routines.
b. Core Processing Modules:
- Pre-training Module:
- Handled by main_pretrain.py and engine_pretrain.py, which leverage models defined in models_mae.py and models_vit.py.
- Submitit pretrain script (submitit_pretrain.py) is used for distributed scheduling of training jobs.
- Fine-tuning / Linear Probing Module:
- Commences from main_finetune.py and main_linprobe.py, with engine_finetune.py and submitit_finetune.py/submitit_linprobe.py managing training and evaluation.
- Visualization Module:
- The demo (e.g., mae_visualize.ipynb) interacts with the trained models to demonstrate model masking and reconstruction visually.
c. Dependencies and Scheduling:
- Dependency on external libraries such as PyTorch and timm (with adjustments made for compatibility with PyTorch 1.8.1+).
- Use of Submitit for managing training jobs shows an architecture that can work in distributed environments.

##### Recommendations
- Data Input & Preprocessing (util modules)
- Model Definition (models_mae.py and models_vit.py)
- Job Scheduling (submitit_* components)
- Visualization/Demo (demo notebook)
- From data pipelines to training engines.
- From model definitions into the engines.
- From training engines to evaluation/visualization.
- Inter-module interactions by grouping related files (for instance, pair main_pretrain.py with engine_pretrain.py, and similar for the finetuning branch).
- Role of external libraries (PyTorch, timm) and provide notes on GPU usage.
- Details on configuration: how training parameters, learning rate schedulers (from lr_sched.py, lr_decay.py), and utilities are integrated.

- Under “Training Modules”, create subcomponents for both local execution (main_*.py and engine_*.py) and distributed execution (submitit_* scripts).
- Data flows from “Data Preprocessing” (util) into “Model Architecture” (models files).
- The output of “Model Architecture” is used in both pre-training and fine-tuning modules.
- Feedback loops or evaluation results flowing into the “Visualization” module.
- Use clear, descriptive labels for all connections, such as “data augmentation”, “optimization”, “checkpoint saving”, and “inference/visualization”.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% Data Preprocessing & Utilities
    subgraph "Data Preprocessing & Utilities"
        ds("Datasets"):::data
        cp("Crop"):::data
        lrD("LR_Decay"):::data
        lrS("LR_Scheduler"):::data
        misc("Miscellaneous"):::data
        pos("Pos_Embed"):::data
        lars("LARS"):::data
    end

    %% Model Architecture
    subgraph "Model Architecture"
        mae("Masked Autoencoder"):::model
        vit("Vision Transformer"):::model
    end

    %% Training Modules
    subgraph "Training Modules"
        %% Pre-Training Module
        subgraph "Pre-Training Module"
            mp("Pre-Training Entry"):::train
            ep("Pre-Training Engine"):::train
            sp("Submitit Pretrain"):::train
        end
        %% Fine-Tuning Module
        subgraph "Fine-Tuning Module"
            mf("Fine-Tuning Entry"):::train
            ef("Fine-Tuning Engine"):::train
            sf("Submitit FineTune"):::train
        end
        %% Linear Probing Module
        subgraph "Linear Probing Module"
            ml("Linear Probing Entry"):::train
            sl("Submitit Linprobe"):::train
        end
    end

    %% Visualization Module
    subgraph "Visualization Module"
        viz("Interactive Demo"):::viz
    end

    %% Data Processing Flow
    ds -->|"augmentation"| cp
    cp -->|"preprocessing"| lrD
    lrD -->|"modifies"| lrS
    lrS -->|"supports"| misc
    misc -->|"aids"| pos
    pos -->|"optimizes"| lars

    %% Model receives data features
    lars -->|"provides features"| mae
    lars -->|"provides features"| vit

    %% Models feed into training modules
    mae -->|"model weights"| mp
    vit -->|"model weights"| mp

    mae -->|"model weights"| mf
    vit -->|"model weights"| mf

    mae -->|"model weights"| ml
    vit -->|"model weights"| ml

    %% Pre-Training Module flow
    mp -->|"initiates"| ep
    ep -->|"queues job"| sp

    %% Fine-Tuning Module flow
    mf -->|"initiates"| ef
    ef -->|"queues job"| sf

    %% Linear Probing Module flow
    ml -->|"queues evaluation"| sl

    %% Training results flow to visualization
    ep -->|"outputs"| viz
    ef -->|"outputs"| viz
    sl -->|"outputs"| viz

    %% Click Events for Pre-Training Module
    click mp "https://github.com/facebookresearch/mae/blob/main/main_pretrain.py"
    click ep "https://github.com/facebookresearch/mae/blob/main/engine_pretrain.py"
    click sp "https://github.com/facebookresearch/mae/blob/main/submitit_pretrain.py"
    click mae "https://github.com/facebookresearch/mae/blob/main/models_mae.py"
    click vit "https://github.com/facebookresearch/mae/blob/main/models_vit.py"

    %% Click Events for Fine-Tuning Module
    click mf "https://github.com/facebookresearch/mae/blob/main/main_finetune.py"
    click ef "https://github.com/facebookresearch/mae/blob/main/engine_finetune.py"
    click sf "https://github.com/facebookresearch/mae/blob/main/submitit_finetune.py"

    %% Click Events for Linear Probing Module
    click ml "https://github.com/facebookresearch/mae/blob/main/main_linprobe.py"
    click sl "https://github.com/facebookresearch/mae/blob/main/submitit_linprobe.py"

    %% Click Event for Visualization Module
    click viz "https://github.com/facebookresearch/mae/blob/main/demo/mae_visualize.ipynb"

    %% Click Events for Data Pipelines and Utilities
    click ds "https://github.com/facebookresearch/mae/blob/main/util/datasets.py"
    click cp "https://github.com/facebookresearch/mae/blob/main/util/crop.py"
    click lrD "https://github.com/facebookresearch/mae/blob/main/util/lr_decay.py"
    click lrS "https://github.com/facebookresearch/mae/blob/main/util/lr_sched.py"
    click misc "https://github.com/facebookresearch/mae/blob/main/util/misc.py"
    click pos "https://github.com/facebookresearch/mae/blob/main/util/pos_embed.py"
    click lars "https://github.com/facebookresearch/mae/blob/main/util/lars.py"

    %% Styles
    classDef data fill:#cce5ff,stroke:#0056b3,stroke-width:2px;
    classDef model fill:#d4edda,stroke:#28a745,stroke-width:2px;
    classDef train fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef viz fill:#f8d7da,stroke:#721c24,stroke-width:2px;
```
