Skip to content
BIASED

Continuous-time diffusion and flow matching

Shrink DDPM's steps to zero and noising becomes an SDE. Its probability-flow ODE samples deterministically and gives exact likelihoods, the reverse-time SDE is DDPM's sampler, and flow matching learns a velocity field directly.

From steps to time

The topic Score matching showed that DDPM's noise predictor is a scaled score, sθ(xt,t)=−εθ(xt,t)/1−αˉts_\theta(x_t, t) = -\varepsilon_\theta(x_t, t)/\sqrt{1 - \bar\alpha_t}, and that each reverse step moves along that score and adds fresh noise. It ended with a pointer: Song et al. (2021) join the two views in continuous time. This topic takes that limit, infinitely many infinitely small steps, and sees what it buys.

Why should a limit exist at all? DDPM cuts the noising into T=1000T = 1000 steps, but the network only ever sees how much noise has been added so far, not how it was cut. So describe the schedule as a rate on t∈[0,1]t \in [0, 1] and split it into TT steps:

β(t)=0.1+19.9 t,βi=β(i/T)T,i=1,…,T.\beta(t) = 0.1 + 19.9\, t, \qquad \beta_i = \frac{\beta(i/T)}{T}, \quad i = 1, \dots, T.

With T=1000T = 1000 this is DDPM's linear schedule, from about 10−410^{-4} to 0.020.02.

The product αˉ\bar\alpha then turns into an integral. For small βi\beta_i, log⁡(1−βi)≈−βi\log(1 - \beta_i) \approx -\beta_i, so log⁡αˉ\log \bar\alpha is a sum −∑iβ(i/T)/T-\sum_i \beta(i/T)/T, a Riemann sum. As TT grows,

αˉ(t)=exp⁡(−∫0tβ(s) ds).\bar\alpha(t) = \exp\Big(-\int_0^t \beta(s)\, ds\Big).

Halfway through the process, the discrete product with T=50T = 50, 200200 and 10001000 steps is 0.05850.0585, 0.07380.0738 and 0.07800.0780, and it settles on a limit. At the end, ∫01β=10.05\int_0^1 \beta = 10.05 and αˉ(1)=e−10.05≈4.3⋅10−5\bar\alpha(1) = e^{-10.05} \approx 4.3 \cdot 10^{-5}, close to DDPM's αˉT\bar\alpha_T.

The payoff is practical. A network that depends only on the noise level can drive any discretization of the continuous process: the 1000 training steps, 50 steps, or an adaptive solver. First we need the equation of that process.

The noising SDE

Take one DDPM step with Δt=1/T\Delta t = 1/T and βi=β(t) Δt\beta_i = \beta(t)\,\Delta t:

xt+Δt=1−β(t)Δt  xt+β(t)Δt  ε,ε∼N(0,I).x_{t + \Delta t} = \sqrt{1 - \beta(t)\Delta t}\; x_t + \sqrt{\beta(t)\Delta t}\; \varepsilon, \qquad \varepsilon \sim \mathcal N(0, I).

Expand the square root, 1−u≈1−u/2\sqrt{1 - u} \approx 1 - u/2, and look at the change:

xt+Δt−xt≈−12β(t) xt Δt+β(t) Δt  ε.x_{t + \Delta t} - x_t \approx -\tfrac12 \beta(t)\, x_t\, \Delta t + \sqrt{\beta(t)}\, \sqrt{\Delta t}\; \varepsilon .

The drift is of order Δt\Delta t, the noise of order Δt\sqrt{\Delta t}. The square root is forced. One unit of time holds 1/Δt1/\Delta t independent kicks, and their variances add, so each kick must have variance proportional to Δt\Delta t for the total to stay finite and nonzero.

Sums of such kicks form a Wiener process ww: its increments wt+Δt−wt∼N(0,Δt I)w_{t + \Delta t} - w_t \sim \mathcal N(0, \Delta t\, I) are independent of the past. Writing dwdw for one increment, the limit of the DDPM chain is the variance-preserving (VP) SDE, a stochastic differential equation:

dx=−12β(t) x dt+β(t) dw.dx = -\tfrac12 \beta(t)\, x\, dt + \sqrt{\beta(t)}\, dw .

The general form is dx=f(x,t) dt+g(t) dwdx = f(x, t)\, dt + g(t)\, dw with a drift ff and a diffusion coefficient gg. Read it as the limit of Euler–Maruyama steps xt+Δt=xt+f Δt+g Δt εx_{t + \Delta t} = x_t + f\,\Delta t + g\,\sqrt{\Delta t}\,\varepsilon; Itô calculus makes this rigorous. Each step adds noise of variance g2Δtg^2 \Delta t, so g(t)2g(t)^2 is the rate at which noise variance is pumped in.

