Search

Search papers and lessons

Flow Matching

Level 4 · 13 min read

Flow matching trains a continuous-time generative model by regressing a vector field onto simple per-sample paths from noise to data, so no ODE simulation is needed during training.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Discrete math
  • Generative models
  • Sign in to see your progress

TL;DR

Flow matching trains a model to generate data by learning a velocity field that carries noise, step by step, to data. The training target is built from simple paths that connect one noise sample to one data sample, so the training loss is a plain regression with no differential equation to solve. At sampling time you start from noise and follow the learned field with a numerical ODE solver.

Where you'll see it: in recent image and video generators that replace diffusion's curved sampling paths with learned flows, and in any generative model that must sample quickly from a continuous-time field.

Intuition

A river that carries noise to data

Picture every point in space as a leaf on a river. Each leaf is pushed by the current, and the current depends on where the leaf is and when it is. If you know the current everywhere, you can drop leaves at random in the start region and watch them drift into the target region. A vector field is that current: for each position xx and time tt it gives a velocity vt(x)v_t(x).

Generative modelling then becomes a question about the river. Start with a region of pure noise at t=0t = 0. Choose a current so that, after time t=1t = 1, the leaves are distributed like real images. Sampling is just letting the leaves drift.

The hard part is designing the current. Diffusion models learn it indirectly, by predicting the noise that was added and converting that prediction into a score. Flow matching skips the detour and regresses the velocity directly. The question is how to get training targets for that velocity without knowing the whole current in advance. The answer is to build the current out of many small, simple pieces, one per training example.

Why "simulation-free" matters

Earlier continuous-time generative models, called continuous normalizing flows (CNFs), were trained by simulating the ODE for every training example and comparing the result with the data. Each gradient step needed a full ODE solve, which is slow and memory-hungry. Flow matching avoids this. Each training example gives one time tt, one point on a simple path, and one known velocity. The model only has to match that velocity at that point. No solver runs during training.

The math

Setup

Definition: Probability path

A probability path is a family of densities pt(x)p_t(x) indexed by time t∈[0,1]t \in [0, 1]. It starts at a simple density p0p_0, for example the standard Gaussian N(0,I)\mathcal{N}(0, I), and ends at a density p1p_1 that should be close to the data distribution qq.

A vector field vt(x)v_t(x) generates the path if the flow it defines moves p0p_0 into ptp_t at every time. Concretely, a point x0∼p0x_0 \sim p_0 follows the ordinary differential equation

ddt ϕt(x0)=vt(ϕt(x0)),ϕ0(x0)=x0,\frac{d}{dt}\, \phi_t(x_0) = v_t(\phi_t(x_0)), \qquad \phi_0(x_0) = x_0,

and ϕt\phi_t is the flow. Here ϕt(x0)\phi_t(x_0) is where the point started at x0x_0 sits at time tt. Pushing p0p_0 through ϕt\phi_t must give ptp_t. This is the ODE that sampling will solve.

A neural network vθ(x,t)v_\theta(x, t) with parameters θ\theta is trained to act as this field. The ideal training objective is the flow matching loss

LFM(θ)=Et∼U[0,1],  x∼pt∥vθ(x,t)−ut(x)∥2,\mathcal{L}_{\text{FM}}(\theta) = \mathbb{E}_{t \sim \mathcal{U}[0,1],\; x \sim p_t}\left\| v_\theta(x, t) - u_t(x) \right\|^2,

where utu_t is the target field that generates ptp_t. Two obstacles block this in practice. We rarely know ptp_t in closed form, and the marginal target ut(x)u_t(x) is an integral over all data points, so it cannot be evaluated either.

Conditional paths

The fix is to define a path for each data point. Fix a data sample x1∼qx_1 \sim q and define a conditional path pt(x∣x1)p_t(x \mid x_1) with p0(x∣x1)=p0p_0(x \mid x_1) = p_0 and with p1(x∣x1)p_1(x \mid x_1) concentrated near x1x_1. The marginal path is the mixture over data:

pt(x)=∫pt(x∣x1) q(x1) dx1.p_t(x) = \int p_t(x \mid x_1)\, q(x_1)\, dx_1.

Each conditional path has its own conditional field ut(x∣x1)u_t(x \mid x_1). The marginal field is the average of these, weighted by how much each data point contributes at xx:

