Flow matching

Flow matching trains a continuous normalizing flow without simulating it: sample noise x₀, data x₁ and a time t, place xₜ on a simple conditional path (OT path: xₜ = (1 − (1 − σ_min)t)x₀ + t·x₁) and regress vθ(xₜ, t) on that path's velocity. Sampling integrates dx/dt = vθ(x, t) from noise to data.

15 min read1071 wordsadvanced0 viewsupdated 2026-10-12

Definition

Flow matching (FM) is a training objective for continuous normalizing flows from Lipman, Chen, Ben-Hamu, Nickel and Le, "Flow Matching for Generative Modeling" (ICLR 2023). A network vθ(x,t)v_\theta(x,t) defines an ODE x˙=vθ(x,t)\dot x=v_\theta(x,t) that carries a simple source p0=N(0,I)p_0=\mathcal N(0,I) at t=0t=0 to the data distribution qq at t=1t=1. Instead of maximising likelihood through the ODE, FM regresses vθv_\theta on a target vector field utu_t that is known to generate a chosen probability path ptp_t. Its practical form, conditional flow matching (CFM), uses per-sample paths from noise x0x_0 to a data point x1x_1, whose velocities are available in closed form. It has the same gradient as the intractable marginal objective.

A continuous normalizing flow (Chen et al., Neural ODEs, NeurIPS 2018) is attractive: any vector field gives an invertible map, and the change of variables needs no special architecture (contrast the coupling layers of Normalizing flows, RealNVP and Glow). Maximum-likelihood training, though, has to solve the ODE forward and backward for every minibatch, which made CNFs slow and hard to scale. Diffusion models scale well, but their training is tied to one family of noising processes and their sampling paths are curved. Lipman et al. showed that a CNF can be trained the way a diffusion model is: pick a path of distributions, write down a regression target, and train on random (x0,x1,t)(x_0,x_1,t) triples with no ODE solve. Diffusion paths are one choice. The straight optimal-transport (OT) path is another, and it trained faster and sampled with fewer function evaluations. Two concurrent papers reached closely related objectives: Rectified flow (Liu, Gong and Liu) and stochastic interpolants (Albergo and Vanden-Eijnden), both ICLR 2023.

Flows, paths and the flow-matching loss

ddtϕt(x)=vt(ϕt(x)),ϕ0(x)=x,pt=[ϕt]# p0,∂tpt+∇ ⁣⋅ ⁣(pt vt)=0\frac{d}{dt}\phi_t(x)=v_t\big(\phi_t(x)\big),\quad \phi_0(x)=x,\qquad p_t=[\phi_t]_{\#}\,p_0,\qquad \partial_t p_t+\nabla\!\cdot\!\big(p_t\,v_t\big)=0
ϕt\phi_t
the flow: where a source point xx is at time tt
vtv_t
the time-dependent vector field (the network vθ(x,t)v_\theta(x,t))
[ϕt]# p0[\phi_t]_\#\,p_0
push-forward: the distribution of ϕt(x0)\phi_t(x_0) for x0∼p0x_0\sim p_0
∂tpt+∇⋅(ptvt)=0\partial_t p_t+\nabla\cdot(p_tv_t)=0
continuity equation; a field utu_t generates a path ptp_t when the pair satisfies it
LFM(θ)=Et∼U[0,1], x∼pt∥vθ(x,t)−ut(x)∥2,pt(x)= ⁣∫ ⁣pt(x∣x1) q(x1) dx1,ut(x)= ⁣∫ ⁣ut(x∣x1) pt(x∣x1) q(x1)pt(x) dx1\mathcal L_{\rm FM}(\theta)=\mathbb E_{t\sim\mathcal U[0,1],\,x\sim p_t}\big\|v_\theta(x,t)-u_t(x)\big\|^2,\qquad 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
pt(x∣x1)p_t(x\mid x_1)
a conditional path: p0(⋅∣x1)=N(0,I)p_0(\cdot\mid x_1)=\mathcal N(0,I) and p1(⋅∣x1)p_1(\cdot\mid x_1) concentrated around the data point x1x_1
ut(x∣x1)u_t(x\mid x_1)
a field that generates the conditional path
pt, utp_t,\ u_t
the marginal path and field; utu_t is the posterior average of the conditional fields, and it generates ptp_t (Theorem 1), with p1≈qp_1\approx q

