Image Generation › Latent Diffusion Models
Language AI HandbookAll chapters Image Generation › Latent Diffusion ModelsOne section · Sections in this chapter4 / 14 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
A key limitation of pixel-space diffusion is computational cost. Running a U-Net over full-resolution images for hundreds of timesteps is expensive. A image has 786,432 pixels; processing this at each of 1000 timesteps during training requires enormous GPU memory and compute. Pixel-space diffusion models like the one used in DALL-E 2's decoder were trained on thousands of GPU-days.
Latent Diffusion Models (LDMs), introduced by Rombach et al. (2022) and forming the basis of Stable Diffusion, solve this by moving the diffusion process into a compressed latent space.
The approach uses a separate variational autoencoder (VAE) that:
- Encodes images to a compact latent representation: , where has spatial dimensions roughly smaller than
- Decodes latent representations back to images:
The VAE encoder-decoder is trained separately with a combination of losses. A reconstruction loss (typically L1 or L2 in pixel space) ensures the decoded image resembles the original. A perceptual loss (computing similarity in the feature space of a pre-trained VGG network rather than raw pixel space) ensures the reconstruction captures high-level semantic content, not just low-level pixel patterns. A GAN loss discriminates between real and decoded images, encouraging sharp textures and preventing blurry averaging. Additionally, a KL regularization term keeps the latent distribution close to a standard Gaussian, enabling sampling.
Once trained, the encoder-decoder is frozen. The diffusion model then operates exclusively on latent codes rather than on pixels. Because latent codes are much smaller (typically with 4 channels for a image), the U-Net processes a element feature map rather than a pixel image. This is a 64x reduction in spatial resolution, reducing the computational cost of each forward pass by roughly two orders of magnitude.
The practical consequence is significant. Models like Stable Diffusion can be trained and run on consumer GPUs with 8-24 GB of VRAM, whereas pixel-space models of comparable quality required data center hardware. This democratization of image generation through latent compression was arguably as important as the diffusion algorithm itself.
The latent space also has a useful structural property: it is smoother and more semantically organized than pixel space. Nearby points in latent space correspond to semantically similar images, which means the diffusion model can learn a smoother, more coherent distribution compared to the raw pixel distribution that contains abrupt transitions between adjacent pixel values.