Search

Search papers and lessons

Flow Matching for Generative Modeling

Yaron Lipman, Ricky T. Q. Chen, Heli Ben-Hamu et al. · 2022

Lipman and colleagues' 2022 paper trains continuous normalizing flows by regressing a vector field onto simple per-sample paths, which beats diffusion training on ImageNet and samples faster with straight-line paths.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Linear algebra
  • Generative models
  • Neural network basics
  • Losses & regularization
  • Sign in to see your progress

TL;DR

Flow Matching is a way to train a continuous normalizing flow, a model that turns noise into data by following a learned velocity field, without simulating the flow during training. The paper's key move is to regress the network onto a simple target vector field built one training example at a time, from a path that carries a noise sample to a data point. The paper shows this beats diffusion-model training on ImageNet in both likelihood and sample quality, and that choosing straight-line optimal transport paths makes sampling need fewer function evaluations.

Where you'll see it: the training recipe behind many modern image and video generators, and the reason "rectified flow" style models sample in a few ODE steps.

The problem

By 2022 image generation ran almost entirely on diffusion models. They were stable to train, but the training target was tied to a specific stochastic process that adds noise step by step. That tie fixed the set of probability paths the model could travel, and the paths were curved, so sampling needed many small steps.

Continuous normalizing flows (CNFs) were a more flexible alternative. A CNF is a neural network that defines a velocity field, and integrating an ordinary differential equation (ODE) along that field moves a noise sample to a data sample. Maximum likelihood training of a CNF, as in the original neural ODE work, needs an ODE solve inside every gradient step, which is slow and does not scale to high-resolution images. Other simulation-free methods existed, but they either involved intractable integrals or produced biased gradients on minibatches. So the question was whether a CNF could be trained as cheaply as a diffusion model while keeping the freedom to pick any probability path.

Key idea

Build the training target from single examples. Pick a data point x1x_1, pick a noise sample x0x_0, and define a path ψt(x0)\psi_t(x_0) that moves x0x_0 to x1x_1 over time t∈[0,1]t \in [0,1]. This path has an exact velocity at every time, and that velocity is a known formula in x0x_0, x1x_1 and tt. The network vtv_t is trained to predict that velocity at the point where the path currently sits.

flowchart LR
  A["noise x0 ~ N(0, I)"] -->|"conditional path psi_t(x0), sampled per example"| B["data x1"]
  C["network v_t(x)"] -->|"regress onto known velocity"| D["u_t(x given x1)"]

The surprising part is that regressing onto these per-example velocities gives the same gradients as regressing onto the true marginal velocity field, which is intractable. You never need the marginal field. The paper calls this the Conditional Flow Matching objective. At sampling time you start from fresh noise and integrate vtv_t with any ODE solver.

The paper's second idea is about which path to choose. Diffusion gives curved paths. The paper picks the path that is the optimal transport (OT) displacement between a Gaussian noise distribution and a narrow Gaussian around the data point. Under that path particles move in straight lines at constant speed, which makes the regression target simpler and the ODE easier to solve.

The method

Setup. The data distribution q(x1)q(x_1) is unknown, but samples from it are available. The noise distribution is p(x0)=N(x0∣0,I)p(x_0) = \mathcal{N}(x_0 \mid 0, I). The model is a vector field vt(x;θ)v_t(x;\theta) with learnable parameters θ\theta. Time tt is drawn uniformly from [0,1][0,1].

Marginal objective. The Flow Matching loss would be

LFM(θ)=Et,pt(x)∥vt(x)−ut(x)∥2,\mathcal{L}_{\text{FM}}(\theta) = \mathbb{E}_{t, p_t(x)} \left\| v_t(x) - u_t(x) \right\|^2,

where ptp_t is the marginal probability path that goes from noise at t=0t=0 to approximately the data distribution at t=1t=1, and utu_t is the marginal vector field that generates it. Both are mixtures over x1x_1, so neither has a closed form.

