ai theory

Working Through Every DDPM Derivation

Junyoung Park · 2023-04-02 · 9 min

Introduction

This article is devoted almost entirely to derivations. For a conceptual introduction to DDPM itself, see the previous post. Looking back, I am not sure I understood every part perfectly when I wrote that one either. The DDPM paper is here. It is hard to believe this difficult paper is already three years old.

Forward and Reverse Processes

Repeatedly add a tiny Gaussian perturbation with small variance β\beta to image x0x_0 for TT steps. The final output xTx_T approaches a Gaussian distribution with the same spatial dimensions as x0x_0. The paper schedules the variance at each step and treats those values as fixed.

Forward Process q(xtxt1)q(x_t \vert x_{t-1})

q(xtxt1)=N(xt;1βtxt1,βtI).q(x_t \vert x_{t-1}) = \mathcal{N}(x_t; \sqrt{1-\beta_t}x_{t-1}, \beta_t I).

Direct stochastic sampling is not differentiable with respect to the distribution parameters, so training uses the reparameterization trick. At every step, the previous output is scaled by 1βt\sqrt{1-\beta_t} before adding noise. The two coefficients preserve unit variance when the signal and noise each have unit variance:

(1βt)2+(βt)2=1.\left(\sqrt{1-\beta_t}\right)^2+\left(\sqrt{\beta_t}\right)^2 = 1.

Define αt=1βt\alpha_t=1-\beta_t. For ϵN(0,I)\epsilon\sim\mathcal{N}(0,I), one step is

xt=αtxt1+1αtϵ.x_t=\sqrt{\alpha_t}x_{t-1}+\sqrt{1-\alpha_t}\epsilon.

Expanding one more step for t>1t>1 gives

xt=αtαt1xt2+αt1αt1ϵt2+1αtϵt1.x_t = \sqrt{\alpha_t}\sqrt{\alpha_{t-1}}x_{t-2} + \sqrt{\alpha_t}\sqrt{1-\alpha_{t-1}}\epsilon_{t-2} + \sqrt{1-\alpha_t}\epsilon_{t-1}.

The independent Gaussian noise terms combine into another Gaussian whose variance is the sum of their variances:

αt1αt1ϵt2+1αtϵt11αtαt1ϵ.\sqrt{\alpha_t}\sqrt{1-\alpha_{t-1}}\epsilon_{t-2} +\sqrt{1-\alpha_t}\epsilon_{t-1} \sim \sqrt{1-\alpha_t\alpha_{t-1}}\epsilon.

Repeating this argument and defining αˉt=s=1tαs\bar{\alpha}_t=\prod_{s=1}^t\alpha_s yields

q(xtx0)=N(xt;αˉtx0,(1αˉt)I).q(x_t \vert x_0) = \mathcal{N}(x_t; \sqrt{\bar{\alpha}_t}x_0, (1-\bar{\alpha}_t)I).

Reparameterizing this marginal gives the formula used throughout the loss derivation:

xt:=αˉtx0+1αˉtϵ,ϵN(0,I).x_t := \sqrt{\bar{\alpha}_t} x_0 + \sqrt{1-\bar{\alpha}_t} \epsilon,\qquad \epsilon \sim \mathcal{N}(0, I).

Reverse Process pθ(xt1xt)p_\theta(x_{t-1} \vert x_t)

The learned reverse process imitates the posterior of the forward process. This is distribution prediction. Wanting the reverse direction to match the forward one should sound familiar: the approach resembles a VAE and therefore optimizes a variational lower bound.

The reverse joint from xTx_T to x0x_0 factors as

pθ(x0:T):=p(xT)t=1Tpθ(xt1xt),p_\theta(x_{0:T}) := p(x_T) \prod_{t=1}^T p_\theta(x_{t-1} \vert x_t),

with each transition modeled as

pθ(xt1xt):=N(xt1;μθ(xt, t),Σθ(xt, t)).p_\theta(x_{t-1} \vert x_t) := \mathcal{N}(x_{t-1}; \mu_{\theta}(x_t,~t), \Sigma_\theta (x_t,~t)).

The forward process adds a very small Gaussian at each step, so the reverse removes a very small Gaussian. The network can sample backward by predicting the noise at each point and subtracting it.

