Exercises — Latent diffusion (Stable Diffusion)
Before we compute anything, let us pin down the numbers that appear again and again, so no symbol is ever used unexplained.
The figure above is the mental picture behind every problem: a big pixel grid squeezed into a tiny latent grid, denoised there, then decoded back.
Level 1 — Recognition
Exercise 1.1
Stable Diffusion uses and a input image. What is the latent spatial grid size , and what is the total number of latent scalars (including channels, )?
Recall Solution
What we do: divide each spatial side by , then multiply by channels. Total latent scalars: Answer: latent grid , total numbers.
Why dividing by is the right move (intuition): the encoder is a stack of stride- convolutions; each halves the side length. Three such halvings give , so one latent cell summarises an patch of pixels. We shrink by per side precisely because neighbouring pixels are highly redundant — an block of a smooth region carries almost the same information as a single well-chosen number, so throwing away that redundancy costs little while cutting the grid dramatically.
Exercise 1.2
Which component is frozen during diffusion training, and which is being trained? Name both.
Recall Solution
- Frozen: the VAE encoder and decoder . They were pretrained separately and are only called to encode/decode.
- Trained: the diffusion U-Net , which learns to predict the added noise inside latent space (here is the text context, not a channel count). The CLIP Text Encoder is also typically frozen (used only to produce ).
Why freeze the VAE (intuition): the U-Net's whole job is to learn the distribution of clean latents . If kept changing, that distribution would move — you cannot learn to hit a target that keeps sliding. Freezing fixes the coordinate system so denoising has a stable meaning.
Level 2 — Application
Exercise 2.1
A image is encoded with , . Compute the pixel dimension, the latent dimension, and the dimension-reduction ratio.
Recall Solution
Pixel dimension (RGB, so channels): Latent dimension: side , so Ratio: Answer: fewer dimensions — the same ratio as the case, because the ratio only depends on and channel counts, not on image size:
Exercise 2.2
Given the forward process (recall is the clean latent code and is standard Gaussian noise) with , what fraction of the latent's standard-deviation budget comes from the clean signal, and what fraction from noise? (Assume and each have unit variance.)
Recall Solution
What we do: the coefficients in front of and are the amplitude weights.
- Signal amplitude: .
- Noise amplitude: .
Why these add in quadrature: variances of independent parts add, so the total variance is . The variance is fully accounted for: from signal, from noise. Answer: signal amplitude , noise amplitude ; in variance terms signal / noise.
Level 3 — Analysis
Exercise 3.1
Self-attention cost scales as where . Compare pixel-space (, ) with latent-space (, ). By what factor does the attention cost shrink? By what factor does the linear (convolution) work shrink?
Recall Solution
Linear part (scales ): Attention part (scales ): Answer: cheaper convolution, cheaper self-attention. This is exactly why latent diffusion made high-res generation feasible: the quadratic term is where pixel-space drowns.
The plot below makes the gap visual: the two operating points sit on the two curves — the latent point drops off the steep quadratic curve entirely, while the pixel point sits high up on it. Look at how far apart the magenta () and orange () curves are at the pixel point versus how close they are at the latent point.
Exercise 3.2
The CFG update combines a conditional and an unconditional noise prediction: Here is the text context, means "empty prompt" and is the guidance scale. If the unconditional prediction is and the conditional is (one representative component), compute for and .
Recall Solution
What we do: substitute into the formula. recovers the plain conditional prediction.
- :
- :
What it means: higher pushes the prediction further along the "text direction" , amplifying how strongly the prompt steers the image — at the cost of saturation/artifacts if is too large. Answer: and .
Level 4 — Synthesis
Exercise 4.1
You run DDIM Sampling with steps on a image. Each U-Net call is one "forward pass". Using CFG, each step needs two forward passes (conditional + unconditional). How many U-Net forward passes does generating one image require? How many VAE decoder calls?
Recall Solution
U-Net passes: forward passes. VAE decoder calls: the decoder is called once, only at the end on , to turn the final latent into pixels. Answer: U-Net passes, decoder call. (The encoder is called times in pure text-to-image — we start from pure noise , not from an image.)
Exercise 4.2
Design check: you want to generate a image with , . (a) What is the latent grid ? (b) Relative to the case (), by what factor does self-attention cost grow?
Recall Solution
(a) side , so . (b) attention : Answer: ; self-attention becomes more expensive than at . Doubling image area more than doubles attention cost — the quadratic bites even inside latent space.
Level 5 — Mastery
Exercise 5.1
Full pipeline trace. A user generates one image, prompt "a corgi in a spacesuit", DDIM steps, CFG scale , batch size . List in order every distinct tensor shape the data passes through, from prompt to pixels, and state which module produces each.
Recall Solution
| Stage | Module | Output shape |
|---|---|---|
| 1. Tokenise + encode text | CLIP Text Encoder (frozen) | |
| 2. Sample start noise | — | |
| 3. Each denoise step (, CFG) | U-Net | |
| 4. Update latent | DDIM rule | |
| 5. Final latent | — | |
| 6. Decode | [[Variational Autoencoders (VAE) | VAE decoder ]] (frozen) |
Key structural fact: the entire loop (steps 2–5) lives in the latent grid — the decoder is touched exactly once. The text context (shape ) enters through cross-attention: from the latent, from .
Exercise 5.2
Sanity-check the noise budget across a whole schedule. Suppose decays geometrically as over timesteps . Find (rounded to the nearest integer) at which the latent is "half signal by variance", i.e. .
Recall Solution
What we do: solve using logarithms — the natural tool for "how many multiplications of reach ?" Rounded: . Check: . ✓ Meaning: by step of this schedule, signal and noise contribute roughly equal variance — the midpoint of the corruption process.
Recall Rapid self-check (cloze)
The VAE downsampling factor in Stable Diffusion is ==, giving a == latent grid from a image. Self-attention cost scales as ==== in the number of spatial locations . Under CFG, each sampling step requires two U-Net forward passes. How many VAE decoder calls per generated image? ::: Exactly one, on the final latent . Why do variances (not amplitudes) of signal and noise add to 1? ::: Because and are independent, so . What does the symbol denote (and what does it NOT denote)? ::: The text-conditioning context from CLIP (); it is not the channel count .