Continuous Normalizing Flows & Flow Matching
From discrete layers to continuous dynamics: neural ODEs for generation, the instantaneous change of variables, flow matching training, rectified flows, and optimal transport connections.
Prerequisites
Table of Contents
- Learning Objectives
- Notation
- Core Intuition
- Neural ODEs for Generation
- Instantaneous Change of Variables
- Flow Matching Objective
- Conditional Flow Matching
- Rectified Flows
- Optimal Transport Connection
- Comparison with Diffusion
- Common Pitfalls
- Summary
- Exercises
Learning Objectives
- Derive the continuous-time change of variables (instantaneous form).
- Explain why flow matching avoids simulation during training.
- Derive the conditional flow matching objective.
- Connect rectified flows to optimal transport.
- Compare flow matching vs diffusion in terms of sample quality and speed.
Notation
- — velocity field
- — flow (diffeomorphism)
- ,
- OT — optimal transport
Core Intuition
Instead of stacking many discrete invertible layers, define a continuous transformation via an ODE: . Integrating from to maps noise to data. The velocity field is learned to transport the noise distribution to the data distribution along efficient (ideally straight) paths.
Continuous Normalizing Flow
Neural ODEs for Generation
Define the generative process as an ODE:
Sampling: Start from , solve ODE forward to get .
Density: The log-probability evolves according to the instantaneous change of variables.
Instantaneous Change of Variables
Log-likelihood:
Problem: Computing the trace requires evaluations or Hutchinson's estimator — expensive during training.
Flow Matching Objective
Key insight: We don't need to simulate the ODE during training. Instead, directly regress the velocity field:
where is the target velocity field that generates the desired flow.
Problem: We don't know or for the marginal flow.
Conditional Flow Matching
Solution: Condition on endpoints. For each pair , define a simple conditional path:
Conditional velocity: .
Training:
Simulation-free: No ODE solving during training. Just sample , interpolate, predict velocity.
Rectified Flows
Straight-line paths: The interpolation defines straight lines in data space.
Reflow: Iteratively straighten trajectories:
- Train flow matching model .
- Generate pairs: sample , integrate ODE to get .
- Retrain on the generated pairs (better coupling than random).
- After 2-3 iterations: nearly straight trajectories.
One-step generation: With perfectly straight flows, a single Euler step suffices: .
Optimal Transport Connection
Random coupling ( independent): Paths may cross, requiring curved trajectories.
OT coupling (minimize transport cost): Paths don't cross, trajectories are straighter.
Mini-batch OT: Approximate OT within each training batch by solving a linear assignment problem. Significantly improves training convergence and sample quality.
Comparison with Diffusion
- Diffusion: Curved paths (VP-SDE), needs 20-50 ODE steps.
- Flow matching: Straight-line paths, needs 5-20 Euler steps.
- Quality: Comparable FID scores; flow matching slightly better at few steps.
- Training: Both simulation-free; flow matching objective is simpler (no noise schedule).
- Used in: Stable Diffusion 3, Flux, DALL-E 3.
Common Pitfalls
Pitfall 1. Random coupling without OT leads to crossing paths, which are hard to learn and require more steps.
Pitfall 2. Using too few Euler steps without reflow. Initial flow matching models still have curved trajectories; single-step quality is poor without reflow.
Pitfall 3. Assuming flow matching replaces diffusion. For highest quality, multi-step diffusion/flow methods are still superior; flow matching excels at the speed-quality tradeoff.
Summary
- CNF: Continuous transformation via neural ODE; exact likelihood via trace formula.
- Flow matching: Simulation-free training; regress velocity on interpolated points.
- Conditional FM: Straight-line interpolation between noise and data.
- Rectified flows: Iterative straightening for few-step generation.
- OT coupling: Reduces path crossing, improves straightness.
- Dominant in modern image/video generation (SD3, Flux).
Exercises
Exercise 1. Derive the instantaneous change of variables (equation 2) from the discrete formula.
Exercise 2. Show that conditional flow matching (equation 6) is equivalent to marginal flow matching (equation 4) in expectation.
Exercise 3. Compute the number of Euler steps needed for FID less than 5 on CIFAR-10 with and without reflow.
Exercise 4. Implement mini-batch OT: given 64 noise samples and 64 data samples, find the optimal pairing.
Exercise 5. Derive the velocity field for the VP-SDE interpolation and compare to the straight-line velocity.