Dive into Deep Learning · §38.7
Flow Matching, Rectified Flows, and Optimal Transport
probability paths · Conditional Flow Matching · score–velocity dictionary · Reflow · Wasserstein optimal transport
Motivation
Unified Chapter Clock
To compare score-based diffusion (§38.6) and flow matching (§38.7) on the exact same footing, this book keeps one clock throughout: data \mathbf{x}_0 \sim p_0 sit at t = 0, and standard Gaussian noise \mathbf{x}_1 = \boldsymbol{\epsilon} \sim p_1 = \mathcal{N}(\mathbf{0}, \mathbf{I}) sits at t = 1.
Flow Matching Formulation
A velocity field \mathbf{u}_t: \mathbb{R}^d \to \mathbb{R}^d realizes a probability path (p_t)_{t \in [0, 1]} between p_0 and p_1 if the pair satisfies the continuity equation \partial_t p_t + \nabla \cdot (p_t\,\mathbf{u}_t) = 0. How do we construct (p_t, \mathbf{u}_t) when p_0 is known only through samples?
Simulation-Free Training
Because the marginal velocity is the posterior mean \mathbf{u}_t(\mathbf{x}) = \mathbb{E}[\mathbf{u}_t(\mathbf{x} \mid \mathbf{z}) \mid \mathbf{x}_t = \mathbf{x}], the Regression Lemma equates the marginal and conditional objectives up to a constant floor C:
\mathcal{L}_{\text{CFM}}(\boldsymbol{\theta}) = \mathbb{E}_{t, \mathbf{z}, \mathbf{x}}\!\left[\left\|\mathbf{u}_{\boldsymbol{\theta}}(\mathbf{x}, t) - \mathbf{u}_t(\mathbf{x} \mid \mathbf{z})\right\|^2\right] = \mathcal{L}_{\text{FM}}(\boldsymbol{\theta}) + C
Unifying Diffusion and Flow Matching
On any Gaussian path \mathbf{x}_t = \alpha_t\mathbf{x}_0 + \sigma_t\boldsymbol{\epsilon}, score \mathbf{s}, noise \hat{\boldsymbol{\epsilon}}, clean point \hat{\mathbf{x}}_0, and velocity \mathbf{u}_t are affine conversions of one another:
\mathbf{u}_t(\mathbf{x}) = \frac{\dot{\alpha}_t}{\alpha_t}\,\mathbf{x} - \left(\sigma_t\dot{\sigma}_t - \sigma_t^2\,\frac{\dot{\alpha}_t}{\alpha_t}\right)\nabla\log p_t(\mathbf{x})
Analytic Flow Matching
Take two data points x_0 \in \{-a, +a\} (a = 2, probability \frac{1}{2} each) paired independently with noise \epsilon \sim \mathcal{N}(0, 1) along the straight path x_t = (1 - t)x_0 + t\epsilon (\dot{x}_t = \epsilon - x_0 = \frac{x_t - x_0}{t}):
The 1-Step Collapse
At t = 1, a noise draw \mathbf{x}_1 = \boldsymbol{\epsilon} carries zero information about its independently drawn data partner \mathbf{x}_0, so \mathbb{E}[\mathbf{x}_0 \mid \mathbf{x}_1 = \mathbf{x}] = \mathbb{E}[\mathbf{x}_0] and \mathbf{u}_1(\mathbf{x}) = \mathbf{x} - \mathbb{E}[\mathbf{x}_0]. A single Euler step of length 1 from t = 1 to t = 0 therefore maps every sample to the data mean \mathbf{x} - \mathbf{u}_1(\mathbf{x}) = \mathbb{E}[\mathbf{x}_0] = \mathbf{0}:
slope at the origin at t = 0.9, 0.8, 0.5: [ 0.56 -0.31 -14. ] t* = 0.8284
1 Euler step(s): mass within 0.1 of +-a 0.000, mean |x| 0.00
2 Euler step(s): mass within 0.1 of +-a 0.359, mean |x| 1.48
4 Euler step(s): mass within 0.1 of +-a 0.987, mean |x| 1.99
8 Euler step(s): mass within 0.1 of +-a 0.999, mean |x| 2.00
Why the Marginal Flow Bends
Rectified Flow pairs \mathbf{x}_0 \sim p_0 and \boldsymbol{\epsilon} \sim p_1 along straight constant-speed segments \mathbf{x}_t = (1-t)\mathbf{x}_0 + t\boldsymbol{\epsilon} and trains \mathbf{u}_{\boldsymbol{\theta}}(\mathbf{x}_t, t) by least squares against the constant difference \boldsymbol{\epsilon} - \mathbf{x}_0:
\mathcal{L}_{\text{RF}}(\boldsymbol{\theta}) = \mathbb{E}_{t,\, \mathbf{x}_0,\, \boldsymbol{\epsilon}}\!\left[\left\|\mathbf{u}_{\boldsymbol{\theta}}\big((1 - t)\mathbf{x}_0 + t\boldsymbol{\epsilon},\; t\big) - (\boldsymbol{\epsilon} - \mathbf{x}_0)\right\|^2\right]
Evaluating 2D Sample Quality
To benchmark flow matching in \mathbb{R}^2, we standardize 8{,}192 training points from the two-moons distribution and measure distributional distance to 2{,}048 held-out points via the squared energy distance:
\mathcal{E}^2(P, Q) = 2\,\mathbb{E}\|X - Y\| - \mathbb{E}\|X - X'\| - \mathbb{E}\|Y - Y'\| \ge 0 \qquad (\mathcal{E}^2 = 0 \iff P = Q)
fresh data sample vs held-out: -0.0000
point mass at the data mean vs held-out: 0.904
The Crossing Variance Floor
Before training any neural network, we can compute the exact irreducible CFM loss floor C = \frac{1}{2}\mathbb{E}_{t, \mathbf{x}_t}\big[\operatorname{Var}(\boldsymbol{\epsilon} - \mathbf{x}_0 \mid \mathbf{x}_t)\big] per coordinate by averaging the posterior variance of \mathbf{x}_0 over the 8{,}192 training points:
floor of the independent pairing, per coordinate: 1.39
Reaching the Crossing Plateau
Training a 2-hidden-layer tanh MLP (256 units per layer) with Adam and cosine learning-rate decay over 10{,}000 minibatches of 2{,}048 independent (\mathbf{x}_0, \boldsymbol{\epsilon}) pairs:
CFM loss, mean of the last 500 batches: 1.40
Discretization vs. Field Error
Integrating the trained velocity field \mathbf{u}_{\boldsymbol{\theta}}(\mathbf{x}, t) backward from t = 1 to t = 0 on 2{,}048 noise points across K \in \{1, 2, 4, 8, 16, 32, 64\} Euler steps:
1: 0.736 2: 0.170 4: 0.031 8: 0.004 16: -0.000 32: -0.001 64: -0.001
Solver Error vs. Network Error
Plotting the generated point clouds across step counts separates the solver’s discretization error from the network’s approximation error:
Increasing the step count from K = 1 to K = 8 unfolds the mean blob into two crescents, and K = 32 clears most of the points strewn between them (removing Euler truncation error). The slightly thicker crescents and the few strays left at K = 32 do not go away with more steps: they come from the \approx 0.03 excess regression loss above the floor C.
Rectified Flow Re-Pairing
Reflow integrates the trained ODE from noise \mathbf{Z}_1 = \mathbf{X}_1 \sim p_1 to t = 0 to obtain deterministic pairs (\mathbf{Z}_0, \mathbf{Z}_1) and retrains on their straight chords:
\mathbb{E}\!\left[\|\mathbf{Z}_1 - \mathbf{Z}_0\|^2\right] \le \mathbb{E}\!\left[\|\mathbf{X}_1 - \mathbf{X}_0\|^2\right] - C
Eliminating Segment Crossings
Generating 8{,}192 coupled pairs (\hat{\mathbf{x}}_0(\boldsymbol{\epsilon}), \boldsymbol{\epsilon}) by integrating the first network for 32 Euler steps and retraining the exact same architecture on these re-paired endpoints:
reflow loss, mean of the last 500 batches: 0.0007
Reflow Measured on Two Moons
Comparing the original and reflowed networks across K \in \{1, 2, 32\} Euler steps and pair displacement costs:
1 Euler step(s): squared energy distance original 0.736 reflowed -0.001
2 Euler step(s): squared energy distance original 0.170 reflowed -0.001
32 Euler step(s): squared energy distance original -0.001 reflowed -0.001
mean squared pair length: independent 4.04, reflowed 0.29
Geometric Straightening
How far do the 32-step ODE trajectories wander from the straight line segment (chord) connecting their noise start \mathbf{Z}_1 and generated data endpoint \mathbf{Z}_0?
mean distance from the chord: original 0.249, reflowed 0.005
Static vs. Dynamic Couplings
A coupling \pi \in \Pi(p_0, p_1) is a joint distribution with marginals p_0 and p_1, and the squared 2-Wasserstein distance is the minimum mean squared displacement across all couplings:
W_2^2(p_0, p_1) = \min_{\pi \in \Pi(p_0, p_1)} \mathbb{E}_{(\mathbf{x}_0, \mathbf{x}_1) \sim \pi}\!\left[\|\mathbf{x}_1 - \mathbf{x}_0\|^2\right]
Kinetic Energy Lower Bound
How does the static Wasserstein distance W_2^2(p_0, p_1) relate to the continuous velocity fields \mathbf{u}_t(\mathbf{x}) learned by flow matching?
Theorem (Benamou–Brenier, 2000): Across all admissible pairs (p_t, \mathbf{u}_t) solving the continuity equation \partial_t p_t + \nabla\cdot(p_t\mathbf{u}_t) = 0 from p_0 to p_1, the minimum kinetic energy equals W_2^2(p_0, p_1): W_2^2(p_0, p_1) = \min_{(p_t, \mathbf{u}_t)} \int_0^1 \int_{\mathbb{R}^d} \|\mathbf{u}_t(\mathbf{x})\|^2\,p_t(\mathbf{x})\,d\mathbf{x}\,dt and is achieved by moving each pair (\mathbf{x}_0, \mathbf{x}_1) of the optimal Brenier coupling \pi^\star along a straight line at constant speed (C = 0, exact in 1 Euler step).
data N(0,4), noise N(0,1): independent pairing 4.99, sorted pairing 0.985
sorted, straight kinetic energy 1.000
independent, straight kinetic energy 1.858
cosine path kinetic energy 1.234
Euler vs. Heun on PF-ODE
When sampling from a curved probability-flow ODE on a tight budget of 10 to 80 function evaluations (NFE), does 2nd-order Heun (2 NFE/step) always beat 1st-order Euler (1 NFE/step)?
NFE 10 20 40 80
Euler, uniform 0.0439 0.0204 0.0099 0.0049
Heun, uniform 0.0814 0.0285 0.0086 0.0024
Euler, squared 0.1084 0.0533 0.0264 0.0131
Heun, squared 0.0340 0.0148 0.0047 0.0013
Diagnosing Step Errors
Decomposing terminal error into signed step contributions c_k carried to t = 0 shows how grid placement interacts with solver order:
surviving share 10 20 40 80
Euler, uniform 0.11 0.11 0.12 0.12
Heun, uniform 0.59 0.66 0.71 0.73
...
Heun, uniform 0.1317 0.0414 0.0119 0.0032
Euler, squared 0.2472 0.1327 0.0691 0.0353
Heun, squared 0.1258 0.0397 0.0113 0.0030
share of the path length covered by t = 0.5: 0.09
Concept Check
Test your understanding of Conditional Flow Matching, Reflow, and Optimal Transport:
Concept Check
Answers:
Key Takeaways
| Concept / Theorem | Mathematical Form | Generative Modeling & Systems Role |
|---|---|---|
| Marginal Velocity | \mathbf{u}_t(\mathbf{x}) = \mathbb{E}\big[\mathbf{u}_t(\mathbf{x} \mid \mathbf{z}) \mid \mathbf{x}_t = \mathbf{x}\big] | Posterior mean of per-sample velocities satisfies \partial_t p_t + \nabla\cdot(p_t\mathbf{u}_t) = 0 |
| CFM Theorem | \mathcal{L}_{\text{CFM}}(\boldsymbol{\theta}) = \mathcal{L}_{\text{FM}}(\boldsymbol{\theta}) + C | Trains \mathbf{u}_{\boldsymbol{\theta}} by simulation-free least squares; floor C measures path crossings |
| Score–Velocity Identity | \mathbf{u}_t(\mathbf{x}) = \frac{\dot{\alpha}_t}{\alpha_t}\mathbf{x} - (\sigma_t\dot{\sigma}_t - \sigma_t^2\frac{\dot{\alpha}_t}{\alpha_t})\nabla\log p_t | Converts any diffusion noise/score predictor into a flow velocity without retraining |
| Rectified Flow & Reflow | \mathbf{x}_t = (1-t)\mathbf{x}_0 + t\boldsymbol{\epsilon}, re-pair (\mathbf{Z}_0, \mathbf{Z}_1) | Eliminates crossings (C \to 0), lowers cost \mathbb{E}\,c(\mathbf{Z}_1 - \mathbf{Z}_0), enables 1-step Euler generation |
| Benamou–Brenier Theorem | W_2^2(p_0, p_1) = \min_{(p_t, \mathbf{u}_t)} \int_0^1 \mathbb{E}_{p_t}\|\mathbf{u}_t\|^2\,dt | Minimum kinetic energy equals W_2^2, achieved by straight constant-speed Brenier maps |