# lth14/fractalgen architecture

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

- Interactive diagram: https://gitdiagram.com/lth14/fractalgen
- Repository: https://github.com/lth14/fractalgen

## Overview

1. **What this repo is**
   - This is a PyTorch/GPU research codebase for **Fractal Generative Models**.
   - The architecture is centered on **pixel-by-pixel image generation** for ImageNet at **64×64 and 256×256**, with both training and evaluation workflows.

2. **Primary entrypoints**
   - `main_fractalgen.py` is the main **CLI orchestration script** for training, likelihood evaluation, and generation.
   - `demo/run_fractalgen.ipynb` is a self-contained **Colab demo** for running pretrained models interactively.
   - `util/download.py` handles downloading pretrained checkpoints and related assets.

3. **Core model boundary**
   - `models/fractalgen.py` is the main implementation of the **FractalGen model family**.
   - `models/ar.py` and `models/mar.py` split the architecture into two named variants:
     - **AR**: autoregressive-style variant
     - **MAR**: masked/autoregressive hybrid variant used in the higher-performing checkpoints
   - These model files are the main boundary between the training script and the generation logic.

4. **Loss / objective layer**
   - `models/pixelloss.py` contains the pixel-level loss used by the model.
   - This indicates the training target is defined at the **per-pixel prediction level**, consistent with the repo’s pixel-by-pixel generation focus.

5. **Training and evaluation engine**
   - `engine_fractalgen.py` is the execution engine used by the main script.
   - It likely coordinates:
     - forward/backward passes
     - optimization
     - checkpointing
     - evaluation loops
     - generation sampling
   - In the architecture diagram, this should sit between the CLI/config layer and the model/data layers.

6. **Data flow at training time**
   - Input images come from an **ImageNet directory** provided via `--data_path`.
   - The training loop uses ImageNet samples, feeds them into the model, computes pixel-wise loss, and updates the model with distributed training.
   - The repo explicitly supports **PyTorch DDP via `torchrun`**, so multi-GPU/multi-node training is a first-class architecture element.

7. **Data flow at evaluation time**
   - There are two main evaluation paths:
     - **NLL evaluation** for likelihood estimation
     - **Generation evaluation** for producing large batches of samples and measuring image quality
   - Evaluation uses pretrained checkpoints from `pretrained_models/...` or `output_dir/resume`.
   - Generation is configured with parameters like **CFG**, **temperature**, and **iteration lists**, which affect sampling behavior rather than training.

8. **External artifacts and metrics**
   - `fid_stats/adm_in64_stats.npz` and `fid_stats/adm_in256_stats.npz` are reference statistics used for **FID evaluation**.
   - These are important diagram nodes because evaluation depends on them, even though they are not learned parameters.
   - The repo also ships pretrained model checkpoints as external/downloaded artifacts.

9. **Utilities boundary**
   - `util/` contains support code rather than core model logic:
     - `crop.py` for image preprocessing/cropping
     - `lr_sched.py` for learning-rate scheduling
     - `misc.py` for general helpers
     - `visualize.py` for displaying/saving outputs
     - `download.py` for checkpoint retrieval
   - These utilities support the training/evaluation pipeline but are not the main algorithmic boundary.

10. **Runtime and tooling**
   - The project is designed for **Python + PyTorch**, with GPU acceleration and distributed execution.
   - `environment.yaml` defines the conda environment, which matters for dependency reproducibility.
   - The README examples show use of `torchrun`, meaning distributed process launch is part of the operational architecture.

11. **Repository-level workflow**
   - Typical flow is:
     1. install environment
     2. download pretrained checkpoints or prepare training from scratch
     3. point the code at ImageNet
     4. run `main_fractalgen.py` for training or evaluation
     5. optionally inspect results in the notebook demo
   - This repo is therefore a **research/training repository**, not a service with persistent online inference.

