# kylesargent/flowmo architecture

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

- Interactive diagram: https://gitdiagram.com/kylesargent/flowmo
- Repository: https://github.com/kylesargent/flowmo

## Overview

##### Project Type and Purpose
- FlowMo is a research-focused Python library and CLI toolkit for training and evaluating Mode-Seeking Diffusion Autoencoders for image tokenization.
- It is not a web app or service-oriented application; rather, it’s a data-processing and model-training pipeline with modular components for data ingestion, configuration, training, evaluation, and example usage.

##### File-Structure Analysis
- Top-level files:
- LICENSE, README.md, requirements.txt, example.ipynb
- Core package directory: flowmo/
- flowmo/baselines/*.py (benchmark implementations)
- flowmo/configs/base.yaml (configuration managed via Hydra-style YAML)
- flowmo/data.py (data loader for ImageNet tarballs and indices)
- flowmo/train.py, flowmo/train_utils.py (main training entrypoint + utilities)
- flowmo/models.py (model definitions for diffusion autoencoder and tokenizers)
- flowmo/perceptual_loss.py; flowmo/inception.py; flowmo/lookup_free_quantize.py (loss and evaluation helper modules)
- flowmo/evaluate.py (evaluation entrypoint for rFID, continuous eval, subsampling)

##### Technical Stack (from README)
- Python 3.13.2, PyTorch 2.x with CUDA 12.4 via torchrun (distributed training).
- Dependencies: torchvision, LPIPS, Hydra-style config overrides, JSON index files for ImageNet.
- Usage scenarios:
- Data preparation via JSON indices + raw tar → no manual preprocessing
- Two-stage training: pretrain diffusion autoencoder → post-train via reward backprop
- Evaluation: continuous and one-shot rFID measurement
- Example notebook for image reconstruction via the learned tokenizer

##### Main Components & Interactions
a. Data Layer
- flowmo/data.py reads ImageNet tar(s) + JSON index → PyTorch Dataset
- Config from flowmo/configs/base.yaml controls paths, batch size, subsampling
b. Configuration
- YAML configs loaded at startup via Hydra style CLI overrides (torchrun –m flowmo.train)
c. Training Pipeline
- Entry: flowmo/train.py
- Utilities: flowmo/train_utils.py
- Model definitions: flowmo/models.py
- Losses: flowmo/perceptual_loss.py
- Outputs: checkpoint .pth files, TensorBoard logs
d. Post-Training & Sampling
- flowmo/train.py with flag model.posttrain_sample=true triggers gradient through sampling chain
e. Evaluation Pipeline
- Entry: flowmo/evaluate.py
- Helpers: flowmo/inception.py for inception score, flowmo/lookup_free_quantize.py for token quantization
- Outputs: rFID metrics, evaluation logs, optional subsampling
f. Baselines
- flowmo/baselines/baselines.py
- Specific baselines: llamagen.py, titok.py, titok_maskgit.py
g. Example Notebook
- example.ipynb demonstrates loading pre-trained tokens, reconstructing images

##### Architectural Patterns & Principles
- Modular Python package with clear separation of concerns (data / config / model / train / eval).
- CLI-driven processes using torchrun + Hydra-style configs for reproducible experiments.
- Two-stage training pipeline (pretrain / posttrain) illustrating a pipeline pattern.
- Utility modules and baselines decoupled from core training logic for research flexibility.

## Diagram (Mermaid)

```mermaid
graph TB
  %% Data Layer
  subgraph "Data Layer"
    direction TB
    IN_TAR[(ImagenetTrainTar)]:::data
    IN_IDX[(ImagenetTrainIndexJSON)]:::data
    DATA_py["flowmo/data.py"]:::data
    IN_TAR -->|provides images| DATA_py
    IN_IDX -->|provides indices| DATA_py
  end

  %% Configuration
  subgraph "Configuration"
    direction TB
    CFG[[flowmo/configs/base.yaml]]:::config
  end

  %% Training Pipeline
  subgraph "Training Pipeline"
    direction TB
    PRETRAIN["flowmo/train.py pretrain"]:::training
    POSTTRAIN["flowmo/train.py posttrain"]:::training
    MODEL((flowmo/models.py)):::training
    LOSS((flowmo/perceptual_loss.py)):::training
    TRAIN_UTILS["flowmo/train_utils.py"]:::training
    CKPT[(CheckpointFiles)]:::training

    CFG -->|configures| PRETRAIN
    CFG -->|configures| POSTTRAIN
    DATA_py -->|feeds data| PRETRAIN
    DATA_py -->|feeds data| POSTTRAIN
    MODEL -->|defines model| PRETRAIN
    MODEL -->|defines model| POSTTRAIN
    LOSS -->|computes loss| PRETRAIN
    LOSS -->|computes loss| POSTTRAIN
    TRAIN_UTILS -->|utilities| PRETRAIN
    TRAIN_UTILS -->|utilities| POSTTRAIN
    PRETRAIN -->|outputs| CKPT
    PRETRAIN -->|initializes| POSTTRAIN
  end

  %% Evaluation Pipeline
  subgraph "Evaluation Pipeline"
    direction TB
    EVAL["flowmo/evaluate.py"]:::eval
    INCEPTION((flowmo/inception.py)):::eval
    QUANT((flowmo/lookup_free_quantize.py)):::eval
    CFG -->|configures| EVAL
    CKPT -->|inputs checkpoint| EVAL
    DATA_py -->|inputs data| EVAL
    INCEPTION -->|score helper| EVAL
    QUANT -->|quantize helper| EVAL
  end

  %% Baselines
  subgraph "Baseline Implementations"
    direction TB
    BASE["flowmo/baselines/baselines.py"]:::baseline
    LLMAG["flowmo/baselines/llamagen.py"]:::baseline
    TITOK["flowmo/baselines/titok.py"]:::baseline
    MASKGIT["flowmo/baselines/titok_maskgit.py"]:::baseline
  end

  %% Example Notebook
  subgraph "Example Notebook"
    direction TB
    NB["example.ipynb"]:::notebook
    CKPT -->|uses checkpoint| NB
    MODEL -->|uses model| NB
  end

  %% Click Events
  click DATA_py "https://github.com/kylesargent/flowmo/blob/main/flowmo/data.py"
  click CFG "https://github.com/kylesargent/flowmo/blob/main/flowmo/configs/base.yaml"
  click PRETRAIN "https://github.com/kylesargent/flowmo/blob/main/flowmo/train.py"
  click POSTTRAIN "https://github.com/kylesargent/flowmo/blob/main/flowmo/train.py"
  click MODEL "https://github.com/kylesargent/flowmo/blob/main/flowmo/models.py"
  click LOSS "https://github.com/kylesargent/flowmo/blob/main/flowmo/perceptual_loss.py"
  click INCEPTION "https://github.com/kylesargent/flowmo/blob/main/flowmo/inception.py"
  click QUANT "https://github.com/kylesargent/flowmo/blob/main/flowmo/lookup_free_quantize.py"
  click EVAL "https://github.com/kylesargent/flowmo/blob/main/flowmo/evaluate.py"
  click BASE "https://github.com/kylesargent/flowmo/tree/main/flowmo/baselines/"
  click BASE "https://github.com/kylesargent/flowmo/blob/main/flowmo/baselines/baselines.py"
  click LLMAG "https://github.com/kylesargent/flowmo/blob/main/flowmo/baselines/llamagen.py"
  click TITOK "https://github.com/kylesargent/flowmo/blob/main/flowmo/baselines/titok.py"
  click MASKGIT "https://github.com/kylesargent/flowmo/blob/main/flowmo/baselines/titok_maskgit.py"
  click NB "https://github.com/kylesargent/flowmo/blob/main/example.ipynb"

  %% Styles
  classDef data fill:#cce5ff,stroke:#1f78b4
  classDef config fill:#d4edda,stroke:#155724
  classDef training fill:#d1ecf1,stroke:#0c5460
  classDef eval fill:#fff3cd,stroke:#856404
  classDef baseline fill:#f8d7da,stroke:#721c24
  classDef notebook fill:#e2e3e5,stroke:#383d41
```
