Search

Search papers and lessons

Flow Straight and Fast: Learning to Generate and Transfer Data with Rectified...

Xingchao Liu, Chengyue Gong, Qiang Liu · 2022

Liu, Gong and Liu (2022) learn an ODE that follows straight lines between two sample sets, then reflow straightens the paths so one Euler step can generate an image.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Discrete math
  • Linear algebra
  • Neural network basics
  • Generative models
  • ReadReady to readNot yet

TL;DR

Liu, Gong and Liu (2022) propose rectified flow, a way to learn a transport map between two sets of samples, such as random noise and images. The learning step is an ordinary least-squares regression: a network learns the direction from a point on a straight line to that line's endpoints. Reflow repeats the procedure on the model's own outputs, which straightens the paths, and the authors report that nearly straight flows give good images with a single Euler step.

Builds on: Ordinary Differential Equation, Optimal Transport · Leads to: Rectified Flow, the method in short form

Why it mattered

Text-to-image models. The training objective of rectified flow, regressing a velocity along straight paths, was adopted by later large text-to-image systems, including Stable Diffusion 3 in 2024. It gave a simple regression target in place of the curved-path objectives used by earlier diffusion models.

Fewer network calls. Diffusion-style samplers solve an ODE or SDE with many steps, and each step is a network call. The paper's central practical claim is that a straight-path flow needs very few. Its Table 1 lists 127 network evaluations for a high-accuracy adaptive solve of the 1-rectified flow on CIFAR-10, a standard benchmark of 60,000 small 32-by-32 colour images. After distillation into a one-step network, the authors report an FID of 4.85 for the 2-rectified flow. FID (Fréchet Inception Distance) compares the statistics of generated images with real ones, so lower is better. That beat the best one-step U-Net result they compared against.

One method for many transfers. The same construction works for any two empirical distributions. The paper applies it to image-to-image translation and to domain adaptation, where a shifted domain is mapped to the training domain before classification.

The problem

Suppose we have samples X0X_0 from a distribution π0\pi_0, usually noise, and samples X1X_1 from π1\pi_1, usually images. We want an ODE dZt=v(Zt,t) dtdZ_t = v(Z_t, t)\,dt that carries samples from π0\pi_0 at time 00 to samples from π1\pi_1 at time 11. Here the velocity vv is a neural network, which makes this a Neural Ordinary Differential Equation.

The obvious choice is the straight line Xt=(1−t)X0+tX1X_t = (1-t)X_0 + tX_1. It has the right endpoints, but it cannot be used as a sampler. To move along it you must already know X1X_1, which is the thing you are trying to generate. The line is non-causal.

Fixing the paths is harder than it looks. Two straight lines from different pairs can cross. An ODE gives each point in space one velocity, so two trajectories that meet at a point must have the same velocity from then on. Straight lines that cross have different velocities, so no flow can follow both lines exactly. The fix has to change the paths at the crossings.

Third, the paths of existing diffusion models are curved. A curved path needs many small steps for an ODE solver to follow it, and each step is a network call. The paper's goal is to remove the curvature, so that a coarse solver is accurate.

How it works

Rectified flow has a short pipeline. Its training step is a regression, and its sampling step is an ODE solve.

  1. Draw a pair (X0,X1)(X_0, X_1), one sample from each distribution. In generation these are drawn independently.
  2. Draw a time tt uniformly between 0 and 1, and place the point XtX_t on the straight line between X0X_0 and X1X_1.
  3. Train a network v(x,t)v(x, t) to output the direction X1−X0X_1 - X_0 at that point, by least squares.
  4. To sample, draw Z0Z_0 from π0\pi_0 and follow the learned field with an ODE solver. The paper's Euler steps are the simplest choice.
  5. Reflow: run the sampler to produce pairs (Z0,Z1)(Z_0, Z_1) and train a new rectified flow on those pairs, exactly as in steps 1 to 3.
flowchart LR
  A["Draw independent pair X0, X1"] --> B["Point on the straight line at a random time t"]
  B --> C["Regress velocity v(x, t) onto X1 minus X0"]
  C --> D["Sample by solving dZ/dt = v with Euler steps"]
  D --> E["Reflow: new pairs (Z0, Z1) from the sampler"]
  E --> B

Trace one tiny case in one dimension. Take the pair X0=−1X_0 = -1 and X1=3X_1 = 3, and a random time t=0.5t = 0.5.

  1. The point on the straight line is Xt=0.5×(−1)+0.5×3=1X_t = 0.5 \times (-1) + 0.5 \times 3 = 1. Check: 11 is halfway between −1-1 and 33.
  2. The regression target is the direction of the line, X1−X0=4X_1 - X_0 = 4. It is the same at every time, because the line has constant speed. Check: moving from −1-1 to 33 is a distance of 44.
  3. If the network outputs 33 at (x,t)=(1,0.5)(x, t) = (1, 0.5), the squared error is (3−4)2=1(3 - 4)^2 = 1. Training lowers it. Check: a perfect network outputs 44 there.
  4. To sample, start at Z0=−1Z_0 = -1 and take one Euler step of size 11 with the learned velocity 44. The result is Z1=−1+1×4=3Z_1 = -1 + 1 \times 4 = 3. Check: the path is straight, so one step lands exactly on the data.

