Chapter 39 of 40 · ~22 min read & lab+85 XP
Chapter 39 · Generative AI

Modern Image/Video Gen: Straight-Line Flow Matching & Diffusion Transformers (DiT)

What replaced U-Net DDPM in Flux, Stable Diffusion 3, Sora, and Veo: Rectified Flow Matching's straight-line velocity ODE (v_θ ≈ x₁ − x₀) for 8–20 step generation, Diffusion Transformers (DiT + adaLN-Zero), and 3D spacetime video patches.

What Is It?

Rectified Flow Matching trains a Diffusion Transformer (DiT) to predict straight-line velocity vectors directly from Gaussian noise to real data latents.

Why Is It Required?

Classic DDPM follows curved, noisy trajectories requiring 50–1,000 denoising steps; straight-line Flow Matching + scalable DiT backbones generate sharper images and video in 8–20 steps.

Everyday Analogy: Flying a direct straight-line flight from noise to image instead of zig-zagging through 50 connecting airports.
Why ML Engineers & Data Scientists Actually Need This

Every 2024–2026 frontier image & video model (Flux.1, SD3.5, Sora, Veo, Wan) runs Flow Matching + Diffusion Transformers (DiT) — leaving classic U-Net DDPM behind!

ML / AI Engineer

DiT unifies visual generation with the exact same Transformer scaling laws, FlashAttention, and RoPE infrastructure you already mastered in Modules 1–2!

Data Scientist

Understanding x_t = (1 − t)x_0 + t·x_1 and velocity v = x_1 − x_0 reveals how remarkably clean modern generative math has become compared to 2020 DDPM variance schedules.

Interview & Debug Edge

Cutting-edge generative interview question: 'Why did Flux, SD3, and Sora switch from U-Net DDPM to DiT + Rectified Flow Matching?'

Step 0 · Test Your Gut Before Any Math!

In Rectified Flow Matching, given noise x_0 ~ N(0, I) and clean latent x_1, what is the constant target velocity along the straight path x_t = (1 − t)x_0 + t·x_1?

What You Will Master in This Chapter
  • Derive Rectified Flow Matching: linear interpolation x_t = (1−t)x_0 + t·x_1 and velocity regression loss ‖v_θ(x_t, t) − (x_1 − x_0)‖²
  • Explain why straight-line ODE trajectories need far fewer sampling steps (8–20) than curved DDPM schedules
  • Contrast classic Convolutional U-Nets with Diffusion Transformers (DiT) using patchification and adaLN-Zero conditioning
  • Explain how Video Generation models (Sora / Veo) generalize DiT to 3D spacetime patches via a 3D Causal VAE
Plain-English Symbol Decoder · Click Any Symbol Before We Begin
1 / 3
x_t = (1 - t)x_0 + t x_1Say aloud:“linear flow interpolation”
Plain-English Meaning
Straight-line bridge from pure Gaussian noise (t=0) to clean latent (t=1)
Everyday Example
Velocity target: v* = x_1 − x_0
Story-First Intuition, Visual Diagrams & Interactive Walkthroughs

Classic DDPM is like driving from New York to Boston along a winding mountain road (you must steer every 50 feet → 100 steps!). Rectified Flow Matching builds a straight laser highway (x_1 − x_0) so you can cruise smoothly in just 10–20 giant Euler steps. Meanwhile, DiT replaces the old CNN U-Net with a pure scalable Transformer that handles arbitrary image aspect ratios AND 3D video cubes seamlessly!

1. Rectified Flow Matching: Straight-Line Velocity ODE

Everyday Story First (Zero Math Yet!)

Drag the Steps slider in the mini-lab below to see how Straight Flow Matching reaches crisp fidelity in just 8–16 steps where curved DDPM is still blurry!

Curved DDPM Trajectory (50–1000 Steps) vs. Straight Flow Matching (8–20 Steps!)Noise x₀Data x₁DDPM Curved SDE (needs tiny steps!)Rectified Flow: v_θ(x_t, t) ≈ (x₁ − x₀) — Straight Line! ✓
Classic DDPM follows curved stochastic trajectories needing 50–1,000 steps; Rectified Flow Matching learns straight constant-velocity ODE paths (x_1 − x_0), generating crisp images in just 8–20 steps!

The 2-Line Flow Matching Objective

xt=(1−t)x0+tx1,LFM(θ)=Et,x0,x1 ⁣[∥vθ(xt,t)−(x1−x0)∥2]x_t = (1 - t)x_0 + t x_1, \qquad \mathcal{L}_{\text{FM}}(\theta) = \mathbb{E}_{t, x_0, x_1}\!\left[ \big\| v_\theta(x_t, t) - (x_1 - x_0) \big\|^2 \right] At inference, simply march from t=0→1t = 0 \to 1 in SS Euler steps: xt+Δt=xt+Δt⋅vθ(xt,t)x_{t + \Delta t} = x_t + \Delta t \cdot v_\theta(x_t, t)!

Animated Mini-Lab • 6.5

Inference Step Budget: DDPM vs. Rectified Flow

Curved DDPM Fidelity
42%
Straight Flow Matching Fidelity
99% ✓

2. Diffusion Transformers (DiT) & MM-DiT Architecture

Everyday Story First (Zero Math Yet!)

Peebles & Xie (2023) proved that replacing the U-Net inductive bias with a plain Transformer unlocks LLM-style scaling laws for image & video quality!

Diffusion Transformer (DiT): Patchified Latents + adaLN-Zero Conditioning1. PatchifyLatent → N tokens2. adaLN-Zerot & text → (γ, β, α)3. TransformerGlobal Self-Attn + MLP4. UnpatchifyPredicted velocity v_θ
Diffusion Transformers (DiT — Sora, Flux, SD3) replace convolutional U-Nets with pure Vision Transformer blocks conditioned on timestep t & text via Adaptive LayerNorm (adaLN-Zero)!

