Sam Babichenko

Reading notes / Diffusion models

Score-based diffusion

Song et al., 2021

Context. SMLD (Song and Ermon, 2019) and DDPM (Ho et al., 2020) both corrupt data with Gaussian noise and learn to undo it, SMLD by score matching at a sequence of noise levels with Langevin sampling, DDPM by a reverse Markov chain whose network predicts the added noise ε\varepsilon. They learn the same object. Denoising score matching says that regressing on the conditional score ∇log⁡p(xt∣x0)\nabla\log p(x_t\mid x_0) has the same minimizer as regressing on the marginal score, and Tweedie's formula makes that score a posterior mean: for xt=αtx0+σtεx_t=\alpha_tx_0+\sigma_t\varepsilon, ∇log⁡pt(x)=αt E[x0∣xt=x]−xσt2=−E[ε∣xt=x]σt,\nabla\log p_t(x)=\frac{\alpha_t\,\mathbb E[x_0\mid x_t=x]-x}{\sigma_t^2}=-\frac{\mathbb E[\varepsilon\mid x_t=x]}{\sigma_t}, so DDPM's ε\varepsilon-network is the score up to the factor −1/σt-1/\sigma_t. This paper lets the number of noise levels go to infinity: the corruption becomes an SDE, sampling becomes its time reversal, and the same field also drives a deterministic ODE with the same marginals. One field, the score of the noised marginals at every time, is all that sampling, exact likelihoods and conditioning need, and it is learned by plain least-squares denoising, so generative modeling becomes a denoising regression at every noise level.

Setting. A fixed Itô SDE dx=f(x,t) dt+g(t) dwdx=f(x,t)\,dt+g(t)\,dw on [0,T][0,T], with no trainable parameters, carries data x(0)∼p0x(0)\sim p_0 to a marginal pTp_T close to a tractable prior, and its affine drift makes the perturbation kernel p0t(x(t)∣x(0))p_{0t}(x(t)\mid x(0)) Gaussian in closed form. The variance-exploding (VE) SDE dx=d[σ2(t)]/dt dwdx=\sqrt{d[\sigma^2(t)]/dt}\,dw is the limit of SMLD's sequence of noise levels; the variance-preserving (VP) SDE dx=−12β(t)x dt+β(t) dwdx=-\tfrac12\beta(t)x\,dt+\sqrt{\beta(t)}\,dw is the limit of DDPM's chain; the new sub-VP SDE has squared diffusion coefficient β(t)(1−e−2∫0tβ)\beta(t)\big(1-e^{-2\int_0^t\beta}\big), so its covariance stays below VP's. Computations run on [ϵ,T][\epsilon,T] with small ϵ\epsilon, because the VE SDE is undefined at t=0t=0 and the VP kernel variance vanishes as t→0t\to0.

Main results. The paper proves no theorems; it assembles classical identities into a method and tests it. A time-dependent network sθ(x,t)s_\theta(x,t) is trained on min⁡θ Et{λ(t) Ex(0)Ex(t)∣x(0)∥sθ(x(t),t)−∇x(t)log⁡p0t(x(t)∣x(0))∥2},t∼U[0,T],\min_\theta\ \mathbb E_t\Big\{\lambda(t)\,\mathbb E_{x(0)}\mathbb E_{x(t)\mid x(0)}\big\|s_\theta(x(t),t)-\nabla_{x(t)}\log p_{0t}(x(t)\mid x(0))\big\|^2\Big\},\qquad t\sim U[0,T], with λ(t)∝1/E∥∇log⁡p0t∥2\lambda(t)\propto1/\mathbb E\|\nabla\log p_{0t}\|^2, and then plugged into the reverse-time SDE to sample, into the probability-flow ODE for deterministic sampling, encoding and exact likelihoods, or into a conditional reverse SDE for class-conditional generation, inpainting and colorization from one unconditional model. On unconditional CIFAR-10 the VE model NCSN++ reaches FID 2.20, better than the best conditional GAN then reported (2.42), and the sub-VP model DDPM++ reaches 2.99 bits/dim on uniformly dequantized CIFAR-10. VE gives the better samples, VP and sub-VP the better likelihoods.

Step 1

The score is learnable by denoising, and DDPM's loss is a case of it. The conditional score is unbiased for the marginal one, E[∇log⁡p0t(x(t)∣x(0)) ∣ x(t)]=∇log⁡pt(x(t))\mathbb E\big[\nabla\log p_{0t}(x(t)\mid x(0))\,\big|\,x(t)\big]=\nabla\log p_t(x(t)), by differentiating pt=∫p0 p0tp_t=\int p_0\,p_{0t} under the integral. So the objective equals E∥sθ−∇log⁡pt∥2\mathbb E\|s_\theta-\nabla\log p_t\|^2 plus a constant free of θ\theta, and with enough capacity its population minimizer is the marginal score. Because the kernel is Gaussian the target is explicit, ∇log⁡p0t=−ε/σt\nabla\log p_{0t}=-\varepsilon/\sigma_t, and λ=σt2\lambda=\sigma_t^2 equalizes the noise levels. With sθ=−εθ/σts_\theta=-\varepsilon_\theta/\sigma_t the weighted loss becomes E∥εθ(xt,t)−ε∥2\mathbb E\|\varepsilon_\theta(x_t,t)-\varepsilon\|^2, DDPM's LsimpleL_{\rm simple} (its weight 1−αi1-\alpha_i is σi2\sigma_i^2), and SMLD's weight σi2\sigma_i^2 is the same rule.

