Flow Matching: Theory & Training

Simulation-free training of continuous normalizing flows: the flow matching objective, conditional flow matching, probability paths, Gaussian paths, and why flow matching dominates modern generative modeling.

Advanced

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. The Flow Matching Framework
  5. Probability Paths
  6. The Marginal Vector Field
  7. Conditional Flow Matching (CFM)
  8. Gaussian Probability Paths
  9. Optimal Transport Path
  10. Connection to Score Matching
  11. Training Algorithm
  12. Common Pitfalls
  13. Summary
  14. Exercises

Learning Objectives

  1. Define a probability path and the associated vector field.
  2. Derive the flow matching objective from the continuity equation.
  3. Prove that conditional flow matching (CFM) has the same gradient as marginal FM.
  4. Construct Gaussian and OT probability paths.
  5. Connect flow matching to score-based diffusion models.

Notation

  • ptp_t — probability density at time t[0,1]t \in [0, 1]
  • ut(x)u_t(\mathbf{x}) — vector field generating ptp_t
  • ψt\psi_t — flow map: dψt/dt=ut(ψt)d\psi_t/dt = u_t(\psi_t)
  • pt(xz)p_t(\mathbf{x}|z) — conditional path given endpoint zz

Core Intuition

We want to train a neural ODE vθ(x,t)v_\theta(\mathbf{x}, t) that transports noise p0=N(0,I)p_0 = \mathcal{N}(0, \mathbf{I}) to data p1=pdatap_1 = p_{\text{data}}. The naive approach (simulate ODE during training) is expensive. Flow matching avoids simulation entirely: define a simple interpolation path between noise and data, compute the velocity along this path analytically, and regress the neural network to match it.

Conditional Flow Matching

t=0.50 · regress v(x,t) = x₁−x₀
Time t
0.50
Explore: Conditional flow matching regresses the velocity field v(x,t) on straight paths between paired samples. Averaging over pairs yields the marginal velocity for generation.

The Flow Matching Framework

Goal: Find vθv_\theta such that the ODE dx/dt=vθ(x,t)dx/dt = v_\theta(x, t) generates a flow from p0p_0 to p1p_1.

Continuity equation: Any valid probability path ptp_t satisfies:

ptt+(ptut)=0.(1)\frac{\partial p_t}{\partial t} + \nabla\cdot(p_t u_t) = 0. \tag{1}

If we can find a vector field utu_t that generates our desired path, we can train vθv_\theta to match it:

LFM(θ)=EtU[0,1],xpt[vθ(x,t)ut(x)2].(2)\mathcal{L}_{\text{FM}}(\theta) = \mathbb{E}_{t \sim U[0,1], \mathbf{x} \sim p_t}\left[\|v_\theta(\mathbf{x}, t) - u_t(\mathbf{x})\|^2\right]. \tag{2}

Problem: We don't know ptp_t or utu_t for the marginal (unconditional) path.


Probability Paths

A probability path is a time-indexed family of distributions {pt}t[0,1]\{p_t\}_{t \in [0,1]} with:

  • p0=N(0,I)p_0 = \mathcal{N}(0, \mathbf{I}) (noise).
  • p1=pdatap_1 = p_{\text{data}} (data).

Examples:

  • Linear interpolation (OT path): pt=[(1t)Id+tT]#p0p_t = [(1-t)\text{Id} + t\text{T}]_\# p_0 where TT is the OT map.
  • VP-SDE path: pt(xx1)=N(αˉtx1,(1αˉt)I)p_t(\mathbf{x}|x_1) = \mathcal{N}(\sqrt{\bar{\alpha}_t}x_1, (1-\bar{\alpha}_t)\mathbf{I}).
  • Gaussian path: pt(xx1)=N(μt(x1),σt2I)p_t(\mathbf{x}|x_1) = \mathcal{N}(\mu_t(x_1), \sigma_t^2\mathbf{I}).

The Marginal Vector Field

Given a conditional vector field ut(xz)u_t(\mathbf{x}|z) that generates the conditional path pt(xz)p_t(\mathbf{x}|z), the marginal vector field is:

ut(x)=ut(xz)pt(xz)q(z)dzpt(xz)q(z)dz=Ezp(zx,t)[ut(xz)].(3)u_t(\mathbf{x}) = \frac{\int u_t(\mathbf{x}|z) p_t(\mathbf{x}|z) q(z) dz}{\int p_t(\mathbf{x}|z) q(z) dz} = \mathbb{E}_{z \sim p(z|\mathbf{x},t)}\left[u_t(\mathbf{x}|z)\right]. \tag{3}

This marginal field is intractable to compute directly — but we don't need it for training.


Conditional Flow Matching (CFM)

Key theorem (Lipman et al., 2023): The conditional flow matching loss:

LCFM(θ)=Et,zq,xpt(z)[vθ(x,t)ut(xz)2](4)\boxed{\mathcal{L}_{\text{CFM}}(\theta) = \mathbb{E}_{t, z \sim q, \mathbf{x} \sim p_t(\cdot|z)}\left[\|v_\theta(\mathbf{x}, t) - u_t(\mathbf{x}|z)\|^2\right]} \tag{4}

has the same gradients with respect to θ\theta as the marginal flow matching loss LFM\mathcal{L}_{\text{FM}}.

Proof sketch: The cross-term vθ,ut\langle v_\theta, u_t \rangle integrated over pt(xz)q(z)p_t(\mathbf{x}|z)q(z) equals the same cross-term integrated over the marginal pt(x)ut(x)p_t(\mathbf{x})u_t(\mathbf{x}) (by definition of the marginal vector field). The vθ2\|v_\theta\|^2 term is the same (both average over ptp_t). The ut2\|u_t\|^2 term differs but is independent of θ\theta.

Consequence: We can train using only conditional paths (easy to sample from) instead of the intractable marginal path.


Gaussian Probability Paths

The most common choice: conditional Gaussian path from noise to a data point x1x_1:

pt(xx1)=N(μt(x1),σt2I),(5)p_t(\mathbf{x}|x_1) = \mathcal{N}(\mu_t(x_1), \sigma_t^2\mathbf{I}), \tag{5}

with boundary conditions μ0=0,σ0=1\mu_0 = 0, \sigma_0 = 1 (noise) and μ1=x1,σ10\mu_1 = x_1, \sigma_1 \approx 0 (data).

Linear interpolation (simplest):

μt=tx1,σt=1t.(6)\mu_t = tx_1, \quad \sigma_t = 1 - t. \tag{6}

Sampling: xt=tx1+(1t)ϵ,ϵN(0,I)\mathbf{x}_t = tx_1 + (1-t)\epsilon, \quad \epsilon \sim \mathcal{N}(0, \mathbf{I}).

Conditional velocity:

ut(xtx1)=x1xt1t=x1(1t)ϵtx11t...u_t(\mathbf{x}_t | x_1) = \frac{x_1 - \mathbf{x}_t}{1 - t} = \frac{x_1 - (1-t)\epsilon - tx_1}{1-t}...

More directly for the linear path:

ut(xtx1)=x1ϵ=x1x0.(7)u_t(\mathbf{x}_t|x_1) = x_1 - \epsilon = x_1 - x_0. \tag{7}

Training loss:

L=Et,x0N(0,I),x1pdata[vθ((1t)x0+tx1,t)(x1x0)2].(8)\mathcal{L} = \mathbb{E}_{t, x_0 \sim \mathcal{N}(0,I), x_1 \sim p_{\text{data}}}\left[\|v_\theta((1-t)x_0 + tx_1, t) - (x_1 - x_0)\|^2\right]. \tag{8}

Optimal Transport Path

Random coupling (x0,x1x_0, x_1 independent): Straight lines may cross → curved velocity field needed.

OT coupling: Match x0x_0 to x1x_1 via optimal transport → non-crossing paths → straighter trajectories.

Mini-batch OT: Within each batch, solve a linear assignment:

π=argminπix0ix1π(i)2.(9)\pi^* = \arg\min_\pi \sum_i \|x_0^i - x_1^{\pi(i)}\|^2. \tag{9}

Cost: O(B3)O(B^3) per batch (Hungarian algorithm) or O(B2)O(B^2) (Sinkhorn).

Effect: 2-3x fewer sampling steps needed; better FID at few steps.


Connection to Score Matching

Score matching (diffusion): Predict noise ϵθ(xt,t)ϵ\epsilon_\theta(\mathbf{x}_t, t) \approx \epsilon.

Flow matching: Predict velocity vθ(xt,t)x1x0v_\theta(\mathbf{x}_t, t) \approx x_1 - x_0.

Relationship: For the VP-SDE path:

vt(x)=dμtdtx1+dσtdtϵ=α˙tx1+σ˙tϵ.(10)v_t(\mathbf{x}) = \frac{d\mu_t}{dt}x_1 + \frac{d\sigma_t}{dt}\epsilon = \dot{\alpha}_t x_1 + \dot{\sigma}_t\epsilon. \tag{10}

So vθv_\theta and ϵθ\epsilon_\theta are linear reparameterizations of each other. Same model, different parameterization.

Advantage of velocity parameterization: More natural for straight-line paths (OT); velocity is bounded while score logpt\nabla\log p_t can diverge.


Training Algorithm

For each training step:
    1. Sample x_1 ~ p_data (data batch)
    2. Sample x_0 ~ N(0, I) (noise batch)
    3. [Optional] Apply mini-batch OT to pair (x_0, x_1)
    4. Sample t ~ U[0, 1]
    5. Compute x_t = (1-t)*x_0 + t*x_1
    6. Compute target velocity: u = x_1 - x_0
    7. Loss = ||v_theta(x_t, t) - u||^2
    8. Gradient step on theta

Sampling (inference):

  • Solve ODE: dx/dt=vθ(x,t)dx/dt = v_\theta(x, t) from t=0t=0 to t=1t=1.
  • Use Euler, midpoint, or adaptive solver.
  • Fewer steps than diffusion (5-50 vs 20-1000).

Common Pitfalls

Pitfall 1. Using random coupling without OT for few-step generation. Without OT, paths cross and the velocity field becomes multi-valued at intermediate times — requiring many steps to resolve.

Pitfall 2. Confusing the conditional and marginal objectives. CFM trains on conditional velocities but the model learns the MARGINAL velocity (which correctly generates the marginal p1p_1).

Pitfall 3. Setting σ1=0\sigma_1 = 0 exactly. This makes the conditional path degenerate at t=1t=1. Use σ1=105\sigma_1 = 10^{-5} or stop integration slightly before t=1t=1.


Summary

  • Flow matching trains continuous flows without simulating ODEs during training.
  • CFM uses conditional paths (easy to sample) with identical gradients to the marginal objective.
  • Linear Gaussian path: xt=(1t)x0+tx1x_t = (1-t)x_0 + tx_1; velocity target: x1x0x_1 - x_0.
  • OT coupling straightens trajectories for faster sampling.
  • Equivalent to diffusion under different path/parameterization choices.
  • Used in Stable Diffusion 3, Flux, DALL-E 3.

Exercises

Exercise 1. Prove that CFM and marginal FM have the same gradient (expand both losses and show the θ\nabla_\theta terms match).

Exercise 2. For the linear path xt=(1t)x0+tx1x_t = (1-t)x_0 + tx_1: derive the time-dependent SNR and compare to the VP-SDE schedule.

Exercise 3. Implement mini-batch OT for a batch of 64 samples in 2D. Compare the resulting trajectories (with/without OT) visually.

Exercise 4. Show that for the VP-SDE path, the flow matching velocity is vt=α˙tx1/αtσ˙tσtlogptv_t = \dot{\alpha}_t x_1/\alpha_t - \dot{\sigma}_t\sigma_t\nabla\log p_t.

Exercise 5. Compute the number of Euler steps needed to achieve FID less than 10 on CIFAR-10 for linear FM vs VP-SDE FM.