4.5.15Generative Models

Latent diffusion (Stable Diffusion)

2,810 words13 min readdifficulty · medium1 backlinks

What Problem Does This Solve?

Problem with pixel-space diffusion: A 512×512 RGB image has 786,432 dimensions. Running U-Net denoising for many timesteps on this is computationally expensive, especially the self-attention layers.

Latent diffusion's solution:

  1. Train a Variational Autoencoder (VAE) to compress images: xRH×W×3zRh×w×cx \in \mathbb{R}^{H×W×3} \rightarrow z \in \mathbb{R}^{h×w×c} where h,wH,Wh,w \ll H,W
  2. Run diffusion only in latent space zz
  3. Decode final z0z_0 back to image with VAE decoder

Stable Diffusion uses f=8f=8 downsampling: 512×512 → 64×64, with c=4c=4 channels → 98% dimension reduction.


Architecture Components

1. The VAE (Autoencoder)

Why VAE and not simple autoencoder? VAE's KL term forces latent space to be continuous and smooth—nearby latents decode to similar images. Diffusion needs this smoothness to interpolate between noisy and clean latents.

Stable Diffusion specifics:

  • Downsampling factor f=8f=8: 512→64, 1024→128
  • Latent channels c=4c=4 (learned, not RGB)
  • Trained separately on massive image datasets (LAION)
  • Trained with L2 pixel reconstruction loss + KL regularization (the released SD VAE uses these; note: no VGG/perceptual loss in the standard KL-autoencoder recipe)
  • Frozen during diffusion training—only used for encode/decode

2. The Diffusion U-Net (in Latent Space)

Instead of denoising pixels, we denoise latents ztz_t:

Reverse process (denoising):

Why this works:

  • zz is 64×64×4 instead of 512×512×3 → U-Net operates on ~2% of original dimensions
  • Latents capture semantic structure, so U-Net learns "object shapes" not "pixel noise patterns"
  • Text conditioning cc (from CLIP) guides the denoising: "add fur texture" vs "add metal texture"

3. Text Conditioning (CLIP Embeddings)

Example: Prompt "a cat wearing sunglasses"

  • Latent region corresponding to head area attends to "cat" + "sunglasses" tokens
  • Latent region for body attends to "cat" token
  • U-Net uses this to inject appropriate features during denoising
Figure — Latent diffusion (Stable Diffusion)

Step-by-Step: How Stable Diffusion Generates an Image


Why Latent Space? A Derivation from First Principles

Question: Why is denoising in latent space more efficient than pixel space?

Computational cost per denoising step (schematically): cost(number of spatial locations)×(channels)×(per-token work)\text{cost} \propto (\text{number of spatial locations}) × (\text{channels}) × (\text{per-token work}) and the self-attention part specifically scales quadratically in the number of spatial locations N=hwN = h\cdot w: attention costN2=(hw)2\text{attention cost} \propto N^2 = (h w)^2

For pixel-space (DDPM):

  • Spatial locations: 512×512=262,144512×512 = 262{,}144
  • Channels: 3 (RGB)
  • Total dim: 262,144×3=786,432262{,}144 × 3 = 786{,}432

For latent-space (Stable Diffusion):

  • Spatial locations: 64×64=4,09664×64 = 4{,}096
  • Channels: 4
  • Total dim: 4,096×4=16,3844{,}096 × 4 = 16{,}384

Dimension ratio: 786,43216,384=48×\frac{786{,}432}{16{,}384} = 48× fewer dimensions.

Since the attention cost scales as N2N^2, the reduction in spatial locations from 262,144262{,}144 to 4,0964{,}096 (a 64×64× drop in NN) yields an attention-cost reduction of roughly: Npix2Nlat2=(262,1444,096)2=642=4096×\frac{N_\text{pix}^2}{N_\text{lat}^2} = \left(\frac{262{,}144}{4{,}096}\right)^2 = 64^2 = 4096× while element-wise convolutional work scales linearly with NN (~64×64× cheaper). The practical end-to-end speedup depends on how attention-heavy the U-Net is; empirically it is large (roughly an order of magnitude or more), which is exactly why latent diffusion made high-res text-to-image feasible on consumer GPUs.

But why no quality loss?

The VAE is trained to reconstruct images (L2 loss) with KL regularization. It discards imperceptible high-frequency detail and spatial redundancy (neighboring pixels being nearly identical) while preserving semantic content—object shapes, colors, composition. For generative tasks we care about semantic correctness ("does it look like a cat?"), not exact pixel values, so this compression is essentially perceptually harmless.