Conditional objective. The paper builds ptp_t and utu_t by marginalizing per-example objects. For each x1x_1, a conditional path pt(x∣x1)p_t(x \mid x_1) runs from p(x)p(x) at t=0t=0 to a concentrated distribution around x1x_1 at t=1t=1, with a conditional vector field ut(x∣x1)u_t(x \mid x_1) that generates it. Averaging over x1∼qx_1 \sim q gives the marginals. The paper proves two results:

  1. The marginal field ut(x)=∫ut(x∣x1) pt(x∣x1) q(x1)pt(x) dx1u_t(x) = \int u_t(x \mid x_1)\, \frac{p_t(x \mid x_1)\, q(x_1)}{p_t(x)}\, dx_1 generates the marginal path ptp_t (Theorem 1).
  2. The loss LCFM(θ)=Et,q(x1),pt(x∣x1)∥vt(x)−ut(x∣x1)∥2\mathcal{L}_{\text{CFM}}(\theta) = \mathbb{E}_{t, q(x_1), p_t(x \mid x_1)} \left\| v_t(x) - u_t(x \mid x_1) \right\|^2 has the same gradient with respect to θ\theta as LFM\mathcal{L}_{\text{FM}} (Theorem 2).

The second result is the reason the method works. The conditional loss is an unbiased minibatch estimator of the gradient of the intractable one, and every term needs only one data sample at a time.

Gaussian paths. The paper restricts the conditional paths to Gaussians, pt(x∣x1)=N(x∣μt(x1),σt(x1)2I)p_t(x \mid x_1) = \mathcal{N}(x \mid \mu_t(x_1), \sigma_t(x_1)^2 I), with μ0=0\mu_0 = 0, σ0=1\sigma_0 = 1, μ1=x1\mu_1 = x_1 and σ1=σmin⁡\sigma_1 = \sigma_{\min}, a small positive constant. The simplest flow that pushes the standard Gaussian to this path is the affine map ψt(x)=σt(x1) x+μt(x1)\psi_t(x) = \sigma_t(x_1)\, x + \mu_t(x_1). Differentiating it gives the conditional field. Theorem 3 states that for Gaussian paths the unique field of this form is

ut(x∣x1)=σt′(x1)σt(x1)(x−μt(x1))+μt′(x1),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),

where a prime is a time derivative. Reparameterizing with x=ψt(x0)x = \psi_t(x_0), the CFM loss becomes a regression of vt(ψt(x0))v_t(\psi_t(x_0)) onto ddtψt(x0)\frac{d}{dt}\psi_t(x_0), with x0∼p(x0)x_0 \sim p(x_0) and x1∼q(x1)x_1 \sim q(x_1).

The two instances the paper studies

Diffusion paths. Choosing μt\mu_t and σt\sigma_t to match the variance-preserving diffusion process of Ho et al. gives a conditional field that the paper shows coincides with the probability-flow field used in score-based sampling. Training this field with the flow matching loss, instead of score matching, is one of the paper's comparisons.

Optimal transport paths. Choosing the mean and standard deviation to change linearly,

μt(x1)=t x1,σt(x1)=1−(1−σmin⁡) t,\mu_t(x_1) = t\, x_1, \qquad \sigma_t(x_1) = 1 - (1 - \sigma_{\min})\, t,

gives the conditional field

ut(x∣x1)=x1−(1−σmin⁡) x1−(1−σmin⁡) t,u_t(x \mid x_1) = \frac{x_1 - (1 - \sigma_{\min})\, x}{1 - (1 - \sigma_{\min})\, t},

which is defined for all t∈[0,1]t \in [0,1]. The flow is ψt(x0)=(1−(1−σmin⁡)t) x0+t x1\psi_t(x_0) = (1 - (1 - \sigma_{\min})t)\, x_0 + t\, x_1. Each sample moves along a straight line from x0x_0 to x1x_1 at constant speed. The paper identifies this flow as the OT displacement map between the two Gaussians, which is why it is the cheapest path to follow with an ODE solver.

