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.
Inspect Architecture: Flow matching / continuous normalizing flows1. 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.
2. The math
3. How it works
4. The code (python)
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_t5. Check yourself
In the catalog
Finished this chapter?
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.