Skip to content
module

Stable Diffusion

13 members
lucid.models.generative.stable_diffusion

Stable Diffusion family — Rombach et al., CVPR 2022.

Image Generation
Paper

Rombach, Robin, et al. "High-Resolution Image Synthesis with Latent Diffusion Models." CVPR, 2022, pp. 10684–10695.

Overview

Diffusion in pixel space spends most of its capacity on detail the eye discards. Latent diffusion splits the problem in two: an autoencoder learns a perceptually equivalent space at a fraction of the resolution, and the diffusion model runs entirely inside it.

The first stage is an encoder E\mathcal{E} and decoder D\mathcal{D} with a downsampling factor f=H/h=W/wf = H/h = W/w. At f=8f = 8 a 512×512×3512 \times 512 \times 3 image becomes a 64×64×464 \times 64 \times 4 latent — 48 times fewer values, so every subsequent convolution and attention costs proportionally less. The paper regularises this space rather than leaving it free: a slight KL penalty toward N(0,I)\mathcal{N}(0, I) keeps the latents from drifting to arbitrary scale, which matters because the diffusion process assumes unit-ish variance.

The second stage is a time-conditional U-Net trained on the usual denoising objective, but on latents:

L=EE(x), y, ϵ∼N(0,1), t[∥ϵ−ϵθ(zt,t,τθ(y))∥22].\mathcal{L} = \mathbb{E}_{\mathcal{E}(x),\, y,\, \epsilon \sim \mathcal{N}(0, 1),\, t} \Big[\big\| \epsilon - \epsilon_\theta\big(z_t, t, \tau_\theta(y)\big) \big\|_2^2\Big].

Conditioning enters by cross-attention rather than concatenation, which is what makes the modality of yy irrelevant to the U-Net. With φi(zt)\varphi_i(z_t) the flattened intermediate representation,

Q=WQ(i)φi(zt),K=WK(i)τθ(y),V=WV(i)τθ(y),Q = W_Q^{(i)} \varphi_i(z_t), \quad K = W_K^{(i)} \tau_\theta(y), \quad V = W_V^{(i)} \tau_\theta(y),

so the latent asks the questions and the conditioning answers them. A text encoder, a class embedding and a layout map all present the same interface — a sequence of vectors — and the U-Net never learns which it is reading.

τθ\tau_\theta is deliberately left open by the paper, which evaluates several. The released models use a frozen CLIP text encoder, so the width below is CLIP's and the conditioning sequence is its 77 tokens.

Classes

Functions

Weights