How lth14/fractalgen works
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 Engine · main_fractalgen.py Main CLI · fractalgen.py FractalGen · run_fractalgen.ipynb Demo notebook
Components 18
Entry points
CLI, notebook, and environment setup
- Main CLI · Primary command-line entrypoint for training, evaluation, and generation workflows.
main_fractalgen.py - Demo notebook · Colab notebook for running pretrained FractalGen models interactively.
demo/run_fractalgen.ipynb - Environment · Conda environment definition for Python and PyTorch dependencies.
environment.yaml
Core pipeline
Training, evaluation, and model implementation
- Engine · Coordinates execution loops, optimization, checkpointing, evaluation, and sampling.
engine_fractalgen.py - FractalGen · Main implementation of the Fractal Generative Models architecture.
models/fractalgen.py - AR variant · Autoregressive-style model variant.
models/ar.py - MAR variant · Masked/autoregressive hybrid variant used by stronger checkpoints.
models/mar.py - Pixel loss · Per-pixel prediction loss used during training.
models/pixelloss.py - DDP runtime · Multi-GPU and multi-node execution via torchrun and PyTorch DDP.
Data and artifacts
Dataset input and evaluation references
- ImageNet data · Directory-based ImageNet training and evaluation data provided via the data path argument.
- Checkpoints · Pretrained checkpoints and resume state used for evaluation and continued training.
- FID 64 stats · Reference statistics for 64×64 FID evaluation.
fid_stats/adm_in64_stats.npz - FID 256 stats · Reference statistics for 256×256 FID evaluation.
fid_stats/adm_in256_stats.npz
Support code
Utilities for preprocessing, scheduling, visualization, and downloads
- Crop util · Image cropping and preprocessing support.
util/crop.py - LR schedule · Learning-rate scheduling utilities.
util/lr_sched.py - Misc utils · Shared helper functions used across the pipeline.
util/misc.py - Visualize · Utilities for displaying and saving generated or evaluated outputs.
util/visualize.py - Download · Retrieves pretrained checkpoints and related assets.
util/download.py
Connections 21
- 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.