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 moduleflow_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:
| paper | vocabulary | the key move |
|---|---|---|
| Lipman et al., Flow Matching | continuous normalizing flows, probability paths | regress on conditional vector fields; same gradient as the intractable marginal one |
| Albergo & Vanden-Eijnden, Stochastic Interpolants | interpolants between two densities | any interpolant defines a velocity; a quadratic loss learns it, between any two distributions |
| Liu, Gong & Liu, Rectified Flow | couplings and transport | straight 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
- Explain why maximum-likelihood training of continuous normalizing flows is expensive, and what flow matching avoids (§1).
- 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).
- Derive the conditional field of a Gaussian path; write the OT and diffusion paths; solve Pset 5’s Gaussian-to-Gaussian problem (§2.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).
- Convert between velocity, score and denoiser; build a bridge between two non-Gaussian distributions (§3).
- State rectified flow’s guarantees (marginals preserved, transport cost non-increasing), and explain reflow, straightness and distillation (§4).
- Compare ODE solvers by function evaluations, fixed and adaptive (§5).
0.3 Notation and time direction
In this lecture time runs from noise () to data (), as in all three papers and Pset 5:
Lectures 12–13 used the opposite direction ( data). So do SD3 and SiT in Lecture 17. Always check a paper’s convention before copying a formula.
0.4 Map
| Section | Content | Code |
|---|---|---|
| §1 | Background: continuous normalizing flows, the continuity equation | — |
| §2 | Flow matching: marginal and conditional fields, Gaussian paths | 01_gaussian_to_gaussian.py, 02_fm_vs_cfm.py, 03_paths.py |
| §3 | Stochastic interpolants: any two densities; velocity and score | 06_interpolants.py |
| §4 | Rectified flow: straightness, reflow, distillation | 04_reflow.py |
| §5 | Sampling: ODE solvers and NFE; Pset 5 on glyphs | 05_glyph_solvers.py |
| §6 | Flow 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:
The flow maps a starting point to where the ODE has carried it by time t. If v is smooth, is invertible (run the ODE backwards). It pushes forward to a distribution . A network 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):
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):
The rate at which mass at x changes equals the net flow of mass into x. We say that v generates the probability path . Code 01 checks this equation numerically for its exact field (relative residual –).
1.3 Why training CNFs was expensive
Maximum likelihood needs for each data point x:
- integrate the ODE backwards from x to ;
- accumulate along the way, using trace estimators in high dimension (FFJORD, Grathwohl et al. 2019);
- 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 that generates a path from = noise to = data, we could just regress on it:
No ODE is solved during training. The rest of §2 shows how to get a usable .
2. Flow matching (Lipman et al.)
2.1 Build the path from conditional paths
Writing down and directly is hopeless: 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 , choose a simple conditional path : at , and a tiny blob around at .
- Choose a conditional velocity that generates it. For Gaussian paths it is known in closed form (§2.4).
- Mix over the data:
The marginal velocity is the posterior average of the conditional velocities: all data points whose conditional path could have produced x at time t, weighted by how likely that is.
Theorem 1 (Lipman et al.). If each generates , then generates .
Proof. Differentiate the mixture, use the conditional continuity equations, and pull the divergence out of the integral:
At the mixture of tiny blobs is (nearly) the data distribution. So the ODE with velocity , 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 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:

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
still involves the unknown . The conditional flow matching loss uses only the conditional field:
Theorem 2 (Lipman et al.). , where C does not depend on θ. Hence the gradients are equal.
Proof. Expand both squares. The terms are identical, because in both. The cross terms are equal because is the posterior mean of :
What remains does not involve θ.
The constant is the variance of the conditional target around its mean, . 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 is known exactly (exact_velocity), for networks at four stages of training:
| training step | difference | ||
|---|---|---|---|
| 0 (random) | 6.753 | 3.318 | 3.435 ± 0.016 |
| 100 | 4.016 | 0.576 | 3.440 ± 0.013 |
| 500 | 3.902 | 0.453 | 3.449 ± 0.014 |
| 4000 | 3.468 | 0.015 | 3.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 ( 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 . Its variance given is the uncertainty about , magnified by :

- At : .
- It rises to 5.8 around : is still uncertain and the magnification grows.
- It collapses once reveals the mode (0.26 for ).
- It rises again to at , where 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 , where the 8 narrow modes form.
2.4 Gaussian conditional paths
The general form. Take , with (noise) and , small. The simplest map that pushes along it is . Its velocity, written at the point , is the conditional vector field (Lipman et al., Theorem 3; Exercise 1):
The CFM loss then becomes a regression on a quantity computed from alone, with :
The OT path (Pset 5, problem 1). and :
Lipman et al. call it the OT path because the conditional flow between two Gaussians, and , is the optimal-transport displacement: a straight line at constant speed. Pset 5 uses , which gives the target . 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): and , with and β from 0.1 to 20. The path does not quite start at pure noise: .
- VE: , with 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 , target , straight lines between independent end points. Then and are jointly Gaussian, and the marginal velocity is a linear regression (Exercise 3):
Code 01 (, ; , ) finds:
- The formula is right. It matches the general Gaussian-mixture code to and satisfies the continuity equation.
- The end points are optimal. The flow sends every to , to . That is the optimal-transport map between the two Gaussians, and the transport cost equals ; independent pairs cost 28.18.
- The route is not. The intermediate distributions are narrower than the OT interpolation: their std at 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 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:
| path | , | straightness S | length ratio | total velocity change | dopri5 NFE (tol ) |
|---|---|---|---|---|---|
| linear (“OT”) | , | 1.547 | 1.411 | 5.10 (26% after t = 0.75) | 163 |
| cosine (Albergo; SiT’s “GVP”) | , | 0.974 | 1.232 | 5.85 (49%) | 187 |
| VP diffusion | , | 2.644 | 1.232 | 9.38 (73%) | 211 |
Mean end-point error of Euler with N uniform steps:
| N | 1 | 2 | 4 | 8 | 16 | 32 | 128 |
|---|---|---|---|---|---|---|---|
| linear | 2.000 | 0.717 | 0.130 | 0.037 | 0.022 | 0.012 | 0.003 |
| cosine | 0.932 | 0.528 | 0.212 | 0.055 | 0.030 | 0.017 | 0.005 |
| VP | 0.932 | 0.828 | 0.517 | 0.218 | 0.047 | 0.017 | 0.005 |

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 , and vs VP to (VP starts at , not 0). The reason is a short argument.
- Two affine paths and with the same signal-to-noise ratio, , are the same random variable up to a factor: with .
- 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 and run on the clock , is a trajectory of the other (Exercise 5).
- At and both paths sit at and unscaled (), so the end-point maps coincide.
The cosine and VP paths are both variance-preserving (), so , and their trajectories coincide even as curves (same length ratio, 1.232). The linear path’s states are the variance-preserving ones shrunk by (0.71 at ), 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:
- 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 : the VP path’s stays near 0 until (), 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.
- 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 , 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 , 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 :
| CIFAR-10 (NLL / FID / NFE) | ImageNet 32 | ImageNet 64 | |
|---|---|---|---|
| FM w/ Diffusion | 3.10 / 8.06 / 183 | 3.54 / 6.37 / 193 | 3.33 / 16.88 / 187 |
| FM w/ OT | 2.99 / 6.35 / 142 | 3.53 / 5.02 / 122 | 3.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 and :
Their main example is the trigonometric interpolant : our cosine path. The distribution of interpolates between the two densities. The paper proves that it is transported by the velocity
and that v is the unique minimizer of a quadratic objective, . Up to a constant this is the CFM loss with target ; 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. can be any data set (§3.3).
- The paper bounds by the training objective (times a factor exponential in the Lipschitz constant of ).
- 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 and , two linear equations link the posterior means and :
Eliminate , then apply Tweedie’s formula for the Gaussian component (, Lecture 12 §7.2):
For the linear path this reads . Velocity, score, denoiser and noise prediction 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 for all three paths. For a learned linear-path model the conversion multiplies the velocity error by :
| t | 0.2 | 0.5 | 0.8 | 0.95 |
|---|---|---|---|---|
| relative velocity error | 0.149 | 0.067 | 0.070 | 0.072 |
| relative score error | 0.014 | 0.055 | 0.103 | 0.159 |
Near the noise end the score is mostly the known Gaussian part , 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:
| interpolant | grid modes / quality | NFE (dopri5) | backwards: ring modes / quality | round-trip error |
|---|---|---|---|---|
| (reference: → grid) | 25 / 0.810 | 55 | — | — |
| ring → grid, | 21 / 0.533 | 109 | 8 / 0.958 | 0.260 |
| + latent noise, peak 0.25 | 25 / 0.761 | 85 | 8 / 0.955 | 0.092 |
| + latent noise, peak 0.5 | 25 / 0.729 | 85 | 8 / 0.942 | 0.108 |
| + latent noise, peak 1.0 | 25 / 0.559 | 97 | 8 / 0.930 | 0.071 |

- 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 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: , with . The end points are unchanged, and the target becomes . With 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 (noise, or any distribution) and (data). Given a coupling, i.e. pairs , independent at first, train
and define the rectified flow , . This is CFM with the linear path, . The paper proves two properties:
- Marginals are preserved (Theorem 3.3): has the same distribution as for every t, so . The flow’s pairs are a new coupling, now deterministic.
- Transport costs do not increase (Theorem 3.5): 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 ; independent pairs cost 6.07.
4.2 Straightness
exactly when every trajectory is a straight line traversed at constant speed. Then one Euler step is exact: . 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 , run the trained flow to get , 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 was already sent to a nearby . 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:
| flow | straightness S | transport cost | Euler 1 step: modes / quality | 2 steps | 4 steps | 100 steps | dopri5 NFE |
|---|---|---|---|---|---|---|---|
| exact 1-rectified | 1.540 | 1.022 | 0 / 0.000 | 0 / 0.003 | 8 / 0.703 | 8 / 0.992 | — |
| learned 1-rectified | 1.516 | 1.018 | 0 / 0.000 | 0 / 0.006 | 8 / 0.596 | 8 / 0.957 | 121 |
| learned 2-rectified | 0.002 | 1.008 | 8 / 0.845 | 8 / 0.868 | 8 / 0.896 | 8 / 0.951 | 61 |
| learned 3-rectified | 0.001 | 1.005 | 8 / 0.875 | 8 / 0.885 | 8 / 0.898 | 8 / 0.938 | 55 |