The loss should therefore make the learned reverse process match the known forward process.

Loss Function

Evidence Lower Bound in DDPM

A VAE negative-log-likelihood bound can be written

E[logpθ(x0)]Eq[logpθ(x0z)logpθ(z)qϕ(zx0)].\mathbb{E}[-\log p_\theta (x_0)] \leq \mathbb{E}_q \left[ -\log p_\theta(x_0 \vert z) -\log \frac{p_\theta(z)}{q_\phi(z \vert x_0)} \right].

The first term trains the decoder to reconstruct x0x_0 from zz. The second is a KL-divergence term that makes encoder posterior qϕ(zx0)q_\phi(z\mid x_0) resemble a predefined latent prior such as Gaussian pθ(z)p_\theta(z).

Rearranging the same log ratio to emphasize the generative joint,

E[logpθ(x0)]Eq[logpθ(z)logpθ(x0z)qϕ(zx0)].\mathbb{E}[-\log p_\theta (x_0)] \leq \mathbb{E}_q \left[ -\log p_\theta(z) -\log \frac{p_\theta(x_0 \vert z)}{q_\phi(z \vert x_0)} \right].

Replace latent zz with a sequence of time-indexed variables and remove a learned encoder—DDPM's forward process qq is fixed—to obtain

E[logpθ(x0)]Eq[logpθ(xT)logpθ(x0:T1xT)q(x1:Tx0)].\mathbb{E}[-\log p_\theta (x_0)] \leq \mathbb{E}_q \left[ -\log p_\theta(x_T) -\log \frac{p_\theta(x_{0:T-1} \vert x_T)}{q(x_{1:T} \vert x_0)} \right].

Both joints are Markov chains and factor over steps:

E[logpθ(x0)]Eq[logpθ(xT)logt1pθ(xt1xt)q(xtxt1)].\mathbb{E}[-\log p_\theta (x_0)] \leq \mathbb{E}_q \left[ -\log p_\theta(x_T) -\log \prod_{t \ge 1} \frac{p_\theta(x_{t-1} \vert x_t)}{q(x_t \vert x_{t-1})} \right].

Moving the product outside the logarithm turns it into a sum:

E[logpθ(x0)]Eq[logpθ(xT)t1logpθ(xt1xt)q(xtxt1)].\mathbb{E}[-\log p_\theta (x_0)] \leq \mathbb{E}_q \left[ -\log p_\theta(x_T) - \sum_{t \ge 1}\log \frac{p_\theta(x_{t-1} \vert x_t)}{q(x_t \vert x_{t-1})} \right].

Why arrange the VAE terms differently? Intuitively, we want the forward and reverse processes to match. More precisely, a VAE decoder learns an implicit conditional distribution pθ(x0z)p_\theta(x_0\mid z), whereas a diffusion decoder learns the stepwise dynamics that reverse the diffusion process.

The problem is that although the forward transition q(xtxt1)q(x_t\mid x_{t-1}) is known, its reverse posterior q(xt1xt)q(x_{t-1}\mid x_t) is intractable:

q(xt1xt)=q(xtxt1)q(xt1)q(xt).q(x_{t-1} \vert x_t) = \frac{q(x_t \vert x_{t-1})q(x_{t-1})}{q(x_t)}.

An intuitive explanation: because we know the noise added to xt1x_{t-1}, the conditional distribution of xtx_t given xt1x_{t-1} is explicitly Gaussian. But we do not know the marginal distributions of noisy states q(xt)q(x_t) and q(xt1)q(x_{t-1}), leaving two intractable terms in Bayes' rule.

Conditioning every term on the original sample x0x_0 makes the posterior tractable because accumulated Gaussian noise from x0x_0 has the closed form derived above:

q(xt1xt,x0)=q(xtxt1,x0)q(xt1,x0)q(xt,x0)=q(xtxt1,x0)q(xt1x0)q(x0)q(xtx0)q(x0)=q(xtxt1,x0)q(xt1x0)q(xtx0).\begin{aligned} q(x_{t-1} \vert x_t,x_0) &= \frac{q(x_t \vert x_{t-1},x_0)q(x_{t-1},x_0)}{q(x_t,x_0)} \newline &= \frac{q(x_t \vert x_{t-1},x_0)q(x_{t-1}\vert x_0)q(x_0)}{q(x_t \vert x_0)q(x_0)} \newline &= q(x_t \vert x_{t-1},x_0) \frac{q(x_{t-1} \vert x_0)}{q(x_t \vert x_0)}. \end{aligned}