LFM\mathcal L_{\rm FM} is the right objective, but it cannot be computed, because ut(x)u_t(x) is an integral over the whole data distribution. The key result is that the conditional version can be computed and gives the same gradients.

LCFM(θ)=Et,  x1∼q,  x∼pt(⋅∣x1)∥vθ(x,t)−ut(x∣x1)∥2,∇θLCFM=∇θLFM\mathcal L_{\rm CFM}(\theta)=\mathbb E_{t,\;x_1\sim q,\;x\sim p_t(\cdot\mid x_1)}\big\|v_\theta(x,t)-u_t(x\mid x_1)\big\|^2,\qquad \nabla_\theta\mathcal L_{\rm CFM}=\nabla_\theta\mathcal L_{\rm FM}
LCFM\mathcal L_{\rm CFM}
sample a data point, sample xx on its conditional path, regress on its conditional velocity
∇θLCFM=∇θLFM\nabla_\theta\mathcal L_{\rm CFM}=\nabla_\theta\mathcal L_{\rm FM}
Theorem 2 (assuming pt(x)>0p_t(x)>0); the two losses differ by a constant, so they share their minimiser vθ=utv_\theta=u_t

The proof is one line. Expand both squares. The ∥vθ∥2\|v_\theta\|^2 terms are identical, since both average over the same pt(x)p_t(x). The cross terms agree as well: Ex1,x[vθ(x)⋅ut(x∣x1)]=Ex[vθ(x)⋅ut(x)]\mathbb E_{x_1,x}[v_\theta(x)\cdot u_t(x\mid x_1)]=\mathbb E_x[v_\theta(x)\cdot u_t(x)], because ut(x)u_t(x) is the posterior mean of ut(x∣x1)u_t(x\mid x_1). What remains, E∥ut(x∣x1)∥2−E∥ut(x)∥2\mathbb E\|u_t(x\mid x_1)\|^2-\mathbb E\|u_t(x)\|^2, does not involve θ\theta. It is the variance of the conditional target given xx, and it is the floor below which the CFM loss cannot fall.

Gaussian conditional paths and the OT path

