Pixel-space DDPM on 256×256 RGB already strained 2020 hardware. Stable Diffusion became a product because Rombach et al. moved noise and denoising into a compressed latent space. This lecture is that VAE: encode, scale, diffuse, decode. SDXL uses a related autoencoder at higher native resolution; SVD diffuses a sequence of the same kind of latents.
Vol. 09 embeddings and Vol. 10 attention taught you “compress then reason.” LDMs apply that to images. Do not confuse this geometric latent with the LoRA weight space you will meet in LoRA (Diffusion).
Learning Objectives
By the end of this lesson, students should be able to:
- Define latent diffusion as DDPM/DDIM operating on VAE codes z rather than pixels x.
- State SD 1.x’s typical spatial compression (f=8) and latent shape for a 512×512 RGB image.
- Explain why the VAE is trained with reconstruction + perceptual (+ adversarial) + KL (or VQ) terms.
- Use the published latent scaling factor correctly before noising in code.
- List artifacts that come from the VAE (smeared text, high-frequency mush) vs from the UNet.
- Contrast KL-f8 (SD 1.x) with VQ latents and with SDXL’s higher-res latent grid.
Latent space in this module is the continuous (or quantized) code z = E(x) produced by a pretrained image autoencoder. A latent diffusion model (LDM) trains the denoiser on z, then returns pixels with x̂ = D(ẑ0). Stable Diffusion 1.x uses a KL-regularized VAE with downsampling factor f = 8 and 4 latent channels: a 512×512×3 image becomes a 64×64×4 tensor. That ~8× spatial cut (and 4 vs 3 channels) is why a UNet fits on a consumer GPU while still decoding to HD-ish stills.
Why Leave Pixel Space?
A 512×512×3 tensor is 786,432 dimensions per image. Most of those dimensions are perceptually redundant—smooth skies, JPEG-like correlations. The VAE spends capacity on a perceptually good reconstruction; the diffusion model spends capacity on semantics and layout in a 16,384-dimensional (64×64×4) grid. Compute for attention inside the UNet scales with spatial tokens; 64×64 is tractable, 512×512 self-attention is not (without windowing / DiT patches, which later models revisit).
| Stage | SD 1.5 @ 512×512 | SDXL base @ 1024×1024 |
|---|---|---|
| Pixel tensor | 3 × 512 × 512 | 3 × 1024 × 1024 |
| Latent (f=8, 4 ch) | 4 × 64 × 64 | 4 × 128 × 128 |
| UNet sees | The latent, not RGB | The larger latent grid |
| Decode once | End of sampling | End (refiner may decode too) |
Training the Autoencoder (Not the UNet)
Reconstruction
- L1/L2 on pixels is not enough.
- LPIPS / perceptual loss keeps edges.
- Patch discriminator (GAN) adds sharpness.
Regularization
- KL toward N(0,I) (SD 1.x KL-f8).
- Or VQ codebook (VQ-GAN LDMs).
- Stops latents from exploding so diffusion prior matches.
Then freeze
- LDM training usually freezes E and D.
- Only the denoiser (+ text encoder optionally) learns.
- Swap VAE later = different look, same UNet caution.
The Scaling Factor You Will Forget Once
SD’s VAE latents are not unit-variance out of the encoder. diffusers multiplies encoded z by a constant (historically 0.18215 for SD 1.5; XL has its own scaling_factor on the config). Diffusion noise and the UNet are trained in that scaled space. Encode → scale → noise/denoise → unscale → decode. Skip the scale and your SNR is wrong: washed-out or crunchy images, even with a perfect prompt.
What Lives in z (and What Does Not)
The four channels are not R, G, B, +alpha. They are a learned basis. Low-frequency layout survives aggressive compression; tiny type, eyelashes, and fabric weave often do not. That is why SD 1.x “cannot spell” even before CLIP’s 77-token limit: the VAE already smeared the glyphs, and the UNet never saw crisp letterforms. SDXL’s larger latent grid helps; it does not make the VAE a typesetting engine (Ideogram / later FLUX still win text among open-ish still models—Vol. 16 catalog, now you know why).
Latent diffusion wins
- VRAM and step time drop sharply.
- UNet attention is feasible.
- Same z-grid works for inpaint/outpaint masks (downsampled).
You pay in fidelity
- High-frequency texture / small text.
- Round-trip color shifts if VAE is weak.
- Video (SVD): temporal flicker if z is inconsistent across frames.
“Latent space means the same thing as a StyleGAN W space or a text embedding, and 4 channels means RGBA.” LDM latents are a spatial feature map aligned with the image grid (just 8× coarser). CLIP text embeddings are a different vector space used only as UNet condition. StyleGAN W is a per-image style code without a 64×64 grid. Mixing these words in a ComfyUI graph is how people connect the wrong tensor into VAEDecode.
Knowledge Check
- Short Answer: For SD 1.5, what is the latent shape of one 512×512 RGB image? Answer: 4 × 64 × 64 (f=8, 4 channels).
- True/False: The SD UNet is trained on RGB pixels, then the VAE is added only at deploy time. Answer: False—the UNet trains on scaled latents.
- Multiple Choice: The constant ~0.18215 is: (a) CFG scale, (b) VAE latent scaling_factor, (c) βT. Answer: (b).
- Short Answer: Name two loss ingredients used to train the SD-class VAE. Answer: Any two of reconstruction, perceptual/LPIPS, adversarial, KL (or VQ).
- True/False: f=8 means the VAE has eight latent channels. Answer: False—f is the spatial downsampling factor; SD 1.x uses 4 channels.
- Multiple Choice: Tiny illegible text in SD 1.5 is often partly the fault of: (a) only the scheduler eta, (b) VAE compression + CLIP token limits, (c) AdamW epsilon. Answer: (b).
- Short Answer: What must you do before
vae.decodeif z was scaled for the UNet? Answer: Divide by scaling_factor (unscale). - Short Answer: How does SVD reuse this lecture? Answer: It diffuses a temporal stack of the same class of latents, then decodes frame by frame.
- Multiple Choice: SDXL at 1024×1024 uses a latent grid of roughly: (a) 4×32×32, (b) 4×128×128, (c) 3×1024×1024 inside the UNet. Answer: (b).
- True/False: KL regularization exists so z stays in a range the Gaussian diffusion prior can model. Answer: True.
Key Takeaways
- LDMs denoise a VAE latent; pixels appear only at encode/decode boundaries.
- SD 1.x: f=8, 4 channels, scale ~0.18215; SDXL keeps the recipe on a larger grid.
- VAE training is perceptual + regularized; freeze it while the UNet learns diffusion.
- Many “SD can’t spell / mushy faces” complaints are VAE bandwidth, not just prompts.
- Next: DDPM—the original discrete training and sampling algorithms, now easy to run on z.
Hands-on idea: Round-trip photos through the SD 1.5 VAE at 512 and at 256. Then decode a random N(0,I) latent without running the UNet. Students see (1) what the VAE alone can reconstruct and (2) that Gaussian z is not a meaningful picture until denoised.
Discussion prompt: If you fine-tune only the VAE on logos, would existing SD 1.5 UNets suddenly render sharp type? Why or why not?
Recap: Stable Diffusion is cheap because it diffuses a small scaled latent, not RGB. Continue with DDPM.