Skip to content

6.8 Latent diffusion and text-to-image

ready≈ 11.5 hoursNeeds: 6.7, 4.5
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (Latent diffusion pipeline)
  4. 4 Do
  5. 5 Check

Generate somewhere smaller. A 512 × 512 RGB image has 786,432 numbers, and most of the bits describe detail that hardly matters to how it looks. Latent diffusion first trains an autoencoder (6.1, 6.2) that compresses an image by a factor f per side into a latent, 64 × 64 × 4 for f = 8, then runs diffusion or flow matching on latents and decodes only the final one (Rombach et al. 2022, section 3). The autoencoder is trained once, with a reconstruction loss and a small KL or quantisation penalty so the latent space stays tame, and frozen. They found f from 4 to 16 a good balance between cost and detail.

The denoiser is a transformer. The Diffusion Transformer (DiT) cuts the latent into p × p patches and treats each as a token, like a vision transformer, with fixed sine-cosine positions (Peebles and Xie 2023, section 3). Time and class enter through adaptive layer norm: a small network maps the condition to a shift, a scale and a gate for each sublayer, all initialised to zero (“adaLN-Zero”), so every block starts as the identity and the model starts by predicting zero.

Text. To follow a prompt, the denoiser attends to the output of a text encoder. CLIP’s was trained on image–caption pairs with a contrastive loss: in a batch, each image’s embedding should be closest to its own caption’s, and vice versa (Radford et al. 2021, section 2.3 and figure 3). Classifier-free guidance (6.7) with the prompt dropped some of the time then makes samples follow it closely.

Putting it together. Prompt → text encoder; noise latent → denoiser for N steps, called twice per step with guidance → VAE decoder → image, the conditioning set-up of Rombach et al. 2022, section 3.3 and figure 3. The notebook builds all of it at MNIST scale.

Why adaLN-Zero starts as the identity, and what CLIP's loss rewardsstep 1 of 5
  1. (γ1,β1,α1,γ2,β2,α2)=W SiLU(c)+b,W=0,  b=0(\gamma_1, \beta_1, \alpha_1, \gamma_2, \beta_2, \alpha_2) = W\,\mathrm{SiLU}(c) + b, \quad W = 0,\; b = 0

Latent diffusion pipeline

How Stable Diffusion-style models fit together

Static view on small screens. Open this page on a laptop to interact.

"a seven, handwritten"text encoder77 tokens → 77 × 768noise latent4 × 64 × 64denoiser1,024 tokensVAE decoder→ 3 × 512 × 512conditions every step× 50 steps = 100 network calls
stage
Cut into 2 × 2 patches, the latent is 1,024 tokens, so each attention layer compares 1.05 million pairs. The same patches on pixels would give 65,536 tokens and 4.29 billion pairs: 4,096× more. 50 steps × 2 (with and without the prompt, combined as v∅ + 7.5 (v − v∅)) = 100 network calls per image.

The expensive part, the denoiser, runs twice per step on the small latent, never on pixels. Try f = 1 (pixel-space diffusion) to see the attention cost explode, and f = 16 to see how small the latent gets. Larger f is cheaper but the decoder has to invent more detail, so reconstructions blur; Rombach et al. found f between 4 and 16 the useful range.

A diagram of the four stages of a text-to-image latent diffusion model, text encoder, noise latent, denoiser and VAE decoder, each labelled with its tensor shape. Buttons select a stage to explain; menus set the image side, the autoencoder’s downsampling factor, the latent channels and the patch size; a slider sets the number of sampling steps and a checkbox and slider turn on guidance and set its scale. The readout gives the numbers in the latent against the image, the tokens and attention pairs for the denoiser against the same patches on pixels, and the number of network calls per image. Keyboard: Tab to the buttons, menus and sliders.

Latent diffusion and text-to-image: edit exercises/stubs/m6_8_latent.py on your own machine. This task needs PyTorch, which the in-page runner does not have.

pip install torch   # CPU build is enough for the tests
mlc check 6.8
Hints · patchify 0/3 revealed
Hints · DiTBlock 0/3 revealed
Hints · clip_loss 0/3 revealed

Then run the lab. Open notebooks/6.8-latent-diffusion.ipynb (a GPU helps; a CPU finishes in about 15 minutes) and paste in your 6.8 file. It trains a small KL-regularised autoencoder to 4 × 7 × 7 latents, trains your DiT on the scaled latents with flow matching and dropped labels, samples digits with guidance and checks them with a classifier, counts the attention cost saved, and trains a tiny CLIP that classifies digits by caption.

1. Why run diffusion on latents instead of pixels?
2. What does the "Zero" in adaLN-Zero refer to?
3. In CLIP's loss, what plays the role of the class labels?
4. Why do latents get rescaled to unit standard deviation before diffusion?
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.