Lecture 16 · Flow Matching

Deep Generative Models · Lecture 16。个人学习讲义与配套实验,按主题整理并持续修订。

课程源码 ZIP ↓

个人整理的学习笔记,非官方讲义;课程来源与整理说明见课程介绍。配套示例未逐一运行验证。

本篇目录

Maps to: MIT 6.S978 Deep Generative Models (Fall 2024), Reading · Thu 10/31/2024 · Flow Matching 1 (student paper session). Problem set 5 (released 11/05) belongs here: conditional flow matching on MNIST, ablations of the ODE solver and of the conditional field, and a Gaussian-to-Gaussian flow in 2-D. Required papers:

  • Y. Lipman, R. T. Q. Chen, H. Ben-Hamu, M. Nickel, M. Le, Flow Matching for Generative Modeling, ICLR 2023;
  • M. S. Albergo, E. Vanden-Eijnden, Building Normalizing Flows with Stochastic Interpolants, ICLR 2023;
  • X. Liu, C. Gong, Q. Liu, Flow Straight and Fast: Learning to Generate and Transfer Data with Rectified Flow, ICLR 2023.

Optional: T. Fjelde, E. Mathieu, V. Dutordoir, An Introduction to Flow Matching (Cambridge MLG blog, 2024).

Basis: This session consisted of student presentations, with no published slides. The notes are organized around one question — can we learn the velocity field of a flow from noise to data directly, by regression, without simulating it? — and the three papers that answered it at the same time, in three different languages. Background the papers rely on (continuous normalizing flows, the continuity equation) comes first. Paper claims are labelled “the paper reports / proves”. All experiments were designed for these notes and run on a CPU. Most use 2-D Gaussian mixtures, for which the marginal velocity field is known exactly, so learned fields, losses and samplers can be compared with the truth. The last experiments use the glyph latents of Lecture 12. Several outcomes differed from what I expected and are reported as they came out:

  • the linear (“OT”) path’s marginal trajectories are more bent than the cosine path’s, yet easier to integrate;
  • all three standard paths turned out to define the same map from noise to data;
  • distilling the reflowed coupling into one step was no easier than distilling the original one;
  • a bridge between two multimodal data sets failed without a little extra noise.

Code: code/lecture16/ — 6 scripts + the shared module flow_matching.py. They run in 10–95 s. Reading time: 4–5 hours including running the code.


0. How to read this lecture

0.1 Where we are

Lectures 12–13 arrived at a deterministic sampler for diffusion models: DDIM, which Lecture 13 §5 showed to be Euler’s method on an ODE, the probability-flow ODE. That ODE moves noise to data, and a network trained to denoise defines its velocity. This lecture turns the order around:

  • Diffusion fixes a noising process, learns to undo it, and then discovers that the result can be written as an ODE.
  • Flow matching starts from the ODE. It chooses how probability mass should travel from noise to data (a probability path), and regresses a network directly on the velocity of that travel.

The three papers of this session found this idea independently in 2022:

papervocabularythe key move
Lipman et al., Flow Matchingcontinuous normalizing flows, probability pathsregress on conditional vector fields; same gradient as the intractable marginal one
Albergo & Vanden-Eijnden, Stochastic Interpolantsinterpolants between two densitiesany interpolant xt=I(t,x0,x1)x_t = I(t, x_0, x_1) defines a velocity; a quadratic loss learns it, between any two distributions
Liu, Gong & Liu, Rectified Flowcouplings and transportstraight lines between pairs; reflow on the flow’s own pairs makes trajectories straight, so few steps suffice

They are three descriptions of one algorithm. The differences that matter in practice are which path, which time weighting and which sampler. Lecture 17 scales the algorithm up (Stable Diffusion 3, SiT).

0.2 Learning goals

  1. Explain why maximum-likelihood training of continuous normalizing flows is expensive, and what flow matching avoids (§1).
  2. Derive the marginal velocity from conditional ones (Theorem 1), and prove that the conditional loss has the same gradient (Theorem 2). Explain the loss floor (§2.1–2.3).
  3. Derive the conditional field of a Gaussian path; write the OT and diffusion paths; solve Pset 5’s Gaussian-to-Gaussian problem (§2.4).
  4. Show that all affine Gaussian paths define the same flow up to a change of clock and scale, and say what a path choice changes instead (§2.5).
  5. Convert between velocity, score and denoiser; build a bridge between two non-Gaussian distributions (§3).
  6. State rectified flow’s guarantees (marginals preserved, transport cost non-increasing), and explain reflow, straightness and distillation (§4).
  7. Compare ODE solvers by function evaluations, fixed and adaptive (§5).

0.3 Notation and time direction

In this lecture time runs from noise (t=0t = 0) to data (t=1t = 1), as in all three papers and Pset 5:

xt=at x1+bt x0,x0∼N(0,I) (noise),x1∼q (data).x_t = a_t\,x_1 + b_t\,x_0,\qquad x_0\sim\mathcal N(0, I)\ \text{(noise)},\qquad x_1\sim q\ \text{(data)}.

Lectures 12–13 used the opposite direction (t=0t = 0 data). So do SD3 and SiT in Lecture 17. Always check a paper’s convention before copying a formula.

0.4 Map

SectionContentCode
§1Background: continuous normalizing flows, the continuity equation—
§2Flow matching: marginal and conditional fields, Gaussian paths01_gaussian_to_gaussian.py, 02_fm_vs_cfm.py, 03_paths.py
§3Stochastic interpolants: any two densities; velocity and score06_interpolants.py
§4Rectified flow: straightness, reflow, distillation04_reflow.py
§5Sampling: ODE solvers and NFE; Pset 5 on glyphs05_glyph_solvers.py
§6Flow matching and diffusion: one family—

1. Background: continuous normalizing flows

1.1 A flow defined by an ODE

Lecture 4 built normalizing flows from a stack of invertible layers. Take the limit of infinitely many infinitesimal layers and you get an ordinary differential equation:

dxtdt=v(xt,t),x0∼p0=N(0,I).\frac{d x_t}{dt} = v(x_t, t),\qquad x_0 \sim p_0 = \mathcal N(0, I).

The flow ϕt\phi_t maps a starting point x0x_0 to where the ODE has carried it by time t. If v is smooth, ϕt\phi_t is invertible (run the ODE backwards). It pushes p0p_0 forward to a distribution ptp_t. A network vθv_\theta in place of v gives a continuous normalizing flow (CNF; Chen et al. 2018). Any network architecture is allowed, because invertibility comes from the ODE, not from the layers.

1.2 How the density moves

Two equivalent descriptions.

Along a trajectory (the instantaneous change of variables):

ddtlog⁡pt(xt)=−∇ ⁣⋅v(xt,t).\frac{d}{dt}\log p_t(x_t) = -\nabla\!\cdot v(x_t, t).

It is the continuous version of Lecture 4’s log-determinant: where the field spreads out (positive divergence), density drops.

At a fixed point (the continuity equation, conservation of mass):

∂pt(x)∂t+∇ ⁣⋅(pt(x) v(x,t))=0.\frac{\partial p_t(x)}{\partial t} + \nabla\!\cdot\big(p_t(x)\,v(x, t)\big) = 0.

The rate at which mass at x changes equals the net flow of mass into x. We say that v generates the probability path (pt)(p_t). Code 01 checks this equation numerically for its exact field (relative residual 10−910^{-9}–10−810^{-8}).

1.3 Why training CNFs was expensive

Maximum likelihood needs log⁡p1(x)\log p_1(x) for each data point x:

  1. integrate the ODE backwards from x to x0x_0;
  2. accumulate ∫∇ ⁣⋅v dt\int\nabla\!\cdot v\,dt along the way, using trace estimators in high dimension (FFJORD, Grathwohl et al. 2019);
  3. backpropagate through the whole solver.

Every training step simulates the ODE. The cost grows with the number of solver steps, which itself grows as training makes the field more complex. CNFs were elegant and slow, and they did not scale to large images.

