Diffusion and Flow Matching
Part I followed models that assign probability by predicting the next token. Part II begins with another choice: define a simple corruption or transport path between data and a known reference distribution, then learn how to travel back to data. diffusion, the train-by-adding-noise and generate-by-denoising recipe, is a major model family for image, audio, and video generation. flow matching trains a continuous transport velocity instead. The two families overlap mathematically, but neither is a universal replacement for the other or for autoregression.
The important change is where sequential dependence lives. An autoregressive model waits for the previous output position. A diffusion or flow sampler can update all positions in its current state together, but it repeats that full network evaluation over several noise levels or solver steps. Each evaluation still pays for the representation's size and the model's architecture. Fewer sampling steps help only when quality and per-step cost are held in view.
Choose a factorization, not a camp
For an ordered representation , where denotes the complete output, autoregression writes
Here, is output position , is every earlier position, and is the modeled probability distribution. This factorization gives text a natural streaming interface and lets an end-of-sequence symbol choose the output length. Images and audio can also be serialized into tokens, so the issue is not that they lack any order. It is that no single serialization is always the best inductive bias for their spatial or temporal structure.
Diffusion and flow models choose a state shape first, then revise that state as a whole. That makes editing, infilling, and global coordination natural, but usually requires a fixed or separately predicted shape. The contrast is operational:
| Property | Autoregressive model | Diffusion or flow model |
|---|---|---|
| Serial axis | output positions | denoising or solver steps |
| Work per serial step | usually one new token with cached history | one full-state network evaluation |
| Length | emitted until a stop condition | chosen before or outside the path |
| Revision | earlier outputs are normally fixed | all current positions may change |
| Common strengths | streaming, variable length, token likelihood | fixed-shape generation, editing, infilling |
These are tendencies, not boundaries. Blockwise and speculative methods mix them, while latent representations change what one “position” costs.
Build the discrete diffusion process
Diffusion probabilistic models were introduced by Sohl-Dickstein et al. in 2015 as a learned reversal of a gradual noising process inspired by nonequilibrium thermodynamics (Sohl-Dickstein et al. 2015). The widely used denoising diffusion probabilistic models (DDPM), Ho et al.'s 2020 denoising-diffusion formulation, chooses a prescribed Gaussian Markov chain (Ho et al. 2020):
Here, is a data sample; is its corrupted state at step ; is the fixed forward process; is the variance added at step ; is the retained fraction for that step; is the cumulative retained fraction; is the identity covariance; and is a Gaussian density over with mean and covariance . The schedule has hyperparameters, but no learned parameters.
Gaussian composition makes the marginal at any step available without running the intervening chain:
Here, is fresh standard Gaussian noise. If is close to zero, the terminal marginal is approximately standard normal. It is not exactly standard normal for an arbitrary finite schedule.
Generation needs a learned reverse transition,
where is the model with parameters , is its reverse mean, and is a fixed or learned reverse covariance. In the common noise-prediction parameterization, the mean is
Here, estimates the noise component in . Ho et al. derive a variational objective and then use a practical reweighted version that drops its timestep-dependent coefficients:
In this expectation, comes from the data, is sampled from the training steps, and is standard Gaussian noise. The loss teaches the mean needed for the reverse transition; the reverse variance remains a separate design choice.
The runnable example removes the neural approximation so the reversal itself can be checked. For one-dimensional Gaussian data, every forward marginal and reverse conditional is analytic. Starting from the exact terminal marginal and applying those reverse conditionals recovers the original distribution.
import numpy as np
rng = np.random.default_rng(0)
data_mean, data_variance = 3.0, 1.0
steps = 1000
beta = np.linspace(1e-4, 0.02, steps)
alpha = 1.0 - beta
alpha_bar = np.cumprod(alpha)
def marginal(alpha_bar_t):
"""Mean and variance of q(x_t) for Gaussian data."""
mean = np.sqrt(alpha_bar_t) * data_mean
variance = alpha_bar_t * data_variance + (1.0 - alpha_bar_t)
return mean, variance
# Start from q(x_T), which is nearly N(0, 1) for this schedule.
terminal_mean, terminal_variance = marginal(alpha_bar[-1])
x = rng.normal(terminal_mean, np.sqrt(terminal_variance), size=10_000)
for t in range(steps - 1, -1, -1):
previous_alpha_bar = alpha_bar[t - 1] if t > 0 else 1.0
previous_mean, previous_variance = marginal(previous_alpha_bar)
current_mean, current_variance = marginal(alpha_bar[t])
# Exact Gaussian conditional q(x_{t-1} | x_t).
gain = np.sqrt(alpha[t]) * previous_variance / current_variance
reverse_mean = previous_mean + gain * (x - current_mean)
reverse_variance = previous_variance - gain**2 * current_variance
x = reverse_mean + np.sqrt(max(reverse_variance, 0.0)) * rng.normal(size=x.shape)
print(f"terminal signal fraction: {alpha_bar[-1]:.6f}")
print(f"target mean={data_mean:.2f}, variance={data_variance:.2f}")
print(f"recovered mean={x.mean():.2f}, variance={x.var():.2f}")
Keep schedules and prediction targets distinct
The noise schedule decides which signal-to-noise ratios receive training and sampling effort. Nichol and Dhariwal found the original linear schedule suboptimal for their and experiments because its late steps were already almost pure noise. Their cosine schedule, where cumulative signal follows a normalized squared cosine, is (Nichol and Dhariwal 2021)
Here, is the number of noising steps, is the step index, is the paper's small offset, and normalizes the cumulative signal fraction to one. This is one documented schedule, not a universal best setting.
The network target is another choice. Let
Here, is the signal coefficient and is the noise coefficient. The model can predict , , or the variance-preserving target
Here, contains the same denoising information for this specified path. Recovering from an prediction divides by and becomes ill-conditioned at high noise. Recovering from an prediction divides by and becomes ill-conditioned at low noise. The conversions from keep bounded coefficients, which made it useful in progressive distillation (Salimans and Ho 2022). This is tied to the angular parameterization of a variance-preserving diffusion path. It is not, without a time conversion, the flow-matching velocity introduced later.
Connect denoising to scores, SDEs, and ODEs
A score is a spatial derivative of log density. At noise level , define
Here, is the marginal density of the noisy data, is a point in its state space, is the natural logarithm, and differentiates with respect to . Noise prediction and score prediction coincide only at the population optimum:
Here, is the conditional mean of the injected noise and . A trained network approximates this relation; an arbitrary is not the exact score. This denoising-score connection predates DDPM (Vincent 2011) and underlies noise-conditional score networks sampled with annealed Langevin dynamics (Song and Ermon 2019).
Continuous time puts the common constructions in one stochastic framework. A forward stochastic differential equation (SDE) can be written as
Here, is the drift, is a scalar diffusion coefficient, is an infinitesimal time increment, and is standard Brownian motion. Under the conditions used by Song et al., the reverse-time process is (Anderson 1982; Song et al. 2021)
Here, is Brownian motion when time is integrated from noise back to data. The DDPM perturbation converges to a variance-preserving SDE, while the perturbation used by score matching with Langevin dynamics converges to a variance-exploding SDE. Generic score matching is a training principle, not itself one of those discretizations.
The same exact score defines a deterministic probability-flow ordinary differential equation (ODE):
Here, the symbols retain their SDE meanings. The ODE and SDE share the same one-time marginal densities under the exact score and regularity assumptions; they do not share individual stochastic paths or transition laws. A learned score makes the equivalence approximate.
The 2015 construction was inspired by nonequilibrium thermodynamics. The modern recipe also depends on denoising score matching, reverse-time stochastic processes, numerical ODE solvers, and learned neural parameterizations. The physics is part of the lineage, not a complete derivation of every later method. Chapter 3 examines that boundary.
Separate the model from the sampler
A deployed diffusion system is not identified by “uses diffusion.” At least six choices must be recorded separately:
| Layer | Examples | What it changes |
|---|---|---|
| Representation | pixels, waveform, autoencoder latent tensor | state size and information bottleneck |
| Backbone | U-Net, transformer | cost and receptive field per evaluation |
| Training target | , , diffusion , score | numerical conditioning and loss weighting |
| Conditioning | class label, text cross-attention, guidance | which conditional distribution is sampled |
| Path and schedule | , , continuous | where training and sampling effort is spent |
| Sampler | ancestral, DDIM, ODE/SDE solver, distilled model | network evaluations, stochasticity, and error |
Latent diffusion reduces the denoiser's state from pixels to a lower-resolution spatial tensor produced by a pretrained autoencoder; the encoder and final decoder sit outside the denoising chain. Cross-attention can inject text, boxes, or other conditions (Rombach et al. 2022). A latent bottleneck saves compute but can also discard detail, so the autoencoder reconstruction error belongs in the evaluation contract.
The backbone is independent of that representation choice. A U-Net uses multi-resolution convolutional blocks. diffusion transformer (DiT) replaces it with a transformer over latent patches. In the paper's class-conditional ImageNet experiments, increasing forward-pass compute through depth, width, or more patches correlated with lower FID (Peebles and Xie 2023). That is controlled evidence within one model family, not a universal scaling law.
Conditioning also changes inference cost. classifier-free guidance (CFG) trains a single model with the condition sometimes replaced by a null input, then combines two predictions at sampling (Ho and Salimans 2022):
Here, is the condition; is the null condition used during dropout training; and are the conditional and unconditional noise predictions; and is guidance strength. With this convention, returns the ordinary conditional prediction and larger values extrapolate away from the unconditional result. The method removes a separate classifier, but normally requires both network predictions per sampling step, whether run separately or batched together.
Sampler step count and network-function evaluations (NFE) are not synonyms. A first-order step may use one denoiser call; a second-order method may use two. DDIM defines non-Markovian forward processes with the same DDPM training objective, and its deterministic setting produced 10 to 50 times lower wall-clock sampling time in the paper's experiments (Song et al. 2021). In the continuous limit and with an optimal predictor, its ODE matches a probability-flow ODE after reparameterization. A coarse finite DDIM update is not simply Euler's method on the usual time coordinate. DPM-Solver instead constructs high-order updates in a log signal-to-noise coordinate and reports strong samples in roughly 10 to 20 NFE for its tested models (Lu et al. 2022). Neither number transfers automatically to another model, resolution, or guidance setting.
EDM made this separation explicit for a broad class of Gaussian diffusion models (Karras et al. 2022). It combines input, output, skip, and noise preconditioning with a chosen training distribution over continuous noise levels. Its power-law sampling grid is
Here, is noise level ; and are the finite endpoints; is the number of nonzero levels; and controls where the grid is dense. The paper used in reported settings and then added a final step. These are paper-specific settings. EDM's reported 35-NFE result also belongs to its stated benchmark configuration, not to the definition of EDM.
Flow matching learns transport directly
Flow matching starts from a continuous normalizing flow rather than a reverse Markov chain. Let an ODE move samples as
where is path time, is the current state, and is a time-dependent velocity field. A sufficiently regular density path is transported by this field when it satisfies the continuity equation
Here, is the time derivative and is divergence in state space. The equation conserves probability mass, but it does not select a unique velocity field by itself.
Marginal flow matching would regress against the velocity of , which is usually unavailable. Conditional flow matching (CFM) chooses tractable conditional paths instead (Lipman et al. 2023):
where ; identifies the sampled conditioning data for one path; is a point on that conditional path; is its analytic conditional velocity; and is the learned marginal velocity. Under the paper's regularity and integrability conditions, the CFM and inaccessible marginal objectives have the same gradient with respect to ; their scalar losses need not be identical.
A simple paired linear path makes the training target concrete:
Here, is the reference distribution, usually Gaussian; is the data distribution; and comes from a declared coupling, often independent sampling unless another coupling is constructed. The target is constant for one pair. The population regressor is instead
Here, averages all pairwise targets that can pass through state at time . Its marginal trajectories can therefore curve even when every conditional interpolation is straight. Flow-matching training is simulation-free because these training points and targets require no ODE solve; sampling still integrates the learned ODE.
The “optimal transport” phrase also needs scope. Lipman et al. study conditional Gaussian optimal-transport paths. That does not mean independent noise-data pairs form the globally optimal coupling between the two marginal distributions. Coupling choice, path choice, and solver choice are separate.
Straighten or collapse the sampling path
rectified flow uses the linear paired target above and can apply reflow: integrate the learned model, pair each reference sample with its generated endpoint, and train again on those new pairs (Liu et al. 2023). Reflow tends to straighten the model-induced coupling and the paper proves non-increasing convex transport costs. It does not guarantee the globally optimal data coupling. One Euler step is exact only for an exactly constant learned trajectory; empirical straightness makes coarse integration useful, not automatically exact.
Stochastic interpolants expose a broader shared construction (Albergo and Vanden-Eijnden 2023; Albergo et al. 2025):
Here, is a chosen interpolation satisfying the endpoint conditions; and are endpoint random variables; is independent standard Gaussian noise; and controls extra stochasticity and vanishes at the endpoints. Under suitable regularity, one interpolant supports a deterministic ODE and families of SDEs. The SDE construction additionally needs a score and a chosen diffusion coefficient, so “unified” does not mean that every algorithm is interchangeable.
There are three distinct routes to fewer evaluations:
| Route | What changes | Representative methods |
|---|---|---|
| Better integration | keep the trained model; reduce numerical error per NFE | DDIM, DPM-Solver, EDM Heun sampler |
| Teacher distillation | train a new student against a multi-step model | progressive distillation, LCM, DMD, ADD |
| Direct few-step objective | train a model whose consistency or interval target supports large jumps | consistency training, MeanFlow |
Progressive distillation repeatedly trains one student step to reproduce two deterministic teacher steps (Salimans and Ho 2022). Latent Consistency Models apply consistency distillation to a classifier-free-guided latent diffusion probability-flow ODE and target two to four steps (Luo et al. 2023). Distribution Matching Distillation uses approximate distribution matching plus a regression term (Yin et al. 2024), while Adversarial Diffusion Distillation combines teacher score distillation with an adversarial loss for one to four steps (Sauer et al. 2024). Their objectives and failure modes are not interchangeable.
Consistency models learn a function that maps any point on one probability-flow ODE trajectory to the same data-side boundary, with an identity condition at that boundary. They support one-step generation by design and can also use several steps to trade compute for quality; training may distill a pretrained diffusion model or use standalone consistency training (Song et al. 2023). MeanFlow is a later teacher-free route that learns an interval-average velocity from a relation to the instantaneous flow field (Geng et al. 2025). It is not the claim that average and instantaneous velocities are equal.
Evaluate the whole generation contract
Training loss alone cannot compare two deployed generators. A useful evaluation holds the prompt or class distribution, output resolution, sample count, and random-seed policy fixed, then reports:
- Distribution quality and coverage: FID or another declared feature-space statistic, together with a coverage or diversity measure. The feature encoder and sample count are part of the result.
- Conditional correctness: prompt alignment, class accuracy, or task-specific constraints, plus human evaluation when the automatic proxy is incomplete.
- Representation loss: reconstruction quality of the autoencoder separately from denoising quality in latent space.
- Efficiency: solver steps, NFE, wall-clock latency, throughput, peak memory, batch size, hardware, precision, and whether conditional and unconditional guidance passes were batched.
- Stability: variation across seeds and guidance scales, plus failure rates for malformed outputs, saturation, or mode loss.
The checkpoint is only one part of the reproducibility record. Also preserve the autoencoder and its scaling constant, prediction parameterization, training noise distribution, loss weighting, conditioning dropout, sampler equations, time or noise grid, solver tolerances, guidance convention, output shape, and decoding post-processing.
Common symptoms point to different layers:
| Symptom | First comparison | Likely layer |
|---|---|---|
| samples improve with more NFE | coarse solver against a fine reference | sampler or path curvature |
| details are missing even with many steps | latent reconstruction against input | autoencoder bottleneck |
| prompts align only at high guidance and diversity collapses | guidance sweep at fixed seeds | conditioning or CFG |
| training loss falls but samples remain poor | fixed evaluation suite and target conversion | objective weighting or parameterization |
| one-step model is sharp but drops modes | precision and coverage against teacher | distillation or adversarial objective |
| reported steps are low but latency is high | NFE and per-evaluation profile | backbone, guidance, or runtime |
Hand continuous states to discrete text
Text changes the state space. D3PM replaces Gaussian noise with categorical
transition matrices; one option sends tokens to an absorbing [MASK] state
(Austin et al. 2021). That connects diffusion to mask-based generation, but an
ordinary fixed-mask-rate language model is not automatically a complete
diffusion generator. SEDD learns ratios between discrete-state probabilities
and reports competitive results against GPT-2-scale autoregressive models in
its evaluated settings (Lou et al. 2024). LLaDA trains an 8B masked-diffusion model
from scratch and reports broadly competitive results on selected zero- and
few-shot tasks, with important differences in data and evaluation from external
autoregressive models (Nie et al. 2025).
Length control, likelihood bounds, cache reuse, and iterative unmasking now become central. Chapter 13 develops those discrete objectives and their serving tradeoffs rather than compressing a fast-moving language-model literature into this continuous-state chapter.
“Diffusion” and “flow matching” are not cleanly opposed camps. A flow-matching objective can use diffusion probability paths, and diffusion sampling can be written as a reverse SDE or a probability-flow ODE. Straight conditional paths do not guarantee straight learned marginal trajectories, and one-step objectives do not guarantee parity with a multi-step teacher. Claims must name the path, target, sampler, NFE, and evaluation setting.
Serving cost is approximately the number of network evaluations multiplied by the cost of each evaluation, plus autoencoder and post-processing work. Guidance may add another prediction per step; larger latent grids increase attention or convolution work; higher-order solvers may call the network more than once per step. A “four-step” model is therefore not a latency claim until the backbone, NFE accounting, batch, hardware, precision, and output shape are fixed. The training objective and the serving system meet at that measurement contract.
Comments
Log in to comment