The crossing problem is shown in the plot. Two pairs, (0,2)(0, 2) and (2,0)(2, 0), have straight paths that cross at x=1x = 1 when t=0.5t = 0.5. Their velocities there are +2+2 and −2-2. The regression gives the average, 00, and that single velocity is what lets the flow avoid crossing.

00.20.40.60.81−0.500.511.522.5time tposition x
  • pair (0, 2)
  • pair (2, 0)
The straight paths of two pairs cross at time one half and position one. Their velocities there are +2 and -2, so the learned field averages them to 0 and the flow is rewired at the crossing.

The math

Definition: Rectified flow

Given samples X0∼π0X_0 \sim \pi_0 and X1∼π1X_1 \sim \pi_1, the rectified flow induced by the pair is the ODE dZt=v(Zt,t) dtdZ_t = v(Z_t, t)\,dt whose velocity vv is the least-squares fit of the direction X1−X0X_1 - X_0 along the straight interpolation Xt=tX1+(1−t)X0X_t = tX_1 + (1-t)X_0, for t∈[0,1]t \in [0, 1].

Write E\mathbb{E} for the Expected Value over the random pair and time, and Unif[0,1]\mathrm{Unif}[0,1] for the uniform distribution on the time interval.

Step 1 in symbols: the straight interpolation

Xt=t X1+(1−t) X0.X_t = t\,X_1 + (1-t)\,X_0.

At t=0t = 0 this is X0X_0 and at t=1t = 1 it is X1X_1. Each term is linear in tt, so the derivative dXt/dt=X1−X0dX_t/dt = X_1 - X_0 does not depend on tt.

Step 2 in symbols: the regression

The velocity is chosen to solve

min⁡v∫01E[ ∥(X1−X0)−v(Xt,t)∥2]dt.\min_v \int_0^1 \mathbb{E}\left[\, \left\lVert (X_1 - X_0) - v(X_t, t) \right\rVert^2 \right] dt.

This is a Mean Squared Error regression with a random time, so it can be trained with ordinary stochastic gradient descent on sampled pairs, as Algorithm 1 in the paper does. The loss is the same kind used in supervised learning, which is why the method scales without extra machinery.

Step 3 in symbols: the minimiser

Because the squared-error minimiser is a conditional mean, the best possible velocity at a point xx and time tt is

vX(x,t)=E[ X1−X0∣Xt=x ].v^{X}(x, t) = \mathbb{E}\left[\, X_1 - X_0 \mid X_t = x \,\right].

This is the average direction of all straight lines that pass through xx at time tt. Where several lines cross, the velocity is their average, as in the plot. This averaging is what makes the ODE causal: it depends only on xx and tt, never on the unseen endpoint.

Step 4 in symbols: Euler sampling

With KK steps of size Δt=1/K\Delta t = 1/K, start from Z0∼π0Z_0 \sim \pi_0 and update

Zk+1=Zk+Δt  v(Zk,tk),tk=k Δt.Z_{k+1} = Z_k + \Delta t\; v(Z_k, t_k), \qquad t_k = k\,\Delta t.

For the one-dimensional trace, K=1K = 1, Z0=−1Z_0 = -1 and v=4v = 4 give Z1=−1+1×4=3Z_1 = -1 + 1 \times 4 = 3. If the path is straight, the error of this step is zero for any KK, because the velocity is constant along the path.

Step 5 in symbols: reflow

Write RectFlow(X0,X1)\mathtt{RectFlow}(X_0, X_1) for the flow learned from the pair. Reflow sets

(Z0k+1,Z1k+1)=RectFlow((Z0k,Z1k)),(Z00,Z10)=(X0,X1).(Z_0^{k+1}, Z_1^{k+1}) = \mathtt{RectFlow}\big((Z_0^k, Z_1^k)\big), \qquad (Z_0^0, Z_1^0) = (X_0, X_1).

A coupling is a joint pair (X0,X1)(X_0, X_1) whose two parts have the distributions π0\pi_0 and π1\pi_1; it says which start goes with which end. A transport cost is E[c(X1−X0)]\mathbb{E}[c(X_1 - X_0)] for a function cc that measures the cost of one move. The function is convex when it is bowl-shaped, so that long moves cost disproportionately more. The paper proves that the rectified coupling has no larger convex transport cost than the input coupling (its Theorem 3.5), so each round moves the coupling towards cheaper transport. It also observes that reflow straightens the paths, which is what makes few-step sampling accurate.

