↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

Latent diffusion

Diffusion in pixel space, as on the DDPM tab, spends most of its effort on detail the eye barely notices, and every step runs the network on every pixel. Latent diffusion (Rombach et al.) splits the work in two. An autoencoder is trained once to squeeze an image into a much smaller latent that still decodes to nearly the same picture; the diffusion model then learns and samples in that latent. Stable Diffusion is a latent diffusion model conditioned on text.

This tab records Stable Diffusion 1.5 drawing a red apple on a wooden table at 256 × 256 pixels: a 4 × 32 × 32 latent, 20 steps, seed 0. The page counts 860 M parameters in the U-Net, 84 M in the VAE and 123 M in the text encoder.

The VAE

The autoencoder downsamples by 8 on each side and keeps 4 channels: 48 times fewer numbers than the RGB image. Its encoder outputs a mean and a standard deviation per latent value, and a small KL penalty keeps that distribution close to a standard normal, which is why it is called a VAE. Rombach et al. trained it with a perceptual loss and a patch-based adversarial loss as well as reconstruction, so the decoder restores sharp texture instead of a blurred average.

The VAE panel runs it on a photo of a beagle: the latent mean and standard deviation, the mean times the scaling factor 0.18215 (which brings the latents to roughly unit variance, the scale the diffusion model expects), and the decoded reconstruction, with mean squared error 0.0132. Every preview image on the page is the VAE's decode of a latent.

Text conditioning by cross-attention

The prompt is read by a frozen text encoder, the text half of OpenAI's CLIP ViT-L/14: 12 causal layers, 768 wide, padded to 77 tokens. The Text encoder panel shows its last hidden state for the prompt (9 real tokens, from start to end) and for the empty prompt. All 77 positions, padding included, are the keys and values of every cross-attention in the U-Net.

Inside each transformer block of the U-Net, the latent's pixels are the queries and the text tokens supply keys and values:

where is the encoder's output. The Where each token lands panel averages one block's cross-attention over heads and draws a map per token; the last row sums the weight on the 68 padding positions. In this recording most of every block's weight goes to the start token, which carries no word and works as a place for attention that has nowhere better to go; each map is scaled to its own maximum, so the words' smaller shares still show where they land.

The U-Net

The denoiser is DDPM's U-Net grown and given text. The U-Net panel shows four levels of 320, 640, 1280 and 1280 channels at 32, 16, 8 and 4 latent pixels. The first three levels interleave ResNet blocks with transformer blocks; the lowest level has ResNet blocks only, and the middle has both. The timestep enters as before, a 320-value sinusoid through Linear 320 → 1280, SiLU, Linear 1280 → 1280, as a per-channel bias in every ResNet block.

Pick the Transformer block (up level 2, block 1) to see one in full: the 8 × 8 = 64 pixels become tokens 1280 wide, pass self-attention with 8 heads, cross-attention to the text, and a feed-forward layer of 5120, each with a residual connection, and a 1 × 1 convolution puts them back on the grid. Self-attention lets distant parts of the image agree with each other; cross-attention is the only place the words enter.

Classifier-free guidance

A model that has learned still produces images that follow the prompt only loosely. Classifier-free guidance (Ho and Salimans) trains one network both with and without its condition, by sometimes replacing the prompt with the empty one (10 % of the time, says the SD 1.5 model card), and at sampling time extrapolates from the unconditional prediction towards the conditional one:

The Guidance panel shows the two predictions, their difference and the result at . The difference is the direction in which the prompt changes the image; multiplying it by 7.5 trades diversity for fidelity to the text. Each step therefore runs the U-Net twice, which the page writes as U-Net ×2.

The Guidance scale strip runs DDIM again with the same prompt and seed at and . At the formula leaves only the prompt's prediction, so the pipeline skips the empty pass, and here it draws no apple at all, just a garbled pattern. A green apple takes shape from to , and it only turns red, as the prompt asks, from 7.5 up; by 20 the colours are oversaturated. In the Same seed card, the four guidance scales switch follows through every step at and .

Samplers

The Sampling loop uses DDIM (Song, Meng and Ermon) with : no fresh noise, so the image is a deterministic function of the starting latent. Each step forms from as DDPM does, then re-noises it straight to the next timestep with the same :

Because DDIM does not depend on the step-by-step Markov chain, it can skip: 20 steps at instead of 1000.

Same seed, three samplers runs the same noise through two other solvers. Euler, in the form Karras et al. made common, writes the noisy latent as with , starting at , and steps ; the U-Net reads . DPM-Solver++ 2M (Lu et al.) is a second-order multistep solver in log signal-to-noise ratio that combines the of this step and the last, falling back to first order at the first and last steps. All three solve the same probability-flow ODE, so they draw similar images by different paths.

Where Stable Diffusion came from

Rombach et al. published latent diffusion in 2021 and released Stable Diffusion with Stability AI and Runway in 2022; version 1.5 is the same architecture trained longer on aesthetically filtered LAION images. The SDXL tab scales this U-Net up and adds a second text encoder. The ControlNet and LCM tabs reuse this exact U-Net, one to steer it with an edge map, the other to sample in four steps. SD 3.5 and FLUX replace it with a transformer.

Papers

  1. High-Resolution Image Synthesis with Latent Diffusion Models Robin Rombach, Andreas Blattmann, Dominik Lorenz, Patrick Esser, Björn Ommer · CVPR 2022 Latent diffusion: the autoencoder, the U-Net with cross-attention to text, and the model Stable Diffusion releases.
  2. Learning Transferable Visual Models From Natural Language Supervision Alec Radford et al. · ICML 2021 CLIP, whose ViT-L/14 text encoder reads the prompt.
  3. Classifier-Free Diffusion Guidance Jonathan Ho, Tim Salimans · arXiv 2022 One model trained with and without its condition, and the extrapolation between the two predictions that the Guidance panel draws.
  4. Denoising Diffusion Implicit Models Jiaming Song, Chenlin Meng, Stefano Ermon · ICLR 2021 DDIM: a deterministic sampler for the same trained model that can skip most of the steps.
  5. Elucidating the Design Space of Diffusion-Based Generative Models Tero Karras, Miika Aittala, Timo Aila, Samuli Laine · NeurIPS 2022 Sampling in σ-space with input scaling, the formulation behind the Euler sampler.
  6. DPM-Solver++: Fast Solver for Guided Sampling of Diffusion Probabilistic Models Cheng Lu et al. · arXiv 2022 The second-order multistep solver in the sampler comparison, designed for guided sampling.

See also