Foundations to Frontier
Tests
H. Generative Models › H7 Diffusion Models

Flow matching

Flow matching trains a continuous normalizing flow without simulating it. Choose a path of distributions from noise to data, for example straight lines x_t = (1 − t) x0 + t x1. Then regress a network v(x, t) on the velocity of each individual path, x1 − x0. Lipman et al. (2023) showed that this conditional objective has the same gradients as regressing the unknown marginal velocity, so sampling is just integrating dx/dt = v from noise to data. Diffusion models are the special case of Gaussian paths. Flow matching with straight paths (also called rectified flow) now trains Stable Diffusion 3, FLUX, the Wan and HunyuanVideo video models, and many robot policies (π0). In the exercises you build the interpolants, their velocities, the loss and an Euler sampler. You then train a small MLP on an 8-mode ring and compare it with the exact velocity field. With the exact field, 8 Euler steps already put 98.8% of samples on a mode on the linear path (98.3% on the cosine path). The small network still left 15% of samples between modes after 32 steps. Its loss was 3.856 against 3.707 for the exact field: the loss has a large floor that no model can beat, and the gap (0.149) matched the true flow-matching error (0.154).

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Learning which way to flow

Picture every grain of noise drifting along a river that ends in a picture. Flow matching learns the river’s current: at each place and time, which way and how fast to move. Training is easy: pick a noise point and a data point, draw a straight line between them, and teach the network the direction of that line. Averaged over many such lines, the network learns a current that carries all of the noise to the data. The demo shows the learned flow on eight blobs arranged in a ring.

Diffusion lab 3: video, flows and few-step sampling
Off-mode share 10.7% after 4 Euler steps of the exact OT (linear) velocity. The paths of the marginal flow are curved even though each conditional path is straight; one step lands every sample on the data mean.
Why engineers care

Flow matching trains Stable Diffusion 3, FLUX and the Wan video models, and the action experts of robot policies such as π0, which turn noise into motor commands.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Flow matching learns…
  2. 2. The training target for one pair is…
  3. 3. Samples are produced by…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Probability path: p_t interpolates from noise p_0 = N(0, I) to data p_1. Conditional Gaussian paths x_t = a(t) x1 + s(t) x0 with a(0) = 0, s(0) = 1, a(1) = 1, s(1) ≈ 0.
  • Conditional velocity: u_t(x | x0, x1) = a’(t) x1 + s’(t) x0; for the linear (OT) path a = t, s = 1 − t it is x1 − x0, constant in time.
  • Marginal velocity: u_t(x) = E[a’ x1 + s’ x0 | x_t = x], the field whose ODE transports p_0 to p_1. It is unknown, but regressing the conditional velocity gives the same minimiser.
  • CFM loss: E over t, x0, x1 of |v_θ(x_t, t) − (x1 − x0)|². Its minimum is not zero: the conditional velocity varies around its mean.
  • Sampling: solve dx/dt = v_θ(x, t) from t = 0 to 1 with Euler, Heun or adaptive solvers; the ODE gives likelihoods through the change of variables (as in FFJORD), without training by simulation.
  • Relation to diffusion: VP diffusion is a Gaussian path with a = sin(πt/2)-like schedules; ε-, x- and v-prediction are linear reparametrisations of the same field.
  • Variants: stochastic interpolants (Albergo and Vanden-Eijnden), minibatch OT couplings (OT-CFM), flow matching on manifolds (Riemannian FM for rotations and proteins), discrete flow matching for tokens.
📝 Quick check (Level 1)
0/3 answered
  1. 1. For the linear path the conditional velocity is…
  2. 2. The minimum CFM loss is…
  3. 3. Diffusion models are flow matching with…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Training and sampling

Exercise 1.

  • Linear (OT) and cosine (VP) interpolants with their velocities.
  • The CFM loss.
  • The exact velocity when the data is a single point.
  • An Euler sampler.

Practice.

  • Sample t from a logit-normal (Stable Diffusion 3) or shift it toward noise for large images and videos; uniform t wastes capacity at the easy ends.
  • Predict velocity directly; use an EMA of the weights; condition with classifier-free guidance as for diffusion.
  • Sample with 20–50 Euler or Heun steps; few-step sampling needs rectification or distillation (H7-13 to H7-15).
  • The loss value says little about sample quality (it has a large floor); evaluate samples.
  • Libraries: diffusers (FlowMatchEulerDiscreteScheduler), torchcfm, Meta’s flow_matching.
import torch
def cfm_step(model, x1, opt):
    x0 = torch.randn_like(x1)
    t = torch.sigmoid(torch.randn(x1.shape[0], device=x1.device))       # logit-normal timesteps
    xt = (1 - t[:, None]) * x0 + t[:, None] * x1
    loss = ((model(xt, t) - (x1 - x0)) ** 2).mean()
    opt.zero_grad(); loss.backward(); opt.step(); return loss.item()
@torch.no_grad()
def sample(model, n, d, steps=32):
    x = torch.randn(n, d)
    for k in range(steps): x = x + model(x, torch.full((n,), k / steps)) / steps
    return x
