Chapter 18: Diffusion models

Generative Models · v1.0.2

2026-11-19 08:50:23

Where we are

Where this fits

  • The previous unit bounded an intractable likelihood with the ELBO and learned an encoder to tighten it
  • Here the encoder is fixed in advance: it adds Gaussian noise on a schedule
  • All parameters sit in the decoder, which learns to invert that process one step at a time

Outline of key topics

  1. The forward process — a fixed Markov chain from data to noise
  2. The diffusion kernel — one step to any noise level, in closed form
  3. The reverse process — the conditional that is tractable, and the decoder
  4. The diffusion ELBO — Jensen, then a telescoping cancellation
  5. The noise-prediction objective — the bound reduced to squared error
  6. Training, sampling and implementation

The shape of the model

Encoder and decoder

The encoder maps \(x\) through \(z_1 \dots z_T\) until only noise remains. The learned decoder passes back through the same variables, removing noise at each stage.

What is fixed and what is learned

Encoder — fixed

  • blends the input with white noise on a schedule \(\{\beta_t\}\)
  • no parameters
  • \(q(z_T|x)\) and \(q(z_T)\) both tend to \(\text{Norm}[0, I]\)

Decoder — learned

  • one network per step, mapping \(z_t\) back to \(z_{t-1}\)
  • carries every parameter \(\phi_{1 \dots T}\)
  • trained so each step inverts the corresponding encoder step

With no encoder network there is no posterior to collapse. The decoder alone must tighten the bound.

The forward process

Definition: the forward process

\[ \begin{aligned} z_1 &= \sqrt{1-\beta_1}\cdot x + \sqrt{\beta_1}\cdot \epsilon_1 \\[2pt] z_t &= \underbrace{\sqrt{1-\beta_t}\cdot z_{t-1}}_{\text{attenuate}} + \underbrace{\sqrt{\beta_t}\cdot \epsilon_t}_{\text{add noise}}, \qquad t = 2, \dots, T \end{aligned} \]

with \(\epsilon_t \sim \text{Norm}[0, I]\). Equivalently,

\[q(z_t|z_{t-1}) = \text{Norm}_{z_t}\!\left[\sqrt{1-\beta_t}\,z_{t-1},\ \beta_t I\right]\]

  • \(\beta_t \in [0,1]\) is the noise schedule, a hyperparameter
  • each conditional has a mean nearer zero than its input, and fixed variance \(\beta_t\)

Why the attenuation factor

Variance removed by the shrink equals variance added by the noise, so \(\text{Var}[z_t]\) stays at \(1\).

The schedule, drawn

Three values propagated through \(T = 100\) latents at \(\beta = 0.03\).

The conditionals are normal, with means pulled toward zero and variance \(\beta_t\).

A Markov chain with no parameters

\(z_t\) depends only on \(z_{t-1}\), so the joint factorises:

\[q(z_{1\dots T}|x) = q(z_1|x)\prod_{t=2}^{T} q(z_t|z_{t-1})\]

  • with enough steps every trace of \(x\) is removed and \(q(z_T|x) = \text{Norm}[0,I]\)
  • the only design decision is \(\{\beta_t\}\)
  • contrast the VAE, whose encoder was a network with weights to be fitted

The diffusion kernel

Derivation \(^1/_3\): substitute one step into the next

Take \(z_2\) and replace \(z_1\) by its definition:

\[ \begin{aligned} z_2 &= \sqrt{1-\beta_2}\cdot \class{hg-key}{z_1} + \sqrt{\beta_2}\cdot\epsilon_2 \\[4pt] &= \sqrt{1-\beta_2}\left(\class{hg-key}{\sqrt{1-\beta_1}\cdot x + \sqrt{\beta_1}\cdot\epsilon_1}\right) + \sqrt{\beta_2}\cdot\epsilon_2 \\[4pt] &= \sqrt{1-\beta_2}\sqrt{1-\beta_1}\cdot x \ +\ \class{hg-alt}{\sqrt{(1-\beta_2)\beta_1}\cdot\epsilon_1 + \sqrt{\beta_2}\cdot\epsilon_2} \end{aligned} \]

One term in \(x\), and two independent noise terms.

Derivation \(^2/_3\): collapse the two noise terms

Two independent mean-zero normals add to one, with the variances summing:

\[ \class{hg-alt}{(1-\beta_2)\beta_1 + \beta_2} = 1 - (1-\beta_1)(1-\beta_2) \]

\[ z_2 = \sqrt{(1-\beta_1)(1-\beta_2)}\cdot x + \sqrt{1 - (1-\beta_1)(1-\beta_2)}\cdot\class{hg-alt}{\epsilon} \]

