Diffusion Models Explained: The Math Behind Stable Diffusion
The Core Idea
Diffusion models belong to the family of latent variable generative models. Their insight is elegant: instead of learning to generate data directly, learn to denoise it.
The training process corrupts data samples by progressively adding Gaussian noise over steps until the data is indistinguishable from pure noise. The model then learns to reverse this process — predicting and removing the noise step by step.
The Forward Process
Given a clean data sample , the forward process defines a Markov chain that gradually adds noise:
where is a fixed noise schedule. A useful property: we can sample directly from in closed form. Let and :
Or equivalently via the reparameterisation trick:
As , and .
The Reverse Process
The reverse process learns to denoise step by step:
The network is trained to predict the noise that was added. The training objective simplifies to:
This is just a denoising regression problem — predict the noise, minimise MSE.
The Network Architecture: U-Net
The denoising network is a U-Net — an encoder-decoder architecture with skip connections between corresponding encoder and decoder feature maps.
Input x_t + timestep embedding
↓
[Conv] → [ResBlock] → [Attention] → [Downsample]
↓ ↓
[Conv] → [ResBlock] → [Attention] → [Downsample]
↓
Middle Block (ResBlock + Attention)
↓
[Upsample] → [ResBlock] → [Attention]
↓
[Upsample] → [ResBlock] → [Attention]
↓
Output (predicted noise ε)
The timestep is encoded as a sinusoidal embedding (similar to Transformer positional encodings) and injected into each ResBlock via FiLM conditioning.
Noise Schedules
The schedule controls how quickly noise accumulates. Common choices:
- Linear (DDPM): increases linearly from to
- Cosine (improved DDPM): — avoids over-noising at early steps
- Flow matching (used in Stable Diffusion 3, Flux): straight paths through data-noise space for faster sampling
DDIM: Faster Sampling
Standard DDPM requires denoising steps to generate one image. DDIM (Denoising Diffusion Implicit Models) reformulates the reverse process as a non-Markovian chain, enabling generation in 20–50 steps with comparable quality.
The DDIM update step:
Latent Diffusion Models
Stable Diffusion operates in latent space, not pixel space. A variational autoencoder (VAE) first compresses the image:
The diffusion process runs on the latent — typically for a image. This reduces the computation by a factor of ~48× compared to pixel-space diffusion.
Classifier-Free Guidance
To steer generation toward a text prompt , classifier-free guidance interpolates between conditional and unconditional predictions:
The guidance scale controls the trade-off between sample quality (higher ) and diversity (lower ). Typical values are 7–15.
Key Takeaways
- Diffusion models frame generation as iterative denoising — a simple regression objective
- The forward process analytically defines from in one step, enabling efficient training
- DDIM reduces inference steps from 1000 to ~20–50 with no retraining
- Latent diffusion (Stable Diffusion) moves the diffusion process into a compressed VAE latent space for tractable high-resolution generation
- Classifier-free guidance is the key lever for controlling output fidelity and prompt adherence