ut(x)=∫ut(x∣x1) pt(x∣x1) q(x1)pt(x) dx1.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 paper proves that this average really does generate the marginal path (its Theorem 1). It is the marginal analogue of the continuity equation, the statement that probability mass is conserved as it flows.

The trick: the conditional loss has the same gradient

The conditional flow matching loss uses only the per-sample targets:

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)}\left\| v_\theta(x, t) - u_t(x \mid x_1) \right\|^2.

Expand both squared norms. The ∥ut∥2\|u_t\|^2 and ∥ut(⋅∣x1)∥2\|u_t(\cdot \mid x_1)\|^2 terms do not depend on θ\theta. The cross term is the one that matters:

∫vθ(x,t)⋅ut(x) pt(x) dx=∫vθ(x,t)⋅∫ut(x∣x1) pt(x∣x1) q(x1) dx1 dx,\int v_\theta(x, t) \cdot u_t(x)\, p_t(x)\, dx = \int v_\theta(x, t) \cdot \int u_t(x \mid x_1)\, p_t(x \mid x_1)\, q(x_1)\, dx_1\, dx,

which is exactly the cross term of LCFM\mathcal{L}_{\text{CFM}}. So the two losses differ only by a constant that does not depend on θ\theta. Their gradients are identical (Theorem 2 of the paper). Minimising the easy per-sample loss therefore trains the same network you would have trained with the intractable one. This is the same idea that makes Score Matching tractable, and the paper draws the comparison explicitly.

Gaussian conditional paths

A simple and general choice is a Gaussian path. Let μt(x1)\mu_t(x_1) be the mean and σt(x1)\sigma_t(x_1) the standard deviation at time tt:

pt(x∣x1)=N(x∣μt(x1),σt(x1)2I),μ0=0, σ0=1, μ1=x1, σ1=σmin⁡≈0.p_t(x \mid x_1) = \mathcal{N}\big(x \mid \mu_t(x_1), \sigma_t(x_1)^2 I\big), \qquad \mu_0 = 0,\ \sigma_0 = 1,\ \mu_1 = x_1,\ \sigma_1 = \sigma_{\min} \approx 0.

The paper shows that a conditional field exists for any such path. The sample-level flow is ψt(x0)=σt(x1) x0+μt(x1)\psi_t(x_0) = \sigma_t(x_1)\, x_0 + \mu_t(x_1), and the conditional field is its time derivative, evaluated at the point where it sits.

Two choices matter most.

  • Diffusion paths. Choosing the means and standard deviations from a noise schedule gives back the paths used by diffusion models. The conditional field is then curved.
  • Optimal transport (OT) paths. Choosing both quantities to change linearly in time gives
μt(x1)=t x1,σt=1−(1−σmin⁡) t.\mu_t(x_1) = t\, x_1, \qquad \sigma_t = 1 - (1 - \sigma_{\min})\, t.

For this choice the flow is ψt(x0)=(1−(1−σmin⁡)t) x0+t x1\psi_t(x_0) = (1 - (1 - \sigma_{\min})t)\, x_0 + t\, x_1, and the target velocity is

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

Along each sample's trajectory the target velocity is the constant x1−(1−σmin⁡) x0x_1 - (1 - \sigma_{\min})\, x_0. Particles move in straight lines at constant speed. The paper shows that this flow is the optimal transport displacement between the two Gaussians, which is why it is called the OT path.

The training loop in practice

For the OT path the loss simplifies to

LCFM(θ)=Et,  x1∼q,  x0∼N(0,I)∥vθ(ψt(x0),t)−(x1−(1−σmin⁡) x0)∥2.\mathcal{L}_{\text{CFM}}(\theta) = \mathbb{E}_{t,\; x_1 \sim q,\; x_0 \sim \mathcal{N}(0, I)}\left\| v_\theta\big(\psi_t(x_0), t\big) - \big(x_1 - (1 - \sigma_{\min})\, x_0\big) \right\|^2.

Each step: draw a data point x1x_1, a noise point x0x_0, a time tt, form ψt(x0)\psi_t(x_0), and regress vθv_\theta at that point onto the constant target. Nothing is integrated.

Worked examples

Example
A one-dimensional straight path, by hand