Derivation \(^3/_3\): the general form

Repeating the substitution through \(z_3, z_4, \dots\) gives

\[ z_t = \sqrt{\class{hg-key}{\alpha_t}}\cdot x + \sqrt{1-\class{hg-key}{\alpha_t}}\cdot\epsilon, \qquad \class{hg-key}{\alpha_t} = \prod_{s=1}^{t}(1-\beta_s) \]

or in probabilistic form, the diffusion kernel

\[q(z_t|x) = \text{Norm}_{z_t}\!\left[\sqrt{\alpha_t}\,x,\ (1-\alpha_t)I\right]\]

Any noise level is reached in one operation, without computing \(z_1 \dots z_{t-1}\).

How \(\alpha_t\) behaves

\(\alpha_t\) is a running product of terms below one, so it decreases to zero and the kernel tends to \(\text{Norm}[0,I]\).

The kernel and the marginal

\(q(z_t|x^*)\) for \(x^* = 2\); cyan lines at \(\pm 2\) standard deviations.

The marginal integrates the kernel against the data density:

\[q(z_t) = \int q(z_t|x)\,Pr(x)\,dx\]

  • a mixture over the whole dataset
  • no closed form, because \(Pr(x)\) is unknown
  • the next section trips over exactly this

The reverse process

The reverse conditional is intractable

Bayes’ rule reverses one step:

\[q(z_{t-1}|z_t) = \frac{q(z_t|z_{t-1})\,\class{hg-alt}{q(z_{t-1})}}{\class{hg-alt}{q(z_t)}}\]

The marginals \(\class{hg-alt}{q(z_{t-1})}\) and \(\class{hg-alt}{q(z_t)}\) have no closed form, so neither does the ratio.

The true \(q(z_{t-1}|z_t^*)\) in cyan: not normal in general, though often well approximated by one.

Derivation: condition on \(x\) as well

During training \(x\) is known. Keeping it in the conditioning replaces the unknown marginals with kernels:

\[ q(z_{t-1}|z_t, \class{hg-key}{x}) = \frac{q(z_t|z_{t-1},\class{hg-key}{x})\ \class{hg-key}{q(z_{t-1}|x)}}{\class{hg-key}{q(z_t|x)}} \ \propto\ q(z_t|z_{t-1})\ \class{hg-key}{q(z_{t-1}|x)} \]

  • \(q(z_t|z_{t-1},x) = q(z_t|z_{t-1})\), because the chain is Markov
  • \(\class{hg-key}{q(z_{t-1}|x)}\) is the diffusion kernel — normal, and known

Both factors are now Gaussian, so their product is Gaussian.

The tractable conditional

\[ q(z_{t-1}|z_t, x) = \text{Norm}_{z_{t-1}}\!\left[\ \underbrace{\frac{(1-\alpha_{t-1})\sqrt{1-\beta_t}}{1-\alpha_t}z_t + \frac{\sqrt{\alpha_{t-1}}\,\beta_t}{1-\alpha_t}x}_{\text{mean}}, \quad \underbrace{\frac{\beta_t(1-\alpha_{t-1})}{1-\alpha_t}I}_{\text{variance}}\ \right] \]

  • every quantity is a hyperparameter or a known variable
  • this is the target the decoder will be trained against

Definition: the decoder

\[ \begin{aligned} Pr(z_T) &= \text{Norm}_{z_T}[0, I] \\[2pt] Pr(z_{t-1}|z_t, \phi_t) &= \text{Norm}_{z_{t-1}}\!\left[\class{hg-key}{f_t[z_t,\phi_t]},\ \sigma_t^2 I\right] \\[2pt] Pr(x|z_1, \phi_1) &= \text{Norm}_{x}\!\left[\class{hg-key}{f_1[z_1,\phi_1]},\ \sigma_1^2 I\right] \end{aligned} \]

  • \(\class{hg-key}{f_t}\) is a network computing the mean of the reverse step
  • the variances \(\sigma_t^2\) are predetermined
  • the normal form is reasonable when \(\beta_t\) is small and \(T\) is large

Generation is ancestral sampling: draw \(z_T\), then \(z_{T-1}\), down to \(x\).

The diffusion ELBO

The likelihood and its obstruction

\[ Pr(x, z_{1\dots T}|\phi) = Pr(x|z_1,\phi_1)\prod_{t=2}^{T} Pr(z_{t-1}|z_t,\phi_t)\cdot Pr(z_T) \]

The likelihood requires marginalising \(z_{1 \dots T}\) out of this product — an integral over \(T\) latent variables with no closed form.

The same obstruction as the VAE, and the same repair: bound it below.

