Skip to content
AI360Xpert
Gen AI

Rectified Flow

If Flow Matching draws a straight line between noise and an image, Rectified Flow is the process of training a model on that line, simulating its predictions, and using those predictions to draw an even straighter line, repeating until the model can generate a perfect image in a single step.

Rectified flow iteratively straightens the curved trajectories of diffusion models into perfectly straight lines, allowing for one-step generation.
Rectified flow iteratively straightens the curved trajectories of diffusion models into perfectly straight lines, allowing for one-step generation.

Why Does This Exist?

Flow Matching solved a lot of the messy math of standard diffusion models by defining straight-line paths between pure noise and real images. However, when you train a neural network to learn those paths, a problem arises during inference. Because you randomly pair Noise Sample A with Image B during training, the network ends up learning a vector field where paths constantly cross each other.

When paths cross in a vector field, the math forces the neural network to average the intersecting vectors. This turns the theoretical "straight line" into a curved, slightly blurry trajectory in practice. Because the path is curved, you still have to use an ODE solver taking multiple small steps (e.g., 20 steps) to generate an image without veering off course.

Rectified Flow was invented to fix this crossing-path problem. By running a process called "Reflow," it iteratively straightens the learned vector field. The ultimate goal is perfectly straight, non-intersecting lines. If the path from noise to data is a perfectly straight line, you only need to evaluate the neural network once to generate a pristine, high-resolution image.

Think of It Like This

Untangling the Headphones

Imagine you pull a mess of tangled headphone wires out of your pocket (the initial Flow Matching vector field). If you trace one wire from the plug (noise) to the earbud (image), your finger has to weave in and out, taking a very convoluted, curved path.

  • Reflow Iteration 1: You shake the tangle out a bit. The paths are still crossed, but tracing a single wire is now a much gentler curve.
  • Reflow Iteration 2: You pull the wires mostly straight.
  • Perfect Rectified Flow: You lay the wires completely flat and parallel on a table. Now, if you start at a plug (noise), you can just draw a straight line forward with a ruler and instantly hit the exact right earbud (image).

How It Actually Works

Rectified Flow is an iterative algorithm that relies on the model's own predictions to train a better version of itself.

1. Base Flow (Iteration 0)

You start by training a standard Flow Matching model. You pair random noise z0z_0 with random images x1x_1 and train the network to predict the straight line between them. Because the pairings are random, the learned vector field v0v_0 is highly curved and tangled.

2. The Reflow Process (Iteration 1)

To untangle the paths, we use the Base Flow model to generate a new training dataset. We sample a batch of pure noise z0z_0. We run the Base Flow model using an ODE solver (taking many steps) to generate the corresponding images x^1\hat{x}_1.

Now, we have perfectly coupled pairs: z0z_0 and x^1\hat{x}_1. We know exactly which noise particle naturally wants to become which image according to the model. We train a new Flow Matching model (Iteration 1), but this time we explicitly pair z0z_0 with x^1\hat{x}_1. Because we are pairing points that already naturally connect, the paths don't cross nearly as much. The learned vector field v1v_1 is significantly straighter.

3. Iteration N (The Straight Line)

You can repeat the Reflow process indefinitely. Generate pairs using the Iteration 1 model, train an Iteration 2 model on those pairs, and so on. With each iteration, the vector field becomes straighter, closely approximating Optimal Transport (the mathematically shortest, non-intersecting path between two distributions).

4. Distillation to 1-Step

Once the vector field is perfectly straight, the ODE trajectory has no curvature. If there is no curvature, Euler's method has zero truncation error. You can evaluate the neural network exactly once at t=0t=0, point in the predicted direction, take a single massive step of size dt=1.0dt=1.0, and arrive perfectly at the high-quality image x1x_1.

Show Me the Code

This code outlines the Reflow data generation and training loop concept.

import torch
def generate_reflow_dataset(base_model, num_samples=1000):    """    Generates coupled (Noise, Image) pairs using the base model to untangle paths.    """    dataset = []    for _ in range(num_samples):        # 1. Sample pure noise        z_0 = torch.randn(1, 3, 64, 64)                # 2. Use an ODE solver (e.g., Euler with 50 steps) to follow the curved path        # This gives us the exact image that this specific noise maps to        x_1_generated = solve_ode(base_model, z_0, steps=50)                # 3. Store the perfectly coupled pair        dataset.append((z_0, x_1_generated))            return dataset
def train_rectified_flow(reflow_dataset, new_model):    """    Trains the next iteration model on the coupled pairs.    """    for z_0, x_1 in reflow_dataset:        # Sample random time t        t = torch.rand(1)                # Interpolate along the perfectly straight line between the coupled pair        x_t = t * x_1 + (1 - t) * z_0                # The target direction is just x_1 - z_0        target_direction = x_1 - z_0                # Train new_model to predict this straight-line direction        predicted_direction = new_model(x_t, t)        loss = torch.nn.functional.mse_loss(predicted_direction, target_direction)        loss.backward()            return new_model

Watch Out For

Model Capacity Bottlenecks

Reflow requires the student model to learn a perfectly deterministic, straight-line mapping from pure noise to complex images. This is an incredibly difficult function to approximate. If the neural network (e.g., the DiT) is not large enough, it will fail to learn the straight paths, resulting in blurry 1-step generations. Rectified Flow shines brightest on massive models with billions of parameters (like Stable Diffusion 3).

The Quick Version

  • Standard Flow Matching learns vector fields with crossing paths, resulting in curved trajectories that require multiple steps to traverse.
  • Rectified Flow solves this by using the model to generate coupled (Noise, Image) pairs, and retraining a new model on those specific pairs.
  • This "Reflow" process straightens the vector field by preventing paths from crossing.
  • After 1 or 2 Reflow iterations, the trajectories become perfectly straight lines.
  • Because the paths are straight, numerical ODE solvers can jump from noise to a pristine image in a single, massive step, enabling real-time generation.
  • Read Consistency Models to see an alternative approach to 1-step generation that forces the model to map every point on the trajectory directly to the origin.
  • Read Flow Matching if you need a refresher on how the base continuous vector fields are defined.

Related concepts