Step 2

Sampling is the time reversal of the forward SDE, and the score is its only unknown. By Anderson's theorem the reversed process xˉ(s)=x(T−s)\bar x(s)=x(T-s) is again a diffusion, which in forward-running time reads dxˉ=[−f(xˉ,T−s)+g(T−s)2 ∇log⁡pT−s(xˉ)] ds+g(T−s) dwˉ(s),xˉ(0)∼pT.d\bar x=\big[-f(\bar x,T-s)+g(T-s)^2\,\nabla\log p_{T-s}(\bar x)\big]\,ds+g(T-s)\,d\bar w(s),\qquad \bar x(0)\sim p_T . A one-line Fokker-Planck computation checks the marginals: qs=pT−sq_s=p_{T-s} satisfies ∂sq=∇⋅(fq)−12g2Δq\partial_sq=\nabla\cdot(fq)-\tfrac12g^2\Delta q, and writing −12g2Δq=−∇⋅(g2q∇log⁡q)+12g2Δq-\tfrac12g^2\Delta q=-\nabla\cdot(g^2q\nabla\log q)+\tfrac12g^2\Delta q turns this into the forward equation of the displayed SDE. So the score term carries twice the diffusion's spreading, half to cancel the noise the reverse SDE injects and half to undo the forward smoothing. With a state-dependent G(x,t)G(x,t) the score term becomes GG⊤∇log⁡ptGG^\top\nabla\log p_t and the drift gains +∇⋅[GG⊤]+\nabla\cdot[GG^\top]. The sampler replaces ∇log⁡pt\nabla\log p_t by sθs_\theta and starts from the prior.

Step 3

The same score defines an ODE with the same marginals, and that ODE has exact likelihoods. Put the whole Laplacian into the transport term instead of half of it: ∂tpt=−∇⋅(fpt)+12g2Δpt=−∇⋅[(f−12g2∇log⁡pt) pt]\partial_tp_t=-\nabla\cdot(fp_t)+\tfrac12g^2\Delta p_t=-\nabla\cdot\big[(f-\tfrac12g^2\nabla\log p_t)\,p_t\big] is a continuity equation, so the probability-flow ODE dxdt=f(x,t)−12g(t)2 ∇log⁡pt(x)\frac{dx}{dt}=f(x,t)-\tfrac12g(t)^2\,\nabla\log p_t(x) moves p0p_0 to pTp_T through the same marginals as the SDE. With sθs_\theta in place of the score it is a neural ODE, so the instantaneous change of variables gives log⁡p0(x(0))=log⁡pT(x(T))+∫0T∇⋅f~θ(x(t),t) dt\log p_0(x(0))=\log p_T(x(T))+\int_0^T\nabla\cdot\tilde f_\theta(x(t),t)\,dt, with the divergence estimated by Skilling-Hutchinson. Since the forward SDE has no parameters, the code x(T)x(T) of an input is fixed by p0p_0 alone given an exact score, and two independently trained architectures give nearly the same codes. The paper does not note that with sθs_\theta in place of the true score the ODE and the reverse SDE are different models, so the reported likelihood is the ODE model's, while the best FIDs come from SDE samplers.

Step 4

DDPM's sampler is one discretization of the reverse VP SDE, and Langevin steps can correct any discretization. Running the forward chain's increments backward with the score added gives the reverse-diffusion sampler, and expanding DDPM's ancestral update 11−β(x+βsθ)+βz\frac{1}{\sqrt{1-\beta}}(x+\beta s_\theta)+\sqrt\beta z to first order in β\beta shows it is the same update up to o(β)o(\beta). The corrector uses that sθ(⋅,t)s_\theta(\cdot,t) is the score of the current marginal: a few Langevin steps x←x+ϵ sθ(x,t)+2ϵ zx\leftarrow x+\epsilon\,s_\theta(x,t)+\sqrt{2\epsilon}\,z at fixed tt target ptp_t itself and pull the iterate back toward it. This predictor-corrector (PC) sampler has the old methods as special cases, SMLD an identity predictor with a Langevin corrector and DDPM an ancestral predictor with no corrector. On CIFAR-10 with 1000 noise levels, one corrector step per predictor step takes VE reverse diffusion from FID 4.79 to 3.60 and VE probability-flow sampling from 15.41 to 3.51, beating 2000 predictor steps at the same cost, while 2000 corrector-only steps give 20.43; for VP the gains are smaller.

Step 5