Diffusion models (Lecture 12) escaped this with a simulation-free objective: noise a data point in closed form, regress a denoiser. Flow matching brings the same trick to CNFs.

1.4 The idea in one sentence

If we knew a velocity field utu_t that generates a path from p0p_0 = noise to p1p_1 = data, we could just regress on it:

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

No ODE is solved during training. The rest of §2 shows how to get a usable utu_t.


2. Flow matching (Lipman et al.)

2.1 Build the path from conditional paths

Writing down ptp_t and utu_t directly is hopeless: p1p_1 is the unknown data distribution. Lipman et al.’s move is to build the path one data point at a time:

  • For each data point x1x_1, choose a simple conditional path pt(x∣x1)p_t(x\mid x_1): p0(x∣x1)=N(0,I)p_0(x\mid x_1) = \mathcal N(0, I) at t=0t = 0, and a tiny blob around x1x_1 at t=1t = 1.
  • Choose a conditional velocity ut(x∣x1)u_t(x\mid x_1) that generates it. For Gaussian paths it is known in closed form (§2.4).
  • Mix over the data:
pt(x)=∫pt(x∣x1) q(x1) dx1,ut(x)=∫ut(x∣x1) pt(x∣x1) q(x1)pt(x) dx1.p_t(x) = \int p_t(x\mid x_1)\,q(x_1)\,dx_1, \qquad u_t(x) = \int u_t(x\mid x_1)\,\frac{p_t(x\mid x_1)\,q(x_1)}{p_t(x)}\,dx_1 .

The marginal velocity is the posterior average of the conditional velocities: all data points x1x_1 whose conditional path could have produced x at time t, weighted by how likely that is.

Theorem 1 (Lipman et al.). If each ut(⋅∣x1)u_t(\cdot\mid x_1) generates pt(⋅∣x1)p_t(\cdot\mid x_1), then utu_t generates ptp_t.

Proof. Differentiate the mixture, use the conditional continuity equations, and pull the divergence out of the integral:

∂tpt(x)=∫∂tpt(x∣x1) q(x1) dx1=−∇ ⁣⋅ ⁣∫ut(x∣x1) pt(x∣x1) q(x1) dx1=−∇ ⁣⋅(pt(x) ut(x)).□\partial_t p_t(x) = \int \partial_t p_t(x\mid x_1)\,q(x_1)\,dx_1 = -\nabla\!\cdot\!\int u_t(x\mid x_1)\,p_t(x\mid x_1)\,q(x_1)\,dx_1 = -\nabla\!\cdot\big(p_t(x)\,u_t(x)\big). \qquad\square

At t=1t = 1 the mixture of tiny blobs is (nearly) the data distribution. So the ODE with velocity utu_t, started from noise, ends at the data.

2.2 Why the marginal trajectories do not cross

A conditional path is simple. With the “OT” path of §2.4, a pair (x0,x1)(x_0, x_1) travels in a straight line. But the lines of different pairs cross. Through a point x at time t pass many lines, heading to different data points.

The ODE cannot follow all of them: it has one velocity per point. Its velocity is their average, and the marginal trajectories therefore never cross. Code 01 draws both pictures for Pset 5’s Gaussian-to-Gaussian problem:

Conditional paths (straight, crossing), the exact marginal flow (curved, never crossing), and the marginal field at t = 0.5

Averaging has a visible cost: even when every conditional path is straight, the marginal trajectories bend (§2.4, §4).

2.3 Conditional flow matching: the same gradient without utu_t

utu_t still involves the unknown qq. The conditional flow matching loss uses only the conditional field:

LCFM(θ)=Et, x1∼q, x∼pt(⋅∣x1) ∥vθ(x,t)−ut(x∣x1)∥2.\mathcal L_{\text{CFM}}(\theta) = \mathbb E_{t,\ x_1\sim q,\ x\sim p_t(\cdot\mid x_1)}\,\big\lVert v_\theta(x, t) - u_t(x\mid x_1)\big\rVert^2 .

Theorem 2 (Lipman et al.). LCFM(θ)=LFM(θ)+C\mathcal L_{\text{CFM}}(\theta) = \mathcal L_{\text{FM}}(\theta) + C, where C does not depend on θ. Hence the gradients are equal.

Proof. Expand both squares. The ∥vθ∥2\lVert v_\theta\rVert^2 terms are identical, because x∼ptx\sim p_t in both. The cross terms are equal because utu_t is the posterior mean of ut(x∣x1)u_t(x\mid x_1):

Ex∼pt⟨vθ(x,t),ut(x)⟩=∫ ⁣ ⁣∫⟨vθ(x,t),ut(x∣x1)⟩ pt(x∣x1) q(x1) dx1 dx.\mathbb E_{x\sim p_t}\langle v_\theta(x,t), u_t(x)\rangle = \int\!\!\int \langle v_\theta(x,t), u_t(x\mid x_1)\rangle\,p_t(x\mid x_1)\,q(x_1)\,dx_1\,dx .

What remains does not involve θ. □\square

The constant is the variance of the conditional target around its mean, C=E∥ut(x∣x1)−ut(x)∥2C = \mathbb E\lVert u_t(x\mid x_1) - u_t(x)\rVert^2. It is the same mechanism that made Lecture 12’s denoising score matching equal to explicit score matching.

Code 02 computes both losses on the ring of 8 Gaussians, where utu_t is known exactly (exact_velocity), for networks at four stages of training:

training stepLCFM\mathcal L_{\text{CFM}}LFM\mathcal L_{\text{FM}}difference
0 (random)6.7533.3183.435 ± 0.016
1004.0160.5763.440 ± 0.013
5003.9020.4533.449 ± 0.014
40003.4680.0153.453 ± 0.014

The constant itself is 3.454. Three practical consequences:

  • The CFM loss curve flattens far above zero. The trained model’s loss (3.47) is almost all constant. Its level says little about the model; only the small excess does.
  • The noise is in the gradient. Over 100,000 samples the two gradients point the same way (cosine 0.999). On a 256-sample minibatch, the CFM gradient has about 8 times the variance of an exact-target gradient (relative variance 0.79 vs 0.10).
  • The price is efficiency, not correctness. Trained for the same 4000 steps on the exact target, the model’s field is 10 times more accurate (LFM\mathcal L_{\text{FM}} 0.0015 vs 0.015; sample quality 0.98 vs 0.96). In practice the exact target is never available.

Where the floor sits in t. With the linear path, the conditional target is x1−x0=(x1−xt)/(1−t)x_1 - x_0 = (x_1 - x_t)/(1 - t). Its variance given xtx_t is the uncertainty about x1x_1, magnified by 1/(1−t)21/(1-t)^2:

The irreducible CFM floor, the trained CFM loss and the trained FM error per bin of t; samples of the CFM- and FM-trained models

  • At t=0t = 0: tr Cov(x1)≈4\mathrm{tr}\,\mathrm{Cov}(x_1) \approx 4.
  • It rises to 5.8 around t=0.3t = 0.3: x1x_1 is still uncertain and the magnification grows.
  • It collapses once xtx_t reveals the mode (0.26 for t∈[0.8,0.9]t\in[0.8, 0.9]).
  • It rises again to tr Cov(x0)=2\mathrm{tr}\,\mathrm{Cov}(x_0) = 2 at t=1t = 1, where x1x_1 is known but the starting noise is not (Exercise 7).

The trained CFM loss tracks this floor in every bin. The model’s real error is largest for t>0.7t > 0.7, where the 8 narrow modes form.

2.4 Gaussian conditional paths

The general form. Take pt(x∣x1)=N(μt(x1),σt(x1)2I)p_t(x\mid x_1) = \mathcal N(\mu_t(x_1), \sigma_t(x_1)^2 I), with μ0=0,σ0=1\mu_0 = 0, \sigma_0 = 1 (noise) and μ1=x1\mu_1 = x_1, σ1=σmin⁡\sigma_1 = \sigma_{\min} small. The simplest map that pushes N(0,I)\mathcal N(0, I) along it is ψt(x0)=σtx0+μt\psi_t(x_0) = \sigma_t x_0 + \mu_t. Its velocity, written at the point x=ψt(x0)x = \psi_t(x_0), is the conditional vector field (Lipman et al., Theorem 3; Exercise 1):

