Skip to content
HasanAI
Log in Sign up

🌫️ Diffusion Models: DDPM and Stable Diffusion

Learn to remove noise, then start from pure noise
Ten Landmark Papers Advanced
Contents

    Diffusion Models: DDPM (Ho et al., 2020) and Stable Diffusion

    A diffusion model learns to generate data by learning to remove noise. Take a training image and add a little Gaussian noise, then a little more, for a thousand steps, until nothing but noise is left. That direction needs no learning. A network is trained to undo one step of it. To generate, start from pure noise and apply the network a thousand times.

    Denoising Diffusion Probabilistic Models (Ho, Jain & Abbeel, 2020) made this work, building on Sohl-Dickstein et al. (2015). It reached a state-of-the-art FID of 3.17 on CIFAR-10, with an objective as simple as "predict the noise". Stable Diffusion (Rombach et al., 2022, latent diffusion) made it cheap enough for high-resolution images by running the same process in a compressed latent space.

    1. The forward process, and the shortcut that jumps to any step
    2. The training objective: predict the noise
    3. Training a denoiser on 2-D data and sampling from it
    4. Conditioning and classifier-free guidance
    5. From DDPM to Stable Diffusion
    helpers ready

    1. The forward process

    Each step shrinks the signal slightly and adds a little noise:

    $$q(x_t \mid x_{t-1}) = \mathcal{N}\big(\sqrt{1-\beta_t}\,x_{t-1},\ \beta_t I\big)$$

    DDPM uses $T = 1000$ steps with $\beta_t$ rising linearly from $10^{-4}$ to $0.02$. Because each step is Gaussian, a thousand of them compose into one Gaussian. With $\alpha_t = 1-\beta_t$ and $\bar\alpha_t = \prod_{s\le t}\alpha_s$:

    $$q(x_t \mid x_0) = \mathcal{N}\big(\sqrt{\bar\alpha_t}\,x_0,\ (1-\bar\alpha_t)\,I\big) \quad\Longleftrightarrow\quad x_t = \sqrt{\bar\alpha_t}\,x_0 + \sqrt{1-\bar\alpha_t}\,\varepsilon$$

    That shortcut is what makes training cheap: you can jump straight to any noise level without simulating the steps in between. The cell checks it by actually running the thousand-step chain.

    ==============================================================================
      THE 1000-STEP CHAIN vs THE ONE-SHOT FORMULA
    ==============================================================================
      t =    1:  abar = 0.999900   signal sqrt(abar) = 0.9999   noise sqrt(1-abar) = 0.0100
      t =  100:  abar = 0.897018   signal sqrt(abar) = 0.9471   noise sqrt(1-abar) = 0.3209
      t =  250:  abar = 0.524085   signal sqrt(abar) = 0.7239   noise sqrt(1-abar) = 0.6899
      t =  500:  abar = 0.078587   signal sqrt(abar) = 0.2803   noise sqrt(1-abar) = 0.9599
      t = 1000:  abar = 0.000040   signal sqrt(abar) = 0.0064   noise sqrt(1-abar) = 1.0000
    
    Start every chain at x0 = 1.7 and simulate all steps
    ------------------------------------------------------------------------------
      t =  100:  simulated mean +1.6105 var 0.1032   formula mean +1.6101 var 0.1030
      t =  500:  simulated mean +0.4787 var 0.9228   formula mean +0.4766 var 0.9214
      t = 1000:  simulated mean +0.0089 var 0.9996   formula mean +0.0108 var 1.0000
      [PASS] simulated chain matches q(x_t | x_0) at every checkpoint (within 0.01)
      [PASS] by t = 1000 the signal is gone: sqrt(abar_T) < 0.01

    2. What the network learns: predict the noise

    The reverse step $q(x_{t-1} \mid x_t)$ is intractable in general, but it is nearly Gaussian when $\beta_t$ is small. DDPM parameterises it with a network $\varepsilon_\theta(x_t, t)$ that predicts the noise that was added. After the variational bound is simplified, the loss that works best is just

    $$L_{\text{simple}} = \mathbb{E}_{t,\,x_0,\,\varepsilon}\Big[\big\|\varepsilon - \varepsilon_\theta\big(\sqrt{\bar\alpha_t}\,x_0 + \sqrt{1-\bar\alpha_t}\,\varepsilon,\ t\big)\big\|^2\Big]$$

    Training is: pick an image, pick a random $t$, add noise in one shot, and regress the noise. There is no adversary, no ELBO with a KL to balance, and no sampling loop during training.

    Here the "images" are points in 2-D, drawn from two interlocking half-moons, so that every step can be watched. The denoiser is an MLP that takes $(x_t, t)$ and also a class label, which section 4 uses. The schedule is the paper's, compressed to $T = 200$ steps.

    ==============================================================================
      TRAINING A DENOISER ON TWO MOONS
    ==============================================================================
      T = 200, beta from 0.0005 to 0.0996,  sqrt(abar_T) = 0.00563
      step    1:  L_simple = 0.9921
      step  500:  L_simple = 0.2576
      step 3000:  L_simple = 0.1954
      step 12000:  L_simple = 0.1988
      (a model that always predicts zero noise would score 1.0)

    3. Sampling: start from noise, denoise 200 times

    DDPM's Algorithm 2. Draw $x_T \sim \mathcal{N}(0, I)$, then for $t = T, \dots, 1$:

    $$x_{t-1} = \frac{1}{\sqrt{\alpha_t}}\Big(x_t - \frac{\beta_t}{\sqrt{1-\bar\alpha_t}}\,\varepsilon_\theta(x_t, t)\Big) + \sigma_t z, \qquad z \sim \mathcal{N}(0, I)$$

    with $\sigma_t^2 = \tilde\beta_t = \frac{1-\bar\alpha_{t-1}}{1-\bar\alpha_t}\beta_t$, the variance of the true one-step posterior. The paper reports that this choice and $\sigma_t^2 = \beta_t$ give similar results. Each step removes a little of the predicted noise and puts back a smaller amount of fresh noise.

    ==============================================================================
      SAMPLING
    ==============================================================================
      mean distance from a point to the nearest training point:
        fresh real data (reference)   0.0214
        diffusion samples             0.0398
        plain Gaussian noise          0.2846
      samples are 7.1x closer to the data than noise is, and 1.9x as far as fresh real data
      [PASS] samples sit on the data manifold: at least 3x closer to the data than Gaussian noise
      share of samples on each moon (nearest training label): 0.504 / 0.496

    4. Conditioning, and classifier-free guidance

    To generate a particular kind of output, such as "the left moon" here or "a red bicycle" in Stable Diffusion, feed a condition $c$ to the denoiser. During training the condition was dropped 20% of the time and replaced by a "no label" token. So the same network can predict both the conditional noise $\varepsilon_\theta(x_t, c)$ and the unconditional noise $\varepsilon_\theta(x_t, \varnothing)$.

    Classifier-free guidance (Ho & Salimans, 2022) extrapolates away from the unconditional prediction:

    $$\hat\varepsilon = \varepsilon_\theta(x_t, \varnothing) + w\,\big(\varepsilon_\theta(x_t, c) - \varepsilon_\theta(x_t, \varnothing)\big)$$

    $w = 0$ ignores the condition, $w = 1$ is plain conditioning, and $w > 1$ pushes harder toward it. That trades diversity for fidelity, and it is the "guidance scale" slider in every image generator.

    ==============================================================================
      GUIDANCE SCALE: FIDELITY vs DIVERSITY
    ==============================================================================
      w = 0    on requested moon 0.508   spread 0.700   off the data (> 0.25 from any point) 0.023
      w = 1    on requested moon 1.000   spread 0.708   off the data (> 0.25 from any point) 0.000
      w = 3    on requested moon 1.000   spread 0.584   off the data (> 0.25 from any point) 0.000
      w = 7    on requested moon 1.000   spread 0.662   off the data (> 0.25 from any point) 0.137
      [PASS] guidance raises fidelity: w = 3 is purer than w = 0
      [PASS] moderate guidance costs diversity: spread at w = 3 is below w = 1
      [PASS] too much guidance leaves the data: w = 7 puts more samples off the manifold than w = 1

    The four panels show the whole trade-off. With no guidance ($w = 0$) the condition is ignored and half the samples land on the wrong moon. At $w = 1$ every sample lands on the requested moon. At $w = 3$ the samples crowd into the middle of the arc: higher fidelity, less diversity. At $w = 7$ the extrapolation overshoots. The guided direction $\varepsilon_c - \varepsilon_u$ is multiplied so strongly that samples are thrown off the data entirely, into regions no training point occupies. That is the 2-D version of the over-saturated, distorted images an image generator produces at very high guidance scales, and it is why the usual default is a moderate value (around 7 for Stable Diffusion, whose scale is defined the same way).

    5. From DDPM to Stable Diffusion

    DDPM on pixels is expensive. Every one of the hundreds of denoising steps runs a U-Net over every pixel. Latent diffusion (Rombach et al., 2022), released as Stable Diffusion, changed where the diffusion happens:

    1. A VAE-style autoencoder, trained once, compresses a 512Γ—512Γ—3 image to a 64Γ—64Γ—4 latent (downsampling factor $f = 8$).
    2. Diffusion runs in that latent space, with the same forward process and the same noise-prediction loss as above.
    3. The denoiser is a U-Net with cross-attention to a text embedding (from a CLIP text encoder), so the prompt steers every step. Classifier-free guidance strengthens it.
    4. The decoder turns the final latent back into pixels.

    Faster samplers such as DDIM (Song et al., 2021) cut the number of steps from 1,000 to 20–50.

    ==============================================================================
      WHAT LATENT DIFFUSION SAVES
    ==============================================================================
      pixel space:  512 x 512 x 3 = 786,432 numbers
      latent space:  64 x  64 x 4 = 16,384 numbers
      48x fewer numbers per denoising step
      steps: DDPM 1,000  ->  DDIM ~50   (20x fewer)

    Summary

    • The forward process adds Gaussian noise over $T$ steps, and $q(x_t \mid x_0)$ has a closed form. Simulating all 1,000 steps matched it to within 0.01.
    • Training is regression: noise a sample to a random $t$ in one shot and predict the noise ($L_{\text{simple}}$). It is stable, with no adversary.
    • Sampling runs the learned reverse step from pure noise. On 2-D data the samples landed on the data manifold: 7Γ— closer to the training data than noise, though still about 2Γ— further than fresh real data.
    • Training with the condition sometimes dropped enables classifier-free guidance. Moving from $w = 0$ to $1$ to $3$ took samples from 51% to 100% on the requested class and then concentrated them. At $w = 7$ guidance overshot and threw samples off the data, which is the failure mode behind over-guided images.
    • Stable Diffusion = this process in a VAE's latent space (48Γ— fewer numbers at 512 px), with text cross-attention and guidance.

    References

    1. Denoising Diffusion Probabilistic Models β€” Ho et al., 2020 (NeurIPS)
    2. High-Resolution Image Synthesis with Latent Diffusion Models β€” Rombach et al., 2022 (CVPR)
    3. Classifier-Free Diffusion Guidance β€” Ho & Salimans, 2022
    4. Deep Unsupervised Learning using Nonequilibrium Thermodynamics β€” Sohl-Dickstein et al., 2015 (ICML)
    5. Denoising Diffusion Implicit Models β€” Song et al., 2021 (ICLR)