- 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 . A network can regress it directly: (Liu et al. §2.3). Code 04, Part 3:
| one-step generator | modes | quality | distance to its teacher’s samples |
|---|---|---|---|
| 2-rectified flow, one Euler step | 8 | 0.845 | — |
| distilled from the 1-rectified coupling | 8 | 0.901 | 0.051 |
| distilled from the 2-rectified coupling | 8 | 0.897 | 0.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 from to 1. Cost is counted in NFE, the number of network evaluations.
| method | NFE per step | order | idea |
|---|---|---|---|
| Euler | 1 | 1 | |
| midpoint | 2 | 2 | slope at the half step |
| Heun | 2 | 2 | average of the slopes at both ends |
| RK4 | 4 | 4 | four slopes, Simpson-like weights |
| adaptive pairs (Heun–Euler, Fehlberg 2, Bogacki–Shampine 3, Dormand–Prince 5) | 2–6 per try | 2–5 | two 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 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 . 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, ) 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:
| solver | NFE | valid | mini FID | error vs dopri5 |
|---|---|---|---|---|
| Euler, 1 step | 1 | 0.170 | 1463 | 0.755 |
| Euler, 4 steps | 4 | 0.824 | 96.5 | 0.250 |
| Euler, 16 steps | 16 | 0.922 | 28.8 | 0.079 |
| Euler, 100 steps | 100 | 0.934 | 16.5 | 0.014 |
| midpoint, 8 steps | 16 | 0.934 | 17.9 | 0.029 |
| Heun, 8 steps | 16 | 0.928 | 15.9 | 0.036 |
| RK4, 4 steps | 16 | 0.935 | 14.7 | 0.023 |
| adaptive Heun, tol | 82 | 0.936 | 15.3 | 0.011 |
| Fehlberg 2, tol | 9 | 0.879 | 32.5 | 0.155 |
| Bogacki–Shampine 3, tol | 34 | 0.934 | 15.5 | 0.011 |
| Dormand–Prince 5, tol | 43 | 0.934 | 13.8 | 0.019 |
| Dormand–Prince 5, tol | 265 | 0.933 | 14.8 | 0 (reference) |

