These notes focus on diffusion-based generative models, like the celebrated Denoising Diffusion Probabilistic Models; the material was presented as a series of lectures I gave at some working groups of mathematicians, so the style is tailored for this audience. In particular, everything is fitted into the continuous-time framework (which is not how it is done in practice).
A special attention is given to the differences between ODE sampling and SDE sampling. The analysis of the time evolution of the densities pt is done using only Fokker-Planck Equations or Transport Equations.
Let p be a probability density on Rd. The goal of generative modelling is twofold: given samples x1,…,xn from p, we want to
learn p;
generate new samples from p.
There are various methods for tackling these challenges: Energy-Based Models, Normalizing Flows and the famous Neural ODEs, vanilla Score-Matching. However, each method has its limitations. For example, EBMs are very challenging to train, NFs lack expressivity and SM fails to capture multimodal distributions. Diffusion models offer sufficient flexibility to (partially) overcome these limitations.
Diffusion models fall into the general framework of stochastic interpolants. The central idea is to continuously transform the density p into another easy-to-sample density π (often called the target), while also transforming the samples xi from p into samples from π; and then, to reverse the process: that is, to generate a sample from π, and to inverse the transformation to get a new sample from p. In other words, we seek a path (pt:t∈[0,T]) with p0=p and pT=q, such that generating samples xt∼pt is doable.
The success of diffusion models came from the realization that some stochastic processes, such as Ornstein-Uhlenbeck processes that connect p0 with a distribution pT very close to pure noise N(0,I), can be reversed when the score function∇logpt is available at each time t. Although unknown, this score can efficiently be learnt using statistical procedures called score matching.
Let (t,x)→ft(x) and t→σt be two smooth functions. Consider the stochastic differential equation
dXt=ft(Xt)dt+2σt2dBt,X0∼p
where dBt denotes integration with respect to a Brownian motion. Under mild conditions on f, an almost-surely continuous stochastic process satisfying this SDE exists. Let pt be the probability density of Xt; it is known that this process could easily be reversed in time. More precisely, the SDE
has the same marginals as Xt reversed in time: more precisely YT−t has the same distribution as Xt, with density noted pt. This inversion needs access to ∇logpt, and we'll explain later how this can be done.
For simple functions f, the process (1) has an explicit representation. Here we focus on the case where ft(x)=−αtx for some function α, that is
dXt=−αtXt+2σt2dBt.
Define μt=∫0tαsds. Then, the solution of (3) is given by the following stochastic process: Xt=e−μtX0+2∫0teμs−μtσsdBs.
In particular, the second term reduces to a Wiener Integral; it is a centered Gaussian with variance 2∫0te2(μs−μt)σs2ds, hence
Xt=lawe−tX0+N(0,2∫0te2μs−2μtσs2ds).
In the pure Orstein-Uhlenbeck case where σt=σ and αt=1, we get μt=t and Xt=e−tX0+N(0,1−e−2t).
Proof of (4). We set F(x,t)=xeμt and Yt=F(Xt,t)=Xteμt; it turns out that Yt satisfies a nicer SDE. Since Δxf=0, ∂tf(x,t)=xeμtαt and ∇xf(x,t)=eμt, Itô's formula says that dYt=∂tF(t,Xt)dt+∂xF(t,Xt)dXt+21ΔxF(t,Xt)dt=Xteμtαtdt+eμtdXt=2σt2e2μtdBt. Consequently, Yt=Y0+∫0t2σs2e2μtdBs and the result holds.
A consequence of the preceding result is that when the variance
σˉt2=2∫0te2μs−2μtσs2ds
is big compared to e−μt, then the distribution of Xt is well-approximated by N(0,σˉt2). Indeed, for σt=1, we have σˉT=1−e−2T≈1 if T is sufficiently large.
It has recently been recognized that the Ornstein-Uhlenbeck representation of pt as in (1), as well as the stochastic process (2) that has the same marginals as pt, are not necessarily unique or special. Instead, what matters are two key features: (i) pt provides a path connecting p and pT∼N(0,I), and (ii) its marginals are easy to sample. There are many other processes besides (1) that have pt as their marginals, and that can also be reversed. The crucial point is that pt is a solution of the Fokker-Planck equation:
∂tpt(x)=Δ(σt2pt(x))−∇⋅(ft(x)pt(x)).
Just to settle the notations once and for all: ∇ is the gradient, and for a function ρ:Rd→Rd, ∇⋅ρ(x) stands for the divergence, that is ∑i=1d∂xiρ(x1,…,xd), and ∇⋅∇=Δ=∑i=1d∂xi2 is the Laplacian.
Importantly, equation (8) can be recast as a transport equation: with a velocity field defined as
Transport equations like (10) come from simple ODEs; that is, there is a deterministic process with the same marginals as (1).
Let x(t) be the solution of the differential equation with random initial condition x′(t)=−vt(x(t))x(0)=X0. Then the probability density of x(t) satisfies (10), hence it is equal to pt.
Proof. Let pt be the probability density of x(t) and let φ be any smooth, compactly supported test function. Then, E[φ(x(t))]=∫pt(x)φ(x)dx, so by derivation under the integral, ∫∂tpt(x)φ(x)dx=∂tE[φ(x(t))]=E[∇φ(x(t))x′(t)]=−∫∇φ(x)vt(x)pt(x)dx=∫φ(x)∇⋅(vt(x)pt(x))dx where the last line uses the multidimensional integration by parts formula.
Up to now, we proved that there are two continuous random processes having the same marginal probability density at time t: a smooth one provided by x(t), the solution of the ODE, and a continuous but not differentiable one, Xt, provided by the solution of the SDE.
We now have various processes x(t),Xt starting at a density p0 and evolving towards a density pT≈π=N(0,I). Can these processes be reversed in time? The answer is yes for both of them. We'll start by reversing their associated equations. From now on, we will note ptb the time-reversal of pt, that is:
ptb(x)=pT−t(x).
The density ptb solves the backward Transport Equation: ∂ptb(x)=∇⋅vtb(x)ptb(x) where
vtb(x)=−vt(x)=−σT−t2∇logpt(x)−αT−tx.
The density ptb also solves the backward Fokker-Planck Equation: ∂ptb(x)=σT−t2Δptb(x)−∇⋅wtb(x)ptb(x) where
wtb(x)=2σT−t2∇logptb(x)+αT−tx.
Proof. Noting p˙t(x) the time derivative of t↦pt(x) at time t, we immediately see that ∂tptb(x)=−p˙T−t(x) and the rest is a mere verification.
Of course, these two equations are the same, but they represent the time-evolution of the density of two different random processes. As explained before, the Transport version (14) represents the time-evolution of the density of the ODE system
y′(t)=−vtb(y(t))y(0)∼pT
while the Fokker-Planck version (16) represents the time-evolution of the SDE system
dYt=wtb(Yt)dt+2σT−t2dBtY0∼pT.
Both of these two processes can be sampled using a range of ODE and SDE solvers, the simplest of which being the Euler scheme and the Euler-Maruyama scheme. However, this requires access to the functions vtb and wtb, which in turn depend on the unknown score ∇logpt. Fortunately, ∇logpt can efficiently be estimated due to two factors.
First: we have samples from pt. Remember that our only information about p is a collection x1,…,xn of samples. But thanks to the representation (5), we can represent xti=e−μtxi+σˉtξi with ξi∼N(0,I) are samples from pt. They are extremely easy to access, since we only need to generate iid standard Gaussian variables ξi.
Second: score matching. If p is a probability density and xi are samples from p, estimating ∇logp (called score) has been thoroughly examined and is fairly doable, a technique known as score matching.
Since our goal is to learn ∇logpt(x), it is natural to choose a parametrized family of functions sθ and to optimize θ so that the divergence
∫pt(x)∣∇logpt(x)−sθ(x)∣2dx
is as small as possible. However, this optimization problem is intractable, due to the presence of the explicit form of pt inside the integral. This is where Score Matching techniques come into play.
Let p be a smooth probability density function supported over Rd and let X be a random variable with density p. The following elementary identity is due to Hyvärinen, 2005; it is the basis for score matching estimation in statistics.
Let s:Rd→Rd be any smooth function with sufficiently fast decay at ∞, and X∼p. Then, E[∣∇logp(X)−s(X)∣2]=c+E[∣s(X)∣2+2∇⋅s(X)] where c is a constant not depending on s.
Proof. We start by expanding the square norm: ∫p(x)∣∇logp(x)−s(x)∣2dx=∫p(x)∣∇logp(x)∣2dx+∫p(x)∣s(x)∣2dx−2∫∇logp(x)⋅p(x)s(x)dx. The first term does not depend on s, it is our constant c. For the last term, we use ∇logp=∇p/p then we use the integration-by-parts formula:
Now, (22) is particularly interesting for us. Remember that if we want to reverse (11), we do not really need to estimate pt but only ∇logpt. We do so by approximating it using a parametrized family of functions, say sθ (typically, a neural network):
First, we need not solve this optimization problem for every t. We could obviously discretize [0,T] with t1,…,tN and only solve for θti independently, but it is actually smarter and cheaper to approximate the whole function (t,x)→∇logpt(x) by a single neural network (a U-net, in general). That is, we use a parametrized family sθ(t,x). This enforces a form of time-continuity which seems natural. Now, since we want to aggregate the losses at each time, we solve the following problem:
where w(t) is a weighting function (for example, w(t) can be higher for t≈0, since we don't really care about approximating pT as precisely as p0).
In the preceding formulation we cannot exactly compute the expectation with respect to pt, but we can approximate it with our samples xti. Additionnaly, we need to approximate the integral, for instance we can discretize the time steps with t0=0<t1<⋯<tN=T. Our objective function becomes
which looks computable… except it's not ideal. Suppose we perform a gradient descent on θ to find the optimal θ for time t. Then at each gradient descent step, we need to evaluate sθ as well as its divergence; and then compute the gradient in θ of the divergence in x, in other words to compute ∇θ∇x⋅sθ. In high dimension, this can be too costly.
Fortunately, there is another way to perform score matching when pt is the distribution of a random variable with gaussian noise added, as in our setting. We'll present this result in a fairly abstract setting; we suppose that p is a density function, and q=p∗g where g is an other density. The following result is due to Vincent, 2010.
Denoising Score Matching Objective
Let s:Rd→Rd be a smooth function. Let X be a random variable with density p, ε an independent random variable with density g, and Xε=X+ε, whose density is pg=p∗g. Then, E[∣∇logpg(Xε)−s(Xε)∣2]=c+E[∣∇logg(ε)−s(Xε)∣2] where c is a constant not depending on s.
Proof. By the same computation as for vanilla score matching, we have
Now by definition, pg(x)=∫p(y)g(x−y)dy, hence ∇pg(x)=∫p(y)∇g(x−y)dy, and the last term above is equal to −2∫∫p(y)∇g(x−y)⋅s(x)dxdy=−2∫∫p(y)g(x−y)∇logg(x−y)⋅s(x)dydx=−2E[∇logg(ε)⋅s(X+ε)]. This last term is equal to −2E[∇logg(ε)⋅s(Xε)]. But then, upon adding and subtracting the term E[∣∇logg(ε)∣2] which does not depend on s, we get another constant c′ such that
E[∣∇logpg(X)−s(X)∣2]=c′+E[∣∇logg(ε)−s(X+ε)∣2].
Now, this Denoising Score Matching loss does not involve any computation of a « double gradient » like ∇θ∇x⋅sθ.
Let us apply this to our setting. Remember that pt is the density of e−μtX0+εt where εt∼N(0,σˉt2), hence in this case g(x)=(2πσˉt2)−d/2e−∣x∣2/2σˉt2 and ∇logg(x)=−x/σˉt2. The objective in (26) becomes
Let τ be a random time on [0,T] with density proportional to w(t); let ξ be a standard Gaussian random variable. The DDPM theoretical objective is ℓ(θ)=E[σˉτ1∣∣∣ξ−rθ(τ,e−μτX0+σˉτξ)∣∣∣2].
Since we have access to samples (xi,ξi,τi) (at the cost of generating iid samples ξi from a standard Gaussian and τi uniform over [0,T]), we get the empirical version:
ℓ^(θ)=n1i=1∑n[σˉτ1∣ξi−rθ(e−μτxi+σˉτξi)∣2].
Up to the constants and the choice of the drift αt and variance σt, this is exactly the loss function (14) from the paper DDPM, for instance.
In practice, for image generations, the go-to choice for the architecture of rθ is a U-net, a special kind of convolutional neural networks with a downsampling phase, an upsampling phase, and skip-connections in between.
Once the algorithm has converged to θ, we get sθ(t,x) which is a proxy for ∇logpt(x). Now, we simply plug this expression in the functions vtb if we want to solve the ODE (18) or wtb if we want to solve the SDE (19).
The ODE sampler solves y′(t)=−v^tb(y(t)) started at y(0)∼N(0,I), where v^tb(x)=−σT−t2sθ(T−t,x)−αT−tx.
The SDE sampler solves dYt=w^tb(Yt)dt+2σt2dBt started at Y0∼N(0,I), where w^tb(x)=2σT−t2sθ(T−t,x)+αT−tx.
We must stress a subtle fact. Equations (8) and (10), or their backward counterparts, are exactly the same equation accounting for pt. But since now we replaced ∇logpt by its approximationsθ, this is no longer the case for our two samplers: their probability densities are not the same. In fact, let us note qtode,qtsde the densities of y(t) and Yt; the first one solves a Transport Equation, the second one a Fokker-Planck equation, and these two equations are different.
Backward Equations for the samplers∂tqtode(x)=∇⋅v^tb(x)qtode(x)q0ode=π∂tqtsde(x)=∇⋅[σT−t2∇logqtsde(x)−w^tb(x)]qtsde(x)q0sde=π
Importantly, the velocity σT−t2∇logqtsde(x)−w^tb(x) is in general not equal to the velocity v^tb(x). They would be equal only in the case sθ(t,x)=∇logpt(x).
Proof. Since y(t) is an ODE, it directly satisfies the transport equation with velocity v^tb. Since Yt is an SDE, it satisfies the Fokker-Planck equation associated with the drift w^tb, which in turn can be transformed in the transport equation shown above.
Considerable work has been done (mostly experimentally) to find good functions αt,βt. Some choices seem to stand out.
the Variance Exploding path takes αt=0 (that is, no drift) and σt a continuous, increasing function over [0,1), such that σ0=0 and σ1=+∞; typically, σt=(1−t)−1.
the Variance-Preserving path takes σt=αt.
the pure Ornstein-Uhlenbeck path takes αt=σt=1, it is a special case of the previous one, mostly suitable for theoretical purposes.
Let s:[0,T]×Rd→Rd be a smooth function, meant as a proxy for ∇logpt. Our goal is to quantify the difference between the sampled densities qtode,qtsde and ptb=pT−t. It turns out that controlling the Fisher divergence E[∣∇logpt(X)−s(t,X)∣2] results in a bound for kl(p∣qTsde), but not for kl(p∣qTode).
The density of the generative process is qtsde, but we'll simply note qt. It satisfies the backward equation (37)
∂tqt(x)=∇⋅ut(x)qt(x)
where
ut(x)=σT−t2∇logqt(x)−2σT−t2s(t,x)−αT−tx.
The original distribution we want to sample is p=p0=pTb, and the output distribution of our SDE sampler is qTsde=qT. Finally, the distribution pT=p0b is approximated with π (in practice, N(0,I)).
The original proof can be found in this paper and uses the Girsanov theorem applied to the SDE representations (1)-(2) of the forward/backward process. This is utterly complicated and is too dependent on the SDE representation. The proof presented below only needs the Fokker-Planck equation and is done directly at the level of probability densities.
The following lemma is interesting on its own since it gives an exact expression for the KL divergence between transport equations.
By an integration by parts, the first term is also equal to −∫ptb(x)vtb(x)⋅∇log(ptb(x)/qt(x))dx. For the second term, it is clearly zero. Finally, for the last one, −∫ptb(x)qt(x)∇⋅(ut(x)qt(x))dx=∫∇(ptb(x)/qt(x))⋅ut(x)qt(x)dx=∫∇log(ptb(x)/qt(x))⋅ut(x)ptb(x)dx.
so that ut−vtb=σT−t2∇logqt−2σT−t2s+σT−t2∇logptb=σT−t2(∇logqt−∇logptb+2(∇logptb−s)). We momentarily note a=∇logptb(x) and b=∇logqt(x) and s=s(t,x). Then, (43) shows that dtdkl(ptb∣qt)=σT−t2∫ptb(x)(a−b)⋅((b−a)+2(s−a))dx=−σT−t2∫pt(x)∣a−b∣2dx+2σT−t2∫pt(x)(a−b)⋅(s−a)dx. We now use the classical inequality 2(x⋅y)⩽∣x∣2+∣y∣2; we get
It turns out that the ODE solver, whose density is qtode, does not have such a nice upper bound. In fact, since qtode solves a Transport Equation, we can still use (43) but with ut replaced with v^tb, and integrate in t just as in (52). We have
There is a significant difference between the score matching objective function and the SDE version. Minimizing the former does not minimize the upper bound, whereas the latter does. This disparity is due to the Fisher divergence, which does not provide control over the KL divergence between the solutions of two transport equations. However, it does regulate the KL divergence between the solutions of the associated Fokker-Planck equations, thanks to the presence of a diffusive term. This could be one of the reasons for the lower performance of ODE solvers that was observed by early experimenters in the field. However, more recent works (see the references just below) seemed to challenge this idea. With different dynamics than the Ornstein-Uhlenbeck one, deterministic sampling techniques like ODEs seem now to outperform the stochastic one. A complete understanding of these phenomena is not available yet; the outstanding paper on stochastic interpolants proposes a remarkable framework towards this task (and inspired most of the analysis in this note).