The closed form survives the limit: xt∣x0∼N(αˉ(t) x0, (1−αˉ(t)) I)x_t \mid x_0 \sim \mathcal N\big(\sqrt{\bar\alpha(t)}\, x_0,\ (1 - \bar\alpha(t))\, I\big), so we can still jump to any time. NCSN from Score matching has a limit too. It only adds noise, up to the level σ(t)\sigma(t), with no shrinking. That is the variance-exploding (VE) SDE, dx=dσ2(t)/dt  dwdx = \sqrt{d\sigma^2(t)/dt}\; dw.

How densities move

An SDE moves single points at random. A generative model cares about the whole cloud: how does the density ptp_t of xtx_t change in time? The answer is the Fokker–Planck equation:

∂tpt=−∇⋅(f pt)+12g(t)2 Δpt,\partial_t p_t = -\nabla \cdot \big(f\, p_t\big) + \tfrac12 g(t)^2\, \Delta p_t ,

where Δ=∇⋅∇\Delta = \nabla \cdot \nabla is the Laplacian, the sum of second derivatives. To derive it, average a smooth test function over one Euler–Maruyama step, expand to first order in Δt\Delta t and integrate by parts.

The two terms are two mechanisms. Transport, −∇⋅(f pt)-\nabla \cdot (f\, p_t), carries mass along the velocity ff, like dye in a current. Diffusion, 12g2Δpt\tfrac12 g^2 \Delta p_t, moves mass from peaks to valleys and smooths ptp_t, like dye spreading in still water.

Two special cases matter for the rest of the topic.

  • No noise, g=0g = 0. What remains, ∂tpt=−∇⋅(f pt)\partial_t p_t = -\nabla \cdot (f\, p_t), is the continuity equation of the ODE dx/dt=fdx/dt = f. Every point moves deterministically, and the density follows the points.
  • Drift along the score, f=12g2 ∇log⁡pf = \tfrac12 g^2\, \nabla \log p. Then f p=12g2 ∇pf\, p = \tfrac12 g^2\, \nabla p, the two terms cancel, and pp stays put. Transport pushes mass uphill exactly as fast as diffusion spreads it. You have already sampled with such an SDE; the check asks where.

The probability-flow ODE

The diffusion term can be disguised as transport. Since ∇p=p ∇log⁡p\nabla p = p\, \nabla \log p,

12g2 Δpt=∇⋅(12g2 pt ∇log⁡pt),\tfrac12 g^2\, \Delta p_t = \nabla \cdot \big(\tfrac12 g^2\, p_t\, \nabla \log p_t\big),

and the Fokker–Planck equation becomes a continuity equation with no noise at all:

∂tpt=−∇⋅([f−12g2 ∇log⁡pt] pt).\partial_t p_t = -\nabla \cdot \Big(\big[f - \tfrac12 g^2\, \nabla \log p_t\big]\, p_t\Big).

So the probability-flow ODE

dxdt=f(x,t)−12g(t)2 ∇log⁡pt(x)\frac{dx}{dt} = f(x, t) - \tfrac12 g(t)^2\, \nabla \log p_t(x)

moves the density exactly as the SDE does: the same ptp_t at every tt. When the velocity is Lipschitz, trajectories are unique and the continuity equation has one solution from a given p0p_0; for a learned score that is an assumption about the network. The paths differ. The SDE jitters every point; the ODE carries it along the average flow.

For the VP SDE, put in the score from the noise predictor. The same trained network now drives an ODE, with no retraining:

dxdt=−12β(t)[x−εθ(x,t)1−αˉ(t)].\frac{dx}{dt} = -\tfrac12 \beta(t) \Big[x - \frac{\varepsilon_\theta(x, t)}{\sqrt{1 - \bar\alpha(t)}}\Big].

A Gaussian example shows the paths. Take data N(2,0.52)\mathcal N(2, 0.5^2). By the closed form, pt=N(2αˉ(t), 0.25 αˉ(t)+1−αˉ(t))p_t = \mathcal N\big(2\sqrt{\bar\alpha(t)},\ 0.25\,\bar\alpha(t) + 1 - \bar\alpha(t)\big) stays Gaussian. In one dimension ODE trajectories cannot cross, so the flow keeps the order of points, and a map that keeps order and turns p0p_0 into ptp_t must keep every point's z-score. The point x0=2.5x_0 = 2.5 sits one standard deviation above the mean. At t=1t = 1 the mean is 0.0130.013 and the standard deviation almost 11, so x1=1.013x_1 = 1.013. Running the ODE backwards from 1.0131.013 returns exactly 2.52.5.

What a deterministic sampler buys

Swapping the SDE for an ODE with the same marginals buys three things.

