The U-Net Architecture for Diffusion

The denoising backbone: encoder-decoder with skip connections, time embedding injection, self-attention at multiple scales, cross-attention for conditioning, and the DiT (Diffusion Transformer) alternative.

Intermediate

Prerequisites

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. U-Net Structure
  5. Time Step Conditioning
  6. Skip Connections
  7. Self-Attention in U-Net
  8. Cross-Attention for Text Conditioning
  9. Diffusion Transformer (DiT)
  10. Common Pitfalls
  11. Summary
  12. Exercises

Learning Objectives

  1. Describe the encoder-decoder structure with multi-scale processing.
  2. Derive the time embedding mechanism (sinusoidal + MLP).
  3. Explain why skip connections are critical for diffusion U-Nets.
  4. Show how cross-attention enables text-to-image generation.
  5. Compare U-Net vs DiT as diffusion backbones.

Notation

  • xtRH×W×C\mathbf{x}_t \in \mathbb{R}^{H \times W \times C} — noisy input at timestep tt
  • tembRd\mathbf{t}_{\text{emb}} \in \mathbb{R}^d — time step embedding
  • cRL×dc\mathbf{c} \in \mathbb{R}^{L \times d_c} — conditioning (e.g., text embeddings)

Core Intuition

The denoising network ϵθ(xt,t,c)\boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t, \mathbf{c}) must predict noise at multiple spatial scales: large-scale structure (is this a face or a landscape?) and fine details (texture, edges). The U-Net processes information at progressively lower resolutions (encoder), captures global context, then reconstructs at full resolution (decoder), combining coarse context with fine-grained features via skip connections.

U-Net for Diffusion

512²256²128²64²BottleneckEncoder ↓Decoder ↑
Level
2
EncoderSkip connectionDecoder
Explore: U-Net downsamples to capture global context, then upsamples with skip connections preserving fine detail. Timestep and condition embeddings are injected at each level.

U-Net Structure

Encoder (downsampling):

  • Input: H×W×CH \times W \times C
  • Each level: ResBlock → ResBlock → Downsample (stride-2 convolution)
  • Feature maps grow in channels: C2C4C8CC \to 2C \to 4C \to 8C
  • Spatial resolution: HH/2H/4H/8H \to H/2 \to H/4 \to H/8

Bottleneck: ResBlock → SelfAttention → ResBlock at lowest resolution.

Decoder (upsampling):

  • Each level: Concatenate skip connection → ResBlock → ResBlock → Upsample
  • Mirrors encoder levels in reverse.
  • Output: H×W×CH \times W \times C (same as input).

ResBlock structure: GroupNorm → SiLU → Conv → GroupNorm → SiLU → Conv + skip.


Time Step Conditioning

The network must know which timestep tt it's denoising — noise level changes behavior dramatically.

Sinusoidal embedding (same as transformer positional encoding):

traw=[sin(ω1t),cos(ω1t),,sin(ωdt),cos(ωdt)].(1)\mathbf{t}_{\text{raw}} = [\sin(\omega_1 t), \cos(\omega_1 t), \ldots, \sin(\omega_d t), \cos(\omega_d t)]. \tag{1}

MLP projection: temb=MLP(traw)Rd\mathbf{t}_{\text{emb}} = \text{MLP}(\mathbf{t}_{\text{raw}}) \in \mathbb{R}^d.

Injection into ResBlocks: After the first convolution, add time embedding (broadcast across spatial dimensions):

h=h+Linear(SiLU(temb)).(2)\mathbf{h} = \mathbf{h} + \text{Linear}(\text{SiLU}(\mathbf{t}_{\text{emb}})). \tag{2}

This modulates the feature maps based on the noise level — the network learns different behavior per timestep.


Skip Connections

Why critical: Without skip connections, the decoder must reconstruct fine details from heavily downsampled (8x smaller) features. Skip connections provide direct access to encoder features at matching resolution.

Concatenation: Decoder features are concatenated with encoder features (channel-wise), then processed by convolutions that learn to combine them.

Information flow:

  • Skip: high-frequency details, edges, exact spatial positions.
  • Bottleneck: semantic content, global structure, conditioning integration.