Adding x0x_0 may appear to make this a different target from pθ(xt1xt)p_\theta(x_{t-1}\mid x_t). During training, however, x0x_0 is known and the Markov property provides the tractable posterior whose conditional mean the model can learn from xtx_t.

The decomposed negative ELBO is

DKL(q(xTx0)pθ(xT))+t>1DKL(q(xt1xt,x0)pθ(xt1xt))Eq[logpθ(x0x1)].D_{KL}(q(x_T \vert x_0) \Vert p_\theta(x_T)) +\sum_{t > 1} D_{KL} (q(x_{t-1} \vert x_t, x_0) \Vert p_\theta(x_{t-1} \vert x_t)) -\mathbb{E}_q[\log p_{\theta}(x_0 \vert x_1)].

Starting from the factorization and replacing the t>1t>1 terms with the tractable posterior gives

LEq[logpθ(xT)t=2Tlogpθ(xt1xt)q(xtxt1)logpθ(x0x1)q(x1x0)]=Eq[logpθ(xT)t=2Tlog(pθ(xt1xt)q(xt1xt,x0)q(xt1x0)q(xtx0))logpθ(x0x1)q(x1x0)]=t>1DKL(q(xt1xt,x0)pθ(xt1xt)) +Eq[logpθ(xT)logq(x1x0)q(xTx0)logpθ(x0x1)q(x1x0)]=DKL(q(xTx0)pθ(xT))+t>1DKL(q(xt1xt,x0)pθ(xt1xt)) Eq[logpθ(x0x1)].\begin{aligned} \mathcal{L} \le& \mathbb{E}_q\left[-\log p_\theta(x_T) -\sum_{t=2}^T \log \frac{p_\theta(x_{t-1} \vert x_t)}{q(x_t \vert x_{t-1})} -\log \frac{p_\theta(x_0 \vert x_1)}{q(x_1 \vert x_0)} \right] \newline =& \mathbb{E}_q\left[-\log p_\theta(x_T) -\sum_{t=2}^T \log \left( \frac{p_\theta(x_{t-1} \vert x_t)}{q(x_{t-1} \vert x_t, x_0)} \frac{q(x_{t-1} \vert x_0)}{q(x_t\vert x_0)}\right) -\log \frac{p_\theta(x_0 \vert x_1)}{q(x_1 \vert x_0)} \right] \newline =& \sum_{t > 1} D_{KL} (q(x_{t-1} \vert x_t, x_0) \Vert p_\theta(x_{t-1} \vert x_t)) \ &+\mathbb{E}_q\left[-\log p_\theta(x_T) -\log \frac{q(x_1 \vert x_0)}{q(x_T \vert x_0)} -\log \frac{p_\theta(x_0 \vert x_1)}{q(x_1 \vert x_0)} \right] \newline =& D_{KL}(q(x_T \vert x_0) \Vert p_\theta(x_T)) +\sum_{t > 1} D_{KL} (q(x_{t-1} \vert x_t, x_0) \Vert p_\theta(x_{t-1} \vert x_t)) \ &-\mathbb{E}_q[\log p_{\theta}(x_0 \vert x_1)]. \end{aligned}

From ELBO to an Objective

The expression above is still abstract. The implemented objective follows after substituting the Gaussian distributions. The ELBO has three pieces.

The first KL term is naturally satisfied by the forward diffusion schedule, which drives xTx_T toward the chosen Gaussian prior, and it contains no learned reverse parameters in the usual setup:

DKL(q(xTx0)pθ(xT)).D_{KL}(q(x_T \vert x_0) \Vert p_\theta(x_T)).

In a VAE, a similar term regularizes a learned encoder. Diffusion does not learn qq, so this term is normally omitted from the trainable loss.

The final log-likelihood term is less intuitive:

Eq[logpθ(x0x1)].\mathbb{E}_q[\log p_{\theta}(x_0 \vert x_1)].