Key Hyperparameters and Design Choices

Parameter Stable Diffusion v1.5 Why This Value?
Latent channels cc 4 Balance: 3 is undercomplete (loses info), 8 is redundant. 4 captures structure well.
Downsampling ff 8 f=4f=4 → still large latents (slow), f=16f=16 → too lossy (VAE artifacts). 8 is sweet spot.
U-Net timesteps TT 1000 (training) Standard DDPM schedule. Higher TT = smoother noise schedule but slower training.
Sampling steps 20–50 (inference) DDIM/other samplers skip steps. ~50 steps ≈ near-max quality (diminishing returns beyond).
CFG scale 7.5 Classifier-Free Guidance scale. Higher = more prompt adherence, lower = more diversity.

Classifier-Free Guidance (CFG): During sampling, we interpolate between conditional and unconditional predictions: ϵ^=ϵθ(zt,t,)+s(ϵθ(zt,t,c)ϵθ(zt,t,))\hat{\epsilon} = \epsilon_\theta(z_t, t, \varnothing) + s \cdot (\epsilon_\theta(z_t, t, c) - \epsilon_\theta(z_t, t, \varnothing)) where ss is guidance scale (7.5), \varnothing is null prompt. This amplifies the text conditioning.



Recall Explain to a 12-year-old

Imagine you want to draw a super detailed picture, but you only have a tiny notepad. Drawing all the details directly would take forever!

Here's the trick Stable Diffusion uses:

  1. Shrink the picture – Take your big picture and compress it into a tiny "summary" (like shrinking a photo on your phone). This summary keeps all the important stuff—the cat's face, the colors—but throws away tiny details like individual hairs.
  2. Draw in the tiny space – Now, instead of drawing on a huge canvas, you draw on this tiny summary. It's way faster! You can sketch the cat, add sunglasses, fix mistakes—all super quick.
  3. Blow it back up – When you're done, you "un-compress" the tiny summary back to a big, detailed picture. Magic!

Why does this work? Because most of a picture is repetitive—blue sky pixels are all similar, grass is just repeated texture. The "summary" keeps the unique parts (cat's eyes, sunglasses shape) and lets you recreate the repetitive parts later. You're working on the meaning of the picture, not every single pixel.



Connections

  • Variational Autoencoders (VAE) – the compression backbone
  • Denoising Diffusion Probabilistic Models (DDPM) – latent diffusion applies DDPM's noise schedule in latent space
  • U-Net Architecture – the denoising model (with attention layers for conditioning)
  • CLIP Text Encoder – converts prompts to embeddings for cross-attention
  • Classifier-Free Guidance – technique to strengthen text conditioning (that CFG scale slider)
  • DDIM Sampling – faster sampling algorithm (why 20–50 steps, not 1000)
  • Attention Mechanisms – how text conditions the image generation
  • Perceptual Loss – related idea; note the SD KL-autoencoder uses L2+KL, not VGG

#flashcards/ai-ml

What is the key innovation of latent diffusion models? :: Running the diffusion process (noise addition and denoising) in a low-dimensional latent space produced by a VAE, rather than directly in pixel space, achieving a large speedup with negligible quality loss.

Why use a VAE (with KL) instead of a plain autoencoder for latent diffusion?
The KL term pushes the latent distribution toward a smooth Gaussian-like prior, preventing "holes" where interpolation breaks. Diffusion requires traversing latent space continuously—without smoothness, denoising produces artifacts.
What is the dimension reduction in Stable Diffusion v1.5?
512×512×3 (786,432 dimensions) → 64×64×4 (16,384 dimensions), a ~98% reduction via f=8 downsampling VAE.
What is the latent diffusion forward process equation?
zt=αˉtz0+1αˉtϵz_t = \sqrt{\bar{\alpha}_t} z_0 + \sqrt{1-\bar{\alpha}_t}\epsilon where z0=E(x)z_0 = \mathcal{E}(x) is the encoded image and ϵN(0,I)\epsilon \sim \mathcal{N}(0,I) is noise.

What is the training objective for latent diffusion? :: L=Ez0,t,ϵ,c[ϵϵθ(zt,t,c)2]\mathcal{L} = \mathbb{E}_{z_0, t, \epsilon, c}[\|\epsilon - \epsilon_\theta(z_t, t, c)\|^2] — train U-Net to predict noise ϵ\epsilon given noisy latent ztz_t, timestep tt, and text conditioning cc.

What is the DDPM reverse-step update?
zt1=1αt[zt1αt1αˉtϵ^]+σtzz_{t-1} = \frac{1}{\sqrt{\alpha_t}}\left[z_t - \frac{1-\alpha_t}{\sqrt{1-\bar{\alpha}_t}}\hat{\epsilon}\right] + \sigma_t \mathbf{z}, with αt=1βt\alpha_t=1-\beta_t and zN(0,I)\mathbf{z}\sim\mathcal{N}(0,I).
How does text conditioning work in Stable Diffusion?
Text is encoded by CLIP into embedding cc, then injected via cross-attention in the U-Net. Query comes from image features, key/value from text, letting each spatial region attend to relevant prompt tokens.
What loss trains the Stable Diffusion VAE?
Pixel-level L2 reconstruction loss + a KL divergence regularization term (the standard KL-autoencoder recipe); no VGG/perceptual loss in that recipe.
Why does self-attention make pixel-space diffusion so expensive?
Attention cost scales as N2N^2 in the number of spatial tokens N=hwN=hw. At 512² there are 262,144 tokens; at 64² only 4,096, so attention cost drops by (262144/4096)2=4096×(262144/4096)^2 = 4096\times.
What are the four Stable Diffusion generation steps?
1) Encode text with CLIP → cc, 2) Sample noise zTN(0,I)z_T \sim \mathcal{N}(0,I), 3) Denoise for T steps using U-Net ϵθ(zt,t,c)\epsilon_\theta(z_t, t, c), 4) Decode z0z_0 to image with VAE decoder D(z0)\mathcal{D}(z_0).
What is Classifier-Free Guidance (CFG)?
ϵ^=ϵθ(zt,t,)+s(ϵθ(zt,t,c)ϵθ(zt,t,))\hat{\epsilon} = \epsilon_\theta(z_t, t, \varnothing) + s(\epsilon_\theta(z_t, t, c) - \epsilon_\theta(z_t, t, \varnothing)). Scale ss controls prompt adherence vs diversity.
Why can't diffusion "fall off" the latent manifold in the SD VAE space?
KL regularization makes latents near-Gaussian and smooth, so all points interpolate to valid images—no "holes" to fall into during denoising.

