DL0103 Stable Diffusion Latent Space

What is latent diffusion, and why does Stable Diffusion run diffusion in a VAE latent space?

Answer

Latent diffusion splits image generation into two stages that are trained separately: a convolutional autoencoder first compresses pixels into a small spatial latent, and the diffusion model then learns the noise process entirely inside that latent grid. Stable Diffusion uses a KL-regularized VAE with downsampling factor f = 8, so a 512 \times 512 \times 3 image becomes a 64 \times 64 \times 4 latent: 786,432 values collapse to 16,384, a 48x reduction in the tensor the U-Net has to denoise. The motivation is that the two stages solve different problems. Pixel-space diffusion wastes most of its capacity and most of its training steps modeling imperceptible high-frequency detail, exactly the information a perceptual codec throws away for free; the autoencoder handles that perceptual compression once, and the diffusion model is left with the semantic compression problem of arranging layout, objects, and style. The compute argument is even stronger than the 48x suggests, because the U-Net contains self-attention: token count drops from 262,144 to 4,096, so any O(N^2) attention block gets f^4 = 4096 times cheaper. That is what turned high-resolution text-to-image training from a large-cluster project into something reproducible on modest hardware, and what makes 50-step sampling on a consumer GPU feasible at all.

(1) Two-Stage Factorization: the autoencoder is trained first and then frozen; the diffusion model never sees a pixel, and the decoder is invoked exactly once at the end of sampling.
(2) Quadratic Savings On Attention: the latent grid has f^2 fewer positions, so convolutions get f^2 cheaper and self-attention gets roughly f^4 cheaper.
(3) It Is Not A Generative VAE: the KL weight is tiny (on the order of 10^{-6}) and the reconstruction loss adds LPIPS plus a patch discriminator, so the encoder behaves as a sharp lossy codec rather than a prior you would ever sample from.
(4) Latent Scaling Matters: encoder outputs are multiplied by a constant (0.18215 for SD 1.x, 0.13025 for SDXL) so the latent has roughly unit variance and the standard noise schedule reaches a true \mathcal{N}(0, I) terminal state.
(5) Cheap Conditioning And Editing: CLIP text embeddings enter through cross-attention at every latent resolution, and img2img, inpainting, and ControlNet all operate on the same small latent grid.
(6) The Decoder Is The Fidelity Ceiling: nothing the diffusion model does can recover detail the 4-channel latent discarded, which is why small text, fine textures, and tiny faces degrade first.

Pipeline diagram: a 512 by 512 by 3 image enters a VAE encoder producing a 64 by 64 by 4 latent, an iterative denoising U-Net conditioned by CLIP text cross-attention operates on the latent for T steps, and a VAE decoder maps the result back to pixels

Figure 1: Training and sampling both live in the 4,096-token latent grid; the frozen encoder and decoder are the only components that ever touch the 262,144-pixel image, and text conditioning enters the denoiser through cross-attention.

The choice of f is a genuine trade-off rather than a free win. The original LDM ablations show that f \in \{4, 8\} is the sweet spot: at f = 1 or 2 training is slow because the model is still doing perceptual compression itself, while at f = 32 with only 4 channels the autoencoder becomes the bottleneck and sample quality saturates no matter how long the diffusion model trains. Channel count c is the other lever, and it is the one that later models moved: SD3 and Flux keep f = 8 but raise the latent to 16 channels, quartering the compression ratio from 48 to 12 to trade a little step cost for a much better reconstruction ceiling on text and fine structure. A practical consequence of the frozen-codec design is that latents are model-specific: an SDXL latent decoded by an SD 1.5 decoder produces color-shifted garbage, because the two autoencoders were trained with different scaling and channel statistics.

Mathematical Formulation:
z_0 = s \cdot \mathcal{E}(x)
\hat{x} = \mathcal{D}(z_0 / s)
z_t = \sqrt{\bar\alpha_t}\, z_0 + \sqrt{1 - \bar\alpha_t}\, \epsilon
\mathcal{L} = \mathbb{E}\left[\lVert \epsilon - \epsilon_\theta(z_t, t, \tau(y)) \rVert_2^2\right]
\rho = \frac{3 f^2}{c}
\rho = \frac{3 \cdot 64}{4} = 48

Where:

  • x \in \mathbb{R}^{H \times W \times 3} is the image and \hat{x} its reconstruction; z_0 \in \mathbb{R}^{h \times w \times c} is the clean latent with h = H/f and w = W/f.
  • \mathcal{E} and \mathcal{D} are the frozen VAE encoder and decoder, and s is the latent scaling constant that normalizes the variance.
  • t \in \{1, \ldots, T\} is the timestep, \bar\alpha_t the cumulative noise schedule, and \epsilon \sim \mathcal{N}(0, I) the sampled Gaussian noise.
  • \epsilon_\theta is the latent denoiser (U-Net in SD 1.x and SDXL, a transformer in DiT-style successors), and \tau(y) is the text-encoder output injected by cross-attention.
  • f is the spatial downsampling factor and c the latent channel count; \rho is the element compression ratio, which is 48 for f = 8, c = 4 and 12 for c = 16.
  • Required initial condition at sampling: z_T \sim \mathcal{N}(0, I) on the latent grid, which only holds if s was applied and \bar\alpha_T \approx 0.
Log-scale bar chart of self-attention cost reduction relative to pixel space at 512 by 512 resolution: factor 1 for pixel space with 262144 tokens, 256 for f equals 4, 4096 for f equals 8, and 65536 for f equals 16

Figure 2: Because self-attention scales as O(N^2) in the token count, compressing by f cuts attention cost by f^4; the f = 8 setting used by Stable Diffusion buys a 4096x reduction while keeping the decoder’s reconstruction error acceptable.

PropertyLatent diffusion (Stable Diffusion)Single-stage pixel diffusionCascaded pixel diffusion
Where noise is addedA 64x64x4 VAE latentDirectly on RGB pixels at full resolutionPixels at 64×64, then two super-resolution diffusion stages
Denoiser input size at 512×5124,096 tokens, 16,384 values262,144 tokens, 786,432 values4,096 tokens in the base model, full resolution in the last upsampler
Training and sampling costLowest; one model, attention roughly 4096x cheaper per blockHighest; attention is usually dropped or windowed to stay tractableModerate but multiplied by the number of stages
Fidelity ceilingBounded by decoder reconstruction error, independent of training lengthNo codec bottleneck; exact pixel modeling is possibleNo codec bottleneck, but upsamplers can hallucinate detail
Typical failure modeMangled small text, smeared fine texture, artifacts on tiny facesUnder-trained global structure for a fixed compute budgetError accumulation and train/test mismatch between stages

Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *