From diffusion to latent diffusion
The previous lesson showed how a diffusion model learns to reverse noise. That idea works on image pixels, but a large image contains many pixel values, so every denoising step can be expensive. Latent diffusion keeps the same broad denoising idea while moving most of the work into a smaller learned representation called a latent.
Think of the latent as a compact grid that preserves image information useful to the model without retaining every pixel directly. A variational autoencoder, or VAE, learns the bridge: its encoder maps an image to the latent grid, and its decoder maps a final latent grid back to pixels. This is why latent diffusion can spend its repeated denoising calculations on a much smaller representation.
This lesson assumes you understand forward noising and reverse denoising. Review Diffusion Models first if those terms are unfamiliar.
Latent Diffusion & Stable Diffusion in Simple Words
Pixel-space diffusion is like sculpting a statue out of individual grains of sand: calculating 50 denoising steps across 786,000 RGB pixels melts ordinary GPUs. Latent diffusion uses a VAE encoder to compress the image $64\times$ spatially into a compact latent blueprint. The heavy U-Net works only in this tiny space, and once complete, the VAE decoder blows it back up into a stunning, full-resolution photograph!
VAE Compression
Compresses 512x512 pixels down to 64x64 latent
CLIP Conditioning
Encodes prompt tokens into semantic embeddings
Latent Denoising
U-Net cross-attention clears noise efficiently
VAE Upscale
Decodes clean latent back into full RGB pixels
A traceable size example
Consider one RGB image stored as a tensor with shape [1, 3, 512, 512]. The first number is the batch size chosen for this example, 3 is supplied by the RGB colour format, and 512 × 512 is the image resolution chosen by the developer. It contains:
The multiplication is calculated from the tensor shape. It counts stored scalar values; it is not a quality score.
In one common Stable-Diffusion-style configuration, the VAE represents that image as a latent with shape [1, 4, 64, 64]. This is an architecture-specific example, not a rule for every model. The 4 learned latent channels are chosen by the architecture, while 64 × 64 results from downsampling the spatial dimensions by 8 on each side.
The example latent stores 48 times fewer scalar values. Its spatial grid has 64 times fewer locations because 8 × 8 = 64, although the channel counts differ. Fewer values generally make each repeated U-Net step cheaper; the exact speed-up also depends on architecture and hardware.
Figure 1 — Latent diffusion repeats its expensive work on a much smaller spatial grid
The dimensions are one common architecture-specific example, not a universal Stable Diffusion rule. The VAE surrounds the latent denoising loop; it is not repeated at every denoising step.
Two lanes compare pixel and latent diffusion. The pixel lane repeatedly denoises a large image tensor shaped one by three by 512 by 512 before producing an image. The latent lane passes an image through a fixed VAE encoder once, repeatedly denoises a much smaller one by four by 64 by 64 latent, then passes the final latent through a fixed VAE decoder once. Labels show 786432 pixel values versus 16384 latent values, or 48 times fewer values in this example.
Pixel-Space Diffusion vs Latent Diffusion Compute Comparison
Compressing spatial dimensions 8× reduces active tensor values by 48×, dropping inference latency by 10×!
How the text and image paths meet
| Component | Receives | Produces | Why it is needed |
|---|---|---|---|
| Tokenizer | Prompt text supplied by the user | Token IDs from a fixed vocabulary | Converts text into discrete inputs the text encoder accepts |
| Text encoder | Token IDs | Contextual token representations | Learns numerical features whose meaning depends on surrounding words |
| U-Net denoiser | Current noisy latent, timestep, and text context | A noise or related denoising prediction | Estimates what must change at the current step |
| Timestep embedding | The scheduler's current step | A learned numerical representation of noise level | Tells the U-Net whether it is handling heavy or light noise |
| Cross-attention | Image-latent features and text representations | Text-conditioned image features | Lets image locations draw information from relevant prompt tokens |
| Scheduler | Current latent, model prediction, and timestep | Updated, usually less noisy latent | Applies the selected sequence of numerical denoising updates |
| VAE decoder | Final latent | RGB image pixels | Returns the compact representation to a viewable image |
Figure 2 — A Stable-Diffusion-style text-to-image system joins text guidance with sampled latent noise
Pure text-to-image starts from sampled latent noise, not a VAE-encoded input image. The learned U-Net predicts an update; the separate scheduler calculates the next latent; the fixed VAE decoder converts only the final latent to pixels.
A user prompt passes through a fixed tokenizer and text encoder to form contextual text representations. A user-selected seed controls sampled latent noise. The noisy latent, timestep, and text representations meet in a fixed U-Net, where a cross-attention arrow carries text information into latent features. A separate scheduler calculates an updated latent and loops it through the U-Net repeatedly. The final latent passes through a fixed VAE decoder to a generated image. A separate inset shows that a VAE encoder is used for training and image-start workflows, not to start pure text-to-image generation.
Training and pure text-to-image inference are not the same path
During training
A real training image is data-supplied. The VAE encoder compresses it into a latent, sampled noise and a sampled timestep create a noisy training input, and the U-Net learns from its prediction error.
During pure text-to-image inference
There is no starting image to encode. The process normally begins with randomly sampled latent noise, repeatedly denoises it under text guidance, and uses only the VAE decoder at the end.
The VAE encoder returns when a workflow starts from an image—for example image-to-image editing or inpainting. In those cases, the supplied image must first enter the model's latent space.
Read the inference pipeline as operations
This pseudocode exposes the information flow without pretending that component names form a universal library API. The loop is inference: pretrained parameters are used, not updated.
# Conceptual pseudocode — component names and APIs vary by implementation.
tokens = tokenizer(prompt)
text_context = text_encoder(tokens)
latent = sample_random_latent(seed)
for timestep in scheduler.timesteps:
predicted_noise = unet(
noisy_latent=latent,
timestep=timestep,
text_context=text_context, # used through cross-attention
)
latent = scheduler.remove_predicted_noise(latent, predicted_noise, timestep)
image = vae_decoder(latent)What can still go wrong?
| Limitation | What the learner may observe | Underlying reason |
|---|---|---|
| VAE reconstruction loss | Fine text or tiny details become softer | Compression does not preserve every pixel perfectly |
| Text-image misalignment | An attribute attaches to the wrong object | Learned language-image associations are imperfect |
| Slow generation | A result takes many model evaluations | The scheduler requires iterative rather than one-pass denoising |
| Inherited bias | Outputs repeat stereotypes or omit groups | Training data and filtering decisions shape learned patterns |
| Prompt interpretation limits | Counting, spatial relations, or unusual compositions fail | Text conditioning guides generation but is not a symbolic scene specification |
Summary / Key Takeaways
- Latent diffusion performs repeated denoising in a compact VAE representation instead of directly across every image pixel.
- In the illustrated configuration, [1, 3, 512, 512] contains 786,432 values while [1, 4, 64, 64] contains 16,384—48 times fewer scalar values.
- During training, a real image is VAE-encoded before noise is added; pure text-to-image inference instead starts from sampled latent noise.
- The tokenizer and text encoder turn a prompt into contextual token representations used by cross-attention.
- The U-Net predicts what should change at each timestep, and the scheduler calculates the next, usually less noisy latent.
- The VAE decoder converts the final latent into RGB pixels; the encoder returns only for workflows that begin from an image.
- Compression reduces repeated work but can soften fine detail, while iterative denoising, text alignment, bias, and prompt interpretation remain limitations.