12. **What to draw in an architecture diagram**
   - A good diagram should show:
     - **User/CLI or notebook**
     - `main_fractalgen.py`
     - `engine_fractalgen.py`
     - `models/*` and `models/pixelloss.py`
     - **ImageNet dataset input**
     - **checkpoint storage/output directory**
     - **FID stat files**
     - **distributed GPU runtime via torchrun/DDP**
   - The most important boundaries are the split between orchestration, model/loss implementation, dataset I/O, and evaluation artifacts.

Main entry points: [`engine_fractalgen.py`](https://github.com/lth14/fractalgen/blob/main/engine_fractalgen.py) (Engine), [`main_fractalgen.py`](https://github.com/lth14/fractalgen/blob/main/main_fractalgen.py) (Main CLI), [`models/fractalgen.py`](https://github.com/lth14/fractalgen/blob/main/models/fractalgen.py) (FractalGen), [`demo/run_fractalgen.ipynb`](https://github.com/lth14/fractalgen/blob/main/demo/run_fractalgen.ipynb) (Demo notebook)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entrypoints["Entry points"]
  node_main_fractalgen["Main CLI<br/>orchestration script<br/>[main_fractalgen.py]"]
  node_demo_notebook["Demo notebook<br/>interactive demo"]
  node_environment_yaml["Environment<br/>conda env<br/>[environment.yaml]"]
end

subgraph group_core["Core pipeline"]
  node_engine_fractalgen["Engine"]
  node_fractalgen_model["FractalGen<br/>model family<br/>[fractalgen.py]"]
  node_ar_model["AR variant<br/>autoregressive variant<br/>[ar.py]"]
  node_mar_model["MAR variant<br/>masked-autoregressive variant<br/>[mar.py]"]
  node_pixel_loss["Pixel loss<br/>objective<br/>[pixelloss.py]"]
  node_torchrun_ddp{{"DDP runtime<br/>distributed PyTorch runtime"}}
end

subgraph group_data["Data and artifacts"]
  node_imagenet_data[("ImageNet data<br/>dataset input")]
  node_checkpoint_store[("Checkpoints<br/>model artifacts")]
  node_fid_stats_64["FID 64 stats<br/>evaluation reference<br/>[adm_in64_stats.npz]"]
  node_fid_stats_256["FID 256 stats<br/>evaluation reference"]
end

subgraph group_support["Support code"]
  node_crop_util["Crop util<br/>preprocessing helper<br/>[crop.py]"]
  node_lr_sched_util["LR schedule<br/>optimization helper<br/>[lr_sched.py]"]
  node_misc_util["Misc utils<br/>general helpers<br/>[misc.py]"]
  node_visualize_util["Visualize<br/>output helper<br/>[visualize.py]"]
  node_download_util["Download<br/>artifact fetcher<br/>[download.py]"]
end

node_main_fractalgen -->|"orchestrates"| node_engine_fractalgen
node_demo_notebook -->|"loads"| node_fractalgen_model
node_demo_notebook -->|"uses"| node_checkpoint_store
node_engine_fractalgen -->|"runs"| node_fractalgen_model
node_engine_fractalgen -->|"optimizes"| node_pixel_loss
node_fractalgen_model -->|"variant"| node_ar_model
node_fractalgen_model -->|"variant"| node_mar_model
node_main_fractalgen -->|"reads"| node_imagenet_data
node_engine_fractalgen -->|"loads batches"| node_imagenet_data
node_main_fractalgen -->|"loads ckpts"| node_checkpoint_store
node_engine_fractalgen -->|"saves/loads"| node_checkpoint_store
node_engine_fractalgen -->|"scores 64"| node_fid_stats_64
node_engine_fractalgen -->|"scores 256"| node_fid_stats_256
node_engine_fractalgen -->|"coordinates"| node_torchrun_ddp
node_main_fractalgen -->|"launches via"| node_torchrun_ddp
node_main_fractalgen -->|"preprocesses"| node_crop_util
node_engine_fractalgen -->|"schedules"| node_lr_sched_util
node_engine_fractalgen -->|"renders"| node_visualize_util
node_main_fractalgen -->|"fetches assets"| node_download_util
node_main_fractalgen -.->|"depends on"| node_environment_yaml
node_engine_fractalgen -->|"uses helpers"| node_misc_util

click node_main_fractalgen "https://github.com/lth14/fractalgen/blob/main/main_fractalgen.py"
click node_demo_notebook "https://github.com/lth14/fractalgen/blob/main/demo/run_fractalgen.ipynb"
click node_engine_fractalgen "https://github.com/lth14/fractalgen/blob/main/engine_fractalgen.py"
click node_fractalgen_model "https://github.com/lth14/fractalgen/blob/main/models/fractalgen.py"
click node_ar_model "https://github.com/lth14/fractalgen/blob/main/models/ar.py"
click node_mar_model "https://github.com/lth14/fractalgen/blob/main/models/mar.py"
click node_pixel_loss "https://github.com/lth14/fractalgen/blob/main/models/pixelloss.py"
click node_fid_stats_64 "https://github.com/lth14/fractalgen/blob/main/fid_stats/adm_in64_stats.npz"
click node_fid_stats_256 "https://github.com/lth14/fractalgen/blob/main/fid_stats/adm_in256_stats.npz"
click node_crop_util "https://github.com/lth14/fractalgen/blob/main/util/crop.py"
click node_lr_sched_util "https://github.com/lth14/fractalgen/blob/main/util/lr_sched.py"
click node_misc_util "https://github.com/lth14/fractalgen/blob/main/util/misc.py"
click node_visualize_util "https://github.com/lth14/fractalgen/blob/main/util/visualize.py"
click node_download_util "https://github.com/lth14/fractalgen/blob/main/util/download.py"
click node_environment_yaml "https://github.com/lth14/fractalgen/blob/main/environment.yaml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main_fractalgen,node_demo_notebook,node_environment_yaml toneBlue
class node_engine_fractalgen,node_fractalgen_model,node_ar_model,node_mar_model,node_pixel_loss,node_torchrun_ddp toneAmber
class node_imagenet_data,node_checkpoint_store,node_fid_stats_64,node_fid_stats_256 toneMint
class node_crop_util,node_lr_sched_util,node_misc_util,node_visualize_util,node_download_util toneRose
```

## Components

### Entry points

CLI, notebook, and environment setup

- [Main CLI](https://github.com/lth14/fractalgen/blob/main/main_fractalgen.py): orchestration script · Primary command-line entrypoint for training, evaluation, and generation workflows. · `main_fractalgen.py`
- [Demo notebook](https://github.com/lth14/fractalgen/blob/main/demo/run_fractalgen.ipynb): interactive demo · Colab notebook for running pretrained FractalGen models interactively. · `demo/run_fractalgen.ipynb`
- [Environment](https://github.com/lth14/fractalgen/blob/main/environment.yaml): conda env · Conda environment definition for Python and PyTorch dependencies. · `environment.yaml`

### Core pipeline

Training, evaluation, and model implementation

- [Engine](https://github.com/lth14/fractalgen/blob/main/engine_fractalgen.py): training/eval engine · Coordinates execution loops, optimization, checkpointing, evaluation, and sampling. · `engine_fractalgen.py`
- [FractalGen](https://github.com/lth14/fractalgen/blob/main/models/fractalgen.py): model family · Main implementation of the Fractal Generative Models architecture. · `models/fractalgen.py`
- [AR variant](https://github.com/lth14/fractalgen/blob/main/models/ar.py): autoregressive variant · Autoregressive-style model variant. · `models/ar.py`
- [MAR variant](https://github.com/lth14/fractalgen/blob/main/models/mar.py): masked-autoregressive variant · Masked/autoregressive hybrid variant used by stronger checkpoints. · `models/mar.py`
- [Pixel loss](https://github.com/lth14/fractalgen/blob/main/models/pixelloss.py): objective · Per-pixel prediction loss used during training. · `models/pixelloss.py`
- **DDP runtime**: distributed PyTorch runtime · Multi-GPU and multi-node execution via torchrun and PyTorch DDP.

### Data and artifacts

Dataset input and evaluation references

- **ImageNet data**: dataset input · Directory-based ImageNet training and evaluation data provided via the data path argument.
- **Checkpoints**: model artifacts · Pretrained checkpoints and resume state used for evaluation and continued training.
- [FID 64 stats](https://github.com/lth14/fractalgen/blob/main/fid_stats/adm_in64_stats.npz): evaluation reference · Reference statistics for 64×64 FID evaluation. · `fid_stats/adm_in64_stats.npz`
- [FID 256 stats](https://github.com/lth14/fractalgen/blob/main/fid_stats/adm_in256_stats.npz): evaluation reference · Reference statistics for 256×256 FID evaluation. · `fid_stats/adm_in256_stats.npz`

### Support code

Utilities for preprocessing, scheduling, visualization, and downloads

- [Crop util](https://github.com/lth14/fractalgen/blob/main/util/crop.py): preprocessing helper · Image cropping and preprocessing support. · `util/crop.py`
- [LR schedule](https://github.com/lth14/fractalgen/blob/main/util/lr_sched.py): optimization helper · Learning-rate scheduling utilities. · `util/lr_sched.py`
- [Misc utils](https://github.com/lth14/fractalgen/blob/main/util/misc.py): general helpers · Shared helper functions used across the pipeline. · `util/misc.py`
- [Visualize](https://github.com/lth14/fractalgen/blob/main/util/visualize.py): output helper · Utilities for displaying and saving generated or evaluated outputs. · `util/visualize.py`
- [Download](https://github.com/lth14/fractalgen/blob/main/util/download.py): artifact fetcher · Retrieves pretrained checkpoints and related assets. · `util/download.py`

## Connections

- Main CLI → Engine: orchestrates · CLI hands off training, evaluation, and generation work to the engine.
- Demo notebook → FractalGen: loads · Notebook demonstrates interactive model usage.
- Demo notebook → Checkpoints: uses · Notebook runs pretrained checkpoints.
- Engine → FractalGen: runs · Engine invokes the core FractalGen model.
- Engine → Pixel loss: optimizes · Training uses the pixel-level objective.
- FractalGen → AR variant: variant · AR is one architectural branch.
- FractalGen → MAR variant: variant · MAR is the higher-performing architectural branch.
- Main CLI → ImageNet data: reads · Training and evaluation consume ImageNet data from the provided path.
- Engine → ImageNet data: loads batches · Engine pulls minibatches for training and evaluation.
- Main CLI → Checkpoints: loads ckpts · Evaluation and resume flows use pretrained or resumed checkpoints.
- Engine → Checkpoints: saves/loads · Engine manages checkpoint persistence.
- Engine → FID 64 stats: scores 64 · FID evaluation uses the 64×64 reference statistics.
- Engine → FID 256 stats: scores 256 · FID evaluation uses the 256×256 reference statistics.
- Engine → DDP runtime: coordinates · Engine operates under distributed execution.
- Main CLI → DDP runtime: launches via · Distributed training is launched with torchrun/DDP.
- Main CLI → Crop util: preprocesses · CLI workflow can use cropping and preprocessing utilities.
- Engine → LR schedule: schedules · Optimization uses learning-rate scheduling helpers.
- Engine → Visualize: renders · Evaluation and sampling outputs can be visualized or saved.
- Main CLI → Download: fetches assets · Pretrained artifacts can be downloaded for use in runs.
- Main CLI → Environment: depends on · Project runtime is defined by the conda environment file.
- Engine → Misc utils: uses helpers · Shared utility functions support execution flow.