Results

Full solves on CIFAR-10. With the DDPM++ U-Net architecture, the 1-rectified flow solved with the RK45 adaptive solver reached an FID of 2.582.58 and a recall of 0.570.57, the best of the ODE methods in the paper's table. It used about 127 network evaluations, against 140 for the VP ODE and 146 for the sub-VP ODE of the earlier work. A 2000-step SDE reached an FID of 2.552.55, so the flow matched a far more expensive sampler.

Few steps. The paper reports that reflow substantially improves FID and recall when the number of steps is small, roughly 80 or fewer. For large step counts, reflow makes results worse, because errors in the learned velocity accumulate over many rounds.

One step. After distillation into a one-step network, the 2-rectified flow reached an FID of 4.854.85 and a recall of 0.500.50. The paper's text says this beats the best one-step U-Net model it compares against, with an FID of 8.918.91, and that its recall of 0.500.50 and the 3-rectified flow's 0.510.51 exceed the best GAN recall it lists, 0.490.49.

Transfers. The paper also reports image-to-image translation and domain-adaptation experiments in Sections 5.3 and 5.4. This lesson does not quote their numbers. The authors describe the translation results as visually good rather than as exact transfers of one distribution onto another.

Limits. The main results are for CIFAR-10 and for unconditional generation. Reflow needs a fresh training round and a set of generated pairs for each step, so it adds training cost. Distillation is needed for a true one-step model, and the one-step results depend on it. The FID and recall numbers are the paper's own, computed with its evaluation protocol.

Reading guide

  • Abstract and Section 1. The motivation: paired data is missing, and sampling is expensive. Needs Gaussian (Normal) Distribution for the noise distribution.
  • Section 2.1. The method, Algorithm 1, and the non-crossing argument. Needs Ordinary Differential Equation and Vector Field. The rewiring picture in Figure 2 is the key intuition.
  • Section 2.2. The marginal-preserving property and the transport-cost claim. Needs Optimal Transport and Convex Function.
  • Section 2.3 and Section 3.5. The connection to probability flow ODEs and to DDIM. Needs Denoising Diffusion Implicit Models (DDIM) and Diffusion Model. Read these after the main method.
  • Sections 3.3 and 3.4. Why reflow straightens paths and how straight and optimal couplings differ. These are the most technical parts. Start with the statements, then the proofs if you need them.
  • Section 5.2. The CIFAR-10 table and the figure with the number of Euler steps. Needs Euler Method and Knowledge Distillation for the one-step model.
  • Sections 5.3 and 5.4. Translation and domain adaptation, which use the same construction on images and on features. The adversarial method it compares against is a Generative Adversarial Network style of training.

Check yourself

QuestionRecall: why does rectified flow fit the velocity to X1−X0X_1 - X_0 instead of following the straight line XtX_t directly during sampling?Show answer

The straight line XtX_t depends on X1X_1, which is the unknown output. Fitting a velocity v(x,t)v(x, t) by least squares gives an ODE that uses only the current point and time, so it can be solved without knowing the endpoint in advance. The fitted field is the conditional average of the line directions through each point.

QuestionComputation: take the pair X0=2X_0 = 2 and X1=−2X_1 = -2. Find the point on the straight line at t=0.75t = 0.75 and the regression target.Show answer

The point is X0.75=0.75×(−2)+0.25×2=−1.5+0.5=−1X_{0.75} = 0.75 \times (-2) + 0.25 \times 2 = -1.5 + 0.5 = -1. The target is X1−X0=−2−2=−4X_1 - X_0 = -2 - 2 = -4, which is the same at every time. So the network is asked to output −4-4 at (x,t)=(−1,0.75)(x, t) = (-1, 0.75).

QuestionWhy: explain why a flow whose paths are exactly straight can be sampled with one Euler step without any error.Show answer

An Euler step adds the velocity times the step size to the current point. On a straight path the velocity is constant along the path, so with a single step of size 11 the point moves by exactly X1−X0X_1 - X_0. The result lands on the endpoint with no discretisation error. This is why the paper ties straightness to few-step sampling.

QuestionApplied: a team trains a rectified flow and samples it with 100 Euler steps. They reflow once and find the FID improves at 10 steps but gets worse at 100 steps. What does the paper suggest is happening, and what should they do if they want a cheap sampler?Show answer

Reflow straightens the paths, which helps when the number of steps is small. At large step counts the errors in the learned velocity build up over the rounds of reflow, so accuracy can fall. For a cheap sampler the team should keep the reflowed model and use few steps, or distil it into a one-step network, which is the route the paper uses for its best one-step result.

Sources

Read the full paper

Xingchao Liu, Chengyue Gong, Qiang Liu, 2022. arXiv:2209.03003