Flow Matching & Rectified Flows

Continuous normalizing flows for generation: the flow matching objective, optimal transport paths, rectified flows, connection to diffusion, and why SD3 and Flux use flow matching.

Advanced

Table of Contents

  1. Learning Objectives
  2. Notation
  3. Core Intuition
  4. Continuous Normalizing Flows
  5. The Flow Matching Objective
  6. Conditional Flow Matching
  7. Rectified Flows
  8. Connection to Diffusion Models
  9. Advantages Over Diffusion
  10. Common Pitfalls
  11. Summary
  12. Exercises

Learning Objectives

  1. Define continuous normalizing flows and the transport equation.
  2. Derive the flow matching training objective.
  3. Explain why conditional flow matching makes training tractable.
  4. Derive rectified flows as straight-line interpolations.
  5. Compare flow matching vs diffusion in terms of sample quality and speed.

Notation

  • ψt:RdRd\psi_t: \mathbb{R}^d \to \mathbb{R}^d — flow (diffeomorphism) at time tt
  • vθ(x,t)\mathbf{v}_\theta(\mathbf{x}, t) — learned velocity field
  • ptp_t — probability density at time tt (evolving)
  • p0=N(0,I)p_0 = \mathcal{N}(0, \mathbf{I}), p1=pdatap_1 = p_{\text{data}}

Core Intuition

Instead of adding and removing noise (diffusion), flow matching learns a velocity field that transports samples from noise (t=0t=0) to data (t=1t=1) along smooth paths. The simplest paths are straight lines between noise and data points. This leads to a simpler objective than diffusion, faster sampling (straighter paths need fewer integration steps), and a unified framework.

Flow Matching vs Diffusion

t = 0.50
Time t
0.50
Flow (ODE, straight)Diffusion (SDE, curved)
Explore: Flow matching learns straight ODE paths from noise to data, enabling fewer integration steps. Diffusion follows stochastic curved paths requiring many denoising steps.

Continuous Normalizing Flows

A flow ψt\psi_t is defined by an ODE:

dψt(x)dt=vt(ψt(x)),ψ0(x)=x.(1)\frac{d\psi_t(\mathbf{x})}{dt} = \mathbf{v}_t(\psi_t(\mathbf{x})), \quad \psi_0(\mathbf{x}) = \mathbf{x}. \tag{1}

The density evolves according to the continuity equation:

ptt+(ptvt)=0.(2)\frac{\partial p_t}{\partial t} + \nabla \cdot (p_t \mathbf{v}_t) = 0. \tag{2}

Goal: Find vt\mathbf{v}_t such that p0=N(0,I)p_0 = \mathcal{N}(0, \mathbf{I}) is transported to p1=pdatap_1 = p_{\text{data}}.


The Flow Matching Objective

Ideal objective: Match the true velocity field ut\mathbf{u}_t that generates the target flow:

LFM=Et,pt(x)[vθ(x,t)ut(x)2].(3)\mathcal{L}_{\text{FM}} = \mathbb{E}_{t, p_t(\mathbf{x})}\left[\|\mathbf{v}_\theta(\mathbf{x}, t) - \mathbf{u}_t(\mathbf{x})\|^2\right]. \tag{3}

Problem: We don't know ut\mathbf{u}_t or ptp_t (they depend on the optimal transport plan).


Conditional Flow Matching

Solution (Lipman et al., 2023): Condition on individual data points. Define a simple conditional flow for each data point x1\mathbf{x}_1:

ψt(x0x1)=(1t)x0+tx1.(4)\psi_t(\mathbf{x}_0 | \mathbf{x}_1) = (1-t)\mathbf{x}_0 + t\mathbf{x}_1. \tag{4}

This is a straight line from noise x0\mathbf{x}_0 to data x1\mathbf{x}_1.

Conditional velocity:

ut(xx1)=x1x0.(5)\mathbf{u}_t(\mathbf{x} | \mathbf{x}_1) = \mathbf{x}_1 - \mathbf{x}_0. \tag{5}

Training objective (equivalent to FM):

LCFM=EtU(0,1),x0N(0,I),x1pdata[vθ(ψt(x0x1),t)(x1x0)2].(6)\boxed{\mathcal{L}_{\text{CFM}} = \mathbb{E}_{t \sim U(0,1), \mathbf{x}_0 \sim \mathcal{N}(0,\mathbf{I}), \mathbf{x}_1 \sim p_{\text{data}}}\left[\|\mathbf{v}_\theta(\psi_t(\mathbf{x}_0|\mathbf{x}_1), t) - (\mathbf{x}_1 - \mathbf{x}_0)\|^2\right].} \tag{6}