ut(x∣x1)=σ˙tσt (x−μt)+μ˙t.u_t(x\mid x_1) = \frac{\dot\sigma_t}{\sigma_t}\,\big(x - \mu_t\big) + \dot\mu_t .

The CFM loss then becomes a regression on a quantity computed from (x0,x1)(x_0, x_1) alone, with x=ψt(x0)x = \psi_t(x_0):

LCFM=E ∥vθ(ψt(x0),t)−ddtψt(x0)∥2.\mathcal L_{\text{CFM}} = \mathbb E\,\big\lVert v_\theta(\psi_t(x_0), t) - \tfrac{d}{dt}\psi_t(x_0)\big\rVert^2 .

The OT path (Pset 5, problem 1). μt=tx1\mu_t = t x_1 and σt=1−(1−σmin⁡)t\sigma_t = 1 - (1 - \sigma_{\min})t:

ψt(x0)=(1−(1−σmin⁡)t)x0+tx1,ut(x∣x1)=x1−(1−σmin⁡)x1−(1−σmin⁡)t,ddtψt(x0)=x1−(1−σmin⁡)x0.\psi_t(x_0) = \big(1 - (1-\sigma_{\min})t\big)x_0 + t x_1, \qquad u_t(x\mid x_1) = \frac{x_1 - (1-\sigma_{\min})x}{1 - (1-\sigma_{\min})t}, \qquad \tfrac{d}{dt}\psi_t(x_0) = x_1 - (1-\sigma_{\min})x_0 .

Lipman et al. call it the OT path because the conditional flow between two Gaussians, N(0,I)\mathcal N(0, I) and N(x1,σmin⁡2I)\mathcal N(x_1, \sigma_{\min}^2 I), is the optimal-transport displacement: a straight line at constant speed. Pset 5 uses σmin⁡=0\sigma_{\min} = 0, which gives the target x1−x0x_1 - x_0. This is rectified flow’s target too (§4).

Diffusion paths. Lipman et al. also write the paths of score-based models in this form.

  • VP (Song et al.’s variance-preserving SDE, read backwards): μt=α1−tx1\mu_t = \alpha_{1-t}x_1 and σt=1−α1−t2\sigma_t = \sqrt{1-\alpha_{1-t}^2}, with αs=e−12∫0sβ\alpha_s = e^{-\frac12\int_0^s\beta} and β from 0.1 to 20. The path does not quite start at pure noise: α1=0.0066\alpha_1 = 0.0066.
  • VE: μt=x1\mu_t = x_1, with σt\sigma_t growing toward the noise end.

Flow matching can train on any of them; diffusion is then one particular choice of path, trained with a different loss weighting (§6).

Pset 5, problem 3: Gaussian to Gaussian. Source N(μ0,σ02I)\mathcal N(\mu_0, \sigma_0^2 I), target N(μ1,σ12I)\mathcal N(\mu_1, \sigma_1^2 I), straight lines xt=(1−t)x0+tx1x_t = (1-t)x_0 + t x_1 between independent end points. Then xtx_t and x1−x0x_1 - x_0 are jointly Gaussian, and the marginal velocity is a linear regression (Exercise 3):

ut(x)=(μ1−μ0)+tσ12−(1−t)σ02st2 (x−mt),mt=(1−t)μ0+tμ1,st2=(1−t)2σ02+t2σ12.u_t(x) = (\mu_1 - \mu_0) + \frac{t\sigma_1^2 - (1-t)\sigma_0^2}{s_t^2}\,(x - m_t), \qquad m_t = (1-t)\mu_0 + t\mu_1,\quad s_t^2 = (1-t)^2\sigma_0^2 + t^2\sigma_1^2 .

Code 01 (μ0=(−2.5,0)\mu_0 = (-2.5, 0), σ0=1\sigma_0 = 1; μ1=(2.5,1)\mu_1 = (2.5, 1), σ1=0.3\sigma_1 = 0.3) finds:

  • The formula is right. It matches the general Gaussian-mixture code to 2⋅10−152\cdot10^{-15} and satisfies the continuity equation.
  • The end points are optimal. The flow sends every x0x_0 to μ1+σ1σ0(x0−μ0)\mu_1 + \frac{\sigma_1}{\sigma_0}(x_0 - \mu_0), to 5⋅10−115\cdot 10^{-11}. That is the optimal-transport map between the two Gaussians, and the transport cost equals W22=26.98W_2^2 = 26.98; independent pairs cost 28.18.
  • The route is not. The intermediate distributions are narrower than the OT interpolation: their std at t=0.5t = 0.5 is 0.52 instead of 0.65, because averaging independent end points shrinks the spread. So the trajectories bend (straightness S = 0.25, defined in §4).
  • CFM learns it from samples. A small network trained for 3000 steps (4 s) is within 1–2% of utu_t at every t. From the same noise, its samples land on average 0.04 away from the exact flow’s.

2.5 Which path? All affine paths are one flow

Code 03 puts three paths side by side on the ring, first with their exact marginal fields, so that no network error interferes:

pathata_t, btb_tstraightness Slength ratiototal velocity changedopri5 NFE (tol 10−610^{-6})
linear (“OT”)tt, 1−t1-t1.5471.4115.10 (26% after t = 0.75)163
cosine (Albergo; SiT’s “GVP”)sin⁡πt2\sin\frac{\pi t}{2}, cos⁡πt2\cos\frac{\pi t}{2}0.9741.2325.85 (49%)187
VP diffusionα1−t\alpha_{1-t}, 1−α1−t2\sqrt{1-\alpha_{1-t}^2}2.6441.2329.38 (73%)211

Mean end-point error of Euler with N uniform steps:

N12481632128
linear2.0000.7170.1300.0370.0220.0120.003
cosine0.9320.5280.2120.0550.0300.0170.005
VP0.9320.8280.5170.2180.0470.0170.005

Exact trajectories of the three paths from the same noise, and Euler's error vs the number of steps

The surprise: one map from noise to data. Run from the same noise, the three exact flows end at the same points: linear vs cosine to 3⋅10−93\cdot 10^{-9}, and vs VP to 5⋅10−65\cdot10^{-6} (VP starts at a0=0.0066a_0 = 0.0066, not 0). The reason is a short argument.

  • Two affine paths xtA=atx1+btx0x^A_t = a_t x_1 + b_t x_0 and xτB=ατx1+βτx0x^B_\tau = \alpha_\tau x_1 + \beta_\tau x_0 with the same signal-to-noise ratio, at/bt=ατ/βτa_t/b_t = \alpha_\tau/\beta_\tau, are the same random variable up to a factor: xtA=c xτBx^A_t = c\,x^B_\tau with c=at/ατ=bt/βτc = a_t/\alpha_\tau = b_t/\beta_\tau.
  • This holds pair by pair, so the conditional velocities transform the same way. So do their posterior averages, the marginal velocities. A trajectory of one flow, rescaled by c(t)c(t) and run on the clock τ(t)\tau(t), is a trajectory of the other (Exercise 5).
  • At t=0t = 0 and t=1t = 1 both paths sit at x0x_0 and x1x_1 unscaled (c=1c = 1), so the end-point maps coincide.

The cosine and VP paths are both variance-preserving (a2+b2=1a^2 + b^2 = 1), so c≡1c \equiv 1, and their trajectories coincide even as curves (same length ratio, 1.232). The linear path’s states are the variance-preserving ones shrunk by t2+(1−t)2\sqrt{t^2 + (1-t)^2} (0.71 at t=0.5t = 0.5), which pulls its trajectories inward: they are the most bent.