Take σmin⁡=0\sigma_{\min} = 0 for hand computation (the paper uses a small positive value so that p1p_1 is a proper density). Let the noise sample be x0=−1x_0 = -1 and the data sample be x1=3x_1 = 3.

  1. The flow is ψt(x0)=(1−t)(−1)+t⋅3=−1+4t\psi_t(x_0) = (1 - t)(-1) + t \cdot 3 = -1 + 4t.
  2. Its time derivative is 44 for every tt. So the target velocity is ut=x1−x0=4u_t = x_1 - x_0 = 4, constant.
  3. Positions at four times: t=0t = 0 gives −1-1; t=0.25t = 0.25 gives 00; t=0.5t = 0.5 gives 11; t=1t = 1 gives 33.
  4. Sampling with a perfect model and two Euler steps of size h=0.5h = 0.5 (see Euler Method): start at x=−1x = -1. The first step gives −1+0.5⋅4=1-1 + 0.5 \cdot 4 = 1. The second gives 1+0.5⋅4=31 + 0.5 \cdot 4 = 3. The solver lands exactly on the data point.

Interpretation: when the target is straight, one step of size hh moves the point exactly hh times the velocity, so even a coarse solver is accurate. Curved diffusion paths need more steps for the same accuracy.

Example
One training step on two-dimensional points

Take two pairs, with σmin⁡=0\sigma_{\min} = 0: x0=(−1,2)x_0 = (-1, 2) paired with x1=(3,0)x_1 = (3, 0).

  1. Draw t=0.25t = 0.25. The point on the path is ψt(x0)=0.75 (−1,2)+0.25 (3,0)=(0,1.5)\psi_t(x_0) = 0.75\,(-1, 2) + 0.25\,(3, 0) = (0, 1.5).
  2. The target velocity is u=x1−x0=(4,−2)u = x_1 - x_0 = (4, -2).
  3. Suppose the current network outputs vθ=(2.5,−1.0)v_\theta = (2.5, -1.0) at that point and time.
  4. The error is vθ−u=(−1.5,1.0)v_\theta - u = (-1.5, 1.0). Its squared norm is 2.25+1.00=3.252.25 + 1.00 = 3.25.
  5. The gradient of this loss with respect to θ\theta pushes the network to output a velocity closer to (4,−2)(4, -2) at the point (0,1.5)(0, 1.5) when t=0.25t = 0.25. No ODE was solved to obtain the target.

Interpretation: a minibatch just averages this quantity over many random pairs (x0,x1)(x_0, x_1) and times tt. Each pair teaches the network about one straight segment, and the network's job is to average over the segments that pass through each point.

Example
How the conditional density spreads out

With σmin⁡=0\sigma_{\min} = 0 and a fixed data point x1=3x_1 = 3, the conditional path is Gaussian with mean 3t3t and standard deviation 1−t1 - t. The density plotted below is the conditional density at three times.

−2−10123400.511.52xdensity
  • t = 0
  • t = 0.5
  • t = 0.8
The conditional density slides from the standard Gaussian toward the data point at x = 3 and narrows as t approaches 1. Each curve still has area 1.

At t=0.5t = 0.5 the mean is 1.51.5 and the standard deviation is 0.50.5, so the peak height is 1/(0.52π)≈0.79791/(0.5\sqrt{2\pi}) \approx 0.7979. At t=0.8t = 0.8 the mean is 2.42.4 and the standard deviation is 0.20.2, giving a peak of 1/(0.22π)≈1.99471/(0.2\sqrt{2\pi}) \approx 1.9947. The mean moves linearly in tt and the width shrinks linearly, which is the straight-path property in density form.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
Using a diffusion path with few sampling stepsCurved trajectories need many small steps, so quality drops at low step countsOT paths, whose straight trajectories tolerate coarse solvers
Setting σmin⁡\sigma_{\min} exactly to 0 in codeThe last-time density is degenerate and divisions by 1−(1−σmin⁡)t1 - (1 - \sigma_{\min})t become unstable near t=1t = 1A small positive σmin⁡\sigma_{\min}, and clipping tt away from 1 during training
Training with a poorly chosen time distributionRegions of time that are rarely sampled during training may be poorly fitted, which hurts sampling in those regionsSample tt uniformly, as the paper does, and check the loss across time bins before trying non-uniform schemes
Expecting exact likelihoods cheaplyComputing likelihoods requires integrating the divergence of the field along trajectories, which is costlyUse it for sampling; use the continuous-time likelihood only when needed
Sampling with an adaptive solver for speedThe number of function evaluations varies per sample, which complicates batchingFixed-step solvers, as the paper does for its low-cost comparisons

