# 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.

Source: https://www.mlfoundry.in/learn/flow-matching  
Section: Deep Learning › Generative Deep Learning Algorithms  
Level: advanced · 15 min read · updated 2026-10-12

**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_\theta(x,t)$ defines an ODE $\dot x=v_\theta(x,t)$ that carries a simple source $p_0=\mathcal N(0,I)$ at $t=0$ to the data distribution $q$ at $t=1$. Instead of maximising likelihood through the ODE, FM regresses $v_\theta$ on a target vector field $u_t$ that is known to generate a chosen **probability path** $p_t$. Its practical form, **conditional flow matching** (CFM), uses per-sample paths from noise $x_0$ to a data point $x_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 $(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

$$
\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
$$

- $\phi_t$: the flow: where a source point $x$ is at time $t$
- $v_t$: the time-dependent vector field (the network $v_\theta(x,t)$)
- $[\phi_t]_\#\,p_0$: push-forward: the distribution of $\phi_t(x_0)$ for $x_0\sim p_0$
- $\partial_t p_t+\nabla\cdot(p_tv_t)=0$: continuity equation; a field $u_t$ **generates** a path $p_t$ when the pair satisfies it

$$
\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
$$

- $p_t(x\mid x_1)$: a **conditional** path: $p_0(\cdot\mid x_1)=\mathcal N(0,I)$ and $p_1(\cdot\mid x_1)$ concentrated around the data point $x_1$
- $u_t(x\mid x_1)$: a field that generates the conditional path
- $p_t,\ u_t$: the **marginal** path and field; $u_t$ is the posterior average of the conditional fields, and it generates $p_t$ (Theorem 1), with $p_1\approx q$

$\mathcal L_{\rm FM}$ is the right objective, but it cannot be computed, because $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.

$$
\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}
$$

- $\mathcal L_{\rm CFM}$: sample a data point, sample $x$ on its conditional path, regress on its conditional velocity
- $\nabla_\theta\mathcal L_{\rm CFM}=\nabla_\theta\mathcal L_{\rm FM}$: Theorem 2 (assuming $p_t(x)>0$); the two losses differ by a constant, so they share their minimiser $v_\theta=u_t$

The proof is one line. Expand both squares. The $\|v_\theta\|^2$ terms are identical, since both average over the same $p_t(x)$. The cross terms agree as well: $\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 $u_t(x)$ is the posterior mean of $u_t(x\mid x_1)$. What remains, $\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 $x$, and it is the floor below which the CFM loss cannot fall.

## Gaussian conditional paths and the OT path

$$
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)
$$

- $\mu_0=0,\ \sigma_0=1$: every conditional path starts at the source $\mathcal N(0,I)$
- $\mu_1=x_1,\ \sigma_1=\sigma_{\min}$: and ends in a small Gaussian around the data point
- $\psi_t$: the conditional flow; $x=\psi_t(x_0)$ with $x_0\sim\mathcal N(0,I)$ samples the path
- $u_t(x\mid x_1)$: Theorem 3: the unique field of this affine flow; primes denote $d/dt$

Substituting $x=\psi_t(x_0)$ gives the loss that is actually implemented, $\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 $\mu_t,\sigma_t$ with the boundary values above is allowed. The paper studies two families.

$$
\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
$$

- $\psi_t(x_0)$: a straight line from $x_0$ to (almost) $x_1$, travelled at constant speed
- $x_1-(1-\sigma_{\min})x_0$: the regression target; it does not depend on $t$
- $u_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
- $\text{"OT"}$: the conditional flow is the optimal-transport displacement map between $\mathcal N(0,I)$ and $\mathcal N(x_1,\sigma_{\min}^2I)$; the **marginal** flow is not, in general, optimal transport

$$
\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}
$$