Encoding. The map from x0x_0 to x1x_1 is deterministic and invertible. Run the ODE forward to turn an image into its noise, and backwards to get the image back. The DDPM sampler cannot do this: it adds fresh noise at every step, so one xTx_T leads to many images, and no image has a single noise of its own.

Exact likelihood. Along an ODE trajectory with velocity v=f−12g2 sθv = f - \tfrac12 g^2\, s_\theta, the log-density changes by minus the divergence:

ddtlog⁡pt(xt)=−∇⋅v(xt,t),log⁡p0(x0)=log⁡p1(x1)+∫01∇⋅v(xt,t) dt.\frac{d}{dt} \log p_t(x_t) = -\nabla \cdot v(x_t, t), \qquad \log p_0(x_0) = \log p_1(x_1) + \int_0^1 \nabla \cdot v(x_t, t)\, dt .

Here log⁡p1\log p_1 is the standard normal log-density. This is the change of variables from The change of variables in continuous time: the log-determinant of a flow becomes an integral of a trace, since the divergence is the trace of the Jacobian of vv. The exact trace costs DD backward passes, the wall that plain score matching hit, so Song et al. estimate it with random projections. The ELBO of DDPM: reverse process and objective only bounded the likelihood; the ODE computes it.

Any solver. The ODE can be integrated with higher-order methods and adaptive step sizes, often in far fewer than 1000 network calls. DDIM, the fast deterministic sampler that the topic Latent diffusion uses, is Euler's method for this ODE in rescaled coordinates.

Running the SDE backwards

The ODE drops the noise. Can we run backwards and keep it? Build the reverse process from two pieces. First, run the probability-flow ODE backwards in time: it carries p1p_1 to p0p_0. Second, at every time add a Langevin pair, the drift 12g2 ∇log⁡pt\tfrac12 g^2\, \nabla \log p_t with the noise g dwg\, dw. The Fokker–Planck block showed that this pair leaves ptp_t unchanged, so it does not spoil the marginals. Run from t=1t = 1 down to t=0t = 0 and the two score drifts add up:

dx=[f(x,t)−g(t)2 ∇log⁡pt(x)] dt+g(t) dwˉ,dx = \big[f(x, t) - g(t)^2\, \nabla \log p_t(x)\big]\, dt + g(t)\, d\bar w ,

where dt<0dt < 0 and wˉ\bar w is a Wiener process running backwards. This is the reverse-time SDE. Anderson (1982) showed that the forward process, read backwards, solves it, so whole paths match in distribution, not just marginals. The score drift is twice the ODE's: one half undoes the forward spreading, the other half cancels the spreading caused by the reverse step's own noise.

DDPM's sampler is this SDE. One Euler–Maruyama step backwards for the VP SDE, with βi=β(t)Δt\beta_i = \beta(t)\Delta t and z∼N(0,I)z \sim \mathcal N(0, I), is

xt−Δt=(1+12βi) xt+βi sθ(xt,t)+βi  z,x_{t - \Delta t} = \big(1 + \tfrac12 \beta_i\big)\, x_t + \beta_i\, s_\theta(x_t, t) + \sqrt{\beta_i}\; z ,

and the DDPM reverse mean (xt+βi sθ)/1−βi\big(x_t + \beta_i\, s_\theta\big)/\sqrt{1 - \beta_i} from Score matching agrees with it up to terms of order βi2\beta_i^2.

The noise is not just decoration. Take the mixture 0.8 N(−4,1)+0.2 N(4,1)0.8\,\mathcal N(-4, 1) + 0.2\,\mathcal N(4, 1) from Score matching, use its exact score, and count samples in the left mode; the target is 0.80.8.

reverse steps SDE ODE
1010 0.7820.782 0.7820.782
100100 0.7990.799 0.7930.793
10001000 0.7990.799 0.7950.795

The ODE stalls because it starts from N(0,1)\mathcal N(0, 1), while the true p1p_1 still has component means ±0.026\pm 0.026. A deterministic flow carries that small error at the start all the way to the end. The SDE's Langevin part keeps pulling samples toward ptp_t and partly forgets the start. Predictor–corrector samplers do this on purpose: after each reverse step they run a few Langevin steps at the current noise level.

Flow matching

So far the path from data to noise was dictated by the SDE, and the ODE's velocity was assembled from ff, gg and a learned score. Flow matching (Lipman et al., 2023) turns this around: choose the path first, then learn its velocity directly.

Any velocity field vθ(x,t)v_\theta(x, t) whose ODE carries N(0,I)\mathcal N(0, I) at t=1t = 1 to the data at t=0t = 0 is a generator. Pick a path of densities ptp_t between the two, and let utu_t be the velocity whose continuity equation moves along it. The ideal loss is