Flow matching is a clean framework, not a guarantee of better samples. The paper reports that, with fixed-step (midpoint) solvers on its ImageNet 32 models, OT paths reach a given numerical error with roughly 60 percent of the function evaluations diffusion paths need, and that they outperform diffusion baselines on likelihood and sample quality in its experiments. Those results depend on architecture, data and training budget, so treat them as evidence for the approach rather than a universal ranking.

Alternatives include diffusion models (which learn the same kind of field through noise prediction), Neural Ordinary Differential Equation training with full simulation (which is slower but was the earlier route to continuous flows), and stochastic differential equation samplers (which add noise during sampling).

History

Continuous normalizing flows were introduced by Chen, Rubanova, Bettencourt and Duvenaud in 2018, in the paper that also introduced neural ODEs. They showed that a network can define a continuous transformation whose log-density change is tractable, but training them needed an ODE solve per gradient step. Grathwohl and colleagues (2018) trained such models by maximum likelihood, and the cost of solving the ODE during training remained the main obstacle.

Diffusion models, popularised by Ho, Jain and Abbeel in 2020, took off because their training was stable and simulation-free, through denoising. Score-based views by Song and colleagues in 2020 and 2021 connected them to continuous-time stochastic differential equations.

Flow matching was proposed by Lipman, Chen, Ben-Hamu, Nickel and Le in late 2022, with the paper published at the 2023 ICLR conference. It combined the continuous-time framework of CNFs with the simulation-free training of diffusion models. Around the same time, Liu and colleagues (rectified flow) and Albergo and Vanden-Eijnden (stochastic interpolants) proposed closely related methods. The idea of straight, optimal-transport paths relies on a much older result: McCann's 1997 displacement interpolation, which the paper builds on. The approach caught on because it trains faster than diffusion with comparable or better quality, and because the same objective works for any choice of path.

Check yourself

QuestionRecall: what does a vector field vt(x)v_t(x) generate, and what equation determines where a point ends up?Show answer

A vector field gives a velocity at each position and time. It generates a probability path if following it moves samples from the start distribution p0p_0 so that their distribution at each time tt equals ptp_t. A single point x0x_0 follows the ODE ddtϕt(x0)=vt(ϕt(x0))\frac{d}{dt}\phi_t(x_0) = v_t(\phi_t(x_0)) with ϕ0(x0)=x0\phi_0(x_0) = x_0, and ϕ1(x0)\phi_1(x_0) is where it ends.

QuestionComputation: with σmin⁡=0\sigma_{\min} = 0, x0=1x_0 = 1, x1=5x_1 = 5, what is the flow ψ0.5(x0)\psi_{0.5}(x_0), and what is the target velocity?Show answer

The flow is ψt(x0)=(1−t) x0+t x1\psi_t(x_0) = (1 - t)\,x_0 + t\,x_1. At t=0.5t = 0.5 this is 0.5⋅1+0.5⋅5=30.5 \cdot 1 + 0.5 \cdot 5 = 3. The target velocity is x1−x0=4x_1 - x_0 = 4, the same at every time. The midpoint of the straight path is 33, and the constant speed is 44 per unit time.

QuestionWhy: the conditional loss uses only one data point per target, yet it trains the same network as the marginal loss. Why?Show answer

Expanding the squared error shows that the only θ\theta-dependent cross term is ∫vθ⋅ut pt dx\int v_\theta \cdot u_t\, p_t\, dx. The marginal field utu_t is defined as the average of the conditional fields weighted by the posterior over x1x_1. Substituting that average into the cross term turns it into the expectation of vθ⋅ut(x∣x1)v_\theta \cdot u_t(x \mid x_1) over x1x_1 and xx. The remaining terms do not depend on θ\theta. So the gradients of the two losses are equal, and the per-sample loss has the same minimiser.

QuestionApplied: you train a flow matching model with diffusion paths, but generation quality is poor with 10 sampling steps and good with 100. What change would you test first, and why?Show answer

Switch to OT conditional paths and keep everything else fixed. Diffusion paths give curved trajectories that a coarse fixed-step solver approximates poorly, so 10 steps leave large discretisation error. OT paths are straight, so the same solver with 10 steps should track the field much more closely. Changing the architecture or the training length would be a weaker first test, because the sampling error comes from the path geometry, not from the network's capacity.

Sources