DL0106 Stable Diffusion Architecture

Explain the key components of the Stable Diffusion architecture and how they interact during image generation.

Answer

Stable Diffusion is three separately trained networks plus one training-free sampler, and only one of the three is trained on the diffusion objective. A frozen KL-regularized VAE maps a 512 \times 512 \times 3 image to a 64 \times 64 \times 4 latent and back; a frozen CLIP text encoder turns the prompt into a 77 \times 768 sequence of token embeddings; and a conditional U-Net (860M parameters in SD 1.5) predicts the noise present in a noisy latent, given the timestep and the text context. At generation time the sampler starts from z_T \sim \mathcal{N}(0, I) on the latent grid and repeats a fixed loop: call the U-Net, combine the conditional and unconditional predictions with classifier-free guidance, then let the scheduler (DDIM, Euler, DPM-Solver) take one step of the reverse process. After 20 to 50 such steps the VAE decoder is called exactly once to turn the final latent into pixels, so the full path is encode → denoise → decode with the text encoder feeding every denoising step through cross-attention.

(1) VAE Codec: a convolutional encoder and decoder pair, frozen after stage-one training with an L1 plus LPIPS plus patch-GAN objective, that removes imperceptible high-frequency detail so the denoiser works on 16,384 values instead of 786,432.
(2) Text Encoder: CLIP ViT-L/14’s transformer, frozen, producing per-token hidden states rather than a single pooled vector, which is what makes word-level prompt control possible.
(3) U-Net Denoiser: the only trained diffusion component, built from ResBlocks that receive a sinusoidal timestep embedding through a learned projection added to their feature maps, interleaved with transformer blocks.
(4) Cross-Attention Is The Conditioning Interface: queries come from the latent feature map, keys and values come from the text context, so the prompt influences the image at 16 separate points in SD 1.5’s U-Net.
(5) Scheduler: not a network at all but a numerical solver for the reverse SDE or probability-flow ODE, which is why you can swap samplers and step counts on a trained checkpoint without retraining.
(6) Classifier-Free Guidance: each step runs the U-Net twice, once with the prompt and once with the empty string, and extrapolates between them, which doubles the per-step cost and is the main knob for prompt adherence.

Block diagram of Stable Diffusion generation: a text prompt enters a frozen CLIP text encoder producing a 77 by 768 context that feeds cross-attention in the U-Net denoiser; starting from Gaussian noise in a 64 by 64 by 4 latent, the U-Net output passes through classifier-free guidance and a scheduler step, loops for 20 to 50 steps, and the final latent is decoded once by the frozen VAE decoder into a 512 by 512 image

Figure 1: The loop is the architecture. The U-Net is evaluated twice per step for guidance, the text encoder runs once for the whole generation, and the decoder runs once at the end; everything inside the loop stays on the small latent grid.

Inside the U-Net the latent descends a resolution ladder, 64 \rightarrow 32 \rightarrow 16 \rightarrow 8, with channel widths 320, 640, 1280, 1280, and climbs back up with skip concatenations from the matching encoder level. Attention is deliberately not applied everywhere: in SD 1.5 the transformer blocks sit at the 64, 32, and 16 levels plus the mid block, and the deepest 8 \times 8 level is pure convolution, because self-attention cost grows as O(N^2) in the token count and the highest-resolution level already carries 4,096 tokens. SDXL rebalances exactly this: it drops attention at the finest level entirely and stacks far more transformer blocks at 32 and 16, which is how it reaches 2.6B parameters while remaining trainable at 1024 \times 1024. Each transformer block is self-attention, then cross-attention, then a GEGLU feed-forward, so spatial coherence and prompt alignment are handled by two different mechanisms in the same block.

U-shaped diagram of the SD 1.5 denoiser: four downsampling levels at 64 by 64 with 320 channels, 32 by 32 with 640, 16 by 16 with 1280 and 8 by 8 with 1280, a mid block with attention, and four upsampling levels, with dashed skip-concatenation links between matching levels and attention marked at the top three levels and the mid block only

Figure 2: The SD 1.5 U-Net applies self-attention and cross-attention at three of four resolutions and leaves the deepest 8 \times 8 level convolution-only; skip concatenations carry high-frequency structure past the bottleneck so the decoder path can restore fine detail.

Mathematical Formulation:
z_T \sim \mathcal{N}(0, I)
Q = W_Q\,\varphi(z_t)
K = W_K\,\tau(y)
V = W_V\,\tau(y)
\mathrm{Attn} = \mathrm{softmax}\!\left(\frac{QK^{\top}}{\sqrt{d}}\right)V
\hat{\epsilon}_t = \epsilon_u + s\,(\epsilon_c - \epsilon_u)
z_{t-1} = \mathrm{Step}(z_t, \hat{\epsilon}_t, t)
\hat{x} = \mathcal{D}(z_0 / s_z)

Where:

  • z_t \in \mathbb{R}^{64 \times 64 \times 4} is the latent at timestep t, and \hat{x} is the decoded image.
  • \varphi(z_t) is the flattened spatial feature map entering a transformer block, and \tau(y) is the frozen CLIP context of shape 77 \times 768.
  • W_Q, W_K, W_V are the per-layer projections and d is the head dimension; queries carry image content while keys and values carry text.
  • \epsilon_c = \epsilon_\theta(z_t, t, \tau(y)) and \epsilon_u = \epsilon_\theta(z_t, t, \tau(\varnothing)) are the conditional and unconditional noise predictions, and s is the guidance scale (typically 5 \leq s \leq 9).
  • \mathrm{Step} is the scheduler update (DDIM, Euler, DPM-Solver), applied for t descending over the chosen 20 to 50 timesteps.
  • \mathcal{D} is the frozen VAE decoder and s_z = 0.18215 the latent scaling constant; the required initial condition is z_T \sim \mathcal{N}(0, I), which only holds because s_z normalizes the latent variance.
ComponentSD 1.5SDXLSD3 / Flux
Latent codecKL-VAE, f = 8, 4 channels, about 84M parametersRetrained f = 8 VAE, still 4 channelsf = 8 VAE widened to 16 channels
Text encoderCLIP ViT-L/14, 77 x 768 contextCLIP ViT-L plus OpenCLIP ViT-bigG, concatenated to 77 x 2048 plus a pooled vectorCLIP-L, CLIP-G and T5-XXL, giving long-prompt and typography understanding
DenoiserU-Net, 860M, attention at 64, 32 and 16U-Net, 2.6B, no attention at the finest level, deep transformer stacks at 32 and 16MMDiT transformer, 2B to 12B, no convolutional U-Net at all
Conditioning pathCross-attention in 16 transformer blocksCross-attention plus pooled text, original size and crop coordinates added to the timestep embeddingJoint self-attention over concatenated text and image tokens, with separate weights per modality
Objective and native resolutionEpsilon-prediction, 512 x 512Epsilon-prediction base with a v-prediction refiner, 1024 x 1024Rectified flow matching, 1024 x 1024 with multi-aspect buckets

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 *