TL;DR
A latent diffusion model does not run diffusion on the pixels of an image. It first uses an autoencoder to squeeze each image into a much smaller grid of numbers, called the latent. A diffusion model then learns to generate in that compressed grid, and a decoder turns the finished latent back into pixels. Most of the work happens on a grid dozens of times smaller than the image, so training and sampling are far cheaper, and the output quality stays high.
Where you'll see it: Stable Diffusion and most open text-to-image systems built on it, where a text prompt steers the denoising through cross-attention.
Intuition
Sketch small, then add detail
Most of the numbers in a photograph are fine texture: grain, the exact pattern of hair, leaf edges. A pixel-space diffusion model spends nearly all its effort modelling that texture, even though a person would call the image the same without it. An architect works the same way. The floor plan is drawn at small scale, and the detailed finish is applied later. Latent diffusion splits the job the same way. A first model learns a compact description of what images look like, with the imperceptible detail stripped out. A second model learns to generate those compact descriptions. A decoder then fills the detail back in.
flowchart LR X[Image 512x512x3] --> E[Encoder] E --> Z[Latent 64x64x4] C[Text prompt] --> D[Diffusion in latent space] Z --> D D --> Z0[Clean latent] Z0 --> DEC[Decoder] DEC --> Y[Generated image]
Why the smaller grid is cheaper
Every denoising step runs a neural network over every position of the grid. Cost grows with the number of values the network must process. A 512 by 512 colour image holds 786,432 numbers. The latent for the same image, compressed by a factor of 8 in each spatial direction into 4 channels, holds 16,384. That is 48 times fewer numbers for the diffusion model to handle at every one of its many steps. The expensive generative work is done on the small grid, and the cheap decoder handles the rest.
There is a catch. The decoder can only restore detail the latent kept. If the autoencoder throws away too much, no diffusion model can recover it. So the compression has to be mild enough to keep what people see, and strong enough to save real computation. Finding that balance is most of the engineering in the method.
The math
Two stages
Train the autoencoder first, with encoder and decoder . An image with shape becomes a latent with shape . The downsampling factor is , and the paper tries . The autoencoder is trained on a perceptual loss and a patch-based adversarial loss. A pixel-wise or loss alone would produce blurry reconstructions, so the adversarial term pushes each small patch to look realistic.
To keep the latent space from drifting to arbitrarily large values, the autoencoder gets one of two regularisers. The KL-regularised version adds a slight penalty pulling the latent toward a standard normal, as a Variational Autoencoder does. The VQ-regularised version quantises the latent through a codebook inside the decoder. Once this stage is done, and are frozen.
Diffusion in the latent space
The second stage is an ordinary diffusion model, but its data is rather than . The forward process corrupts the latent with Gaussian noise. Using the closed form from Diffusion Model,
The network is trained to predict the noise. The objective is
with drawn uniformly from the steps. This is the same denoising loss as pixel diffusion, with the image replaced by its latent. The denoiser is a time-conditioned U-Net built mostly from 2D convolutional layers, which suits the two-dimensional structure of the latent. Sampling starts from , runs the reverse steps to get , and decodes once: .
Conditioning with cross-attention
To control generation with text or a layout, the paper adds a cross-attention layer inside the U-Net. A domain-specific encoder turns the condition into a sequence of tokens. At each intermediate U-Net feature map, the spatial positions form queries, and the condition tokens form keys and values:
Here is the flattened intermediate feature, and , , are learned projections. This is the same Attention Mechanism used in transformers, applied between an image and a condition. Each image position decides which words of the prompt to read from.
The paper also uses unconditional guidance. The model is trained with the condition sometimes dropped, so it can also predict noise with no condition. At sampling time the two predictions are mixed with a scale :
Setting gives the plain conditional prediction. Larger pushes the sample harder toward the condition, usually at some cost in diversity.
Worked examples
Take a 512 by 512 RGB image and two downsampling factors.
- Pixel count: numbers.
- With and , the latent is numbers. The ratio is .
- With and , the latent is numbers. The ratio is .
- Interpretation: cuts the diffusion workload by a factor of 48 at every step. cuts it further, but the paper finds that overly strong compression () makes quality stagnate early and recommends in the range 4 to 16, with 4 and 8 giving the best results overall. On ImageNet, the paper reports an FID gap of 38 between pixel-space diffusion () and after 2M training steps.
Suppose one latent has two entries. The true noise is , and the network predicts .
- Squared differences: and .
- Sum: .
- Average over the two entries: .
- Interpretation: the loss is small but not zero. The gradient step moves the network toward predicting a larger negative value for the second entry. Averaged over many latents, times and noise draws, these small corrections teach the network to remove noise at every level.
At one sampling step, the unconditional prediction gives and the conditional prediction gives . Use .
- Difference between the two predictions: .
- Scaled difference: .
- Guided prediction: .
- Interpretation: the guided prediction lies beyond the conditional one, so the sample is pushed further toward what the prompt describes than the plain conditional model would go.
Tradeoffs and failure modes
| Situation | Problem | Better choice |
|---|---|---|
| Very strong compression, | The autoencoder discards detail the diffusion model can never recover, so quality stalls early | Use between 4 and 8 |
| Very mild compression, or | Training is slow, because the diffusion model still does most of the perceptual work | Use or |
| Blurry or distorted faces and text | The decoder is the bottleneck, not the diffusion model | Train a better autoencoder with a stronger perceptual or adversarial loss |
| Need exact pixel-level likelihoods | The model gives a bound on the latent, not a clean likelihood on pixels | Use a pixel model or a method built for likelihoods |
| Fine detail must be exact, such as a logo | Small errors in the autoencoder are visible after decoding | Keep the pixel path, or use a higher-fidelity autoencoder |
| Many sampling steps | Each step runs the U-Net, so sampling is still slow | Use a faster sampler such as DDIM, or distil the model |
The two-stage design has one structural weakness. The autoencoder is frozen before diffusion begins, so the diffusion loss cannot repair information the encoder dropped. If a feature is absent from the latent, it is absent from every sample.
History
Diffusion-style generative models were proposed in the mid-2010s, with Sohl-Dickstein and colleagues framing them as a learned reversal of a gradual noising process. Denoising diffusion probabilistic models, by Ho, Jain and Abbeel in 2020, made them competitive with GANs on image synthesis. The cost of pixel-space training kept high-resolution generation expensive.
Rombach and colleagues posted the latent diffusion approach in late 2021 and presented it at CVPR in 2022. Their key move was to separate perceptual compression from generative modelling, and to add cross-attention so that diffusion could be conditioned on text and other inputs. Open release of a trained text-to-image model in the summer of 2022 made the method widely used. Its name became, for most people, the name of the system it powers.
Check yourself
QuestionRecall: what does the first-stage autoencoder compress, and why is that step separate from the diffusion model?Show answer
It compresses each image into a smaller latent that keeps the perceptually important structure and drops imperceptible detail. Keeping it separate means the autoencoder is trained once, with its own reconstruction and adversarial objective, and then frozen. The diffusion model then works on a grid that is much cheaper to process at every denoising step.
QuestionComputation: a 256 by 256 RGB image is encoded with downsampling factor and channels. How many numbers does the latent hold, and what is the ratio to the pixel count?Show answer
The latent has numbers. The pixel count is . The ratio is , so the diffusion model works on a grid 12 times smaller.
QuestionWhy: why not train the autoencoder and the diffusion model jointly, end to end, on one loss?Show answer
Joint training forces a trade-off between two goals, reconstructing the image well and making the latent easy to generate, and the paper notes that earlier joint approaches needed a delicate weighting between the two. A joint denoising loss could also reward an encoder for making the latent trivially easy to predict, at the expense of reconstruction. Training the autoencoder first on its own objective and then freezing it avoids that weighting: the latent stays faithful to the image, and the diffusion model only has to learn to generate it.
QuestionApplied: a text-to-image model receives the prompt 'a red bicycle by a lake'. Where does the information about the words enter the network, and which tensor plays the role of the query?Show answer
The words are encoded by into a sequence of condition tokens. These become the keys and values of a cross-attention layer inside the U-Net. The query comes from the image features at each spatial position, so each region of the latent can attend to the words most relevant to it, such as the bicycle or the lake.
Sources
- High-Resolution Image Synthesis with Latent Diffusion Models (Rombach et al., 2021)
- Denoising Diffusion Probabilistic Models (Ho, Jain, Abbeel, 2020)
- Auto-Encoding Variational Bayes (Kingma, Welling, 2013)
- U-Net: Convolutional Networks for Biomedical Image Segmentation (Ronneberger et al., 2015)
- Stable Diffusion (Wikipedia)