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.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- U-Net Structure
- Time Step Conditioning
- Skip Connections
- Self-Attention in U-Net
- Cross-Attention for Text Conditioning
- Diffusion Transformer (DiT)
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Describe the encoder-decoder structure with multi-scale processing.
- Derive the time embedding mechanism (sinusoidal + MLP).
- Explain why skip connections are critical for diffusion U-Nets.
- Show how cross-attention enables text-to-image generation.
- Compare U-Net vs DiT as diffusion backbones.
Notation
- — noisy input at timestep
- — time step embedding
- — conditioning (e.g., text embeddings)
Core Intuition
The denoising network 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
U-Net Structure
Encoder (downsampling):
- Input:
- Each level: ResBlock → ResBlock → Downsample (stride-2 convolution)
- Feature maps grow in channels:
- Spatial resolution:
Bottleneck: ResBlock → SelfAttention → ResBlock at lowest resolution.
Decoder (upsampling):
- Each level: Concatenate skip connection → ResBlock → ResBlock → Upsample
- Mirrors encoder levels in reverse.
- Output: (same as input).
ResBlock structure: GroupNorm → SiLU → Conv → GroupNorm → SiLU → Conv + skip.
Time Step Conditioning
The network must know which timestep it's denoising — noise level changes behavior dramatically.
Sinusoidal embedding (same as transformer positional encoding):
MLP projection: .
Injection into ResBlocks: After the first convolution, add time embedding (broadcast across spatial dimensions):
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., or ) where the sequence length is manageable:
Purpose: Captures long-range spatial dependencies (e.g., symmetric structures, global color consistency) that convolutions miss.
Cost: at the applied resolution — feasible at ( entries) but not at ( entries).
Cross-Attention for Text Conditioning
In text-to-image models (Stable Diffusion), text information is injected via cross-attention:
- 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 patches → sequence of tokens.
- Process with standard transformer blocks (self-attention + FFN).
- Time and class conditioning via adaptive layer norm (adaLN):
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 : attention matrix = 4.3 billion entries. Only apply at downsampled resolutions ().
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 or ), 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 , 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 : compute the self-attention cost at vs 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 .