Worked example (OT path, one sample). Take x1=2x_1 = 2, a noise draw x0=1x_0 = 1, and t=0.5t = 0.5. To keep the arithmetic exact, set σmin⁡=0\sigma_{\min} = 0 here; the paper uses a small positive value, which changes the numbers slightly.

  1. Position: ψ0.5(1)=(1−0.5)⋅1+0.5⋅2=0.5+1=1.5\psi_{0.5}(1) = (1 - 0.5)\cdot 1 + 0.5 \cdot 2 = 0.5 + 1 = 1.5.
  2. Target velocity: ut(x∣x1)=(x1−x)/(1−t)u_t(x \mid x_1) = (x_1 - x)/(1 - t) with x=1.5x = 1.5 gives (2−1.5)/0.5=1(2 - 1.5)/0.5 = 1.
  3. Check against the path: ddtψt(x0)=x1−x0=2−1=1\frac{d}{dt}\psi_t(x_0) = x_1 - x_0 = 2 - 1 = 1. The two agree.

Interpretation: the velocity target is constant along the path, equal to x1−x0x_1 - x_0, so the network only has to learn one direction per pair of endpoints. This is the property the paper contrasts with the diffusion target, which is the score ∇log⁡pt(x∣x1)\nabla \log p_t(x \mid x_1) and changes direction over time.

Training loop. The algorithm is short:

  1. Sample a data point x1x_1, a time t∼U[0,1]t \sim \mathcal{U}[0,1] and a noise point x0∼N(0,I)x_0 \sim \mathcal{N}(0, I).
  2. Compute xt=ψt(x0)x_t = \psi_t(x_0) and the target u=ddtψt(x0)u = \frac{d}{dt}\psi_t(x_0).
  3. Take a gradient step on ∥vθ(xt,t)−u∥2\| v_\theta(x_t, t) - u \|^2, the mean squared error between predicted and target velocity.

No ODE is solved during training. Sampling solves ddtϕt(x)=vt(ϕt(x))\frac{d}{dt}\phi_t(x) = v_t(\phi_t(x)) from t=0t=0 to t=1t=1 with any solver.

Results

The paper trains the same U-Net architecture with five training methods on CIFAR-10 and ImageNet at 32 and 64 pixels: DDPM, score matching (SM), score flow (SF), flow matching with a diffusion path (FM w/ Diffusion), and flow matching with the OT path (FM w/ OT). The metrics are negative log-likelihood in bits per dimension (NLL, lower is better), Fréchet Inception Distance (FID, lower is better), and the average number of function evaluations (NFE) the adaptive solver needs at its tolerance. All models share architecture, hyperparameters and iteration count, and baselines get extra iterations.

ModelCIFAR-10 NLLCIFAR-10 FIDCIFAR-10 NFEImageNet-32 NLLImageNet-32 FIDImageNet-32 NFEImageNet-64 NLLImageNet-64 FIDImageNet-64 NFE
DDPM3.127.482743.546.992623.3217.36264
Score matching3.1619.942423.565.681783.4019.74441
Score flow3.0920.784283.5514.141953.3624.95601
FM w/ Diffusion3.108.061833.546.371933.3316.88187
FM w/ OT2.996.351423.535.021223.3114.45138

FM with OT is best on every metric in this table. The gap in FID is largest on ImageNet-64, where it is 14.45 against 16.88 for the best diffusion-path model and 17.36 for DDPM. The paper notes that its CIFAR-10 FID is higher than earlier published numbers for the same family, which it attributes to an architecture not tuned for CIFAR-10.

ImageNet-128. The paper reports that FM with OT on unconditional ImageNet at 128 by 128 pixels has state-of-the-art FID among the compared methods, except for IC-GAN, which uses class-conditioning with a self-supervised ResNet-50 and is left out of the table. The exact FID value is in the right half of the paper's Table 1; this lesson does not quote it.

Training speed. Figure 5 of the paper shows FID against iterations on ImageNet-64: FM-OT lowers FID faster and to a lower level than the alternatives. For ImageNet-128 the paper compares 500k iterations at batch size 1.5k for FM against 4.36m iterations at batch size 256 for the Dhariwal and Nichol baseline, and reports 33 percent less image throughput for FM, with a model 25 percent larger. The authors also note that the sampling cost of score-matching models changes during training, while it stays constant for flow matching.

