A brave soul, eager to dive into the mathematical depths of latent diffusion models! I'll provide a detailed explanation, but be warned: this will be a long and math-heavy answer. Grab a cup of coffee, get comfortable, and let's dive in!
Latent Diffusion Models (LDMs), also known as Stable Diffusion, are a type of generative model that combines the strengths of:
- Diffusion-based image synthesis (e.g., DDPM, Improved DDPM): excellent mode coverage, flexibility, and image quality.
- Variational Autoencoders (VAEs): efficient encoding and decoding of high-dimensional data into a compact, meaningful latent space.
The key innovation in LDMs is to perform diffusion in the latent space of a VAE, rather than in the pixel space. This reduces computational costs, allows for more efficient sampling, and preserves the benefits of diffusion models.
Notation
x: input image (pixel space, ℝᴰ)
z: latent representation (latent space, ℝᵈ, where ᵈ ≪ ᴰ)
ε: noise schedule (a sequence of noise vectors, ℝᵈ)
α_t, β_t, σ_t: time-dependent coefficients (scalars)
t: time step (integer, 0 ≤ t ≤ T)
p, q: probability distributions (boldface denotes densities)
θ: learnable model parameters
Background: Variational Autoencoders (VAEs)
A VAE consists of:
- Encoder (
q(z|x; θ)): maps input x to a probabilistic latent representation z ∼ 𝒩(z; μ(x), σ²(x)), where μ(x) and σ²(x) are neural network outputs.
- Decoder (
p(x|z; θ)): reconstructs x from z, typically modeled as 𝒩(x; μ(z), σ²) (simplified notation).
The VAE objective is:
ℒ_VAE(θ) = -𝔼_q(z|x; θ) [log p(x|z; θ)] + KL(q(z|x; θ) || p(z))
where p(z) is a prior (usually 𝒩(0, I)). The first term is the reconstruction loss, and the second is the KL-divergence regularizer.
Diffusion Models (DDPM)
A diffusion model progressively adds noise to the input data x₀ over T steps, creating a Markov chain:
x₀ → x₁ → … → x_T, where x_t = √(α_t) x_{t-1} + √(1-α_t) ε_t
Here, ε_t ∼ 𝒩(0, I) is a noise vector, and α_t controls the noise schedule (e.g., α_t = 0.999 for small t, α_T ≈ 0). The forward process is:
q(x_t|x_{t-1}) = 𝒩(x_t; √(α_t) x_{t-1}, (1-α_t)I)
The reverse process (denoising) is learned using a neural network p(x_{t-1}|x_t; θ):
p(x_{t-1}|x_t; θ) = 𝒩(x_{t-1}; μ_θ(x_t, t), σ_t²I)
The diffusion loss trains the reverse process:
ℒ_diff(θ) = ∑_{t=1}^T 𝔼_q(x_t|x₀) [KL(q(x_{t-1}|x_t, x₀) || p(x_{t-1}|x_t; θ))]
Latent Diffusion Models (LDMs)
Now, let's combine VAEs and diffusion models. We'll perform diffusion in the latent space z, rather than pixel space x.
Encoding: Map input x to z₀ using the VAE encoder: z₀ ∼ q(z|x; θ).
Latent Diffusion Process:
z₀ → z₁ → … → z_T, where z_t = √(α_t) z_{t-1} + √(1-α_t) ε_t
The forward process in latent space is:
q(z_t|z_{t-1}) = 𝒩(z_t; √(α_t) z_{t-1}, (1-α_t)I)
Denoising (reverse process) is performed by a neural network p(z_{t-1}|z_t; θ):
p(z_{t-1}|z_t; θ) = 𝒩(z_{t-1}; μ_θ(z_t, t), σ_t²I)
Key difference: In LDMs, μ_θ(z_t, t) is a neural network operating on latent vectors z_t, not pixel values x_t.
The latent diffusion loss is:
ℒ_ldm(θ) = ∑_{t=1}^T 𝔼_q(z_t|z₀) [KL(q(z_{t-1}|z_t, z₀) || p(z_{t-1}|z_t; θ))]
Decoder (from z₀ to x): Use the VAE decoder p(x|z₀; θ) to generate the final image.
Training Objective
The final LDM objective combines the VAE loss and the latent diffusion loss:
ℒ(θ) = ℒ_VAE(θ) + λ ℒ_ldm(θ)
where λ balances the two terms (e.g., λ = 0.1).
Sampling from LDMs
- Sample
z_T ∼ 𝒩(0, I) (prior noise).
- Run the reverse diffusion process (
T steps):
- For
t = T, T-1, ..., 1:
- Sample
z_{t-1} ∼ p(z_{t-1}|z_t; θ) = 𝒩(z_{t-1}; μ_θ(z_t, t), σ_t²I)
- Decode
z₀ to x using the VAE decoder: x ∼ p(x|z₀; θ)
Mathematical Details
For those who want to dig deeper:
- Noise schedule:
α_t, β_t, and σ_t are defined as:
α_t = 1 - β_t
β_t = β_min + (β_max - β_min) \* (t/T)
σ_t² = (1 - α_{t-1}) / (1 - α_t) \* β_t
(e.g., β_min = 0.00085, β_max = 0.012)
- KL-divergence in
ℒ_ldm:
KL(q(z_{t-1}|z_t, z₀) || p(z_{t-1}|z_t; θ)) = ... (omitted for brevity)
- Simplifies to:
0.5 \* (||μ_θ(z_t, t) - μ_q(z_t, z₀)||² / σ_t² + d \* log(σ_t²))
μ_q(z_t, z₀) = (z_t - √(1-α_t) \* ε_t) / √(α_t) (posterior mean)
- Latent diffusion reverse process:
μ_θ(z_t, t) = 1/√(α_t) \* (z_t - (1-α_t)/√(1-α_t) \* ε_θ(z_t, t))
ε_θ(z_t, t) is a neural network predicting the noise ε_t added at step t
The neural network ε_θ(z_t, t) is typically a U-Net (or similar architecture) operating on z_t, conditioned on t (e.g., via Fourier features or embedding layers).
Why LDMs work well
- Efficient encoding: VAEs compress high-dimensional
x into compact z, reducing diffusion steps' computational cost.
- Meaningful latent space:
z captures essential features and semantics, making diffusion more effective.
- Flexibility: LDMs allow various downstream applications (e.g., image editing, conditional generation) by manipulating
z.
That's a wrap! You've now seen the mathematical foundation of Latent Diffusion Models. While the equations might seem daunting, understanding the concepts will help you appreciate the elegance of combining VAEs and diffusion models.
If you're still hungry for more:
Happy diffusing!