Conditioning needs only a gradient added to the unconditional score, plus an approximation when the condition is part of xx. By Bayes, ∇xlog⁡pt(x∣y)=∇xlog⁡pt(x)+∇xlog⁡pt(y∣x)\nabla_x\log p_t(x\mid y)=\nabla_x\log p_t(x)+\nabla_x\log p_t(y\mid x), so the conditional reverse SDE uses the unconditional model plus a classifier pt(y∣x(t))p_t(y\mid x(t)) trained on noised inputs, since the term needed is the label likelihood given the current noisy state. For imputation with known coordinates Ω(x(0))=y\Omega(x(0))=y the exact conditional score of the unknown coordinates z(t)z(t) is intractable, so the paper conditions instead on Ω^(x(t))\hat\Omega(x(t)), a fresh draw of the known coordinates noised to level tt, and takes the gradient from the joint score. This heuristic has no error control, since it drops the dependence of z(t)z(t) on yy beyond what Ω^(x(t))\hat\Omega(x(t)) carries. Colorization is the same after an orthogonal change of color basis.

Step 6

The experiments separate sampler, objective and architecture. With the discrete objectives fixed, the PC comparisons isolate the sampler; new architectures then give FID 2.45 (VE) and 2.78 (VP), and the continuous objective with doubled depth 2.20 and 2.41. For likelihood, the exact ODE value for Ho et al.'s own DDPM model is 3.28 bits/dim against its reported ELBO bounds of 3.70 and 3.75 (computed on discrete data, so the comparison is indicative), and sub-VP beats VP in every pairing. A black-box ODE solver cuts score evaluations by over 90% with little visible loss, though ODE samples without a corrector have worse FID than SDE samples.

Consequences and limits. The SDE view became the standard language for diffusion models: samplers are integrators of one of two equations, the probability-flow ODE underlies fast deterministic samplers (DDIM's update is an Euler step of it in rescaled variables) and later flow matching, and guidance is the conditional score of Step 5. The analysis came later, from Chen et al. (2023), who bound the sampler's error by the L2L^2 score error, and from stochastic localization (Montanari). The limits are the absence of any guarantee, including on how score and discretization errors reach the samples; hundreds to thousands of network evaluations per sample; and an ODE likelihood that belongs to a different model than the SDE sampler. The phrase "with sufficient data and model capacity" also leaves out a point taken up by Kadkhodaie et al. (not made in this paper): with nn training points and unlimited capacity the empirical minimizer is the exact score of the training set convolved with the Gaussian kernel, ∇log⁡1n∑iN(x;αtxi,σt2I)\nabla\log\frac1n\sum_iN(x;\alpha_tx_i,\sigma_t^2I), whose reverse process returns training points, so any novelty in the samples comes from what the network fails to fit.

Terms

∇log⁡pt(x)\nabla\log p_t(x)
score of the noised marginal at time tt; the only learned object
Tweedie's formula
∇log⁡pt(x)=(αtE[x0∣x]−x)/σt2=−E[ε∣x]/σt\nabla\log p_t(x)=(\alpha_t\mathbb E[x_0\mid x]-x)/\sigma_t^2=-\mathbb E[\varepsilon\mid x]/\sigma_t
Denoising score matching
regress sθs_\theta on ∇log⁡p0t(xt∣x0)=−ε/σt\nabla\log p_{0t}(x_t\mid x_0)=-\varepsilon/\sigma_t; same minimizer as the marginal score
ε\varepsilon-prediction
sθ=−εθ/σts_\theta=-\varepsilon_\theta/\sigma_t; weight λ=σt2\lambda=\sigma_t^2 turns DSM into DDPM's LsimpleL_{\rm simple}
VE / VP / sub-VP SDE
continuum limits of SMLD and DDPM chains, and a VP variant with smaller variance
Reverse-time SDE
drift −f+g2∇log⁡pT−s-f+g^2\nabla\log p_{T-s} in forward-running time (Anderson, 1982)
Probability-flow ODE
x˙=f−12g2∇log⁡pt\dot x=f-\tfrac12g^2\nabla\log p_t; same marginals as the SDE, invertible
Skilling-Hutchinson
unbiased trace estimate ϵ⊤∇f~ ϵ\epsilon^\top\nabla\tilde f\,\epsilon for the divergence in the likelihood
Predictor-corrector
reverse-SDE step, then Langevin steps at fixed tt that target ptp_t
Conditional score
∇log⁡pt(x)+∇log⁡pt(y∣x)\nabla\log p_t(x)+\nabla\log p_t(y\mid x), with a classifier trained on noised x(t)x(t)

Quiz

  1. How is DDPM's noise-prediction loss E∥εθ(xt,t)−ε∥2\mathbb E\|\varepsilon_\theta(x_t,t)-\varepsilon\|^2 related to score matching?
  2. The paper reports exact likelihoods computed with the probability-flow ODE and a learned sθs_\theta. Whose likelihood is it?
  3. Two independently trained score networks, with different architectures, map the same image to nearly the same code x(T)x(T) under the probability-flow ODE. Why?

Sources