Latent diffusion inference (SD 1.5)

I saw the launch announcement of Google’s Nano Banana 2, and this piqued my interest in image generation models. These are models that take a textual prompt, and with a few optional reference images, generate a new image. The main criteria are cost of generation, speed of generation, prompt adherence, consistency of the characters in the image, and style manipulation ability.

The most popular type of image generation model is a latent diffusion model based on a Diffusion Transformer (DiT) for noise prediction. Earlier, a U-Net was used for noise prediction instead.

High-level process

  • Start with a latent tensor of noise.
  • Loop:
    • Predict the noise conditioned on the prompt.
    • Remove some of the noise.
  • Decode the latent into a full image.

Overall architecture of a latent diffusion model during inference

  1. Create a latent of shape 4x64x64 and initialize it with random Gaussian noise.
  2. Embed the input text prompt using a text encoder (CLIP in SD 1.5; newer models also add a larger encoder like T5). This produces the conditioning embedding.
  3. Run a loop of 20-50 iterations:
    • Feed the current latent and conditioning embedding to a noise prediction model (a U-Net in SD 1.5; a DiT in newer models).
    • The model predicts the noise present in the latent.
    • Use a sampler like DDIM to remove some of the noise.
    • The latent is now a bit closer to adhering to the prompt.
  4. Pass the latent through the decoder (the second half of the VAE) to upsample it back to a full-resolution image (512x512x3 for SD 1.5).

A couple of notes:

  • This trick of running two predictions per step — one conditioned on the prompt embedding and one on a null (empty) prompt — is called classifier-free guidance (CFG). The two predictions are extrapolated apart by a guidance scale (around 7.5) to push the result toward better prompt adherence.
  • Looping on the small latent tensor, rather than on full-resolution pixels, is what makes this approach practically useful and fast enough. That is the key idea behind latent diffusion.