So the choice of an affine Gaussian path does not change what the exact flow computes. It changes two things:

  1. How hard the ODE is for a solver with a given time grid. What matters to Euler is how much the velocity changes along the way. That is smallest for the linear path (5.1) and largest for VP (9.4), three quarters of which comes after t=0.75t = 0.75: the VP path’s ata_t stays near 0 until t≈0.5t \approx 0.5 (a0.3=0.08a_{0.3} = 0.08), then everything happens at once. With uniform steps, the linear path is the most accurate from 4 steps on, and dopri5 needs the fewest evaluations there. Liu et al.’s straightness S, which favours the cosine path, did not predict this ranking.
  2. What the network must learn, and how the loss weights each time. The regression targets differ in scale and noise; script 03’s learned models show it (below).

One step is a special case. The first Euler step only knows E[x1∣x0]=E[x1]\mathbb E[x_1\mid x_0] = \mathbb E[x_1], the data mean (Exercise 6):

  • the linear path moves every sample to the centre of the ring (error 2.0, the ring’s radius);
  • the cosine and VP paths start with zero velocity and do not move at all.

No choice of path makes few-step sampling work for multimodal data; that needs straight marginal trajectories (§4).

With learned fields (same network and 4000 training steps per path), the linear path gives the best samples at small step counts. With 4 Euler steps its quality is 0.58, against 0.25 for cosine and 0.03 for VP. From 16 steps on, linear and cosine are equal. dopri5 needs 55 evaluations for these two models and 79 for the VP model. The VP model also ends lower (0.90 vs 0.96 at 100 steps), probably because its targets are largest and noisiest near t=1t = 1, exactly where its field changes fastest.

What the paper reports. Lipman et al. train the same U-Net with the OT and the diffusion (VP) path and sample with dopri5 at tolerance 10−510^{-5}:

CIFAR-10 (NLL / FID / NFE)ImageNet 32ImageNet 64
FM w/ Diffusion3.10 / 8.06 / 1833.54 / 6.37 / 1933.33 / 16.88 / 187
FM w/ OT2.99 / 6.35 / 1423.53 / 5.02 / 1223.31 / 14.45 / 138

The paper also reports DDPM at 3.12 / 7.48 / 274 on CIFAR-10, and that FM-OT needs roughly 60% of the evaluations of the diffusion models to reach the same numerical error. Our toy ordering — same quality family, fewer evaluations for the OT path — is the same.


3. Stochastic interpolants (Albergo & Vanden-Eijnden)

3.1 Interpolate samples, not densities

Albergo & Vanden-Eijnden start from an interpolant between samples of two arbitrary densities ρ0\rho_0 and ρ1\rho_1:

xt=I(t,x0,x1),I(0,x0,x1)=x0,I(1,x0,x1)=x1,x0∼ρ0, x1∼ρ1 independent.x_t = I(t, x_0, x_1),\qquad I(0, x_0, x_1) = x_0,\quad I(1, x_0, x_1) = x_1,\qquad x_0\sim\rho_0,\ x_1\sim\rho_1 \ \text{independent}.

Their main example is the trigonometric interpolant I=cos⁡(πt2)x0+sin⁡(πt2)x1I = \cos(\frac{\pi t}{2})x_0 + \sin(\frac{\pi t}{2})x_1: our cosine path. The distribution ρt\rho_t of xtx_t interpolates between the two densities. The paper proves that it is transported by the velocity

vt(x)=E[∂tI(t,x0,x1) ∣ xt=x],v_t(x) = \mathbb E\big[\partial_t I(t, x_0, x_1)\ \big|\ x_t = x\big],

and that v is the unique minimizer of a quadratic objective, G(v^)=E[∥v^t(xt)∥2−2 ∂tI⋅v^t(xt)]G(\hat v) = \mathbb E\big[\lVert\hat v_t(x_t)\rVert^2 - 2\,\partial_t I\cdot\hat v_t(x_t)\big]. Up to a constant this is the CFM loss with target ∂tI\partial_t I; the “stochastic” in the title refers to the random end points.

Same algorithm, different emphasis. Compared with Lipman et al.:

  • No Gaussian is needed at either end. ρ0\rho_0 can be any data set (§3.3).
  • The paper bounds W2(ρ1,ρ^1)W_2(\rho_1, \hat\rho_1) by the training objective (times a factor exponential in the Lipschitz constant of v^\hat v).
  • It links interpolants to optimal transport. A max-min over interpolants yields the Benamou–Brenier optimal transport; the authors leave the consequences to future work.

The paper reports a CIFAR-10 NLL of 2.99 bits/dim (FID 10.27) and ImageNet-32 NLL 3.48 (FID 8.49), with likelihoods computed by the ODE as for any CNF. It also reports training at 128 × 128, previously out of reach for maximum-likelihood ODE flows.

3.2 A velocity model is a score model

When ρ0=N(0,I)\rho_0 = \mathcal N(0, I) and I=atx1+btx0I = a_t x_1 + b_t x_0, two linear equations link the posterior means E1=E[x1∣xt=x]E_1 = \mathbb E[x_1\mid x_t = x] and E0=E[x0∣xt=x]E_0 = \mathbb E[x_0\mid x_t = x]:

x=atE1+btE0,vt(x)=a˙tE1+b˙tE0.x = a_t E_1 + b_t E_0,\qquad v_t(x) = \dot a_t E_1 + \dot b_t E_0 .

Eliminate E1E_1, then apply Tweedie’s formula for the Gaussian component (∇log⁡pt=−E0/bt\nabla\log p_t = -E_0/b_t, Lecture 12 §7.2):

E0=atv−a˙txatb˙t−a˙tbt,∇xlog⁡pt(x)=−E0bt.E_0 = \frac{a_t v - \dot a_t x}{a_t\dot b_t - \dot a_t b_t},\qquad \nabla_x\log p_t(x) = -\frac{E_0}{b_t}.

For the linear path this reads ∇log⁡pt(x)=t v−x1−t\nabla\log p_t(x) = \frac{t\,v - x}{1 - t}. Velocity, score, denoiser x^=E1\hat x = E_1 and noise prediction ε^=E0\hat\varepsilon = E_0 are one object in four units (Exercise 4). That is why a flow-matching network can drive the SDE samplers of diffusion models (SiT, Lecture 17).

Code 06, Part 1 checks the formula with exact fields: the score from the velocity matches the true score to 10−1510^{-15} for all three paths. For a learned linear-path model the conversion multiplies the velocity error by t/(1−t)t/(1-t):

t0.20.50.80.95
relative velocity error0.1490.0670.0700.072
relative score error0.0140.0550.1030.159

Near the noise end the score is mostly the known Gaussian part −x/(1−t)-x/(1-t), so even a poor velocity gives a good score. Near the data the factor reaches 19, and score-based samplers inherit the amplified error.

3.3 Bridges between two data sets

Code 06, Parts 2–3 transport the ring of 8 Gaussians to the 5 × 5 grid of 25 Gaussians with the linear interpolant. Training uses the same regression as before, and there is no Gaussian anywhere:

interpolantgrid modes / qualityNFE (dopri5)backwards: ring modes / qualityround-trip error
(reference: N(0,I)\mathcal N(0, I) → grid)25 / 0.81055——
ring → grid, xt=(1−t)x0+tx1x_t = (1-t)x_0 + tx_121 / 0.5331098 / 0.9580.260
+ latent noise, γ\gamma peak 0.2525 / 0.761858 / 0.9550.092
+ latent noise, γ\gamma peak 0.525 / 0.729858 / 0.9420.108
+ latent noise, γ\gamma peak 1.025 / 0.559978 / 0.9300.071

Ring samples; their image under the flow without and with latent noise; the backward flow returns the ring

  • The bridge works and is invertible. Run backwards from its own output, the flow returns the ring (8 modes, quality 0.96). Liu et al. use exactly this for unpaired image-to-image transfer (cat → wild animal faces) and domain adaptation.
  • But it was harder than starting from noise (21 of 25 modes, twice the evaluations). I did not expect this. Each ring mode is a tiny blob (std 0.05) whose mass must fan out to several grid modes, so near t=0t = 0 the field must change direction within 0.05. That is hard to learn and stiff to integrate.
  • The follow-up paper’s latent noise fixes it. Albergo, Boffi & Vanden-Eijnden (2023, Stochastic Interpolants: A Unifying Framework for Flows and Diffusions) add noise to the interpolant: xt=(1−t)x0+tx1+γ(t)zx_t = (1-t)x_0 + tx_1 + \gamma(t)z, with γ(0)=γ(1)=0\gamma(0) = \gamma(1) = 0. The end points are unchanged, and the target becomes x1−x0+γ˙(t)zx_1 - x_0 + \dot\gamma(t)z. With γ\gamma peaking at 0.25 all 25 modes appear, the solver needs fewer evaluations, and the round trip is three times more accurate. The noise smooths the intermediate densities.
  • Too much noise hurts again (peak 1.0, quality 0.56): the intermediate distribution forgets the source’s structure.

4. Rectified flow (Liu, Gong & Liu)

4.1 Straight lines and couplings

Liu et al. phrase the problem as transport between π0\pi_0 (noise, or any distribution) and π1\pi_1 (data). Given a coupling, i.e. pairs (X0,X1)(X_0, X_1), independent at first, train

min⁡v ∫01E ∥(X1−X0)−v(Xt,t)∥2 dt,Xt=tX1+(1−t)X0,\min_v\ \int_0^1\mathbb E\,\big\lVert (X_1 - X_0) - v(X_t, t)\big\rVert^2\,dt,\qquad X_t = tX_1 + (1-t)X_0,

and define the rectified flow dZt=v(Zt,t) dtdZ_t = v(Z_t, t)\,dt, Z0=X0Z_0 = X_0. This is CFM with the linear path, σmin⁡=0\sigma_{\min} = 0. The paper proves two properties:

  • Marginals are preserved (Theorem 3.3): ZtZ_t has the same distribution as XtX_t for every t, so Z1∼π1Z_1\sim\pi_1. The flow’s pairs (Z0,Z1)(Z_0, Z_1) are a new coupling, now deterministic.
  • Transport costs do not increase (Theorem 3.5): E c(Z1−Z0)≤E c(X1−X0)\mathbb E\,c(Z_1 - Z_0)\le\mathbb E\,c(X_1 - X_0) for every convex cost c. The proof is two applications of Jensen’s inequality (Exercise 8): once over time along a trajectory, and once over the pairs whose average defines v.

Because the flow never crosses itself, it “rewires” the crossing straight lines into non-crossing paths between nearby partners. Code 04 measures it on the ring. The exact flow’s coupling costs E∥Z1−Z0∥2=1.02\mathbb E\lVert Z_1 - Z_0\rVert^2 = 1.02; independent pairs cost 6.07.

4.2 Straightness

S(Z)=∫01E ∥(Z1−Z0)−Z˙t∥2 dt.S(Z) = \int_0^1\mathbb E\,\big\lVert (Z_1 - Z_0) - \dot Z_t\big\rVert^2\,dt .

S=0S = 0 exactly when every trajectory is a straight line traversed at constant speed. Then one Euler step is exact: Z1=Z0+v(Z0,0)Z_1 = Z_0 + v(Z_0, 0). The 1-rectified flow is not straight, because the marginal velocity averages crossing lines (§2.2). On the ring S = 1.5, and one Euler step sends everything to the centre (Exercise 6).

4.3 Reflow

Draw fresh noise Z0Z_0, run the trained flow to get Z1Z_1, and train a new flow on straight lines between these pairs. This is the 2-rectified flow; repeat for k. The new pairs barely cross: each Z0Z_0 was already sent to a nearby Z1Z_1. So there is little left to average, and the new flow is straighter. Code 04 trains on 50,000 pairs per round, each round starting from the previous weights, as the paper does:

flowstraightness Stransport costEuler 1 step: modes / quality2 steps4 steps100 stepsdopri5 NFE
exact 1-rectified1.5401.0220 / 0.0000 / 0.0038 / 0.7038 / 0.992—
learned 1-rectified1.5161.0180 / 0.0000 / 0.0068 / 0.5968 / 0.957121
learned 2-rectified0.0021.0088 / 0.8458 / 0.8688 / 0.8968 / 0.95161
learned 3-rectified0.0011.0058 / 0.8758 / 0.8858 / 0.8988 / 0.93855

Trajectories of the 1-, 2- and 3-rectified flows, and a one-step generator distilled from the 2-rectified flow

  • One reflow straightens almost completely (S 1.52 → 0.002). With a single Euler step, the 2-rectified flow covers all 8 modes with quality 0.85, where the 1-rectified flow produced nothing.
  • The transport cost decreases, slightly (1.018 → 1.008 → 1.005), as Theorem 3.5 promises.
  • Errors accumulate. Each round learns from the previous model’s samples, so the many-step quality drops (0.957 → 0.951 → 0.938).

The paper reports the same pattern on CIFAR-10. One Euler step gives FID 378 for the 1-rectified flow, 12.21 for the 2-rectified and 8.15 for the 3-rectified. With an adaptive RK45 solver the FID gets worse with each round (2.58, 3.36, 3.96), while the number of evaluations falls (127, 110, 104).

4.4 Distillation

A deterministic coupling is a function Z0↦Z1Z_0\mapsto Z_1. A network can regress it directly: Z1≈Z0+v(Z0,0)Z_1 \approx Z_0 + v(Z_0, 0) (Liu et al. §2.3). Code 04, Part 3:

one-step generatormodesqualitydistance to its teacher’s samples
2-rectified flow, one Euler step80.845—
distilled from the 1-rectified coupling80.9010.051
distilled from the 2-rectified coupling80.8970.049
  • Distillation beats the reflowed flow’s own single step (0.90 vs 0.85).
  • The reflowed coupling was no easier to distill. I expected it to be: the paper reports one-step FID 4.85 after 2-rectified + distillation against 6.18 for 1-rectified + distillation. Here the two are equal, and the transport costs explain why. Reflow barely changed the coupling (1.018 vs 1.008); it straightened the paths between nearly the same pairs. Distillation only sees the pairs.

In 2-D the 1-rectified coupling is already a simple function. For images it apparently is not, and reflow simplifies it. The paper’s own framing: distillation approximates the existing coupling, while rectification produces a new one with lower transport cost.

Lecture 18 meets the same goal — one-step generation from a flow — with consistency models, and InstaFlow (Liu et al. 2024) scales reflow + distillation to Stable Diffusion.


5. Sampling: solvers and function evaluations

5.1 The solver zoo

Sampling a flow means solving x˙=vθ(x,t)\dot x = v_\theta(x, t) from t=0t = 0 to 1. Cost is counted in NFE, the number of network evaluations.

methodNFE per steporderidea
Euler11x+h v(x,t)x + h\,v(x, t)
midpoint22slope at the half step
Heun22average of the slopes at both ends
RK444four slopes, Simpson-like weights
adaptive pairs (Heun–Euler, Fehlberg 2, Bogacki–Shampine 3, Dormand–Prince 5)2–6 per try2–5two solutions of different orders from the same stages; their difference estimates the error; accept or shrink the step

“Order p” means the error shrinks like hph^p once the step h is small. Code 05, Part 0 measures every order on a test ODE with a known solution: Euler 1.01, Heun 1.98, RK4 4.01, and 3.04 and 5.13 for the solutions of bosh3 and dopri5, which also checks their Butcher tableaus.

Adaptive solvers choose their own steps from a tolerance: they take large steps where the field is smooth and small ones where it changes fast. Lipman et al. sample with dopri5 at tolerance 10−510^{-5}. Pset 5 asks to compare dopri8, bosh3, fehlberg2 and adaptive_heun (torchdiffeq’s names; dopri8 is not implemented here).

5.2 Pset 5 on glyphs

Code 05 trains CFM (OT path, σmin⁡=0\sigma_{\min} = 0) on Lecture 12’s 64-dimensional glyph latents: 6000 steps, 26 s. The model is cached for Lecture 18. It is then sampled from the same 2000 noise vectors with 16 solver settings:

solverNFEvalidmini FIDerror vs dopri5 10−510^{-5}
Euler, 1 step10.17014630.755
Euler, 4 steps40.82496.50.250
Euler, 16 steps160.92228.80.079
Euler, 100 steps1000.93416.50.014
midpoint, 8 steps160.93417.90.029
Heun, 8 steps160.92815.90.036
RK4, 4 steps160.93514.70.023
adaptive Heun, tol 10−310^{-3}820.93615.30.011
Fehlberg 2, tol 10−310^{-3}90.87932.50.155
Bogacki–Shampine 3, tol 10−310^{-3}340.93415.50.011
Dormand–Prince 5, tol 10−310^{-3}430.93413.80.019
Dormand–Prince 5, tol 10−510^{-5}2650.93314.80 (reference)

FID vs NFE for every solver, OT and VP paths; samples after 1, 2, 4 and 100 Euler steps from the same noise

  • One step returns a blurred average glyph (FID 1463), the posterior mean of §2.5. Quality rises steeply to ~16–32 evaluations, then saturates near FID 14–16. Beyond that the model’s error, not the solver’s, limits the samples: dopri5 with 265 evaluations is no better than with 43.
  • At an equal budget of 16 evaluations, higher order pays. RK4 with 4 steps gives FID 14.7 against 28.8 for Euler with 16 steps. Lecture 13 found the opposite for Heun at ≤ 17 evaluations with an exact diffusion denoiser. The flow here is smooth enough that the higher-order methods’ assumptions hold.
  • A tolerance is not a guarantee. Fehlberg 2 stopped after 9 evaluations with poor samples. An adaptive pair estimates its error by the difference between its two formulas. Fehlberg 2’s nominally first-order companion is, at practical step sizes, more accurate than the second-order solution (Part 0: 2.2e-5 vs 8.1e-5 with 20 steps on the test ODE). So the difference understates the error of the solution actually used, and the solver takes steps that are too large.

Pset 5’s second ablation: the diffusion conditional field. The same network trained on the VP path is worse at every setting:

solverOT path (FID / valid)VP path (FID / valid)
Euler, 8 steps45.9 / 0.89555.3 / 0.746
Euler, 100 steps16.5 / 0.93420.1 / 0.857
dopri5, tol 10−310^{-3}13.8 / 0.934 (43 NFE)19.1 / 0.865 (55 NFE)

The VP model is not only harder to integrate; it is a worse model, as in code 03. This is the same ordering as Lipman et al.’s table in §2.5.


6. Flow matching and diffusion: one family

Put Lectures 12–13 and this lecture side by side, with both written in the same time direction:

diffusion (Lectures 12–13)flow matching (this lecture)
forward corruptionz=αx+σεz = \alpha x + \sigma\varepsilon (VP: α2+σ2=1\alpha^2 + \sigma^2 = 1)xt=atx1+btx0x_t = a_t x_1 + b_t x_0 (any a,ba, b; linear: a+b=1a + b = 1)
network predictsε (or x, or v)velocity a˙x1+b˙x0\dot a x_1 + \dot b x_0
loss weightingε-MSE = SNR-weighted x-errorvelocity MSE = another weighting (Exercise 4)
samplerancestral SDE or DDIM = Euler on the PF-ODEany ODE solver; an SDE via the score (Lecture 17)

Everything in the left column has a counterpart on the right.

  • The paths are the same family. VP diffusion is one affine Gaussian path, and by §2.5 every affine path defines the same exact flow up to clock and scale.
  • The predictions are interconvertible (§3.2; Lecture 13 §1.2).
  • The losses differ only in how they weight noise levels. Kingma & Gao (2023) write the common diffusion objectives, the flow-matching one included, as weightings of one denoising loss across noise levels (and, for monotonic weightings, as the ELBO of augmented data).

What actually differs between a “diffusion model” and a “flow-matching model” in practice is a bundle of choices: path, target, time sampling, solver. Lipman et al.’s OT path, Albergo’s trigonometric interpolant and Liu’s rectified flow each pick one bundle, and Lecture 17 shows how much each choice matters at scale. Gao et al.’s blog post Diffusion Meets Flow Matching: Two Sides of the Same Coin (2024) makes the equivalence explicit. MIT6.S184 Lecture 2 (in Chinese) derives flow matching from the ODE / SDE side.


7. Summary

  1. Flow matching regresses a network on a velocity field that generates a path from noise to data. No simulation is needed during training.
  2. Conditional → marginal. The marginal velocity is the posterior average of simple conditional velocities (Theorem 1). CFM regresses on the conditional ones and has the same gradient (Theorem 2). The loss floor (3.45 on the ring) is the conditional target’s variance, and minibatch gradients are ~8× noisier than with the exact target.
  3. Gaussian paths. The conditional field is u=σ˙σ(x−μ)+μ˙u = \frac{\dot\sigma}{\sigma}(x - \mu) + \dot\mu. The OT path gives the target x1−(1−σmin⁡)x0x_1 - (1-\sigma_{\min})x_0. Between two Gaussians the flow realizes the OT map at the end points but bends in between.
  4. All affine Gaussian paths define the same flow up to clock and scale. Paths differ in how easily solvers integrate them (linear best; VP’s changes pile up near the data) and in how well networks learn them.
  5. Stochastic interpolants. Any two distributions, any interpolant. Velocity ↔ score by two linear equations. Data-to-data bridges benefit from a little latent noise.
  6. Rectified flow. It preserves marginals and does not increase convex transport costs. Reflow straightens (S 1.52 → 0.002 in one round), enabling one-step sampling, at the price of accumulated error. Distillation turns a coupling into a one-step generator.
  7. Solvers. Quality saturates at ~16–40 evaluations on glyphs. RK4 with 4 steps matches dopri5. Adaptive tolerances can mislead (Fehlberg 2).

8. Common pitfalls

  1. “The CFM loss should go to zero.” It plateaus at the conditional target’s variance; only the excess over that floor measures the model (§2.3).
  2. “The OT path gives straight trajectories.” Its conditional paths are straight. The marginal trajectories bend whenever the data are not a single Gaussian (§2.2, §2.5); straight marginal trajectories need reflow or distillation.
  3. “Different paths learn different generative models.” With exact fields, affine Gaussian paths define the same noise-to-data map (§2.5). They differ in learnability and integrability.
  4. “More solver accuracy, better samples.” Beyond ~40 evaluations, model error dominates (§5.2). And adaptive solvers only control their own error estimate (Fehlberg 2).
  5. “Reflow is free.” Each round trains on generated samples and accumulates error; the paper’s RK45 FIDs get worse with every round (§4.3).
  6. “Flow matching needs a Gaussian source.” Any source works (§3.3). Gaussian noise does make the flow’s job easier, and latent noise in the interpolant helps bridges.
  7. Time direction. Flow-matching papers use t = 0 for noise, diffusion papers t = 0 for data. Formulas copied across without flipping t produce nonsense.

9. Exercises

Exercise 1 (the conditional field of a Gaussian path). Let ψt(x0)=σtx0+μt\psi_t(x_0) = \sigma_t x_0 + \mu_t. Show that the velocity of ψt(x0)\psi_t(x_0), written as a function of the current point x=ψt(x0)x = \psi_t(x_0), is ut(x∣x1)=σ˙tσt(x−μt)+μ˙tu_t(x\mid x_1) = \frac{\dot\sigma_t}{\sigma_t}(x - \mu_t) + \dot\mu_t. Specialize it to the OT path.

Answer

ddtψt(x0)=σ˙tx0+μ˙t\frac{d}{dt}\psi_t(x_0) = \dot\sigma_t x_0 + \dot\mu_t. Invert the map: x0=(x−μt)/σtx_0 = (x - \mu_t)/\sigma_t. Substitute to get the formula.

OT path. μt=tx1\mu_t = tx_1, σt=1−(1−σmin⁡)t\sigma_t = 1 - (1-\sigma_{\min})t, so σ˙t=−(1−σmin⁡)\dot\sigma_t = -(1-\sigma_{\min}) and