LFM(θ)=Et,  x∼pt∥vθ(x,t)−ut(x)∥2,\mathcal L_{\text{FM}}(\theta) = \mathbb E_{t,\; x \sim p_t} \big\lVert v_\theta(x, t) - u_t(x) \big\rVert^2 ,

but neither ptp_t nor utu_t is known. The simplest path joins each image to its own noise draw by a straight line:

xt=(1−t) x0+t ε,t∈[0,1].x_t = (1 - t)\, x_0 + t\, \varepsilon, \qquad t \in [0, 1] .

For one pair the velocity is constant, dxt/dt=ε−x0dx_t/dt = \varepsilon - x_0. The marginal velocity at a point averages it over all pairs whose lines pass through that point: ut(x)=E[ε−x0∣xt=x]u_t(x) = \mathbb E[\varepsilon - x_0 \mid x_t = x].

This is the trick of denoising score matching again. Regress onto the per-pair target; squared error is minimized by the conditional mean, which is exactly utu_t. The conditional flow matching loss

LCFM(θ)=Et, x0, ε∥vθ(xt,t)−(ε−x0)∥2=LFM(θ)+C\mathcal L_{\text{CFM}}(\theta) = \mathbb E_{t,\, x_0,\, \varepsilon} \big\lVert v_\theta(x_t, t) - (\varepsilon - x_0) \big\rVert^2 = \mathcal L_{\text{FM}}(\theta) + C

differs from the ideal one by a constant CC that does not depend on θ\theta, so it has the same gradients and the same minimizer. To sample, draw x1∼N(0,I)x_1 \sim \mathcal N(0, I) and integrate dx/dt=vθdx/dt = v_\theta from t=1t = 1 down to 00, for example with Euler steps xt−Δt=xt−Δt vθ(xt,t)x_{t - \Delta t} = x_t - \Delta t\, v_\theta(x_t, t).

The link to diffusion is close. Given xtx_t, knowing x0x_0 fixes ε\varepsilon, so the velocity, the noise and the score are all affine functions of one denoiser E[x0∣xt]\mathbb E[x_0 \mid x_t]. A velocity network, a noise network and a score network are three parameterizations of the same object. The VP path of diffusion is just another Gaussian path, with coefficients αˉ(t)\sqrt{\bar\alpha(t)} and 1−αˉ(t)\sqrt{1 - \bar\alpha(t)} instead of 1−t1 - t and tt.

Curved paths and rectified flow

Every pair moves on a straight line. Why not sample in one Euler step? Because the marginal paths are not straight. Lines from different images cross: a point xtx_t lies on many lines, and utu_t averages their directions. As a sample moves, the mix of lines through it changes, and its trajectory bends.

The extreme case is t=1t = 1. There x1=εx_1 = \varepsilon, which is independent of the image, so E[x0∣x1]=E[x0]\mathbb E[x_0 \mid x_1] = \mathbb E[x_0] and u1(x)=x−E[x0]u_1(x) = x - \mathbb E[x_0]. Here is the same mixture 0.8 N(−4,1)+0.2 N(4,1)0.8\,\mathcal N(-4, 1) + 0.2\,\mathcal N(4, 1), sampled with its exact marginal velocity:

Euler steps share in left mode std in left mode
22 0.9970.997 0.540.54
1010 0.830.83 0.980.98
100100 0.800.80 1.001.00

With two steps almost every sample lands in the left mode, squeezed to half its width. A hundred steps are accurate. The check asks what a single step does.

Rectified flow (Liu, Gong and Liu, 2023) straightens the paths. Lines cross because x0x_0 and ε\varepsilon were paired independently. Reflow runs the trained ODE from noise draws ε\varepsilon to generated images x^0\hat x_0 and retrains with the same loss on these coupled pairs (x^0,ε)(\hat x_0, \varepsilon). Their lines barely cross, so the new flow is much straighter. On CIFAR-10 with one Euler step, the paper reports FID 378 without reflow, 12.21 after one reflow and 8.15 after two; FID measures sample quality, lower is better, and the course defines it later. The price is a second training run on 4 million generated pairs, and slightly worse quality with an accurate solver.

The continuous picture is now complete: the VP SDE, its probability-flow ODE, the reverse-time SDE that DDPM discretizes, and flow matching that learns a velocity directly. Solvers and straighter paths cut sampling from 1000 steps to tens, sometimes one. But every step is still a full pass of a large network over every pixel: D=786,432D = 786{,}432 numbers for a 512×512512 \times 512 colour image. The next topic, Latent diffusion, moves the whole machine into the small latent space of an autoencoder and samples it with DDIM, a discretization of the probability-flow ODE.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.