6.8 Latent diffusion and text-to-image
1. Learn
Section titled “1. Learn”- MIT 6.S184 course sitelecture 4 video, lab 3 "A Conditional Generative Model for Images" (classifier-free guidance and a DiT), and lecture notes §6.1–6.3 (DiT, latent space, the SD3 case study)
- Rombach et al., High-Resolution Image Synthesis with Latent Diffusion Modelssections 3–3.3 and figure 3
- Peebles and Xie, Scalable Diffusion Models with Transformers (DiT)section 3 and figure 3
- Radford et al., Learning Transferable Visual Models From Natural Language Supervision (CLIP)section 2.3 and figures 1 and 3
2. Bridge
Section titled “2. Bridge”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.
3. Explore
Section titled “3. Explore”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.8Then 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.
5. Check
Section titled “5. Check”- not yet: All tests pass (0/?)
- not yet: Checkpoint quiz ≥ 80%
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.