ut(x∣x1)=−(1−σmin⁡)(x−tx1)1−(1−σmin⁡)t+x1=x1−(1−σmin⁡)x1−(1−σmin⁡)t.u_t(x\mid x_1) = \frac{-(1-\sigma_{\min})(x - tx_1)}{1-(1-\sigma_{\min})t} + x_1 = \frac{x_1 - (1-\sigma_{\min})x}{1 - (1-\sigma_{\min})t}.

The last step puts both terms over the common denominator; the tx1tx_1 terms cancel.

Exercise 2 (Theorem 2 in full). Prove that ∇θLCFM=∇θLFM\nabla_\theta\mathcal L_{\text{CFM}} = \nabla_\theta\mathcal L_{\text{FM}}, and identify the constant C. Where does the assumption pt(x)>0p_t(x) > 0 enter?

Answer

LFM=Ept[∥v∥2−2⟨v,ut⟩+∥ut∥2]\mathcal L_{\text{FM}} = \mathbb E_{p_t}\big[\lVert v\rVert^2 - 2\langle v, u_t\rangle + \lVert u_t\rVert^2\big] and LCFM=Eq,pt(⋅∣x1)[∥v∥2−2⟨v,ut(⋅∣x1)⟩+∥ut(⋅∣x1)∥2]\mathcal L_{\text{CFM}} = \mathbb E_{q, p_t(\cdot\mid x_1)}\big[\lVert v\rVert^2 - 2\langle v, u_t(\cdot\mid x_1)\rangle + \lVert u_t(\cdot\mid x_1)\rVert^2\big].

  • First terms. Equal: the marginal of x is ptp_t in both.
  • Cross terms. Equal by the definition of utu_t as the posterior average. Its definition divides by pt(x)p_t(x), hence the assumption pt(x)>0p_t(x) > 0.
  • Last terms. Do not involve θ.

So C=E∥ut(x∣x1)∥2−E∥ut(x)∥2=E∥ut(x∣x1)−ut(x)∥2C = \mathbb E\lVert u_t(x\mid x_1)\rVert^2 - \mathbb E\lVert u_t(x)\rVert^2 = \mathbb E\lVert u_t(x\mid x_1) - u_t(x)\rVert^2, the variance of the conditional target (law of total variance).

Exercise 3 (Pset 5, problem 3a). Derive the marginal velocity of §2.4 for the Gaussian-to-Gaussian problem.

Answer

The joint distribution. xt=(1−t)x0+tx1x_t = (1-t)x_0 + tx_1 and D=x1−x0D = x_1 - x_0 are jointly Gaussian (linear in independent Gaussians), per coordinate:

  • Ext=mt\mathbb E x_t = m_t and Var xt=st2=(1−t)2σ02+t2σ12\mathrm{Var}\,x_t = s_t^2 = (1-t)^2\sigma_0^2 + t^2\sigma_1^2;
  • ED=μ1−μ0\mathbb E D = \mu_1 - \mu_0;
  • Cov(D,xt)=Cov(x1−x0,(1−t)x0+tx1)=tσ12−(1−t)σ02\mathrm{Cov}(D, x_t) = \mathrm{Cov}(x_1 - x_0, (1-t)x_0 + tx_1) = t\sigma_1^2 - (1-t)\sigma_0^2.

The regression. For jointly Gaussian variables, E[D∣xt]=ED+Cov(D,xt)Var xt(xt−mt)\mathbb E[D\mid x_t] = \mathbb E D + \frac{\mathrm{Cov}(D, x_t)}{\mathrm{Var}\,x_t}(x_t - m_t).

A check. The flow is xt=mt+stσ0(x0−μ0)x_t = m_t + \frac{s_t}{\sigma_0}(x_0 - \mu_0). Its velocity, m˙t+s˙tst(x−mt)\dot m_t + \frac{\dot s_t}{s_t}(x - m_t), matches the formula because sts˙t=tσ12−(1−t)σ02s_t\dot s_t = t\sigma_1^2 - (1-t)\sigma_0^2. At t=1t = 1 it gives the OT map μ1+σ1σ0(x0−μ0)\mu_1 + \frac{\sigma_1}{\sigma_0}(x_0 - \mu_0).

Exercise 4 (one object, four units). For the path xt=ax1+bx0x_t = a x_1 + b x_0 with Gaussian x0x_0, express x^1=E[x1∣xt]\hat x_1 = \mathbb E[x_1\mid x_t], x^0\hat x_0 and the score in terms of the velocity v. Show that the velocity MSE equals (a˙−ab˙/b)2(\dot a - a\dot b/b)^2 times the x^1\hat x_1-MSE.

Answer

The conversions. From x=ax^1+bx^0x = a\hat x_1 + b\hat x_0 and v=a˙x^1+b˙x^0v = \dot a\hat x_1 + \dot b\hat x_0:

x^0=av−a˙xab˙−a˙b,x^1=x−bx^0a,∇log⁡pt=−x^0/b.\hat x_0 = \frac{av - \dot a x}{a\dot b - \dot a b},\qquad \hat x_1 = \frac{x - b\hat x_0}{a},\qquad \nabla\log p_t = -\hat x_0 / b .

The MSE. Substitute x^0=(x−ax^1)/b\hat x_0 = (x - a\hat x_1)/b into v:

v=b˙bx+(a˙−ab˙b)x^1.v = \frac{\dot b}{b}x + \Big(\dot a - \frac{a\dot b}{b}\Big)\hat x_1 .

Two velocity predictions at the same x differ by (a˙−ab˙/b)(\dot a - a\dot b/b) times the difference of their x^1\hat x_1, so the squared errors scale by its square.

For the linear path. The factor is 1+t/(1−t)=1/(1−t)1 + t/(1-t) = 1/(1-t). The velocity loss weights the x^1\hat x_1-error by 1/(1−t)21/(1-t)^2, heavily toward the data end: yet another weighting of the same denoising problem (Lecture 13 Exercise 3).

Exercise 5 (affine paths are reparameterizations). Let paths A and B satisfy at/bt=ατ(t)/βτ(t)a_t/b_t = \alpha_{\tau(t)}/\beta_{\tau(t)}, and let c(t)=bt/βτ(t)c(t) = b_t/\beta_{\tau(t)}. Show that if ZBZ^B solves B’s marginal ODE, then ZtA=c(t) Zτ(t)BZ^A_t = c(t)\,Z^B_{\tau(t)} solves A’s, and that the two flows have the same end-point map.

Answer

Pathwise relation. For each pair, xtA=atx1+btx0=c(t)(ατx1+βτx0)=c xτBx^A_t = a_t x_1 + b_t x_0 = c(t)(\alpha_\tau x_1 + \beta_\tau x_0) = c\,x^B_\tau. Differentiating: ∂txtA=c˙ xτB+c τ˙ ∂τxτB\partial_t x^A_t = \dot c\,x^B_\tau + c\,\dot\tau\,\partial_\tau x^B_\tau.

Take conditional expectations. Given xtA=xx^A_t = x, equivalently xτB=x/cx^B_\tau = x/c:

utA(x)=c˙cx+c τ˙ uτB(x/c).u^A_t(x) = \frac{\dot c}{c}x + c\,\dot\tau\,u^B_\tau(x/c).

The trajectory. ZtA=cZτBZ^A_t = cZ^B_\tau satisfies Z˙A=c˙ZB+cτ˙uτB(ZB)\dot Z^A = \dot c Z^B + c\dot\tau u^B_\tau(Z^B). Since ZB=ZA/cZ^B = Z^A/c, this is exactly utA(ZA)u^A_t(Z^A), so ZAZ^A solves A’s ODE.

The end points. At t=0t = 0: a=α=0a = \alpha = 0 and b=β=1b = \beta = 1, so c=1c = 1 and τ=0\tau = 0. At t=1t = 1: b=β=0b = \beta = 0 and a=α=1a = \alpha = 1, so c=1c = 1 (by c=a/αc = a/\alpha) and τ=1\tau = 1. Hence Z1A=Z1BZ^A_1 = Z^B_1 from the same Z0Z_0.

