Notes on Diffusion Models and Flow Matching
Entropy and KL Divergence
Self-information
For an event of probability \(p\), its self-information is
which is also the optimal code length (e.g. Huffman) assigned to that event. Rare events (small \(p\)) need more bits to encode.
Entropy and cross entropy
Let \(p\) be the true distribution and \(q\) a model of it. Coding samples from \(p\) gives an average code length of
using the optimal code for \(p\) and the (mismatched) optimal code for \(q\) respectively. A mismatched code is never shorter, so \(H(p,q) \ge H(p)\).
KL divergence
The extra code length caused by the mismatch is the KL divergence:
with \(D_{\text{KL}} \ge 0\), and \(D_{\text{KL}}(p \parallel q) \ne D_{\text{KL}}(q \parallel p)\).
How to memorize KL divergence
Stand in the true world and measure the true/fake ratio: in \(D_{\text{KL}}(\textbf{True} \parallel \textbf{Fake})\) the left argument is the one we take the expectation over, and inside the log truth sits on the numerator.
Varitional Autoencoders (VAEs)
Evidence Lower Bound (ELBO)
Model the observation \(x\) jointly with a latent variable \(z\). The likelihood can be recovered from \(p(x,z)\) in two ways:
Both are intractable: the first integrates over all latents, the second requires the true posterior \(p(z\mid x)\). Instead we introduce a variational distribution \(q_\phi(z \mid x)\) approximating \(p(z\mid x)\), and maximise the evidence lower bound
Prior, likelihood, evidence, posterior
- Prior \(p(z)\) β belief about the latent before seeing any data; in VAEs fixed to \(\mathcal{N}(0, I)\).
- Conditional likelihood \(p_\theta(x \mid z)\) β how well a given latent reconstructs the data; the decoder.
- Marginal likelihood (evidence) \(p(x) = \int p(x\mid z)p(z)\,\mathrm{d}z\) β total probability the model assigns to the observed data, with the latent integrated out.
- Posterior \(p(z \mid x)\) β latent (prior) inferred from the data. Intractable, approximated by the encoder \(q_\phi(z\mid x)\).
In one sentence: the encoder infers the posterior over \(z\), the decoder maximises the likelihood of \(x\) given \(z\), and the prior keeps the latent space from drifting arbitrarily.
Derivation 1 (Jensen). From the marginalisation form,
This gives the bound but hides why the gap exists.
How to memorize the Jensen's inequality
For a concave \(f\) (such as \(\log\)), \(\mathbb{E}[f(X)] \le f(\mathbb{E}[X])\): averaging first, then taking the log, is larger.

Derivation 2 (explicit gap). The key trick is that \(\log p(x)\) does not depend on \(z\), so it is a constant with respect to \(q_\phi(z\mid x)\) and can be pushed inside an expectation for free:
Starting from there and applying the chain-rule form \(p(x) = p(x,z)/p(z\mid x)\),
So the evidence equals the ELBO plus the KL between the approximate and true posterior β the term Jensen's inequality silently discarded. Two consequences:
- The bound holds because the gap is a non-negative KL.
- Maximising the ELBO is exactly equivalent to minimising KL of posterior: \(D_{\text{KL}}(q_\phi(z\mid x) \parallel p(z\mid x))\), which we cannot minimise directly since \(p(z\mid x)\) is unknown.\(\log p(x)\) is constant in \(\phi\), so
Encoderβdecoder form. Expanding \(p(x,z) = p_\theta(x\mid z)p(z)\) splits the ELBO into
where \(q_\phi(z\mid x)\) is the encoder and \(p_\theta(x\mid z)\) the decoder.
Variational Autoencoders
A VAE directly maximises the ELBO jointly over \(\phi\) (encoder) and \(\theta\) (decoder).
- The reconstruction term forces the latents to retain enough information to regenerate \(x\).
- The prior matching term keeps \(q_\phi(z\mid x)\) close to \(p(z)\), preventing the encoder from collapsing into a Dirac delta.
Why variational?
The name refers to optimising over a family of distributions: we search for the best \(q_\phi(z\mid x)\) amongst all posteriors parameterised by \(\phi\). Since \(\log p(x)\) is constant in \(\phi\), maximising the ELBO is equivalent to minimising the KL divergence between the approximate and true posterior:
Parameterisation. The encoder \(\phi\) is taken to be a diagonal Gaussian and the prior a standard Gaussian:
Why Parameterisation?
Neural networks cannot output a distribution directly. To minimize \(\ D_{\text{KL}}\big(q_\phi(z\mid x) \parallel p(z\mid x)\big)\), we need to parameterise the distributions, and supervise the networks with parameters of the distributions.
This makes the KL term analytic, while the reconstruction term is approximated by Monte Carlo with \(L\) samples \(z^{(l)} \sim q_\phi(z\mid x)\):
Reparameterisation trick. Sampling \(z^{(l)}\) is stochastic and hence non-differentiable in \(\phi\). Rewriting the sample as a deterministic function of the parameters and an external noise variable restores the gradient path:
i.e. an arbitrary Gaussian is a standard Gaussian shifted by \(\mu\) and stretched by \(\sigma\). The randomness now sits in \(\epsilon\), which carries no parameters, so \(\nabla_\phi\) flows through \(\mu_\phi\) and \(\sigma_\phi\).
Hierarchical Variational Autoencoders
An HVAE stacks \(T\) layers of latents, each generated from a more abstract one above it. In a Markovian HVAE (MHVAE) every step depends only on its immediate neighbour, so the model is just a chain of VAEs: \(q\) encodes upward, \(p\) decodes downward.

