# microsoft/vq-diffusion architecture

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

- Interactive diagram: https://gitdiagram.com/microsoft/vq-diffusion
- Repository: https://github.com/microsoft/vq-diffusion

## Overview

##### Project type and purpose
- This is a research‐grade machine learning framework (VQ-Diffusion) for text-to-image synthesis. It provides training and inference pipelines built around a Vector Quantized VAE (VQ-VAE) latent space and a conditional Denoising Diffusion Probabilistic Model (DDPM).

##### File structure
- Top level:
- OUTPUT/pretrained_model – holds downloaded VQ-VAE and diffusion checkpoints
- configs – YAML files defining dataset, model, and training hyperparameters
- image_synthesis – core code: data loaders, distributed launch, training engine, model definitions
- inference_VQ_Diffusion.py – high‐level inference API
- running_command/*.py & train.py – command‐line training scripts
- install_req.sh, vqdiffusion_download_checkpoints.sh – setup utilities
- Within image_synthesis:
- data – dataset wrappers and preprocessing utilities
- distributed – multi‐GPU training launcher and helpers
- engine – training loop components: optimizer, scheduler, logger, solver
- modeling – construction of codecs (VQ-VAE), text/image embeddings, conditional diffusion models, and transformer utilities
- taming – integration of external Taming‐Transformer VQGAN models
- utils – shared IO and miscellaneous functions

##### Key insights from README
- The architecture comprises two main stages:
a) A pretrained VQ-VAE (from Taming Transformer) that encodes images into discrete latent codes
b) A conditional diffusion transformer that operates on these codes, conditioned on text embeddings (via CLIP) or class labels
- Supports training on MSCOCO, CUB-200, ImageNet, FFHQ, and fine-tuning with classifier-free guidance
- Provides both training and inference (sample generation with various sampling strategies)

##### Detail
- Submodule where complexity matters (e.g., CLIP tokenizer vs CLIP model vs embedding projection)
- Where the EMA module acts in the training loop
- Which components load from pretrained checkpoints (VQGAN, diffusion) and which produce new checkpoints
- Notes on sampling strategies (guidance_scale, infer_speed, purity_sampling) as annotations on the inference path

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Data and Configurations
    Configs["Configs (YAML)"]:::cylinder
    DataSources["Raw Images & Text"]:::cylinder

    %% Data Preprocessing
    DataPreprocess["Data Preprocessing Layer"]:::blue

    %% VQ-VAE Codec
    subgraph "VQ-VAE Codec" 
        direction TB
        VQEncoder["VQ-VAE Encoder"]:::green
        VQDecoder["VQ-VAE Decoder"]:::green
        VQCheckpoints["Pretrained Checkpoints"]:::cylinder
    end

    %% Text Embedding Module
    subgraph "Text Embedding Module (CLIP)"
        direction TB
        CLIPTokenizer["CLIP Tokenizer"]:::green
        CLIPModel["CLIP Model"]:::green
        CLIPEmbedder["Embedding Projection"]:::green
    end

    %% Diffusion Model
    subgraph "Diffusion Transformer Model"
        direction TB
        ConditionalDALLE["conditional_dalle.py"]:::green
        DiffusionTransformer["diffusion_transformer.py"]:::green
    end

    %% Training Engine and Distributed
    subgraph "Training Engine"
        direction TB
        Solver["solver.py"]:::orange
        LRScheduler["lr_scheduler.py"]:::orange
        EMA["ema.py"]:::orange
        Logger["logger.py"]:::orange
    end
    DistributedLauncher["Distributed Launcher"]:::orange

    %% Inference and External
    InferenceAPI["Inference API"]:::purple
    HuggingFace["HuggingFace Diffusers"]:::cloud

    %% Connections
    DataSources -->|"image tensors/text labels"| DataPreprocess
    Configs -->|drives hyperparams| DataPreprocess

    DataPreprocess -->|"preprocessed images/tokens"| VQEncoder
    VQEncoder -->|"latent codes"| VQDecoder
    VQDecoder -->|"reconstructed images"| DataPreprocess

    Configs -->|drives hyperparams| VQEncoder
    VQEncoder --> VQCheckpoints
    VQCheckpoints -.-> VQEncoder

    DataPreprocess -->|"image tensors"| CLIPTokenizer
    CLIPTokenizer -->|"token ids"| CLIPModel
    CLIPModel -->|"text embeddings"| CLIPEmbedder

    CLIPEmbedder -->|"text embeddings"| ConditionalDALLE
    VQEncoder -->|"latent codes"| ConditionalDALLE
    ConditionalDALLE -->|"predicted latents"| DiffusionTransformer
    DiffusionTransformer -->|"denoised latents"| VQDecoder

    Configs -->|drives hyperparams| ConditionalDALLE

    %% Training paths
    VQEncoder -->|"forward/backprop"| Solver
    ConditionalDALLE --> Solver
    Solver --> LRScheduler
    Solver --> EMA
    Solver --> Logger
    DistributedLauncher --> Solver

    Configs -->|drives hyperparams| Solver

    %% Inference paths
    InferenceAPI --> VQEncoder
    InferenceAPI --> DiffusionTransformer
    InferenceAPI --> VQDecoder
    InferenceAPI --> CLIPEmbedder
    InferenceAPI -->|optional| HuggingFace

    %% Click Events
    click DataPreprocess "https://github.com/microsoft/vq-diffusion/tree/main/image_synthesis/data"
    click VQEncoder "https://github.com/microsoft/vq-diffusion/blob/main/image_synthesis/taming/models/vqgan.py"
    click VQCheckpoints "https://github.com/microsoft/vq-diffusion/tree/main/OUTPUT/pretrained_model/taming_dvae"
    click CLIPTokenizer "https://github.com/microsoft/vq-diffusion/blob/main/image_synthesis/modeling/modules/clip/clip_tokenizer.py"
    click CLIPModel "https://github.com/microsoft/vq-diffusion/blob/main/image_synthesis/modeling/modules/clip/model.py"
    click CLIPEmbedder "https://github.com/microsoft/vq-diffusion/blob/main/image_synthesis/modeling/modules/clip/clip.py"
    click ConditionalDALLE "https://github.com/microsoft/vq-diffusion/blob/main/image_synthesis/modeling/models/conditional_dalle.py"
    click DiffusionTransformer "https://github.com/microsoft/vq-diffusion/blob/main/image_synthesis/modeling/transformers/diffusion_transformer.py"
    click Solver "https://github.com/microsoft/vq-diffusion/blob/main/image_synthesis/engine/solver.py"
    click LRScheduler "https://github.com/microsoft/vq-diffusion/blob/main/image_synthesis/engine/lr_scheduler.py"
    click EMA "https://github.com/microsoft/vq-diffusion/blob/main/image_synthesis/engine/ema.py"
    click Logger "https://github.com/microsoft/vq-diffusion/blob/main/image_synthesis/engine/logger.py"
    click DistributedLauncher "https://github.com/microsoft/vq-diffusion/blob/main/image_synthesis/distributed/launch.py"
    click InferenceAPI "https://github.com/microsoft/vq-diffusion/blob/main/inference_VQ_Diffusion.py"
    click Configs "https://github.com/microsoft/vq-diffusion/blob/main/configs/*.yaml"

    %% Styles
    classDef blue fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef green fill:#90EE90,stroke:#000,stroke-width:1px
    classDef orange fill:#FFD580,stroke:#000,stroke-width:1px
    classDef purple fill:#D8BFD8,stroke:#000,stroke-width:1px
    classDef cylinder fill:#FFFACD,stroke:#000,stroke-width:1px,shape:cylinder
    classDef cloud fill:#E0FFFF,stroke:#000,stroke-width:1px,shape:cloud
```