Code 03 confirms it to 3⋅10−93\cdot10^{-9}.

Exercise 6 (one Euler step). For the linear path with σmin⁡=0\sigma_{\min} = 0 and a Gaussian source independent of the data, show that one Euler step from x0x_0 gives E[x1]\mathbb E[x_1]. What does it give for the cosine path?

Answer

Linear path. x0+u0(x0)x_0 + u_0(x_0), with u0(x)=E[x1−x0∣x0=x]=E[x1]−xu_0(x) = \mathbb E[x_1 - x_0\mid x_0 = x] = \mathbb E[x_1] - x by independence. So the step lands at E[x1]\mathbb E[x_1] for every starting point: the centre of the ring (code 03’s error 2.0), or the average glyph (code 05’s FID 1463).

Cosine path. a˙0=π/2\dot a_0 = \pi/2 and b˙0=0\dot b_0 = 0, so u0(x)=π2E[x1∣x0]=π2E[x1]u_0(x) = \frac{\pi}{2}\mathbb E[x_1\mid x_0] = \frac{\pi}{2}\mathbb E[x_1]. That is 0 for centred data: the step does not move. Either way, one step from a non-straight flow cannot produce samples.

Exercise 7 (the floor at the ends). For the linear path, show that the CFM floor E∥(x1−x0)−ut(xt)∥2\mathbb E\lVert(x_1 - x_0) - u_t(x_t)\rVert^2 equals tr Cov(x1)\mathrm{tr}\,\mathrm{Cov}(x_1) at t=0t = 0 and tr Cov(x0)\mathrm{tr}\,\mathrm{Cov}(x_0) at t=1t = 1.

Answer

At t = 0. xt=x0x_t = x_0 and u0=E[x1]−x0u_0 = \mathbb E[x_1] - x_0, so the residual is x1−Ex1x_1 - \mathbb E x_1, with variance tr Cov(x1)\mathrm{tr}\,\mathrm{Cov}(x_1) (≈ 4 on the ring).

At t = 1. xt=x1x_t = x_1 and u1=x1−E[x0∣x1]=x1−Ex0u_1 = x_1 - \mathbb E[x_0\mid x_1] = x_1 - \mathbb E x_0 by independence. The residual is −(x0−Ex0)-(x_0 - \mathbb E x_0), with variance tr Cov(x0)=d=2\mathrm{tr}\,\mathrm{Cov}(x_0) = d = 2.

Code 02’s first and last bins: 4.43 and 1.15. Bins average over a range of t; the last bin covers the dip just before t=1t = 1.

Exercise 8 (reflow does not increase transport costs). Prove E c(Z1−Z0)≤E c(X1−X0)\mathbb E\,c(Z_1 - Z_0)\le\mathbb E\,c(X_1 - X_0) for convex c, using Theorem 3.3 (equal marginals).

Answer

Jensen over time. Z1−Z0=∫01v(Zt,t) dtZ_1 - Z_0 = \int_0^1 v(Z_t, t)\,dt, so c(Z1−Z0)≤∫01c(v(Zt,t)) dtc(Z_1 - Z_0)\le\int_0^1 c(v(Z_t, t))\,dt.

Jensen over pairs. v(z,t)=E[X1−X0∣Xt=z]v(z, t) = \mathbb E[X_1 - X_0\mid X_t = z], so c(v(z,t))≤E[c(X1−X0)∣Xt=z]c(v(z, t))\le\mathbb E[c(X_1 - X_0)\mid X_t = z].

Combine. Take expectations and use that ZtZ_t and XtX_t have the same distribution:

E c(Z1−Z0)≤∫01E[c(X1−X0)] dt=E c(X1−X0).\mathbb E\,c(Z_1 - Z_0)\le\int_0^1\mathbb E\big[c(X_1 - X_0)\big]\,dt = \mathbb E\,c(X_1 - X_0).

Equality requires both Jensen steps to be tight: straight trajectories, and no averaging over different directions. That is a fixed point of reflow.

Exercise 9 (hands-on). (a) In 03_paths.py, sample the VP model with Euler on a grid uniform in log-SNR instead of in t. How much of its gap to the linear path closes? (b) In 04_reflow.py, generate the reflow pairs with the exact 1-rectified flow instead of the learned one. Does the 100-step quality still drop across rounds? (c) In 05_glyph_solvers.py, add Euler on a grid that is denser near t=1t = 1 (e.g. ti=1−(1−i/N)2t_i = 1 - (1 - i/N)^2). Does Euler with 8 steps improve?

Answer (sketch)

(a) Code 03 traced most of the VP path’s handicap to its velocity changes piling up after t = 0.75. A grid that follows the signal-to-noise ratio spreads the steps evenly over those changes, so expect a large part of the gap to close. It will not reach the linear path, though: the learned VP field is also less accurate.

(b) With exact pairs the 2-rectified flow trains on the true coupling, so the inherited error disappears. The remaining drop is the reflow network’s own fitting error.

(c) The glyph flow’s velocity changes fastest near the data, where the modes form; a quadratic grid puts more steps there. Expect a visible gain at 8 steps and little at 100.


10. Further reading

Required papers

  • Y. Lipman, R. T. Q. Chen, H. Ben-Hamu, M. Nickel, M. Le, Flow Matching for Generative Modeling, ICLR 2023.
  • M. S. Albergo, E. Vanden-Eijnden, Building Normalizing Flows with Stochastic Interpolants, ICLR 2023.
  • X. Liu, C. Gong, Q. Liu, Flow Straight and Fast: Learning to Generate and Transfer Data with Rectified Flow, ICLR 2023.

Optional

  • T. Fjelde, E. Mathieu, V. Dutordoir, An Introduction to Flow Matching (Cambridge MLG blog), 2024 — Pset 5’s Gaussian-to-Gaussian figure follows its Figures 11–12.

Background and follow-ups

  • R. T. Q. Chen, Y. Rubanova, J. Bettencourt, D. Duvenaud, Neural Ordinary Differential Equations, NeurIPS 2018; W. Grathwohl et al., FFJORD, ICLR 2019 (CNFs trained by maximum likelihood).
  • M. S. Albergo, N. M. Boffi, E. Vanden-Eijnden, Stochastic Interpolants: A Unifying Framework for Flows and Diffusions, 2023 (latent noise γ(t)z\gamma(t)z; ODE and SDE samplers).
  • A. Tong et al., Improving and Generalizing Flow-Based Generative Models with Minibatch Optimal Transport, TMLR 2024 (OT couplings within minibatches instead of reflow).
  • X. Liu et al., InstaFlow: One Step is Enough for High-Quality Diffusion-Based Text-to-Image Generation, ICLR 2024 (reflow + distillation for Stable Diffusion).
  • D. Kingma, R. Gao, Understanding Diffusion Objectives as the ELBO with Simple Data Augmentation, NeurIPS 2023; R. Gao et al., Diffusion Meets Flow Matching: Two Sides of the Same Coin (blog), 2024.
  • Y. Lipman et al., Flow Matching Guide and Code, 2024 (a comprehensive tutorial with a reference library).

Related notes in this course


11. Next lecture

Lecture 17: Scaling flow matching, and discrete flow matching (reading session · 11/05).

This lecture established that path, target, time weighting and sampler are separate choices around one regression. Lecture 17 asks which choices matter at scale:

  • Stable Diffusion 3 trains rectified flows with time samplers that favour the middle of the path (logit-normal), a two-stream Transformer (MM-DiT), and a resolution-dependent shift of t — Lecture 13’s simple-diffusion shift in flow language.
  • SiT separates interpolant, prediction target and ODE vs SDE sampling on a DiT backbone, and measures each.
  • Discrete flow matching carries probability paths and velocities over to tokens, where masking (Lecture 15) reappears as one path among many.
← 返回课程目录