Why DiT + adaLN-Zero Beat U-Nets

  • Patchify: Slices the (64 × 64 × 4) latent into (2 × 2) patches → a clean sequence of 1,024 tokens!
  • adaLN-Zero Conditioning: Predicts LayerNorm shift/scale (γ,β)(\gamma, \beta) and residual gate α\alpha from timestep tt, initializing α=0\alpha = 0 so every DiT block starts as an exact identity function (rock-solid deep training!).
  • MM-DiT (Flux / SD3): Concatenates text tokens and image patches into bidirectional joint self-attention — letting text attend back to image patches and enabling crisp typography inside generated images!

3. Generalizing to Video: 3D Spacetime Patches (Sora / Veo)

Everyday Story First (Zero Math Yet!)

How do you turn a 2D image DiT into a 1080p physics-consistent video generator?

Video Generation (Sora / Veo): 3D Spacetime Patches in a Unified DiTRaw Video (T × H × W)Frames over time3D Causal VAE + CubesSpacetime visual tokensFull 3D DiTCoherent motion ✓
Video DiTs (Sora / Veo) compress video with a 3D Causal VAE into spacetime patches (T × H × W), letting standard 3D Self-Attention model physics and motion consistently across frames!

Spacetime Cubes + 3D RoPE

Compress raw video frames (T×H×W×3)(T \times H \times W \times 3) through a 3D Causal VAE into a compact spacetime latent grid, slice it into 3D spacetime patches, attach 3D RoPE (time + height + width), and run a giant DiT — enabling native variable resolution, aspect ratio, and duration in ONE architecture!

Walk Through With Me · Interactive Worked Example

Worked Example: One Euler Sampling Step of Flow Matching

Suppose at t=0.0t = 0.0 (pure noise) x0=−1.0x_0 = -1.0, velocity network predicts vθ(x0,0)=+2.4v_\theta(x_0, 0) = +2.4, and we use S=4S = 4 steps (Δt=0.25\Delta t = 0.25).

11. Advance along the velocity vectorStep 1 / 2

x0.25=x0+Δt⋅vθ=−1.0+0.25(2.4)=−0.40x_{0.25} = x_0 + \Delta t \cdot v_\theta = -1.0 + 0.25(2.4) = \mathbf{-0.40}.

Teacher's Note: No complex β_t / σ_t variance schedules needed!
Pause & Check · Quick Intuition Pulse

In Rectified Flow Matching (x_t = (1 − t)x_0 + t·x_1), what is the target that the neural network v_θ(x_t, t) is trained to predict?

Try It YourselfStandard

Why does MM-DiT (Flux / SD3) spell text inside images (e.g., neon signs reading 'AIMLCode') dramatically better than older SD 1.5 U-Nets?

“Wait, I'm Confused...” · Teacher Office Hours Q&A

The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.

Because t≈0t \approx 0 (pure noise) and t≈1t \approx 1 (almost clean) are easy to predict; the hardest semantic decisions happen in the middle (t≈0.5t \approx 0.5)! Logit-Normal weighting focuses training compute right on the middle timesteps.

Interactive Visual Lab
Lab Challenge: Watch the 60 FPS Curved DDPM vs. Straight Flow Matching Racer reach x₁ cleanly along the straight line.
Interactive Animation • 6.5 Deep Lab

Curved DDPM SDE vs. Straight Rectified Flow Matching Racer

See why Rectified Flow Matching (Flux, SD3, Sora) needs only 8–20 ODE steps along a straight line instead of 50+ tiny curved steps.

Progress 0%
FlowDDPM
0%
t = 0.00
Intuition Takeaway: Rectified Flow Matching learns straight constant-velocity ODEs (vθ≈x1−x0v_\theta \approx x_1 - x_0) for fast 8–20 step sampling, while DiT / MM-DiT replaces U-Nets with scalable Transformers for both images and 3D spacetime video.
Where This Powers Real ML, Deep Learning & Generative AI

Flux.1, Stable Diffusion 3.5, Sora & Veo

Flow Matching + DiT is the unified backbone of modern photorealistic image and video synthesis.

Production NumPy & PyTorch Code Patterns
import torch
import torch.nn as nn

# Rectified Flow Matching: Straight-Line Interpolation x_t = (1-t)*x_0 + t*x_1
# Target velocity is constant: v* = x_1 - x_0
def flow_matching_loss(velocity_net: nn.Module, x1_data: torch.Tensor) -> torch.Tensor:
    B = x1_data.size(0)
    x0_noise = torch.randn_like(x1_data)
    t = torch.rand(B, 1)                         # uniform t in [0, 1]
    x_t = (1.0 - t) * x0_noise + t * x1_data     # linear OT path
    target_v = x1_data - x0_noise
    pred_v = velocity_net(torch.cat([x_t, t], dim=-1))
    return nn.functional.mse_loss(pred_v, target_v)

net = nn.Sequential(nn.Linear(17, 64), nn.SiLU(), nn.Linear(64, 16))
loss = flow_matching_loss(net, torch.randn(8, 16))
print(f"Rectified Flow velocity MSE loss: {loss.item():.4f}")
Common ML Interview Traps & Numerical Pitfalls
Trap #1: Saying DiT still uses convolutional U-Net downsampling/upsampling skips.
Best Practice: DiT is a pure isotropic Transformer operating on patchified latent tokens (no U-Net convolutions!) — which is why it scales cleanly like LLMs!
Chapter Mastery Check (+85 XP)
Score: 0 / 1
1Why do Flux, SD3, and Sora prefer Rectified Flow Matching over classic DDPM?

Apply What You Learned — Solve Matching Coding Problems