Sampling efficiency. Figure 7 (left) compares per-pixel error of low-NFE samples against 1000-NFE reference samples, on ImageNet-32 models with the midpoint solver. The paper says FM with OT needs roughly 60 percent of the function evaluations of diffusion models to reach the same error. Figure 7 (right) shows FID against NFE: FM with OT keeps a decent FID at very low NFE.

Conditional generation. On ImageNet super-resolution from 64 to 256 pixels (Table 2), FM with OT reaches FID 3.4 and Inception score 200.8, against 5.2 and 180.1 for SR3. Its PSNR (24.7) and SSIM (0.747) are lower than SR3's 26.4 and 0.762, and the regression baseline has the best PSNR and SSIM. The paper argues FID and IS are better measures of perceived quality than PSNR for this task.

Likelihood. The NLL column uses uniformly dequantized data and an importance-weighted estimate, evaluated with the dopri5 adaptive solver at tolerance 1e-5. The NLL differences are small: across the methods in the table, the spread is about 0.2 bits per dimension on CIFAR-10 and under 0.1 on ImageNet-32 and ImageNet-64, so the FID and NFE gaps carry most of the story.

Why it mattered

The paper showed that the probability path can be chosen directly, instead of being inherited from a stochastic process. That made it possible to train continuous-time generative models at the scale of diffusion models, with a simpler regression target and no ODE simulation during training. The paper's own evidence is that the straight OT trajectories need fewer solver steps. Later work built on this family of objectives; the paper itself does not report those adoptions, so treat them as outside its scope. Related concurrent work by Liu et al. and by Albergo and Vanden-Eijnden arrived at similar conditional objectives concurrently, as the paper itself notes.

Reading guide

Read the sections in this order. Each row names the lessons that carry the prerequisites for that part of the paper.

Check yourself

QuestionRecall: what does the Conditional Flow Matching loss sample to form a training example?Show answer
A time tt uniformly from [0,1][0,1], a data point x1x_1 from qq, and a noise point x0x_0 from N(0,I)\mathcal{N}(0, I). It then forms xt=ψt(x0)x_t = \psi_t(x_0) and regresses the network output at (xt,t)(x_t, t) onto the derivative ddtψt(x0)\frac{d}{dt}\psi_t(x_0). No marginal field or ODE solve is involved.
QuestionComputation: with ψt(x0)=(1−(1−σmin⁡)t)x0+tx1\psi_t(x_0) = (1 - (1-\sigma_{\min})t)x_0 + t x_1, take x0=−1x_0 = -1, x1=3x_1 = 3, t=0.25t = 0.25 and σmin⁡=0\sigma_{\min} = 0. What are the position and the target velocity?Show answer
The position is (1−0.25)(−1)+0.25⋅3=−0.75+0.75=0(1 - 0.25)(-1) + 0.25 \cdot 3 = -0.75 + 0.75 = 0. The target velocity is x1−x0=3−(−1)=4x_1 - x_0 = 3 - (-1) = 4, which is constant in tt and so the same at every point on the path.
QuestionWhy: the paper says the conditional loss has the same gradient as the marginal Flow Matching loss. Why does that matter for training?Show answer
The marginal vector field ut(x)u_t(x) is an integral over all data points that has no closed form, so the marginal loss cannot be estimated without bias from minibatches. The conditional loss uses only one data point per term and is an unbiased estimator of the same gradient, so standard stochastic gradient training works.
QuestionApplied: you want a generator that needs as few sampler steps as possible. Would you pick a curved diffusion path or the OT path, and what does the paper's evidence suggest?Show answer
The OT path. Its trajectories are straight lines at constant speed, and the paper finds that it needs about 60 percent of the function evaluations of diffusion paths to reach the same numerical error on ImageNet-32, and has better FID at low NFE. A straight path is also easier for a fixed-step solver to integrate accurately.

Sources

Read the full paper

Yaron Lipman, Ricky T. Q. Chen, Heli Ben-Hamu et al., 2022. arXiv:2210.02747