pt(x∣x1)=N(x; μt(x1), σt(x1)2I),ψt(x0)=σt(x1) x0+μt(x1),ut(x∣x1)=σt′(x1)σt(x1)(x−μt(x1))+μt′(x1)p_t(x\mid x_1)=\mathcal N\big(x;\,\mu_t(x_1),\,\sigma_t(x_1)^2I\big),\qquad \psi_t(x_0)=\sigma_t(x_1)\,x_0+\mu_t(x_1),\qquad u_t(x\mid x_1)=\frac{\sigma_t'(x_1)}{\sigma_t(x_1)}\big(x-\mu_t(x_1)\big)+\mu_t'(x_1)
μ0=0, σ0=1\mu_0=0,\ \sigma_0=1
every conditional path starts at the source N(0,I)\mathcal N(0,I)
μ1=x1, σ1=σmin⁡\mu_1=x_1,\ \sigma_1=\sigma_{\min}
and ends in a small Gaussian around the data point
ψt\psi_t
the conditional flow; x=ψt(x0)x=\psi_t(x_0) with x0∼N(0,I)x_0\sim\mathcal N(0,I) samples the path
ut(x∣x1)u_t(x\mid x_1)
Theorem 3: the unique field of this affine flow; primes denote d/dtd/dt

Substituting x=ψt(x0)x=\psi_t(x_0) gives the loss that is actually implemented, LCFM=Et,x1,x0∥vθ(ψt(x0),t)−ddtψt(x0)∥2\mathcal L_{\rm CFM}=\mathbb E_{t,x_1,x_0}\big\|v_\theta(\psi_t(x_0),t)-\tfrac{d}{dt}\psi_t(x_0)\big\|^2. Any μt,σt\mu_t,\sigma_t with the boundary values above is allowed. The paper studies two families.

OT:  μt=t x1,  σt=1−(1−σmin⁡)t  ⇒  ψt(x0)=(1−(1−σmin⁡)t)x0+t x1,ddtψt(x0)=x1−(1−σmin⁡) x0\textbf{OT:}\ \ \mu_t=t\,x_1,\ \ \sigma_t=1-(1-\sigma_{\min})t\ \ \Rightarrow\ \ \psi_t(x_0)=\big(1-(1-\sigma_{\min})t\big)x_0+t\,x_1,\quad \tfrac{d}{dt}\psi_t(x_0)=x_1-(1-\sigma_{\min})\,x_0
ψt(x0)\psi_t(x_0)
a straight line from x0x_0 to (almost) x1x_1, travelled at constant speed
x1−(1−σmin⁡)x0x_1-(1-\sigma_{\min})x_0
the regression target; it does not depend on tt
ut(x∣x1)=x1−(1−σmin⁡)x1−(1−σmin⁡)tu_t(x\mid x_1)=\frac{x_1-(1-\sigma_{\min})x}{1-(1-\sigma_{\min})t}
the same field written as a function of position
"OT"\text{"OT"}
the conditional flow is the optimal-transport displacement map between N(0,I)\mathcal N(0,I) and N(x1,σmin⁡2I)\mathcal N(x_1,\sigma_{\min}^2I); the marginal flow is not, in general, optimal transport
VP:  μt=α1−t x1,  σt=1−α1−t2,  αs=e−12∫0sβ(r) drVE:  μt=x1,  σt=σ1−t\textbf{VP:}\ \ \mu_t=\alpha_{1-t}\,x_1,\ \ \sigma_t=\sqrt{1-\alpha_{1-t}^2},\ \ \alpha_s=e^{-\frac12\int_0^s\beta(r)\,dr}\qquad\textbf{VE:}\ \ \mu_t=x_1,\ \ \sigma_t=\sigma_{1-t}
VP\text{VP}
the variance-preserving diffusion of Denoising diffusion probabilistic models (DDPM) and score SDEs, with time reversed so that data sits at t=1t=1
VE\text{VE}
the variance-exploding path of noise-conditional Score-based generative models
β(r)\beta(r)
noise schedule, e.g. linear from 0.1 to 20

With a diffusion path, FM trains a network whose ODE is the diffusion model's probability-flow ODE, but with a different and more stable regression target than denoising score matching (see Diffusion noise-prediction loss). For any Gaussian path written xt=atx1+btx0x_t=a_tx_1+b_tx_0, the marginal velocity and the score are affinely related: ut(x)=a˙tatx+(a˙tatbt2−btb˙t)∇xlog⁡pt(x)u_t(x)=\frac{\dot a_t}{a_t}x+\big(\frac{\dot a_t}{a_t}b_t^2-b_t\dot b_t\big)\nabla_x\log p_t(x). A trained velocity model therefore also gives a score model, and the reverse holds. Diffusion is one special case of flow matching. Flow matching itself is not tied to Gaussian noise or to a stochastic process.

Setup in the paper

Images
CIFAR-10 and ImageNet at 32232^2, 64264^2 and 1282128^2; the U-Net of Dhariwal and Nichol (2021) with minimal changes
Paths compared
FM with the OT path, FM with the VP diffusion path, and diffusion baselines (DDPM, score matching, ScoreFlow) trained on the same architecture
Sampling
dopri5 adaptive ODE solver, absolute and relative tolerance 10−510^{-5}; low-NFE comparisons with fixed-step solvers
Likelihood
exact NLL in bits per dimension via the instantaneous change of variables along the learned ODE
Toy 2-D
an MLP with 5 layers of 512 units
Other uses
super-resolution and class-conditional ImageNet; later the training objective of Stable Diffusion 3 (rectified-flow form) and of many audio, video and molecule generators
modelCIFAR-10ImageNet 32×32ImageNet 64×64
DDPM3.12 / 7.48 / 2743.54 / 6.99 / 2623.32 / 17.36 / 264
Score matching3.16 / 19.94 / 2423.56 / 5.68 / 1783.40 / 19.74 / 441
ScoreFlow3.09 / 20.78 / 4283.55 / 14.14 / 1953.36 / 24.95 / 601
FM, VP diffusion path3.10 / 8.06 / 1833.54 / 6.37 / 1933.33 / 16.88 / 187
FM, OT path2.99 / 6.35 / 1423.53 / 5.02 / 1223.31 / 14.45 / 138
Unconditional generation, NLL (bits/dim) / FID / NFE of the adaptive solver (Lipman et al. 2023, Table 1)

With the same network, the OT path gave the best likelihood and FID on all three datasets and needed the fewest function evaluations. FM with the diffusion path already beat the score-matching baselines, so part of the gain comes from the objective and part from the path. On ImageNet 1282128^2 FM-OT reached 2.90 bits/dim and FID 20.9.

N(0, I) → 8 Gaussians · x_t = a_t x₁ + b_t x₀ · arrows = u_t(x)● 240 samples on Euler paths of dx/dt = u_t(x), 8 steps○ data modes (radius 3σ) · t = 0.60OT path, σ_min = 0.01a = t, b = 1 − (1 − σ_min)ta_t 0.600 b_t 0.406sampling (ODE, Euler)NFE 8on a mode at t = 1 97.5%mean |x₁ − x₁(200 steps)| 0.068path length / chord 1.373regression targets at t400 pairs, u = ȧ x₁ + ḃ x₀E‖u(x₁, x₀)‖² 8.686E‖u_t(x_t)‖² 5.701L_CFM − L_FM 3.208the gap does not depend on θ, soCFM and FM have the same gradientand minimiser u_t (the arrows)
pathshow
Euler steps
fig.Source N(0, I) and a mixture of 8 Gaussians (radius 2.5, std 0.15). For a Gaussian mixture the marginal field uₜ(x) = E[u(x₀, x₁) | xₜ = x] has a closed form, so the arrows are the exact minimiser of the CFM loss: a perfectly trained network. With the OT path, 8 Euler steps put 97.5% of 240 samples within 3σ of a mode, at a mean distance of 0.068 from the 200-step solution. With 4 steps the share is 85.4%, with 2 steps 14.2%. One step gives 0%, because at t = 0 the field points every sample at the data mean. The VP diffusion path, sampled with the same exact field, gets 26.7% at 4 steps and 84.2% at 8, and only 97.9% at 16. Almost nothing happens before t = 0.5 (7% of the path length, against 26% for OT), so fixed steps are wasted early and too coarse late. The marginal OT paths are not straight. With 64 steps their length is 1.26× the chord, against 1.16× for VP: OT wins on time parameterisation, not on geometry. Under 'conditional pairs' you see the straight per-sample lines the network is trained on. They cross freely, which is why the target is noisy. The bars at t = 0.6 show E‖u(x₀, x₁)‖² = 8.686 against E‖uₜ(xₜ)‖² = 5.701 (400 pairs). The gap, 3.208, is the irreducible CFM loss, and it does not depend on θ.

Worked example: one CFM target, and the marginal field in 1-D

Take the OT path with σmin⁡=0.01\sigma_{\min}=0.01, a noise sample x0=(0.5, −1)x_0=(0.5,\,-1), a data point x1=(2, 1)x_1=(2,\,1) and t=0.25t=0.25. Then, in one dimension, take data q=12δ−1+12δ+1q=\tfrac12\delta_{-1}+\tfrac12\delta_{+1} with σmin⁡→0\sigma_{\min}\to0, so that xt=t x1+(1−t) x0x_t=t\,x_1+(1-t)\,x_0, and evaluate the marginal field at t=0.5t=0.5, x=0.25x=0.25.

x₁posterior weight p(x₁ | xₜ = 0.25)conditional velocity (x₁ − x)/(1 − t)
+10.73111.5
−10.2689−2.5
marginal uₜ(0.25)0.4242
1-D marginal field at t = 0.5, x = 0.25 (computed in Node)

From a training triple to the loss floor

  1. 1Position: ψ0.25(x0)=(1−0.99⋅0.25) x0+0.25 x1=0.7525 (0.5,−1)+(0.5,0.25)=(0.87625, −0.5025)\psi_{0.25}(x_0)=(1-0.99\cdot0.25)\,x_0+0.25\,x_1=0.7525\,(0.5,-1)+(0.5,0.25)=(0.87625,\,-0.5025).
  2. 2Target: x1−0.99 x0=(1.505, 1.99)x_1-0.99\,x_0=(1.505,\,1.99). The field form gives the same: (x1−0.99 x)/(1−0.99⋅0.25)=(1.1325, 1.4975)/0.7525=(1.505, 1.99)(x_1-0.99\,x)/(1-0.99\cdot0.25)=(1.1325,\,1.4975)/0.7525=(1.505,\,1.99).
  3. 3If the network outputs vθ=(1.2, 2.3)v_\theta=(1.2,\,2.3), this sample contributes (0.305)2+(0.31)2=0.1891(0.305)^2+(0.31)^2=0.1891 to LCFM\mathcal L_{\rm CFM}.
  4. 41-D posterior: given x1x_1, xt∼N(0.5 x1, 0.25)x_t\sim\mathcal N(0.5\,x_1,\,0.25), so the weights are ∝e−0.125\propto e^{-0.125} and e−1.125e^{-1.125}, i.e. 0.73110.7311 and 0.26890.2689. The marginal field is 0.7311⋅1.5+0.2689⋅(−2.5)=0.42420.7311\cdot1.5+0.2689\cdot(-2.5)=0.4242.
  5. 5The best network outputs 0.42420.4242 at this point, yet the per-sample targets are 1.51.5 or −2.5-2.5. The residual loss is E[u2]−ut2=3.3258−0.1800=3.1458\mathbb E[u^2]-u_t^2=3.3258-0.1800=3.1458, the conditional variance. A CFM loss that plateaus well above zero is normal.
  6. 6Check against the score: ∇log⁡p0.5(0.25)=−0.0758\nabla\log p_{0.5}(0.25)=-0.0758, and a˙ax+(a˙ab2−bb˙)∇log⁡p=2(0.25)+(0.5+0.5)(−0.0758)=0.4242\frac{\dot a}{a}x+(\frac{\dot a}{a}b^2-b\dot b)\nabla\log p=2(0.25)+(0.5+0.5)(-0.0758)=0.4242 with a=ta=t, b=1−tb=1-t.
python
1import math, torch, torch.nn as nn
2
3class VelocityMLP(nn.Module):
4 def __init__(self, d=2, h=256):
5 super().__init__()
6 self.net = nn.Sequential(nn.Linear(d + 1, h), nn.SiLU(), nn.Linear(h, h), nn.SiLU(),
7 nn.Linear(h, h), nn.SiLU(), nn.Linear(h, d))
8
9 def forward(self, x, t): # x (B, d), t (B,)
10 return self.net(torch.cat([x, t[:, None]], -1))
11
12def sample_data(n): # 8 Gaussians on a circle of radius 2.5
13 k = torch.randint(0, 8, (n,))
14 ang = 2 * math.pi * k / 8
15 mu = 2.5 * torch.stack([ang.cos(), ang.sin()], -1)
16 return mu + 0.15 * torch.randn(n, 2)
17
18def cfm_loss(model, x1, sigma_min=1e-4):
19 x0 = torch.randn_like(x1) # source sample, p_0 = N(0, I)
20 t = torch.rand(x1.size(0)) # t ~ U[0, 1]
21 xt = (1 - (1 - sigma_min) * t[:, None]) * x0 + t[:, None] * x1 # psi_t(x0), OT path
22 target = x1 - (1 - sigma_min) * x0 # d/dt psi_t(x0)
23 return ((model(xt, t) - target) ** 2).sum(-1).mean()
24
25@torch.no_grad()
26def sample(model, n, steps=8): # Euler on dx/dt = v_theta(x, t), t: 0 -> 1
27 x = torch.randn(n, 2)
28 for i in range(steps):
29 t = torch.full((n,), i / steps)
30 x = x + model(x, t) / steps
31 return x
32
33torch.manual_seed(0)
34model = VelocityMLP()
35opt = torch.optim.Adam(model.parameters(), lr=1e-3)
36for it in range(3000): # no ODE solve anywhere in training
37 loss = cfm_loss(model, sample_data(512))
38 opt.zero_grad(); loss.backward(); opt.step()
39x = sample(model, 1000, steps=8)
40print(x.shape) # torch.Size([1000, 2])
41print(round(loss.item(), 2)) # 4.69: stays well above 0, the conditional target is noisy
42print(round(x.norm(dim=-1).mean().item(), 1)) # 2.4: samples sit near the circle of radius 2.5
Conditional flow matching with the OT path on a 2-D toy, and Euler sampling

Use when

  • +Simulation-free training: one network evaluation per sample, a plain regression loss
  • +Any conditional path is allowed; the OT path gives a constant, tt-independent target and fewer sampling steps
  • +Exact likelihood and deterministic, invertible encoding through the learned ODE
  • +Contains diffusion as a special case, so tools such as guidance and latent training transfer

Avoid when

  • −Sampling still needs an ODE solve (tens to hundreds of NFE without distillation)
  • −Marginal paths stay curved with independent coupling; few-step sampling needs reflow, minibatch OT or distillation
  • −The loss has a high noise floor, so it is a poor progress signal
  • −The time convention and the σmin⁡\sigma_{\min} boundary differ between papers and codebases

Check yourself

Why can flow matching train on per-sample conditional velocities u_t(x | x₁) instead of the marginal field u_t(x)?

Check yourself

With the OT path, σ_min = 0.01, x₀ = (0.5, −1), x₁ = (2, 1) and t = 0.25, what does the network regress on?

Remember

  • ✓Conditional flow matching: LCFM=Et,x0,x1∥vθ(ψt(x0),t)−ddtψt(x0)∥2\mathcal L_{\rm CFM}=\mathbb E_{t,x_0,x_1}\|v_\theta(\psi_t(x_0),t)-\tfrac{d}{dt}\psi_t(x_0)\|^2 has the same gradient as regressing the marginal field ut(x)u_t(x), so training needs no ODE simulation.
  • ✓OT path: ψt(x0)=(1−(1−σmin⁡)t) x0+t x1\psi_t(x_0)=(1-(1-\sigma_{\min})t)\,x_0+t\,x_1 with target x1−(1−σmin⁡)x0x_1-(1-\sigma_{\min})x_0; sample by integrating x˙=vθ(x,t)\dot x=v_\theta(x,t) from noise (t=0t=0) to data (t=1t=1).
  • ✓Gaussian paths N(μt(x1),σt2I)\mathcal N(\mu_t(x_1),\sigma_t^2I) have field σt′σt(x−μt)+μt′\frac{\sigma_t'}{\sigma_t}(x-\mu_t)+\mu_t'; diffusion (VP/VE) paths are special cases, and the velocity is affine in the score.
  • ✓The loss floor is the conditional variance of the target; a high plateau is expected.
  • ✓Lipman et al. (Table 1): FM-OT 2.99 bits/dim and FID 6.35 on CIFAR-10 with 142 NFE, best of all compared methods.
← back to Deep Learning roadmap