Concept Map

compressed by

produces

removed by

forward process

denoised by

conditions

predicts noise to recover

reconstructed by

yields

98% smaller so

Image 512x512x3

VAE Encoder

Latent 64x64x4

Add Gaussian noise

Diffusion U-Net

Text embedding c

Clean latent z0

VAE Decoder

Output image

Pixel redundancy

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, ek image ko denoise karna directly 512×512×3 dimensions me matlab lagbhag 7.8 lakh numbers ke saath khelna—yeh bahut hi expensive aur slow hota hai, khaaskar jab diffusion me hume kai timesteps ke liye U-Net chalana padta hai. Latent Diffusion ka core idea yeh hai ki natural images me bahut saara redundancy hota hai—paas ke pixels aksar similar hote hain, textures repeat hote hain. Toh pehle hum ek autoencoder (VAE) se image ko ek chhote se latent space me compress kar dete hain, jaise 64×64×4 (matlab sirf ~2% dimensions bache), fir denoising wahi latent space me karte hain, aur end me VAE decoder se wapas pixels me convert kar dete hain. Yeh Stable Diffusion ka pura backbone hai.

Ab yeh kyun kaam karta hai? Kyunki VAE image ka sirf semantic content rakhta hai aur faltu redundancy phenk deta hai. Iska ek khaas point hai ki hum VAE use karte hain simple autoencoder nahi—VAE ka KL divergence term latent space ko smooth aur continuous banata hai, jisse paas ke latents similar images me decode hote hain. Diffusion ko yahi smoothness chahiye taaki noisy se clean latent ke beech transition sahi se ho paaye. Aur text conditioning ke liye CLIP embeddings use hote hain jo cross-attention ke through U-Net ko batate hain ki kaha "cat" banana hai aur kaha "sunglasses"—har spatial region apne relevant text tokens ko dhoondh leta hai.

Yeh matter isliye karta hai kyunki isi trick ki wajah se aaj tum apne normal GPU par hi high-quality image generate kar paate ho—warna pixel-space diffusion itna heavy hai ki common hardware par chalana mushkil ho jaata. Compute aur memory dono bachate hain, aur upar se latents par denoising karne se model asli me "object shapes" seekhta hai, sirf "pixel noise patterns" nahi. Isliye Stable Diffusion type models itne practical aur popular bane—yeh compression + diffusion + conditioning ka smart combination hai jo quality aur efficiency dono deta hai.

Go deeper — visual, from zero

Test yourself — Generative Models

Connections