๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #305

Latent Diffusion

Latent diffusion makes one crucial efficiency change: instead of running the entire, expensive diffusion process directly on full-resolution pixel images, first compress images into a much smaller latent space, and run diffusion there instead โ€” this single idea is what makes Stable Diffusion practical to run on consumer hardware.

The Core Idea

\[ \mathbf{z}_0 = \text{Encoder}(\mathbf{x}_0) \qquad \text{(compress the image, once)} \] \[ \text{diffusion process runs entirely on } \mathbf{z}, \text{ not } \mathbf{x} \] \[ \hat{\mathbf{x}}_0 = \text{Decoder}(\hat{\mathbf{z}}_0) \qquad \text{(decompress back to pixels, once, at the very end)} \]

The Encoder and Decoder here are exactly a VAE, from Variational Autoencoder โ€” trained separately (and typically kept frozen during diffusion training), compressing images into a smaller spatial latent representation while preserving the perceptually important information needed to reconstruct a visually similar image.

Why This Matters โ€” The Compute Savings

Recall the quadratic attention cost from Context Window โ€” the same principle applies here to spatial resolution: a diffusion process running directly on, say, 512ร—512 pixel images is vastly more expensive (in both compute and memory) than one running on a much smaller, compressed latent representation โ€” commonly reduced 8x or more per spatial dimension (e.g. down to a 64ร—64 latent grid). Since the entire iterative denoising loop (potentially dozens to hundreds of steps) runs in this smaller space, the total compute savings across the whole generation process are enormous.

Diagram

Image (pixel space) Latent z All diffusion steps run HERE (small, cheap) Generated Image

Encoding and decoding happen once each, at the start and end; the expensive, many-step diffusion loop runs entirely in the much smaller latent space.

Code

from diffusers import AutoencoderKL, UNet2DConditionModel
import torch

vae = AutoencoderKL.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="vae")
unet = UNet2DConditionModel.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="unet")

image = torch.rand(1, 3, 512, 512)   # a full-resolution image
with torch.no_grad():
    latent = vae.encode(image).latent_dist.sample()
print(latent.shape)   # (1, 4, 64, 64) -- a MUCH smaller representation the diffusion process actually works on

# The U-Net (noise predictor) operates entirely on this 64x64 latent, not the 512x512 image

Common Mistakes

  • Assuming the VAE encoder/decoder are trained jointly with the diffusion U-Net โ€” they're typically trained separately, beforehand, and then kept frozen while the diffusion process is trained purely in the resulting fixed latent space.
  • Confusing "latent diffusion" with an entirely different generative technique โ€” it's the exact same forward/reverse diffusion process from earlier notes in this category, just relocated to run on compressed latents instead of raw pixels.

Interview Relevance

Q: "Why does running diffusion in a compressed latent space, rather than directly on pixels, matter so much practically?" The entire iterative denoising process โ€” potentially dozens to hundreds of sequential steps โ€” has to run once per generated image. Running it on a much smaller latent representation (commonly 8x smaller per spatial dimension) instead of full-resolution pixels dramatically reduces the compute and memory cost of every single one of those steps, making high-quality image generation practical on far more modest hardware.

Practice Question

If an image is compressed from 512ร—512 pixels to a 64ร—64 latent grid, roughly how much smaller (in total spatial elements) is the representation the diffusion process actually operates on?

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Latent Diffusion โ€“ FAQs

Quick answers about learning Latent Diffusion in Deep Learning.

This free note from CodingNow 2.0 explains Latent Diffusion in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Latent Diffusion, is 100% free with no signup required.
With focused practice, most students grasp Latent Diffusion in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now