- $\text{VP}$: the variance-preserving diffusion of **Denoising diffusion probabilistic models (DDPM)** and score SDEs, with time reversed so that data sits at $t=1$
- $\text{VE}$: the variance-exploding path of noise-conditional **Score-based generative models**
- $\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 $x_t=a_tx_1+b_tx_0$, the marginal velocity and the score are affinely related: $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 $32^2$, $64^2$ and $128^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^{-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

Unconditional generation, NLL (bits/dim) / FID / NFE of the adaptive solver (Lipman et al. 2023, Table 1)

| model | CIFAR-10 | ImageNet 32×32 | ImageNet 64×64 |
| --- | --- | --- | --- |
| DDPM | 3.12 / 7.48 / 274 | 3.54 / 6.99 / 262 | 3.32 / 17.36 / 264 |
| Score matching | 3.16 / 19.94 / 242 | 3.56 / 5.68 / 178 | 3.40 / 19.74 / 441 |
| ScoreFlow | 3.09 / 20.78 / 428 | 3.55 / 14.14 / 195 | 3.36 / 24.95 / 601 |
| FM, VP diffusion path | 3.10 / 8.06 / 183 | 3.54 / 6.37 / 193 | 3.33 / 16.88 / 187 |
| FM, OT path | 2.99 / 6.35 / 142 | 3.53 / 5.02 / 122 | 3.31 / 14.45 / 138 |

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 $128^2$ FM-OT reached 2.90 bits/dim and FID 20.9.

> Figure: 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 $\sigma_{\min}=0.01$, a noise sample $x_0=(0.5,\,-1)$, a data point $x_1=(2,\,1)$ and $t=0.25$. Then, in one dimension, take data $q=\tfrac12\delta_{-1}+\tfrac12\delta_{+1}$ with $\sigma_{\min}\to0$, so that $x_t=t\,x_1+(1-t)\,x_0$, and evaluate the marginal field at $t=0.5$, $x=0.25$.

1-D marginal field at t = 0.5, x = 0.25 (computed in Node)

| x₁ | posterior weight p(x₁ \| xₜ = 0.25) | conditional velocity (x₁ − x)/(1 − t) |
| --- | --- | --- |
| +1 | 0.7311 | 1.5 |
| −1 | 0.2689 | −2.5 |
| marginal uₜ(0.25) |  | 0.4242 |

**From a training triple to the loss floor**

1. Position: $\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. Target: $x_1-0.99\,x_0=(1.505,\,1.99)$. The field form gives the same: $(x_1-0.99\,x)/(1-0.99\cdot0.25)=(1.1325,\,1.4975)/0.7525=(1.505,\,1.99)$.
3. If the network outputs $v_\theta=(1.2,\,2.3)$, this sample contributes $(0.305)^2+(0.31)^2=0.1891$ to $\mathcal L_{\rm CFM}$.
4. 1-D posterior: given $x_1$, $x_t\sim\mathcal N(0.5\,x_1,\,0.25)$, so the weights are $\propto e^{-0.125}$ and $e^{-1.125}$, i.e. $0.7311$ and $0.2689$. The marginal field is $0.7311\cdot1.5+0.2689\cdot(-2.5)=0.4242$.
5. The best network outputs $0.4242$ at this point, yet the per-sample targets are $1.5$ or $-2.5$. The residual loss is $\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. Check against the score: $\nabla\log p_{0.5}(0.25)=-0.0758$, and $\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=t$, $b=1-t$.

```python
import math, torch, torch.nn as nn

class VelocityMLP(nn.Module):
    def __init__(self, d=2, h=256):
        super().__init__()
        self.net = nn.Sequential(nn.Linear(d + 1, h), nn.SiLU(), nn.Linear(h, h), nn.SiLU(),
                                 nn.Linear(h, h), nn.SiLU(), nn.Linear(h, d))

    def forward(self, x, t):                       # x (B, d), t (B,)
        return self.net(torch.cat([x, t[:, None]], -1))

def sample_data(n):                                # 8 Gaussians on a circle of radius 2.5
    k = torch.randint(0, 8, (n,))
    ang = 2 * math.pi * k / 8
    mu = 2.5 * torch.stack([ang.cos(), ang.sin()], -1)
    return mu + 0.15 * torch.randn(n, 2)

def cfm_loss(model, x1, sigma_min=1e-4):
    x0 = torch.randn_like(x1)                      # source sample, p_0 = N(0, I)
    t = torch.rand(x1.size(0))                     # t ~ U[0, 1]
    xt = (1 - (1 - sigma_min) * t[:, None]) * x0 + t[:, None] * x1   # psi_t(x0), OT path
    target = x1 - (1 - sigma_min) * x0             # d/dt psi_t(x0)
    return ((model(xt, t) - target) ** 2).sum(-1).mean()

@torch.no_grad()
def sample(model, n, steps=8):                     # Euler on dx/dt = v_theta(x, t), t: 0 -> 1
    x = torch.randn(n, 2)
    for i in range(steps):
        t = torch.full((n,), i / steps)
        x = x + model(x, t) / steps
    return x

torch.manual_seed(0)
model = VelocityMLP()
opt = torch.optim.Adam(model.parameters(), lr=1e-3)
for it in range(3000):                             # no ODE solve anywhere in training
    loss = cfm_loss(model, sample_data(512))
    opt.zero_grad(); loss.backward(); opt.step()
x = sample(model, 1000, steps=8)
print(x.shape)                                     # torch.Size([1000, 2])
print(round(loss.item(), 2))                       # 4.69: stays well above 0, the conditional target is noisy
print(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

> **The loss does not go to zero:** $\mathcal L_{\rm CFM}$ at the optimum equals $\mathbb E\,\mathrm{Var}[u_t(x\mid x_1)\mid x]$, which is large wherever several data points are plausible for the same $x_t$ (3.208 at $t=0.6$ in the figure, 3.146 in the 1-D example). A flat loss curve near that floor is not a bug, and comparing loss values across different paths or couplings says little about sample quality. Use FID, likelihood or a held-out ODE metric.

> **Which end is noise?:** Lipman et al. and Liu et al. put noise at $t=0$ and data at $t=1$. Score SDEs, DDPM and **Stable Diffusion** 3 put data at $t=0$, which flips the sign of the velocity. Check the convention before mixing code, schedules or pretrained weights. A sampler integrating the wrong way produces pure noise.

> **Straight conditional paths, curved marginal paths:** Each training line $x_0\to x_1$ is straight, but the marginal field averages many crossing lines, so the generated trajectories bend (1.26× the chord in the figure). The OT path still samples with fewer steps because its speed is spread evenly over $[0,1]$. **Rectified flow** straightens the marginal paths by retraining on the model's own noise–data pairs (reflow). Tong et al. ("Improving and generalizing flow-based generative models with minibatch optimal transport", TMLR 2024) do it inside training: they pair the $x_0$ and $x_1$ of each minibatch by an exact OT assignment before applying the straight path (**OT-CFM**). That approximates dynamic optimal transport and reduces the steps needed.

> **Sampling and likelihood:** Generation is any ODE solver from $t=0$ to $1$: Euler, Heun, midpoint or adaptive dopri5. Because the model is a CNF, the exact log-likelihood follows from $\log p_1(x_1)=\log p_0(x_0)-\int_0^1\nabla\!\cdot v_\theta(x_t,t)\,dt$ (with a Hutchinson trace estimator in high dimension). Guidance, conditioning and latent spaces carry over from diffusion: train $v_\theta(x,t,c)$ in the latent space of an autoencoder as in **Latent diffusion models**.

**Strengths**

- Simulation-free training: one network evaluation per sample, a plain regression loss
- Any conditional path is allowed; the OT path gives a constant, $t$-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

**Limitations**

- 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 $\sigma_{\min}$ boundary differ between papers and codebases

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

- A. Because u_t(x | x₁) = u_t(x) for every x₁
- B. Because the two losses differ by a constant independent of θ, so they have the same gradient and the same minimiser
- C. Because the ODE is solved during training to correct the bias
- D. Because the conditional loss is an upper bound that becomes tight only at σ_min = 0

**Answer:** B. Because the two losses differ by a constant independent of θ, so they have the same gradient and the same minimiser — Expanding the squares, the $\|v_\theta\|^2$ and cross terms coincide because $u_t(x)=\mathbb E[u_t(x\mid x_1)\mid x]$. The remaining difference $\mathbb E\|u_t(x\mid x_1)\|^2-\mathbb E\|u_t(x)\|^2$ does not involve $\theta$ (Theorem 2). The conditional fields are generally very different from the marginal one; their average is what the network learns.

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

- A. (1.505, 1.99)
- B. (0.876, −0.503)
- C. (1.5, 2.0)
- D. (2, 1)

**Answer:** A. (1.505, 1.99) — The target is $\frac{d}{dt}\psi_t(x_0)=x_1-(1-\sigma_{\min})x_0=(2-0.495,\ 1+0.99)$, independent of $t$. $(0.876,-0.503)$ is the input position $\psi_{0.25}(x_0)$. $(1.5,2.0)$ is the $\sigma_{\min}=0$ value $x_1-x_0$.

**Key points**

- Conditional flow matching: $\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 $u_t(x)$, so training needs no ODE simulation.
- OT path: $\psi_t(x_0)=(1-(1-\sigma_{\min})t)\,x_0+t\,x_1$ with target $x_1-(1-\sigma_{\min})x_0$; sample by integrating $\dot x=v_\theta(x,t)$ from noise ($t=0$) to data ($t=1$).
- Gaussian paths $\mathcal N(\mu_t(x_1),\sigma_t^2I)$ have field $\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.

---

From MLFoundry (https://www.mlfoundry.in). The web page adds interactive figures and diagrams.
