Latent Diffusion Models (Stable Diffusion)
Diffusion in compressed latent space: VAE encoder/decoder, the latent space advantage, training pipeline, the full Stable Diffusion architecture, and efficiency analysis.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- Pixel-Space vs Latent-Space
- The VAE Component
- Latent Diffusion Training
- Full Stable Diffusion Pipeline
- Efficiency Analysis
- SDXL and SD3 Improvements
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Explain why diffusion in latent space is more efficient than pixel space.
- Derive the compression ratio and its effect on computational cost.
- Describe the two-stage training pipeline (VAE then diffusion).
- Trace the full generation pipeline: text → CLIP → U-Net → VAE decoder → image.
- Analyze the computational savings quantitatively.
Notation
- — VAE encoder
- — VAE decoder
- — latent representation
- — downsampling factor ( means )
Core Intuition
Running diffusion directly on high-resolution images () is computationally prohibitive — the U-Net must process 786K pixels. Latent diffusion first compresses images into a low-dimensional latent space using a pre-trained VAE (), then runs diffusion in this compressed space. The result: 50–100x less compute with comparable quality.
Latent Diffusion Architecture
Pixel-Space vs Latent-Space
Pixel-space diffusion (DDPM, ADM):
- Input dimension: .
- U-Net processes full resolution.
- Self-attention at costs entries.
Latent-space diffusion (LDM/Stable Diffusion):
- Encode: values.
- U-Net processes latent.
- Self-attention at costs entries.
Compression ratio: .
The VAE Component
Architecture: Encoder maps images to latents; decoder reconstructs images from latents.
Training objective:
KL regularization: Forces latents to be roughly standard normal — important for diffusion to start from .
Perceptual loss (LPIPS): Ensures reconstructions are perceptually faithful, not just pixel-accurate.
Typical architecture: Downsampling factor . Input → latent . Latent channels (vs 3 for RGB).
Latent Diffusion Training
Two-stage pipeline:
Stage 1: Train VAE on images (autoencoder reconstruction).
- Train until reconstruction quality is high (PSNR > 30 dB).
- Freeze VAE after training.
Stage 2: Train diffusion U-Net in latent space.
- Encode training images: .
- Run standard DDPM training on instead of .
- Loss: .
Generation:
- Sample .
- Run reverse diffusion in latent space: .
- Decode: .
Full Stable Diffusion Pipeline
Components:
- Text encoder (CLIP ViT-L/14): Text → 77 × 768 embeddings.
- U-Net (860M params): Denoises latents conditioned on text.
- VAE decoder: Latents → pixels.
- Scheduler (DDIM/DPM-Solver): Controls sampling steps.
Generation flow:
Classifier-free guidance (CFG): At each denoising step:
where is the guidance scale and is the null (unconditional) embedding.
Efficiency Analysis
Compute savings: For a 50-step DDIM with images:
- Pixel-space U-Net: ~2000 GFLOPS per step × 50 = 100 TFLOPS.
- Latent U-Net: ~40 GFLOPS per step × 50 = 2 TFLOPS.
- VAE decode: ~50 GFLOPS (one-time).
- Total savings: ~50x.
Memory savings: Latent attention matrices are vs in pixel space. At the same relative resolution: 64x less attention memory.
SDXL and SD3 Improvements
SDXL:
- Larger U-Net (2.6B params).
- Two text encoders (CLIP + OpenCLIP).
- Higher latent resolution ().
- Refiner model for high-frequency details.
Stable Diffusion 3 (SD3):
- Replaces U-Net with MMDiT (Multimodal Diffusion Transformer).
- Separate transformer streams for image and text, merged via cross-attention.
- Flow matching objective (rectified flow) instead of DDPM.
- Three text encoders (CLIP, OpenCLIP, T5-XXL).
Common Pitfalls
Pitfall 1. Assuming VAE is lossless. The VAE introduces reconstruction error — fine details may be lost. This limits the maximum quality achievable by latent diffusion.
Pitfall 2. Using a poorly-regularized VAE. If the latent space is not well-structured (e.g., holes, mismatched scale), the diffusion model struggles to learn proper distributions.
Pitfall 3. Ignoring the VAE decode cost for real-time applications. While the U-Net runs on small latents, the final VAE decode still operates at full resolution.
Summary
- Latent diffusion runs diffusion in compressed VAE space: ~50x more efficient.
- Two-stage training: (1) train VAE, (2) train U-Net in latent space.
- Stable Diffusion: CLIP encoder + latent U-Net + VAE decoder + CFG.
- Compression factor : images → latents .
- Modern evolution: DiT replacing U-Net (SD3, DALL-E 3).
Exercises
Exercise 1. Compute the compression ratio for with latent channels vs the original image.
Exercise 2. Estimate the minimum VAE PSNR needed for latent diffusion to achieve FID comparable to pixel-space diffusion.
Exercise 3. Derive the total FLOPs for generating a image with Stable Diffusion (latent , 50 steps).
Exercise 4. Explain why KL regularization of the VAE is necessary for the diffusion model to generate from .
Exercise 5. Compare the inductive biases of U-Net vs DiT for latent diffusion at resolution .