Simplicity: Sample noise x0\mathbf{x}_0, sample data x1\mathbf{x}_1, interpolate to get xt\mathbf{x}_t, predict the velocity (direction from noise to data). That's it.


Rectified Flows

Rectified flow (Liu et al., 2022): The straight-line interpolation xt=(1t)x0+tx1\mathbf{x}_t = (1-t)\mathbf{x}_0 + t\mathbf{x}_1 defines a "rectified" flow.

Property: If the learned velocity field perfectly matches, then the ODE trajectories are straight lines — requiring only one Euler step for perfect generation.

Reflow procedure: Iteratively straighten trajectories:

  1. Train flow matching model.
  2. Generate pairs (x0,x1)(\mathbf{x}_0, \mathbf{x}_1) by integrating the learned ODE.
  3. Retrain on these pairs (now the coupling is better than random).
  4. Repeat: trajectories become straighter each iteration.

After 2–3 reflow iterations, 1–4 step generation becomes possible.


Connection to Diffusion Models

Diffusion as a special flow: The VP-SDE diffusion process defines a specific (curved) path:

xt=αˉtx1+1αˉtx0.(7)\mathbf{x}_t = \sqrt{\bar{\alpha}_t}\mathbf{x}_1 + \sqrt{1-\bar{\alpha}_t}\mathbf{x}_0. \tag{7}

This is NOT a straight line (nonlinear interpolation). The corresponding velocity involves the score function.

Flow matching uses straight lines (equation 4): simpler, straighter paths → fewer ODE steps needed.

Equivalence: Both can achieve the same final quality, but flow matching reaches it in fewer steps because trajectories are straighter (less curvature → less numerical error per step).


Advantages Over Diffusion

1. Simpler objective: Predict x1x0\mathbf{x}_1 - \mathbf{x}_0 instead of noise ϵ\boldsymbol{\epsilon}. No noise schedule needed.

2. Fewer sampling steps: Straight-line flows need 10–20 Euler steps for high quality (vs 50+ for diffusion).

3. No variance schedule tuning: The interpolation t[0,1]t \in [0,1] is the only "schedule."

4. Unified framework: Natural extension to other source distributions (not just Gaussian noise), optimal transport couplings, and conditional generation.


Common Pitfalls

Pitfall 1. Assuming flow matching is always better. For very high-quality samples (FID < 2), well-tuned diffusion with many steps can still match or beat flow matching.

Pitfall 2. Using random coupling between x0\mathbf{x}_0 and x1\mathbf{x}_1. Random pairing creates crossing trajectories that are hard to learn. Optimal transport coupling (mini-batch OT) significantly improves quality.

Pitfall 3. Ignoring the simulation-free property. Flow matching is trained without simulating the ODE — each training step only needs a random tt and the interpolated point.


Summary

  • Flow matching learns a velocity field transporting noise to data along straight lines.
  • Conditional flow matching makes training tractable by conditioning on individual data points.
  • Objective: Predict the velocity x1x0\mathbf{x}_1 - \mathbf{x}_0 at interpolated points.
  • Rectified flows iteratively straighten trajectories for few-step generation.
  • Simpler than diffusion: no noise schedule, fewer steps, unified framework.
  • Used in: Stable Diffusion 3, Flux, modern image/video generation.

Exercises

Exercise 1. Verify that ψt(x0x1)=(1t)x0+tx1\psi_t(\mathbf{x}_0|\mathbf{x}_1) = (1-t)\mathbf{x}_0 + t\mathbf{x}_1 satisfies ψ0=x0\psi_0 = \mathbf{x}_0 and ψ1=x1\psi_1 = \mathbf{x}_1.

Exercise 2. Derive the conditional velocity field for the VP-SDE interpolation xt=αˉtx1+1αˉtx0\mathbf{x}_t = \sqrt{\bar{\alpha}_t}\mathbf{x}_1 + \sqrt{1-\bar{\alpha}_t}\mathbf{x}_0.

Exercise 3. Prove that the marginal flow matching objective (equation 3) equals the conditional flow matching objective (equation 6) in expectation.

Exercise 4. Explain why straight-line flows need fewer Euler steps than curved diffusion paths.

Exercise 5. Design a reflow procedure for a trained model and estimate the quality after 1, 2, and 3 reflow iterations.