This Markov structure factorises the joint and the posterior into per-step products:
The ELBO extends unchanged, treating \(z_{1:T}\) as a single latent:
Substituting the two factorisations turns the bound into a single ratio of products:
No clean per-step KL yet
The above term is not a KL divergence, because the two densities are over different variables: \(p_\theta\) is over \(z_{t-1}\) while \(q_\phi\) is over \(z_t\). So the term stays a cross-conditional expectation.
Diffusion models repair it by using Bayes' rule to flip the encoder step into \(q(z_{t-1}\mid z_t, x)\): now both densities are over \(z_{t-1}\), the ratio inverts so that \(q\) sits on the numerator, and the expectation is over the matching distribution. Each step then becomes a genuine, individually computable KL:
This is the step that makes the VDM objective trainable, and it is the only real gap between the MHVAE bound above and the diffusion ELBO.
Variational Diffusion Models
A VDM is an MHVAE with three restrictions:
- The latent dimension equals the data dimension β so latents and data share the notation \(x_t\), with \(x_0\) the data and \(t \in [1,T]\) the noise levels.
- The encoder is not learned: each step is a fixed linear Gaussian centred on the previous state.
- The noise schedule is arranged so that the final latent \(x_T\) is a standard Gaussian.

Forward and reverse process
Forward (noising). The posterior is the same Markov chain as in the MHVAE, now written over \(x_t\):
Each step is a fixed linear Gaussian centred on the previous state:
The coefficients \(\sqrt{\alpha_t}\) and \(1-\alpha_t\) are chosen so the latents keep a similar scale β the process is variance-preserving. Note there is no \(\phi\): the encoder is a fixed corruption process, not a learned network.
Reverse (denoising). The generative chain runs the other way, starting from pure noise:
Since \(q\) carries no parameters, only the denoising transitions \(p_\theta(x_{t-1}\mid x_t)\) are learned. Sampling means drawing \(x_T \sim \mathcal{N}(0,I)\) and running them \(T\) times.
ELBO: consistency form (Deprecated)
Start from the same Jensen bound as before and substitute the two factorisations, we have:
Three readings:
- Reconstruction β same as the vanilla VAE, predicting \(x_0\) from \(x_1\).
- Prior matching β no trainable parameters; for large enough \(T\) the corrupted \(x_T\) is Gaussian and this vanishes.
- Consistency β at every intermediate \(x_t\), denoising from above (\(p_\theta(x_t\mid x_{t+1})\)) must match noising from below (\(q(x_t\mid x_{t-1})\)). This dominates the cost, since it runs over all timesteps.
Why this form is not used
The consistency term is an expectation over two random variables \(\{x_{t-1}, x_{t+1}\}\) per timestep. Summed over \(T-1\) steps, its Monte Carlo estimate has high variance.
ELBO: denoising matching form
The fix is to condition on \(x_0\): \(q(x_t\mid x_{t-1}) = q(x_t\mid x_{t-1}, x_0)\) (Markov property) and apply Bayes' rule:
Substituting this into the denominator makes the ratio \(q(x_t\mid x_0)/q(x_{t-1}\mid x_0)\) telescope β consecutive factors cancel, leaving only the two endpoints:
Now redo the derivation, this time peeling off only \(t=1\):
Every term is now an expectation over at most one random variable, which is the whole point: the Monte Carlo estimate has far lower variance than the consistency form. This is the objective actually optimised. The ground-truth denoising step \(q(x_{t-1}\mid x_t, x_0)\) acts as the supervision signal: it says how to denoise \(x_t\) given knowledge of the clean \(x_0\).
Sanity checks
Both derivations use only the Markov assumption, so they hold for any Markovian HVAE. Setting \(T=1\) recovers the vanilla VAE ELBO exactly.
Closed form denoising step
The denoising-matching term needs the ground-truth step \(q(x_{t-1}\mid x_t, x_0)\) in closed form. By Bayes' rule,
The forward step \(q(x_t\mid x_{t-1}, x_0) = q(x_t\mid x_{t-1}) = \mathcal{N}(x_t;\sqrt{\alpha_t}\,x_{t-1},(1-\alpha_t)I)\) is already given. What we still need is the marginal \(q(x_t\mid x_0)\) β how noise accumulates from the clean image directly to step \(t\).
Three Gaussian algebra rules
For independent \(X_1\sim\mathcal{N}(\mu_1,\sigma_1^2)\) and \(X_2\sim\mathcal{N}(\mu_2,\sigma_2^2)\):
- Scaling: \(aX_1 \sim \mathcal{N}(a\mu_1,\, a^2\sigma_1^2)\) β mean scales by \(a\), variance by \(a^2\) (so the std scales by \(|a|\)).
- Sum: \(X_1 + X_2 \sim \mathcal{N}(\mu_1+\mu_2,\, \sigma_1^2+\sigma_2^2)\) β means add, variances add (not stds).
- Merging two noises ("Pythagoras"): combining the above for \(\epsilon_1,\epsilon_2\sim\mathcal{N}(0,I)\),
Think of \(a,b\) as the legs of a right triangle and the merged amplitude as the hypotenuse \(\sqrt{a^2+b^2}\). This is exactly the cancellation below: \(\big(\sqrt{\alpha_t}\sqrt{1-\alpha_{t-1}}\big)^2 + \big(\sqrt{1-\alpha_t}\big)^2 = \alpha_t - \alpha_t\alpha_{t-1} + 1-\alpha_t = 1-\alpha_t\alpha_{t-1}\).
Deriving \(q(x_t\mid x_0)\). Unroll the forward recursion with the reparameterisation trick, repeatedly merging two independent Gaussian noises into one (means add, variances add). With \(\bar\alpha_t = \prod_{i=1}^{t}\alpha_i\):
so that
Assembling the posterior. All three factors are now Gaussian. Substituting into Bayes' rule and completing the square in \(x_{t-1}\) gives
with \(\Sigma_q(t) = \sigma_q^2(t)I\) and \(\sigma_q^2(t) = \dfrac{(1-\alpha_t)(1-\bar\alpha_{t-1})}{1-\bar\alpha_t}\). Both depend only on the fixed \(\alpha\) schedule, so this posterior is fully known β the perfect regression target for \(p_\theta\).
Completing the square = matching two coefficients
A Gaussian is determined by only two numbers, so its exponent β a quadratic in \(x_{t-1}\) β carries only two pieces of information. Expanding the target form makes them explicit:
So the entire derivation is: whatever multiplies \(x_{t-1}^2\) is \(\tfrac{1}{\sigma_q^2}\), and whatever multiplies \(x_{t-1}\) is \(-2\tfrac{\mu_q}{\sigma_q^2}\) β everything without an \(x_{t-1}\) is an irrelevant constant. Expanding the numerator \(q(x_t\mid x_{t-1})\,q(x_{t-1}\mid x_0)\) and collecting these two:
(the first uses \(\alpha_t\bar\alpha_{t-1} = \bar\alpha_t\)). Now \(\sigma_q^2\) is just the reciprocal of the first, and \(\mu_q = \sigma_q^2 \cdot (\text{second})\) β the shared denominator \((1-\alpha_t)(1-\bar\alpha_{t-1})\) cancels, giving the clean forms above.
From KL to a regression loss
Model \(p_\theta(x_{t-1}\mid x_t)\) as a Gaussian with the same variance \(\Sigma_q(t)\) β legitimate since all \(\alpha\) are fixed. The KL between two Gaussians of equal covariance reduces to a distance between means:
Since \(\mu_\theta\) may not condition on \(x_0\), match \(\mu_q\) by substituting a network prediction \(\hat{x}_\theta(x_t, t)\) in place of \(x_0\). Everything else cancels:
So training a VDM reduces to a neural network predicting the clean image from a noisy one. In practice the sum over \(t\) is replaced by a uniform sample: