Learn machine learning from scratch

Start at chapter 1 and build up: what a model is, how it measures its own mistakes, and how it learns. Everything is worked out in plain Python, with the maths shown rather than assumed. Two shorter tracks hold reference notes on the maths and on modern architectures.

Track Progress: 0 Completed
0%
Modern ArchitecturesLEVEL 4 · FRONTIERChapter 5

Diffusion Models & Rectified Flow Matching

Continuous-time ODE trajectories, straight paths, and FLUX.1 velocity matching.

Reference notes. Condensed summaries of each topic, not step-by-step lessons. The Core Course is the one to work through.

6 min Prerequisites: Probability, ODEs & Transformer blocks
Inspect Architecture: Flow matching / continuous normalizing flows

1. The idea

Modern image and video generation models (FLUX.1, Stable Diffusion 3, Sora) have transitioned from stochastic DDPM diffusion to Rectified Flow Matching, learning straight ODE velocity trajectories connecting noise to data.

Diagram of rectified flow as five small images on a straight path from noise to data, at t = 0, 0.25, 0.5, 0.75 and 1. Each is the blend x_t = (1 minus t) x0 plus t x1: pure noise at t = 0, then 0.75 noise plus 0.25 data, half and half, 0.25 noise plus 0.75 data, and the data image, a ring, at t = 1. Each arrow between them is one Euler step of a quarter times the predicted velocity v_theta.Diagram of rectified flow as five small images on a straight path from noise to data, at t = 0, 0.25, 0.5, 0.75 and 1. Each is the blend x_t = (1 minus t) x0 plus t x1: pure noise at t = 0, then 0.75 noise plus 0.25 data, half and half, 0.25 noise plus 0.75 data, and the data image, a ring, at t = 1. Each arrow between them is one Euler step of a quarter times the predicted velocity v_theta.
Rectified flow trains the network to point along the straight line from noise to data, so a few Euler steps are enough to sample. The noise and the ring are made up, only to show the blend.

2. The math

Rectified Flow Linear Interpolation
xt=(1−t)x0+tx1t∈[0,1]x_t = (1 - t) x_0 + t x_1 \qquad t \in [0, 1]
x_0: pure Gaussian noise, x_1: target training image, x_t: intermediate sample.
Flow Matching Velocity Objective
LFM(θ)=Et,x0,x1[∥vθ(xt,t)−(x1−x0)∥2]\mathcal{L}_{\text{FM}}(\theta) = \mathbb{E}_{t, x_0, x_1}\left[ \| v_\theta(x_t, t) - (x_1 - x_0) \|^2 \right]
Neural network v_θ learns the direct velocity vector pointing from noise to data.

3. How it works

1
Sample Noise & Data Pair
Draw clean data x_1 ~ p_data and noise x_0 ~ N(0, I).
2
Interpolate at Random Time t
Sample timestep t ~ U(0, 1) and compute straight trajectory point x_t = (1-t)x_0 + t x_1.
3
Predict Velocity Field
Pass noisy image x_t and timestep t through transformer backbone to predict velocity v_θ(x_t, t).
4
Euler ODE Generation
Sample by integrating dx/dt = v_θ(x, t) from t=0 to t=1 in 4 to 20 steps.

4. The code (python)

diffusion.py
import torch
import torch.nn as nn

class RectifiedFlowTrainer:
    """Rectified Flow Matching (FLUX.1 / SD3 style) Training Step in PyTorch."""
    def __init__(self, model: nn.Module):
        self.model = model

    def training_step(self, x_1: torch.Tensor) -> torch.Tensor:
        B = x_1.shape[0]
        x_0 = torch.randn_like(x_1)
        t = torch.rand((B, 1, 1, 1), device=x_1.device)
        x_t = (1.0 - t) * x_0 + t * x_1
        target_v = x_1 - x_0
        pred_v = self.model(x_t, t.squeeze())
        loss = torch.mean((pred_v - target_v) ** 2)
        return loss

    @torch.no_grad()
    def sample_euler(self, noise: torch.Tensor, steps: int = 10) -> torch.Tensor:
        dt = 1.0 / steps
        x_t = noise
        for step in range(steps):
            t = torch.full((noise.shape[0],), step * dt, device=noise.device)
            v = self.model(x_t, t)
            x_t = x_t + v * dt
        return x_t

5. Check yourself

Answer all 1 questions correctly to complete the chapter · 0 / 1 done
Q1/1 Why does Rectified Flow Matching enable high-quality generation in fewer sampling steps (4-10 steps) than older DDPM diffusion models (50-1000 steps)?

Finished this chapter?

Weekly newsletter

The Epoch Zero newsletter

What changed in ML architectures this week: new models worth knowing, benchmark moves, and one architecture explained in plain words. Free, once a week, unsubscribe anytime.