Self-Attention in U-Net

Applied at lower resolutions (e.g., 32×3232 \times 32 or 16×1616 \times 16) where the sequence length is manageable:

SelfAttn(z)=softmax(QKTd)V,Q,K,V=Linear(z).(3)\text{SelfAttn}(\mathbf{z}) = \text{softmax}\left(\frac{\mathbf{Q}\mathbf{K}^T}{\sqrt{d}}\right)\mathbf{V}, \quad \mathbf{Q}, \mathbf{K}, \mathbf{V} = \text{Linear}(\mathbf{z}). \tag{3}

Purpose: Captures long-range spatial dependencies (e.g., symmetric structures, global color consistency) that convolutions miss.

Cost: O(H2W2)O(H^2W^2) at the applied resolution — feasible at 32×3232 \times 32 (1M\approx 1M entries) but not at 256×256256 \times 256 (4B\approx 4B entries).


Cross-Attention for Text Conditioning

In text-to-image models (Stable Diffusion), text information is injected via cross-attention:

CrossAttn(z,c)=softmax((zWQ)(cWK)Td)(cWV).(4)\text{CrossAttn}(\mathbf{z}, \mathbf{c}) = \text{softmax}\left(\frac{(\mathbf{zW}^Q)(\mathbf{cW}^K)^T}{\sqrt{d}}\right)(\mathbf{cW}^V). \tag{4}
  • Query: Spatial features from U-Net (flattened to sequence).
  • Key/Value: Text encoder output (CLIP or T5 embeddings).

Placement: After each self-attention layer in the U-Net (at selected resolutions).

Effect: Each spatial position "reads" from the text prompt, determining what content to generate at that location.


Diffusion Transformer (DiT)

Alternative backbone (Peebles & Xie, 2023): Replace U-Net entirely with a vision transformer.

Architecture:

  • Patchify input: divide noisy image into p×pp \times p patches → sequence of tokens.
  • Process with standard transformer blocks (self-attention + FFN).
  • Time and class conditioning via adaptive layer norm (adaLN):
adaLN(h,c)=γ(c)LayerNorm(h)+β(c).(5)\text{adaLN}(\mathbf{h}, \mathbf{c}) = \gamma(\mathbf{c}) \cdot \text{LayerNorm}(\mathbf{h}) + \beta(\mathbf{c}). \tag{5}

Advantages: Simpler architecture, better scaling properties (follows transformer scaling laws), no need for multi-scale design.

Used in: DALL-E 3, Sora, Stable Diffusion 3.


Common Pitfalls

Pitfall 1. Applying self-attention at full resolution. At 256×256256 \times 256: attention matrix = 4.3 billion entries. Only apply at downsampled resolutions (64×64\leq 64 \times 64).

Pitfall 2. Removing skip connections. Without them, the model loses fine-grained spatial information and produces blurry outputs.

Pitfall 3. Using the same architecture for pixel-space and latent-space diffusion. Latent space is lower resolution (typically 64×6464 \times 64 or 32×3232 \times 32), allowing self-attention at all levels.


Summary

  • U-Net: Multi-scale encoder-decoder with skip connections; standard for image diffusion.
  • Time conditioning: Sinusoidal embedding + MLP, injected via addition to ResBlock features.
  • Self-attention: Captures global dependencies at low resolutions.
  • Cross-attention: Injects text/conditioning information from external encoders.
  • DiT: Transformer alternative; simpler, scales better, dominant in latest models.

Exercises

Exercise 1. Compute the total parameters for a U-Net with channel multipliers [1, 2, 4, 8], base channels C=256C=256, and attention at the two lowest resolutions.

Exercise 2. Explain why time embedding is added (not concatenated) to spatial features in ResBlocks.

Exercise 3. For input 256×256256 \times 256: compute the self-attention cost at 32×3232 \times 32 vs 64×6464 \times 64 resolution.

Exercise 4. Compare DiT and U-Net in terms of inductive biases for image generation (locality, multi-scale, global context).

Exercise 5. Derive the number of cross-attention FLOPs for text conditioning with text length 77 and spatial resolution 32×3232 \times 32.