Flow Models
Invertible transformations, exact likelihoods, and the modern convergence of flows with diffusion.
Invertible transformations and exact likelihood
Every generative model family answers the same question differently: how do you turn simple noise into complex data? Normalizing flows take the mathematically cleanest path — define an invertible mapping between a Gaussian and the data distribution, then compute the exact likelihood using the change of variables formula. No lower bounds, no adversaries, no denoising.
A normalizing flow defines a bijection $f_\theta: \mathcal{Z} \to \mathcal{X}$. To sample: draw $z \sim \mathcal{N}(0,I)$, compute $x = f_\theta(z)$. To evaluate density: compute $z = f_\theta^{-1}(x)$, apply the change of variables formula. Both directions work because $f_\theta$ is invertible.
Exact likelihood gives flows unique advantages: principled model comparison (log-likelihood on held-out data), no mode collapse (maximum likelihood covers all modes), no posterior gap (unlike VAEs), and lossless compression. The price: every layer must be invertible, constraining architecture design.
The change of variables formula
Two terms: the base density at the mapped point, plus a volume correction from the Jacobian determinant. When $f$ stretches a region, density decreases to keep total probability at 1. The Jacobian determinant measures exactly how much local volume changes.
For a chain of $K$ layers: $\log p_\theta(x) = \log p_\mathcal{Z}(z_0) - \sum_{k=1}^K \log|\det J_{f_k}|$. Training maximizes average log-likelihood — a clean, unbiased objective with no variational bounds or surrogate losses.
The design constraint: a general $D \times D$ determinant costs $O(D^3)$. For images ($D = 196{,}608$ for 256×256×3), this is impossible. Every practical flow architecture uses special structure — triangular Jacobians, block-diagonal patterns — to bring the cost to $O(D)$.
Coupling layers — the trick that makes the Jacobian tractable
The affine coupling layer (Dinh et al., 2015/2017) splits the input into two halves and transforms one conditioned on the other:
where $s$ (scale) and $t$ (translation) are arbitrary neural networks. They can be deep ResNets — their complexity doesn't affect the Jacobian cost.
Since each layer only transforms half the dimensions, you alternate which half is transformed across layers. Stack 8–16 coupling layers with alternating masks and the flow becomes expressive enough for complex distributions.
The MAF vs IAF tradeoff
Autoregressive flows take coupling to its extreme: each dimension conditions on all previous dimensions. This maximizes expressiveness per layer but introduces a fundamental asymmetry:
MAF
Conditions on observed data. Density evaluation is fast (parallel). Sampling is slow ($D$ sequential steps). Use for: training, evaluation.
IAF
Conditions on latent variables. Sampling is fast (parallel). Density evaluation is slow. Use for: generation, VAE posteriors.
MAF and IAF are mathematical inverses. Coupling layers are a special case with two groups — sacrificing expressiveness for speed in both directions.
Continuous normalizing flows — infinite layers
Take $K \to \infty$ with infinitesimal steps: instead of a discrete chain, define the transformation as an ODE: $dx/dt = v_\theta(x, t)$. The velocity field $v_\theta$ is any neural network — no invertibility constraints. To sample, integrate from $t=0$ to $t=1$. FFJORD (Grathwohl et al., 2019) showed this works, using Hutchinson's trace estimator for the log-determinant. Downside: ODE simulation during training is expensive. This sets the stage for flow matching.
Glow — 1×1 convolutions
Glow (Kingma & Dhariwal, 2018) was the first flow to generate realistic faces, using three innovations: invertible 1×1 convolutions as learned permutations (replacing fixed channel shuffling), multi-scale architecture (split off dimensions at each scale), and actnorm (data-dependent activation normalization). Glow demonstrated that flows could reach near-GAN quality — with exact log-likelihood as a bonus.
Flow matching — simulation-free training
Flow matching (Lipman et al., 2023) trains a continuous normalizing flow without ODE simulation and without architectural constraints. Define a linear interpolation between noise and data:
The conditional velocity along this path is simply $x_1 - x_0$ — the direction from noise to data. The training objective is regression on this velocity:
The linear interpolation is the optimal transport path — the straightest possible trajectory between noise and data. Straighter = fewer ODE solver steps at inference (10–50 instead of 20–1000). Stable Diffusion 3, Flux, and SD 3.5 all use rectified flow matching.
Rectified flows — even straighter paths
Rectified flows (Liu et al., 2023) iteratively straighten trajectories: train a flow, generate matched pairs $(x_0, x_1)$ by running the ODE, retrain on straight-line interpolation between matched pairs, repeat. After rectification, trajectories are nearly straight — solvable with 1–2 Euler steps. This is the foundation of SD3's fast generation.
Classical flows vs flow matching vs diffusion
The deep connection: $v_t(x) = f(t)x + g(t)^2 \nabla_x \log p_t(x)$ — velocity is a linear combination of drift and score. Flow matching and diffusion use the same architectures (DiT), same conditioning (CLIP/T5), same infrastructure. Whether SD3 is "a diffusion model" or "a flow model" has no clean answer.
Flows on 2D distributions
Experiments from the Module 06 notebook. Implements RealNVP coupling layers and flow matching on 2D distributions (moons, rings).
Watching coupling layers warp space
The notebook visualizes how 8 affine coupling layers progressively warp a Gaussian grid into the two-moons distribution. Each layer applies a smooth, invertible deformation — grid lines bend but never cross (invertibility means no two points map to the same location). Early layers do coarse rearrangement; later layers handle fine-grained shape matching.
Exact density on a grid
Because flows compute exact likelihood, you can evaluate $\log p_\theta(x)$ at every point on a 2D grid and plot the learned density surface. The result matches the empirical distribution — peaks at the moons, valleys between. No other generative model family gives you this exact, pointwise density evaluation.
Flow matching trajectories
The flow matching implementation shows particle trajectories from noise to data. Unlike coupling layers (discrete jumps), flow matching particles follow nearly straight lines — each particle takes an almost direct path from noise to data. This straightness directly explains why flow matching needs fewer ODE steps.
Velocity field at multiple timesteps
At $t=0$, the velocity arrows point outward from the origin toward data. At $t=0.5$, the field is most structured — the target shape is visible. At $t \approx 1$, arrows become small — particles have arrived.
Watch particles flow from noise to data
Particles start as Gaussian noise and follow straight trajectories to form a ring of clusters. Drag the slider to scrub through time, or press play. Toggle "trails" to see how straight the paths are.
t = 0.00 · Gaussian noise · drag slider or press play
Takeaways & exercises
- Flows compute exact likelihood via the change of variables formula — no bounds, no adversaries.
- Coupling layers make the Jacobian tractable by exploiting triangular structure — $O(D)$ not $O(D^3)$.
- MAF and IAF are inverses — fast density/slow sampling vs fast sampling/slow density.
- Flow matching removes all constraints — learn a velocity field via regression, no invertibility needed.
- Straight trajectories = fewer steps. OT interpolation produces straighter paths than diffusion.
- Flow matching and diffusion have converged. SD3 and Flux use flow matching with diffusion-style architectures.
1. Why must flow transformations be invertible? What would go wrong if two inputs mapped to the same latent code?
2. Write out the 4×4 Jacobian for a 2+2 affine coupling split. Verify it's triangular and its determinant is $\prod \exp(s_j)$.
3. Open the notebook. Compare coupling-layer grid warping with flow matching trajectories. Which looks straighter?
4. In the playground above, toggle trails on. Are the paths straight? Compare mentally to the diffusion demo in Chapter 04.
5. List three differences between flow matching and DDPM training (interpolation, target, path shape).
Further reading
- Dinh et al. (2015/2017) — NICE and RealNVP. Coupling-layer flows.
- Papamakarios et al. (2017) — MAF. Masked autoregressive flows.
- Kingma & Dhariwal (2018) — Glow. Invertible 1×1 convolutions.
- Grathwohl et al. (2019) — FFJORD. Continuous normalizing flows.
- Lipman et al. (2023) — Flow Matching for Generative Modeling.
- Liu et al. (2023) — Rectified Flows. Iterative straightening.
- Esser et al. (2024) — Stable Diffusion 3. Flow matching in production.
- P. Kulkarni — Module 06 notebook.