Latent Diffusion & Stable Diffusion
After this lesson, you will be able to:
- Say why working on a squeezed copy of the picture (the latent) makes each step about 64 times cheaper in convolution work, even though it stores only 48 times fewer values, and state the 8x spatial compression of Stable Diffusion's VAE
- Follow the pipeline from prompt to final picture: text encoder, VAE encoder, U-Net, VAE decoder
- Say why guidance doubles the number of U-Net calls per step (the Diffusion Models lesson teaches guidance itself)
- Say in one sentence how cross-attention lets the prompt steer every layer of the U-Net
Before you start
A small grid of numbers that stands for a picture. Stable Diffusion's latent is 64 by 64 cells with 4 numbers per cell.
A pair of networks. The encoder squeezes a picture into a latent, and the decoder rebuilds a picture from a latent.
Random speckle mixed into a picture. Diffusion learns to remove it a little at a time.
The network that looks at a noisy latent and guesses the noise in it. Its shape is a V: it shrinks the grid, then grows it back.
A network that turns the prompt into a list of vectors (lists of numbers) that the U-Net can use.
A step inside the U-Net where each part of the picture looks at the prompt vectors and takes what it needs.
A dial for how hard the model follows the prompt. The Diffusion Models lesson explains it; here we only count its cost.
A step is one round of guessing the noise and removing some of it. The scheduler is the rule that turns the guess into the slightly cleaner latent for the next step.
#The Pixel Problem
Before latent diffusion, models that worked directly on pixels (DDPM and Guided Diffusion) gave good results but needed a lot of computing. One 256x256 picture took minutes on a top GPU, and larger sizes were far too costly. Latent diffusion models, introduced by Rombach and colleagues in 2022, fixed this by working on the small latent instead.
#The Four Parts of Stable Diffusion
Stable Diffusion chains four parts. A text encoder turns the prompt into numbers. A VAE encoder squeezes pictures into the latent (used in training and in editing). A U-Net removes noise from the latent, one step at a time. A VAE decoder turns the final latent into a picture.
#Stage 1: Text Encoder
The text encoder turns your prompt into a list of vectors (a vector is a list of numbers). It runs once per picture. How it works, and how the U-Net reads those vectors, is in the Text Conditioning section below.
#Stage 2: VAE Encoder (Image -> Latent Space)
A pretrained VAE compresses images from pixel space (512x512x3) to latent space (64x64x4). The compression is lossy but preserves the essential visual information. The diffusion process operates entirely in this compressed space.
- Do: Lower Bottleneck Size, switch on Show Recon. Error, then drag in the latent plot to decode a point.
- Notice: A smaller bottleneck gives a blurrier reconstruction. The overall layout survives, and fine detail goes first.
- Why: A smaller code keeps fewer numbers, so the decoder must guess the rest. Drawn stand-in decoder, not a trained model.
The 8x and 48x figures above are both correct, but they count different things. The derivation separates them and shows what a U-Net actually saves. It is optional reading.
Latent values must also be on the scale the noise schedule expects. The VAE's KL penalty is deliberately weak (Rombach et al call it a slight KL-penalty), so its latents do not have unit variance. Its weight and form are derived in Autoencoders; here we only need the consequence.
Move the slider to see the derivation's claim as a picture. A latent with standard deviation 5.49 makes the real SNR 30 times the designed one, so the step where noise overtakes signal moves from step 355 to step 817. Dividing by sigma puts the two curves back on top of each other.
- Do: Press the Stable Diffusion preset, then switch the rescale toggle on and off.
- Notice: Without rescaling, noise overtakes signal at step 817 instead of the designed step 355. With it, the curves match.
- Why: A latent with spread 5.49 carries about 30 times more signal than the schedule expects. Dividing by the spread fixes it. Computed from the real schedule.
#Stage 3: U-Net Denoiser (The Core)
#Stage 4: VAE Decoder (Latent -> Pixels)
After denoising is complete (z_0 is recovered), the VAE decoder maps back to pixel space:
The VAE in Stable Diffusion was trained SEPARATELY from the U-Net. Why is this a good design choice?
Decoupling the VAE from the diffusion model is a powerful architectural decision. The VAE provides a general-purpose image compression/decompression capability that can be:
- Trained once on a large dataset and reused
- Shared across different diffusion models (SD 1.5, SDXL, etc.)
- Upgraded independently (better VAE decoders can improve all existing models)
- Used for other tasks (image editing, inpainting, super-resolution)
#Text Conditioning: Text Encoder and Cross-Attention
#Classifier-Free Guidance: The Quality Dial
The common default is s = 7.5. Higher is not better: above about 10 to 12, colours burn and the picture looks over-processed.
Try it! Open Stable Diffusion (or any free alternative like Playground AI). Generate the same image with CFG guidance scale = 1 (the plain conditional model), 7 (default), and 20 (high). Watch how scale 1 follows the prompt only loosely, while high guidance follows your prompt precisely but may look over-saturated. The sweet spot is usually 7-12.
Once you know that fact, the cost of one whole picture is simple counting. The derivation is optional reading.
#The Complete Pipeline
Read the diagram in two rows. The top row turns the prompt into vectors. The bottom row starts from a random latent, repeats the U-Net step 20 to 50 times while reading the vectors through cross-attention, then decodes the clean latent into a picture. The steps below walk through it with numbers.
#Step 1: Text Prompt "a cat on mars"
The user provides a natural language prompt — for example, "a cat on mars." This plain-text string is the only input to the entire pipeline. Everything that follows is the system translating this sentence into a photorealistic image.
#Step 2: Text Encoder
The prompt is tokenized and passed through a pretrained text encoder, such as the CLIP text encoder. Output: a sequence of vectors, one per token (77 of dimension 768 in SD 1.5). This takes about 5 ms and is cached for all denoising steps.
#Step 3: Cross-Attention
#Step 4: Diffusion in Latent Space
A random latent tensor z_T of shape 64x64x4 is sampled from N(0, I). Diffusion operates entirely in this compressed latent space — about 48x fewer values than pixel space. The U-Net does not see pixels; it sees latent features. This is the key efficiency insight of Latent Diffusion Models.
#Step 5: Denoise for T Steps
The U-Net iteratively denoises the latent tensor over 20-50 steps. At each step, it predicts noise conditioned on both the timestep and the text embeddings. Classifier-free guidance (CFG) combines a conditional prediction (with text) and an unconditional prediction (without text) to amplify prompt adherence. The scheduler computes z_(t-1) from z_t and the guided noise prediction. This loop consumes 95% of total generation time.
#Step 6: VAE Decoder
The fully denoised latent z_0 is passed through the VAE decoder, which upsamples from 64x64x4 back to 512x512x3 pixel space. This single forward pass (~20ms) adds the fine pixel-level details (textures, sharp edges, color gradients, and sub-pixel information) that the latent space had abstracted away.
#Step 7: Final Image
The output is a photorealistic 512x512 image of a cat sitting on the Martian surface. Total pipeline time: ~3 seconds on an A100 (50 steps), ~8 seconds on an RTX 3090. The seed, prompt, guidance scale, and scheduler settings can be saved and shared for exact reproduction. The image is novel — it matches the prompt but was never in the training data.
#Evolution: SD 1.4 to FLUX to Video Diffusion
The Stable Diffusion / latent-diffusion family has evolved rapidly. The table below covers the open-weights image lineage from 2022 through 2024-2025, plus the leading video-diffusion analogs. Video limits change often and some figures are undisclosed, so check each vendor's page for current limits.
| Model | Release | Backbone | Params | Resolution | Text Encoder | Key Innovation |
|---|---|---|---|---|---|---|
| SD 1.4 | Aug 2022 | U-Net | ~860M | 512x512 | CLIP ViT-L/14 | First public latent diffusion release |
| SD 1.5 | Oct 2022 | U-Net | ~860M | 512x512 | CLIP ViT-L/14 | The de-facto SD 1.x checkpoint everyone fine-tunes |
| SD 2.0 | Nov 2022 | U-Net | ~865M | 768x768 | OpenCLIP ViT-H | OpenCLIP encoder, NSFW filter, retrained data |
| SD 2.1 | Dec 2022 | U-Net | ~865M | 768x768 | OpenCLIP ViT-H | Fixes to 2.0's data filtering, v-prediction |
| SDXL 1.0 | Jul 2023 | U-Net | ~2.6B U-Net (6.6B with refiner) | 1024x1024 | CLIP-L + OpenCLIP-G | Two text encoders, base + refiner pipeline |
| SDXL Turbo | Nov 2023 | U-Net | ~2.6B | 512x512 | CLIP-L + OpenCLIP-G | ADD (adversarial distillation), 1-4 steps |
| SD 3.0 (Medium) | Jun 2024 | MM-DiT | ~2B | 1024x1024 | CLIP-L + CLIP-G + T5-XXL | First production MM-DiT, rectified flow |
| SD 3.5 (Large) | Oct 2024 | MM-DiT | ~8B | 1024x1024 | CLIP-L + CLIP-G + T5-XXL | Scale-up of 3.0, MMDiT-X variants |
| FLUX.1 [dev] | Aug 2024 | MM-DiT | ~12B | up to 2048 | T5-XXL + CLIP-L | Black Forest Labs, non-commercial license, SOTA quality |
| FLUX.1 [schnell] | Aug 2024 | MM-DiT (distilled) | ~12B | up to 2048 | T5-XXL + CLIP-L | Apache 2.0, 1-4 step distillation |
| FLUX.1 [pro] | Aug 2024 | MM-DiT | ~12B | up to 2048 | T5-XXL + CLIP-L | Closed-API only, the highest-quality FLUX tier |
| Sora | Feb 2024 / GA Dec 2024 | DiT (video latent) | ~undisclosed | 1080p, short clips (limits change) | proprietary text encoder | Video latent diffusion + spatio-temporal patches |
| Veo 2 | Dec 2024 | DiT (video latent) | undisclosed | short clips (limits change) | undisclosed | Google DeepMind, strong physics + camera control |
| Veo 3 | May 2025 | DiT (video latent) | undisclosed | short clips (limits change) | undisclosed | Native audio generation, improved prompt adherence |
The trend is clear: larger models, multiple text encoders, higher resolutions, U-Net → transformer (DiT/MM-DiT), DDPM → rectified flow, and the same architecture lifted from images to video.
#How a Diffusion Transformer Reads a Latent
#Shifting the Noise Schedule for Higher Resolution
A noise level that fully hides the layout of a small image leaves the layout of a large image visible, because the large image has many more pixels to average the noise over. The same holds for the grid of latent positions. Simple diffusion (Hoogeboom, Heek and Salimans, 2023) corrects this by shifting the schedule. The derivation is optional reading.
#CodePlayground: CFG Guidance Scale Exploration
Guidance is taught in the Diffusion Models lesson. This toy is kept here so you can check the cost story on numbers by hand.
Tests: Verify that s=0 gives mean 0 and std 1, that s=1 gives the conditional mean 2 and std 0.5, and that for s above 1 the std keeps falling (less variety).
High-Resolution Image Synthesis with Latent Diffusion Models
Robin Rombach, Andreas Blattmann, Dominik Lorenz, Patrick Esser, Bjorn Ommer (2022)
The Latent Diffusion Model paper. Moving diffusion to compressed latent space made high-resolution generation practical on consumer hardware. This architecture became Stable Diffusion.
Classifier-Free Diffusion Guidance
Jonathan Ho, Tim Salimans (2022)
Introduced the technique of training a single model for both conditional and unconditional generation, then interpolating at inference time. This simple idea dramatically improved text-to-image adherence.
#Key Takeaways
- Diffusing in latent space is far cheaper: about 48x fewer values and about 64x less convolution work for an 8x downsampling. By compressing images to a smaller latent representation via a VAE first, the U-Net operates on much smaller tensors without sacrificing perceptual quality
- Stable Diffusion chains four components. A text encoder (CLIP) converts prompts to embeddings, a VAE encoder compresses images to latent space, a U-Net denoises in latent space, and a VAE decoder reconstructs the final image
- Guidance makes the picture follow the prompt but costs two U-Net calls per step. The Diffusion Models lesson teaches how it works
- Cross-attention lets the prompt vectors steer every U-Net layer, and its cost grows only in step with the picture area
#Quick Check
Why does Stable Diffusion perform diffusion in latent space instead of pixel space?