- 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:
| solver | OT path (FID / valid) | VP path (FID / valid) |
|---|---|---|
| Euler, 8 steps | 45.9 / 0.895 | 55.3 / 0.746 |
| Euler, 100 steps | 16.5 / 0.934 | 20.1 / 0.857 |
| dopri5, tol | 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 corruption | (VP: ) | (any ; linear: ) |
| network predicts | ε (or x, or v) | velocity |
| loss weighting | ε-MSE = SNR-weighted x-error | velocity MSE = another weighting (Exercise 4) |
| sampler | ancestral SDE or DDIM = Euler on the PF-ODE | any 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
- Flow matching regresses a network on a velocity field that generates a path from noise to data. No simulation is needed during training.
- 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.
- Gaussian paths. The conditional field is . The OT path gives the target . Between two Gaussians the flow realizes the OT map at the end points but bends in between.
- 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.
- Stochastic interpolants. Any two distributions, any interpolant. Velocity ↔ score by two linear equations. Data-to-data bridges benefit from a little latent noise.
- 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.
- Solvers. Quality saturates at ~16–40 evaluations on glyphs. RK4 with 4 steps matches dopri5. Adaptive tolerances can mislead (Fehlberg 2).
8. Common pitfalls
- “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).
- “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.
- “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.
- “More solver accuracy, better samples.” Beyond ~40 evaluations, model error dominates (§5.2). And adaptive solvers only control their own error estimate (Fehlberg 2).
- “Reflow is free.” Each round trains on generated samples and accumulates error; the paper’s RK45 FIDs get worse with every round (§4.3).
- “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.
- 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 . Show that the velocity of , written as a function of the current point , is . Specialize it to the OT path.
Answer
. Invert the map: . Substitute to get the formula.
OT path. , , so and
The last step puts both terms over the common denominator; the terms cancel.
Exercise 2 (Theorem 2 in full). Prove that , and identify the constant C. Where does the assumption enter?
Answer
and .
- First terms. Equal: the marginal of x is in both.
- Cross terms. Equal by the definition of as the posterior average. Its definition divides by , hence the assumption .
- Last terms. Do not involve θ.
So , 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. and are jointly Gaussian (linear in independent Gaussians), per coordinate:
- and ;
- ;
- .
The regression. For jointly Gaussian variables, .
A check. The flow is . Its velocity, , matches the formula because . At it gives the OT map .
Exercise 4 (one object, four units). For the path with Gaussian , express , and the score in terms of the velocity v. Show that the velocity MSE equals times the -MSE.
Answer
The conversions. From and :
The MSE. Substitute into v:
Two velocity predictions at the same x differ by times the difference of their , so the squared errors scale by its square.
For the linear path. The factor is . The velocity loss weights the -error by , 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 , and let . Show that if solves B’s marginal ODE, then solves A’s, and that the two flows have the same end-point map.
Answer
Pathwise relation. For each pair, . Differentiating: .
Take conditional expectations. Given , equivalently :
The trajectory. satisfies . Since , this is exactly , so solves A’s ODE.
The end points. At : and , so and . At : and , so (by ) and . Hence from the same .
Code 03 confirms it to .
Exercise 6 (one Euler step). For the linear path with and a Gaussian source independent of the data, show that one Euler step from gives . What does it give for the cosine path?
Answer
Linear path. , with by independence. So the step lands at 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. and , so . 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 equals at and at .
Answer
At t = 0. and , so the residual is , with variance (≈ 4 on the ring).
At t = 1. and by independence. The residual is , with variance .
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 .
Exercise 8 (reflow does not increase transport costs). Prove for convex c, using Theorem 3.3 (equal marginals).
Answer
Jensen over time. , so .
Jensen over pairs. , so .
Combine. Take expectations and use that and have the same distribution:
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 (e.g. ). 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 ; 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
- Lecture 4 (discrete normalizing flows); Lecture 12 (Tweedie, the probability-flow ODE); Lecture 13 §1 (parameterizations) and §5 (DDIM = Euler, EDM samplers); MIT6.S184 Lecture 2 (in Chinese).
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.