🐍 Exercise 1: interpolants, loss and sampler
Write the OT and VP interpolants and velocities, the CFM loss, the single-point velocity and an Euler sampler.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Stable Diffusion 3 samples t from…
  2. 2. A low flow-matching loss means…
  3. 3. Typical sampling uses…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why conditional regression works. For any v, E|v(x_t) − u(x_t | x0, x1)|² = E|v(x_t) − u_t(x_t)|² + E|u(x_t | x0, x1) − u_t(x_t)|², because u_t(x) is the conditional mean of u(x_t | ·) given x_t. The second term does not depend on v, so the CFM and FM losses have the same gradients (Lipman et al., 2023). It is also why the CFM loss cannot reach zero.

The marginal field transports the marginals. u_t satisfies the continuity equation ∂p_t/∂t + ∇·(p_t u_t) = 0, so its ODE moves p_0 to p_1 exactly.

Exact field for Gaussian-mixture data. x_t = a x1 + s x0 means x_t / a is the data plus Gaussian noise of scale s / a. So E[x1 | x_t] is the exact denoiser of the mixture, E[x0 | x_t] = (x_t − a E[x1 | x_t]) / s, and u = a’ E[x1 | x_t] + s’ E[x0 | x_t]. This page uses it as ground truth.

Curvature. Each conditional path is straight, but the marginal paths bend: at t = 0, v(x, 0) = E[x1] − x points every sample at the data mean, and only later does each path turn toward its mode. One Euler step therefore puts every sample on the mean. Straight marginal paths need a better coupling (minibatch OT, rectification).

📝 Quick check (Level 3)
0/3 answered
  1. 1. CFM and FM losses differ by…
  2. 2. At t = 0 the marginal velocity points to…
  3. 3. Marginal paths are curved because…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How close does a small network get to the exact flow-matching field, and does the choice of path matter?

History.

  • Y. Lipman, R. Chen, H. Ben-Hamu, M. Nickel and M. Le, “Flow matching for generative modeling” (2023).
  • X. Liu, C. Gong and Q. Liu, “Flow straight and fast: learning to generate and transfer data with rectified flow” (2023).
  • M. Albergo and E. Vanden-Eijnden, “Building normalizing flows with stochastic interpolants” (2023).
  • A. Tong et al., “Improving and generalizing flow-based generative models with minibatch optimal transport” (2024).
  • P. Esser et al., “Scaling rectified flow transformers for high-resolution image synthesis” (Stable Diffusion 3, 2024).

Exercise 2. 2-D data: 8 equal Gaussians (std 0.15) on a circle of radius 2.

  • Model: MLP (inputs x, t, sin πt, cos πt; two tanh layers of 32; Adam, 1400 steps of batch 256) trained with the CFM loss on the linear path.
  • Ground truth: the exact marginal velocity on the linear (OT) path and on the cosine (VP) path.
  • Measures: off-mode share (farther than 4 std from every mode) and spread (RMS distance to the nearest mode / (0.15 √2), 1 = data) of 1000 samples after N Euler steps; CFM loss on 20000 fresh triples; FM loss (squared error to the exact field).
field off-mode, N = 4 N = 8 N = 32 spread, N = 32
exact, OT path 0.086 0.012 0.001 0.99
exact, VP path 0.095 0.017 0.001 0.97
trained MLP, OT path 0.289 0.198 0.152 1.95
loss value
CFM loss, exact field 3.707
CFM loss, MLP 3.856
difference 0.149
FM loss of the MLP 0.154
  • Paths: with exact fields both paths reach the data; the linear path was slightly better at 4 and 8 steps on all three seeds, a small effect on this data.
  • Network error dominates: the 32-unit MLP left 15% of samples in the gaps between modes even with 32 steps; more steps cannot remove model error.
  • The loss floor: the exact field itself scores 3.707. The MLP’s excess (0.149) equals its true FM error (0.154) up to Monte Carlo noise, as the theorem predicts.
  • Seeds 1 and 2: MLP off-mode at 32 steps 0.169 and 0.166; loss excess 0.184 and 0.166 against FM loss 0.185 and 0.161; exact OT vs VP at 4 steps 0.08 vs 0.093 and 0.106 vs 0.124.
  • Caveat: a 32-unit MLP trained for 1400 steps (kept small to run in the browser); larger networks trained longer close much of the gap.
🐍 Exercise 2 (research): how close does a small network get to the exact flow-matching field, and does the choice of path matter?
Train a flow-matching MLP on a ring of Gaussians and compare it with the exact OT and VP velocity fields.

Flow matching in AI, robotics and aerospace

  • Image and video generation: Stable Diffusion 3, FLUX, Wan, HunyuanVideo and Movie Gen all train with flow matching on linear paths.
  • Robot policies: π0 (Physical Intelligence, 2024) and later vision-language-action models generate action chunks with a flow-matching action expert; flow policies need fewer steps than diffusion policies for real-time control.
  • Science: flow matching on SE(3) generates protein backbones (FrameFlow, FoldFlow); in weather and Earth observation it is used for ensemble generation and downscaling.

References

  1. Y. Lipman et al. (2023). “Flow matching for generative modeling.” ICLR.
  2. X. Liu, C. Gong and Q. Liu (2023). “Flow straight and fast: learning to generate and transfer data with rectified flow.” ICLR.
  3. M. Albergo and E. Vanden-Eijnden (2023). “Building normalizing flows with stochastic interpolants.” ICLR.
  4. A. Tong et al. (2024). “Improving and generalizing flow-based generative models with minibatch optimal transport.” TMLR.
  5. P. Esser et al. (2024). “Scaling rectified flow transformers for high-resolution image synthesis.” ICML.
  6. Y. Lipman et al. (2024). “Flow matching guide and code.” arXiv.