Derivation \(^1/_3\): Jensen’s inequality

Multiply and divide by the encoder distribution, then apply Jensen:

\[ \begin{aligned} \log\!\left[Pr(x|\phi)\right] &= \log\!\left[\int \class{hg-key}{q(z_{1\dots T}|x)}\, \frac{Pr(x, z_{1\dots T}|\phi)}{\class{hg-key}{q(z_{1\dots T}|x)}}\, dz_{1\dots T}\right] \\[6pt] &\ge\ \int q(z_{1\dots T}|x)\,\class{hg-alt}{\log}\!\left[ \frac{Pr(x, z_{1\dots T}|\phi)}{q(z_{1\dots T}|x)}\right] dz_{1\dots T} \end{aligned} \]

\(q\) has no parameters, so the decoder alone must tighten the bound.

Derivation \(^2/_3\): split the logarithm

Substituting both products and separating the terms:

\[ \log\!\left[\frac{Pr(x,z_{1\dots T}|\phi)}{q(z_{1\dots T}|x)}\right] = \log\!\left[\frac{Pr(x|z_1,\phi_1)}{q(z_1|x)}\right] + \log\!\left[\frac{\prod_{t=2}^{T}Pr(z_{t-1}|z_t,\phi_t)}{\prod_{t=2}^{T}\class{hg-alt}{q(z_t|z_{t-1})}}\right] + \log\!\left[Pr(z_T)\right] \]

The denominator of the middle term is rewritten by the Markov property and Bayes’ rule:

\[ \class{hg-alt}{q(z_t|z_{t-1})} = q(z_t|z_{t-1},x) = \frac{q(z_{t-1}|z_t,x)\ \class{hg-key}{q(z_t|x)}}{\class{hg-key}{q(z_{t-1}|x)}} \]

Derivation \(^3/_3\): the ratios telescope

Each numerator cancels the denominator of the next factor. Only \(q(z_1|x)\) and \(q(z_T|x)\) survive, and \(\log\left[Pr(z_T)/q(z_T|x)\right] \approx \log 1 = 0\), because the forward process ends at a standard normal and so does the prior.

The bound that remains

\[ \text{ELBO}[\phi] \approx \underbrace{\mathbb{E}_{q(z_1|x)}\!\left[\log Pr(x|z_1,\phi_1)\right]}_{\text{reconstruction}} \ -\ \sum_{t=2}^{T} \underbrace{\mathbb{E}_{q(z_t|x)}\!\left[D_{KL}\!\left[q(z_{t-1}|z_t,x)\,\big\|\,Pr(z_{t-1}|z_t,\phi_t)\right]\right]}_{\text{one per timestep}} \]

  • the reconstruction term matches the VAE’s, estimated by one sample from \(q(z_1|x)\)
  • each remaining term compares two spherical Gaussians known in closed form

Each KL is a squared difference

Most of the KL between two Gaussians does not depend on \(\phi\), so it reduces to the distance between the means plus a constant:

\[ D_{KL}\!\left[\cdot\,\|\,\cdot\right] = \frac{1}{2\sigma_t^2}\left\| \underbrace{\frac{(1-\alpha_{t-1})\sqrt{1-\beta_t}}{1-\alpha_t}z_t + \frac{\sqrt{\alpha_{t-1}}\beta_t}{1-\alpha_t}x}_{\text{target}} \ -\ \underbrace{\class{hg-key}{f_t[z_t,\phi_t]}}_{\text{prediction}}\right\|^2 + C \]

A variational bound over \(T\) latents has become a per-step regression problem.

The reverse process, drawn

Left: five ancestral sample paths from \(Pr(z_T)\) back to \(x\). Right: the estimated \(Pr(z_{t-1}|z_t)\) in brown against the true \(q(z_{t-1}|z_t)\) in cyan.

The noise-prediction objective

Derivation \(^1/_2\): substitute for \(x\)

The kernel \(z_t = \sqrt{\alpha_t}x + \sqrt{1-\alpha_t}\,\epsilon\) rearranges to

\[x = \frac{1}{\sqrt{\alpha_t}}z_t - \frac{\sqrt{1-\alpha_t}}{\sqrt{\alpha_t}}\class{hg-alt}{\epsilon}\]

Substituting this into the target and simplifying collapses it to two terms:

\[ \frac{1-\alpha_{t-1}}{1-\alpha_t}\sqrt{1-\beta_t}\,z_t + \frac{\sqrt{\alpha_{t-1}}\beta_t}{1-\alpha_t}x \ =\ \frac{1}{\sqrt{1-\beta_t}}z_t - \frac{\beta_t}{\sqrt{1-\alpha_t}\sqrt{1-\beta_t}}\class{hg-alt}{\epsilon} \]

