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.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- The Flow Matching Framework
- Probability Paths
- The Marginal Vector Field
- Conditional Flow Matching (CFM)
- Gaussian Probability Paths
- Optimal Transport Path
- Connection to Score Matching
- Training Algorithm
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Define a probability path and the associated vector field.
- Derive the flow matching objective from the continuity equation.
- Prove that conditional flow matching (CFM) has the same gradient as marginal FM.
- Construct Gaussian and OT probability paths.
- Connect flow matching to score-based diffusion models.
Notation
- — probability density at time
- — vector field generating
- — flow map:
- — conditional path given endpoint
Core Intuition
We want to train a neural ODE that transports noise to 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
The Flow Matching Framework
Goal: Find such that the ODE generates a flow from to .
Continuity equation: Any valid probability path satisfies:
If we can find a vector field that generates our desired path, we can train to match it:
Problem: We don't know or for the marginal (unconditional) path.
Probability Paths
A probability path is a time-indexed family of distributions with:
- (noise).
- (data).
Examples:
- Linear interpolation (OT path): where is the OT map.
- VP-SDE path: .
- Gaussian path: .
The Marginal Vector Field
Given a conditional vector field that generates the conditional path , the marginal vector field is:
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:
has the same gradients with respect to as the marginal flow matching loss .
Proof sketch: The cross-term integrated over equals the same cross-term integrated over the marginal (by definition of the marginal vector field). The term is the same (both average over ). The term differs but is independent of .
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 :
with boundary conditions (noise) and (data).
Linear interpolation (simplest):
Sampling: .
Conditional velocity:
More directly for the linear path:
Training loss:
Optimal Transport Path
Random coupling ( independent): Straight lines may cross → curved velocity field needed.
OT coupling: Match to via optimal transport → non-crossing paths → straighter trajectories.
Mini-batch OT: Within each batch, solve a linear assignment:
Cost: per batch (Hungarian algorithm) or (Sinkhorn).
Effect: 2-3x fewer sampling steps needed; better FID at few steps.
Connection to Score Matching
Score matching (diffusion): Predict noise .
Flow matching: Predict velocity .
Relationship: For the VP-SDE path:
So and 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 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: from to .
- 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 ).
Pitfall 3. Setting exactly. This makes the conditional path degenerate at . Use or stop integration slightly before .
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: ; velocity target: .
- 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 terms match).
Exercise 2. For the linear path : 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 .
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.