It assigns likelihood to reconstructing clean x0x_0 from slightly noised x1x_1. The middle terms compare Gaussian distributions via KL divergence and do not require mapping the density into a discrete pixel probability. This endpoint term does.

Consider only

pθ(x0x1).p_\theta(x_0 \vert x_1).

The reverse network predicts a Gaussian distribution over every component of x0x_0:

pθ(x0x1)N(x0; μθ(x1,1),σ12).p_\theta(x_0 \vert x_1) \sim \mathcal{N}(x_0;~\mu_\theta(x_1, 1), \sigma_1^2).

To obtain the likelihood of a discrete image, integrate this Gaussian over the quantization bin for every pixel. Assume images are normalized to [1,1][-1,1]. Values below 1-1 map to the boundary pixel 1-1, values above 11 map to 11, and intermediate 8-bit values occupy bins of width approximately 2/2552/255.

Suppose the ground-truth component should be zero when mapping x1x_1 back to x0x_0.

Then pθ(x0x1)p_\theta(x_0\mid x_1) assigns the area of the predicted density around that quantized value—the yellow region below.

Multiplying over DD image components gives the joint probability:

pθ(x0x1)=i=1Dδ(x0i)δ+(x0i)N(x;μθi(x1,1),σ12)dx.p_\theta(x_0 \vert x_1) = \prod_{i=1}^D \int_{\delta_{-}(x_0^i)}^{\delta_+(x_0^i)} \mathcal{N}(x; \mu_\theta^i (x_1, 1), \sigma_1^2)\,dx.

The bin boundaries clamp the endpoints:

δ+(x)={,if x=1x+1255,if x<1,δ(x)={,if x=1x1255,if x>1.\delta_+(x) = \begin{cases} \infty,&\text{if }x=1 \newline x+\frac{1}{255},&\text{if }x<1 \end{cases},\qquad \delta_-(x) = \begin{cases} -\infty,&\text{if }x=-1 \newline x-\frac{1}{255},&\text{if }x>-1. \end{cases}

Optimizing the Intermediate Steps L1:T1L_{1:T-1}

The discussion above says that LTL_T requires no learned optimization and that L0L_0 can be evaluated through discretized Gaussian likelihood. What about the intermediate denoising terms?

t>1DKL(q(xt1xt,x0)pθ(xt1xt)).\sum_{t > 1} D_{KL} (q(x_{t-1} \vert x_t, x_0) \Vert p_\theta(x_{t-1} \vert x_t)).

We first show that q(xt1xt,x0)q(x_{t-1}\mid x_t,x_0) is Gaussian. The tractable posterior is

q(xt1xt,x0)=q(xtxt1,x0)q(xt1x0)q(xtx0).q(x_{t-1} \vert x_t,x_0) = q(x_t \vert x_{t-1},x_0) \frac{q(x_{t-1} \vert x_0)}{q(x_t \vert x_0)}.

Every factor is known in Gaussian form:

q(xtx0)N(αˉtx0,(1αˉt)I)q(xtxt1)N(αtxt1,βtI).\begin{aligned} q(x_t \vert x_0) &\sim \mathcal{N}(\sqrt{\bar{\alpha}_t}x_0, (1-\bar{\alpha}_t)I) \newline q(x_t \vert x_{t-1}) &\sim \mathcal{N}(\sqrt{\alpha_t}x_{t-1},\beta_tI). \end{aligned}

Substitute their Gaussian densities and ignore normalization constants that do not depend on xt1x_{t-1}:

q(xt1xt,x0)exp[12((xtαtxt1)2βt+(xt1αˉt1x0)21αˉt1(xtαˉtx0)21αˉt)]=exp[12(xt22αtxtxt1+αtxt12βt+xt122αˉt1x0xt1+αˉt1x021αˉt1xt22αˉtx0xt+αˉtx021αˉt)].\begin{aligned} q(x_{t-1} \vert x_t, x_0) \propto& \exp \left[-\frac{1}{2} \left( \frac{(x_t - \sqrt{\alpha_t}x_{t-1})^2}{\beta_t} + \frac{(x_{t-1} - \sqrt{\bar{\alpha}_{t-1}}x_0)^2}{1-\bar{\alpha}_{t-1}} - \frac{(x_t - \sqrt{\bar{\alpha}_t}x_0)^2}{1-\bar{\alpha}_t} \right) \right] \newline =& \exp \left[-\frac{1}{2} \left( \frac{x_t^2 - 2\sqrt{\alpha_t}x_t x_{t-1}+\alpha_tx_{t-1}^2}{\beta_t} + \frac{x_{t-1}^2 - 2\sqrt{\bar{\alpha}_{t-1}}x_0x_{t-1} + \bar{\alpha}_{t-1}x_0^2}{1-\bar{\alpha}_{t-1}} - \frac{x_t^2 -2\sqrt{\bar{\alpha}_t}x_0x_t + \bar{\alpha}_tx_0^2}{1-\bar{\alpha}_t} \right) \right]. \end{aligned}

Collect terms in xt1x_{t-1}:

exp[12((αtβt+11αˉt1)xt12(2αtxtβt+2αˉt1x01αˉt1)xt1+C(xt,x0))].\exp \left[-\frac{1}{2} \left( \left( \frac{\alpha_t}{\beta_t} +\frac{1}{1-\bar{\alpha}_{t-1}} \right)x_{t-1}^2 - \left(2\frac{\sqrt{\alpha_t}x_t}{\beta_t} + 2\frac{\sqrt{\bar{\alpha}_{t-1}}x_0}{1-\bar{\alpha}_{t-1}}\right)x_{t-1} + C(x_t, x_0) \right) \right].

Completing the square—or using the vertex b/(2a)-b/(2a)—gives posterior mean

μ~t(xt, x0)=αtxtβt+αˉt1x01αˉt1αtβt+11αˉt1=αt(1αˉt1)1αˉtxt+βtαˉt11αˉtx0,\begin{aligned} \tilde{\mu}_t(x_t,~x_0) &= \frac{\frac{\sqrt{\alpha_t}x_t}{\beta_t} + \frac{\sqrt{\bar{\alpha}_{t-1}}x_0}{1-\bar{\alpha}_{t-1}}} {\frac{\alpha_t}{\beta_t} +\frac{1}{1-\bar{\alpha}_{t-1}}} \newline &= \frac{\sqrt{\alpha_t}(1-\bar{\alpha}_{t-1})}{1-\bar{\alpha}_t}x_t + \frac{\beta_t \sqrt{\bar{\alpha}_{t-1}}}{1-\bar{\alpha}_t}x_0, \end{aligned}

and posterior variance

β~t=σ~t2=βt(1αˉt11αˉt).\tilde{\beta}_t = \tilde{\sigma}_t^2 = \beta_t \left( \frac{1-\bar{\alpha}_{t-1}}{1-\bar{\alpha}_t} \right).

The DDPM paper moves past this algebra quickly, which is why I wanted to leave the full derivation here.

The model distribution is

pθ(xt1xt)=N(xt1;μθ(xt,t),Σθ(xt,t)).p_\theta(x_{t-1}\mid x_t) =\mathcal{N}(x_{t-1};\mu_\theta(x_t,t),\Sigma_\theta(x_t,t)).

In the original simplified setup, its variance is fixed rather than learned, often to βt\beta_t or β~t\tilde{\beta}_t. The experiments report little difference between those choices. The network therefore needs to predict only μθ(xt,t)\mu_\theta(x_t,t).

With equal fixed covariance, KL divergence between the two Gaussians reduces, up to parameter-independent constant CC, to squared error between their means:

Lt1:=Eq[12σt2μ~t(xt,x0)μθ(xt,t)2]+C.L_{t-1} := \mathbb{E}_q \left[ \frac{1}{2\sigma_t^2} \left\lVert \tilde{\mu}_t(x_t, x_0) - \mu_\theta(x_t, t)\right\rVert^2 \right]+C.

To express the stochastic xtx_t differentiably, substitute

xt(x0,ϵ)=αˉtx0+1αˉtϵ.x_t(x_0,\epsilon) =\sqrt{\bar{\alpha}_t}x_0+\sqrt{1-\bar{\alpha}_t}\epsilon.

The original sample can likewise be written from xtx_t and noise:

x0=xtαˉt1αˉtαˉtϵ.x_0=\frac{x_t}{\sqrt{\bar{\alpha}_t}} -\frac{\sqrt{1-\bar{\alpha}_t}}{\sqrt{\bar{\alpha}_t}}\epsilon.

Then

Lt1C=Eq[12σt2μ~t(xt(x0,ϵ),xt(x0,ϵ)αˉt1αˉtαˉtϵ)μθ(xt(x0,ϵ),t)2].\begin{aligned} L_{t-1}-C = \mathbb{E}_q \Bigg[ \frac{1}{2\sigma_t^2}\Bigg\lVert \tilde{\mu}_t\left( x_t(x_0,\epsilon), \frac{x_t(x_0,\epsilon)}{\sqrt{\bar{\alpha}_t}} -\frac{\sqrt{1-\bar{\alpha}_t}}{\sqrt{\bar{\alpha}_t}}\epsilon \right) -\mu_\theta(x_t(x_0,\epsilon),t) \Bigg\rVert^2\Bigg]. \end{aligned}

Substitute into the posterior mean:

μ~t(xt,x0)=αt(1αˉt1)1αˉtxt+βtαˉt11αˉtx0=1αtxt(x0,ϵ)βt(1αˉt)αtϵ=1αt(xt(x0,ϵ)βt1αˉtϵ).\begin{aligned} \tilde{\mu}_t(x_t,x_0) &= \frac{\sqrt{\alpha_t}(1-\bar{\alpha}_{t-1})}{1-\bar{\alpha}_t}x_t + \frac{\beta_t\sqrt{\bar{\alpha}_{t-1}}}{1-\bar{\alpha}_t}x_0 \newline &= \frac{1}{\sqrt{\alpha_t}}x_t(x_0,\epsilon) -\frac{\beta_t}{\sqrt{(1-\bar{\alpha}_t)\alpha_t}}\epsilon \newline &= \frac{1}{\sqrt{\alpha_t}} \left(x_t(x_0,\epsilon)-\frac{\beta_t}{\sqrt{1-\bar{\alpha}_t}}\epsilon\right). \end{aligned}

The mean-matching objective is therefore

Eq[12σt21αt(xt(x0,ϵ)βt1αˉtϵ)μθ(xt(x0,ϵ),t)2].\mathbb{E}_q \left[ \frac{1}{2\sigma_t^2} \left\lVert \frac{1}{\sqrt{\alpha_t}} \left(x_t(x_0,\epsilon)-\frac{\beta_t}{\sqrt{1-\bar{\alpha}_t}}\epsilon\right) -\mu_\theta(x_t(x_0,\epsilon),t) \right\rVert^2 \right].

At time tt, xtx_t is already the network input. The unknown part of the forward construction is the added noise ϵ\epsilon, so parameterize the model mean through a noise predictor ϵθ\epsilon_\theta:

μθ(xt,t)=1αt(xtβt1αˉtϵθ(xt,t)).\mu_\theta(x_t, t) = \frac{1}{\sqrt{\alpha_t}} \left(x_t - \frac{\beta_t}{\sqrt{1-\bar{\alpha}_t}} \epsilon_\theta(x_t, t)\right).

Sampling xt1x_{t-1} uses this predicted mean plus the fixed variance term:

xt1=1αt(xtβt1αˉtϵθ(xt,t))+σtz,zN(0,I).x_{t-1} = \frac{1}{\sqrt{\alpha_t}} \left(x_t - \frac{\beta_t}{\sqrt{1-\bar{\alpha}_t}} \epsilon_\theta(x_t, t)\right)+\sigma_t z,\qquad z\sim\mathcal{N}(0,I).

Finally, substituting the mean parameterization reduces the intermediate objective to weighted noise-prediction error:

Ex0,ϵ[βt22σt2αt(1αˉt)ϵϵθ(αˉtx0+1αˉtϵ,t)2].\mathbb{E}_{x_0,\epsilon} \left[ \frac{\beta_t^2}{2\sigma_t^2 \alpha_t(1-\bar{\alpha}_t)} \left\lVert \epsilon - \epsilon_\theta(\sqrt{\bar{\alpha}_t}x_0 + \sqrt{1-\bar{\alpha}_t}\epsilon, t) \right\rVert^2\right].

This is the derivation behind the familiar DDPM noise-prediction objective.