The target no longer mentions \(x\). It mentions \(z_t\), which the network sees, and \(\epsilon\), which it could predict.

Derivation \(^2/_2\): reparameterize the network

Replace \(f_t\) by a network \(\class{hg-key}{g_t}\) that predicts the noise:

\[ f_t[z_t,\phi_t] = \frac{1}{\sqrt{1-\beta_t}}z_t - \frac{\beta_t}{\sqrt{1-\alpha_t}\sqrt{1-\beta_t}}\class{hg-key}{g_t[z_t,\phi_t]} \]

The \(z_t\) terms in target and prediction are now identical and cancel, leaving a scaled squared difference between true and predicted noise.

The simplified loss

Dropping the per-timestep scaling factors, which is an empirical choice:

\[ L[\phi] = \sum_{i=1}^{I}\sum_{t=1}^{T} \left\| \class{hg-key}{g_t}\!\left[\sqrt{\alpha_t}\,x_i + \sqrt{1-\alpha_t}\,\epsilon_{it},\ \phi_t\right] - \epsilon_{it}\right\|^2 \]

  • corrupt an example to a random noise level
  • ask the network what noise was added
  • penalise the squared error

Nothing in this line looks probabilistic. It is the variational bound, reparameterized.

Training and sampling

Training

repeat
  for i in batch:
    t   ~ Uniform[1..T]
    eps ~ Norm[0, I]
    z_t = sqrt(a_t) x_i
          + sqrt(1-a_t) eps
    l_i = || g_t[z_t, phi_t]
            - eps ||^2
  gradient step
until converged

Sampling

z_T ~ Norm[0, I]
for t = T ... 2:
  zhat = z_t/sqrt(1-b_t)
       - b_t/(sqrt(1-a_t)
              sqrt(1-b_t))
         g_t[z_t, phi_t]
  eps  ~ Norm[0, I]
  z_{t-1} = zhat + sig_t eps
x = (same update at t=1,
     without added noise)

Training reaches any timestep in one operation; sampling runs \(T\) steps in series.

The cost asymmetry

The \(T\) evaluations are serial, each depending on the last, so they do not parallelise. This is the main practical objection to diffusion models.

Implementation and extensions

The time-conditioned U-Net

A single U-Net shared across all timesteps: downsampling for global context, skip connections for spatial detail, residual blocks with periodic global self-attention, and a sinusoidal time embedding injected at every stage.

Faster sampling

Left: the reparameterized model, stochastic at every step. Right: DDIM, which adds no noise and follows a deterministic trajectory.
  • the loss requires only that the kernel have the form \(\text{Norm}[\sqrt{\alpha_t}x, (1-\alpha_t)I]\), and a family of forward processes share it
  • a trained model can therefore be sampled under a different member without retraining
  • DDIM makes the trajectory deterministic; accelerated models define the forward process on a sub-sequence of steps
  • good samples at 50 steps rather than 1000

Conditioning and cascades

A cascade conditioned on text: \(64^2\) from a sentence embedding, super-resolved to \(256^2\) and \(1024^2\).

Classifier guidance

adds \(\sigma_t^2\,\partial \log Pr(c|z_t)/\partial z_t\) to the update; needs a classifier trained on noisy inputs

Classifier-free guidance

puts \(c\) inside \(g_t[z_t,\phi_t,c]\) and drops it at random during training, so one model does both

Summary

Summary

  • The forward process is a fixed Markov chain, \(z_t = \sqrt{1-\beta_t}\,z_{t-1} + \sqrt{\beta_t}\,\epsilon_t\), carrying no parameters
  • Composing its Gaussian steps collapses to the diffusion kernel \(q(z_t|x) = \text{Norm}[\sqrt{\alpha_t}x, (1-\alpha_t)I]\), with \(\alpha_t = \prod(1-\beta_s)\)
  • The reverse conditional \(q(z_{t-1}|z_t)\) is intractable, but conditioning on \(x\) makes it a closed-form Gaussian
  • Jensen’s inequality gives the ELBO; the interior ratios telescope, leaving a reconstruction term and one KL per timestep
  • Each KL reduces to a squared difference of means, and reparameterizing target and network turns the whole bound into \(\|g_t[z_t,\phi_t] - \epsilon\|^2\)
  • Training is one network evaluation; sampling is \(T\), in series

Where this goes next

The next unit is CLIP, and the module closes with classifier-guided and classifier-free diffusion.

  • guidance was stated here in outline: the mechanics are taken up properly there
  • conditioning on text requires a joint image-text embedding, which is what CLIP supplies
  • the cascade and the guidance weight are the two levers on sample quality