Foundations to Frontier
Tests
H. Generative Models › H7 Diffusion Models

Score SDEs: VP and VE SDEs and the probability-flow ODE

Song et al. (ICLR 2021) unified DDPM and noise-conditional score networks (NCSN) as two discretisations of a stochastic differential equation that turns data into noise. The variance-preserving (VP) SDE is the continuous limit of DDPM; the variance-exploding (VE) SDE is the limit of NCSN's growing noise levels. A result of Anderson (1982) says that any such diffusion can be run backwards in time if the score ∇ log p_t(x) is known at every noise level, and a learned score network supplies it. The same marginals are also produced by a deterministic probability-flow ODE, which turns the model into a continuous normalizing flow: exact likelihoods, unique latent codes and the basis for fast ODE solvers. This page derives the forward marginals, the reverse SDE, the Fokker–Planck argument behind the probability-flow ODE and the likelihood computation, and covers predictor–corrector sampling and sub-VP SDEs. In the exercises you code the SDE pieces, then sample an 8-mode 2-D mixture with its exact score. With 100 steps every sampler reproduced the data; with 10 steps the VP SDE sampled by Euler–Maruyama failed (47% of samples off-mode) while its ODE degraded gracefully (7%), and for VE the SDE (spread 1.17) beat the ODE (1.52). The probability-flow ODE's log-likelihood matched the exact value to 0.0003 nats on average (1.0874 against 1.0877), and an encode–decode round trip was accurate to 2 × 10⁻⁴.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

From many small steps to one smooth equation

DDPM adds noise in a thousand discrete steps. Shrink the steps and add more of them, and the noising becomes a continuous random drift, described by a stochastic differential equation, the same kind of equation used for gyro drift or particle diffusion. Two families are popular: one shrinks the signal while adding noise, keeping the total size fixed (variance preserving); the other just keeps adding noise (variance exploding). The remarkable part is that this random process can be run backwards, from noise to data, provided you know which way the probability density increases at each moment (the score). There is even a version with no randomness at all, a smooth flow, that ends up with exactly the same distribution. The demo lets you switch between VP and VE and between the random and the smooth sampler, and shows a dozen paths.

Diffusion lab
Spread 1.07 (1 = data), off-mode 2.3%. VP: β(t) linear from 0.1 to 20. 20 uniform steps in t. The PF-ODE paths are smooth and deterministic.
Why engineers care

The SDE view is what made fast diffusion samplers, exact diffusion likelihoods, inpainting and inverse-problem solvers possible; it is the language of nearly every diffusion paper since 2021, including those for weather, molecules and robot policies.

📝 Quick check (Level 0)
0/3 answered
  1. 1. The VP SDE is the continuous-time limit of…
  2. 2. To run a diffusion backwards in time you need…
  3. 3. The probability-flow ODE produces…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Forward SDEs, the reverse SDE and the ODE

  • Forward SDE: dx = f(x, t) dt + g(t) dw, t ∈ [0, 1], with p_0 the data and p_1 close to a known prior.
  • VP SDE (DDPM limit): dx = −½ β(t) x dt + √β(t) dw, β(t) = β_min + t (β_max − β_min) with 0.1 and 20. Marginal: x_t | x0 ~ N(√ā(t) x0, (1 − ā(t)) I) with ā(t) = exp(−∫₀ᵗ β). At t = 1, ā = e^(−10.05) = 4.3 × 10⁻⁵.
  • VE SDE (NCSN limit): dx = √(dσ²/dt) dw with geometric σ(t) = σ_min (σ_max/σ_min)^t; x_t | x0 ~ N(x0, (σ(t)² − σ(0)²) I). Song et al. set σ_max to about the largest distance between training points (e.g. 50 for CIFAR-10).
  • sub-VP SDE: dx = −½β x dt + √(β (1 − e^(−2∫β))) dw, noise variance never above VP’s; it gave the best likelihoods.
  • Reverse SDE (Anderson, 1982): dx = [f − g² ∇_x log p_t(x)] dt + g dw̄, integrated from t = 1 to 0 with reverse-time Brownian motion.
  • Probability-flow ODE: dx/dt = f − ½ g² ∇_x log p_t(x); its solutions have the same marginals p_t as the SDE.
  • Training: a time-conditioned score network s_θ(x, t) by denoising score matching, E_t λ(t) E ‖s_θ(x_t, t) − ∇ log p(x_t | x0)‖², with ∇ log p(x_t | x0) = −(x_t − m x0)/v; ε-prediction is the same thing rescaled, s = −ε/√(1 − ā).
  • Samplers: Euler–Maruyama (or the ‘reverse diffusion’ predictor) for the SDE; predictor–corrector (PC) adds Langevin MCMC corrector steps at each noise level; black-box ODE solvers (RK45) for the PF-ODE.
  • Results (CIFAR-10): FID 2.20 and Inception score 9.89 (NCSN++, VE); 2.99 bits/dim (DDPM++ with sub-VP, via the PF-ODE); first 1024×1024 score-based samples (CelebA-HQ).
📝 Quick check (Level 1)
0/3 answered
  1. 1. VP SDE's drift is…
  2. 2. The PF-ODE drift uses the score with a factor of…
  3. 3. Predictor–corrector sampling adds…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Coding the SDE toolkit

Exercise 1.

  • The VP marginal (mean factor and variance) and the VE marginal standard deviation.
  • Reverse-SDE and probability-flow drifts from f, g and the score.
  • An Euler–Maruyama step (tested on Brownian motion), and a check that the VE PF-ODE rescales Gaussian data exactly.

Practice.

  • Train with ε- or score-prediction and λ(t) = variance (equivalent to DDPM’s L_simple).
  • For likelihoods, solve the PF-ODE with an adaptive solver (RK45, tolerances about 1e-5) and Hutchinson trace estimates, and dequantise discrete data.
  • Start the reverse process at t = 1 and stop at a small ε (1e-3 to 1e-5) rather than 0, where the score blows up; optionally finish with one denoising step x̂0 = E[x0 | x_ε].
  • The official score_sde (JAX) and score_sde_pytorch repositories implement VP, sub-VP, VE, PC samplers and likelihoods.
  • For inverse problems (inpainting, super-resolution, deblurring) add a data-consistency term to the reverse drift.
import torch
def vp_pf_ode_drift(score_model, x, t, bmin=0.1, bmax=20.0):
    beta = bmin + t * (bmax - bmin)
    return -0.5 * beta * x - 0.5 * beta * score_model(x, t)       # f − ½ g² ∇log p_t
def reverse_sde_step(score_model, x, t, dt, bmin=0.1, bmax=20.0):  # dt < 0
    beta = bmin + t * (bmax - bmin)
    drift = -0.5 * beta * x - beta * score_model(x, t)            # f − g² ∇log p_t
    return x + drift * dt + (beta * -dt) ** 0.5 * torch.randn_like(x)
🐍 Exercise 1: forward marginals, reverse drifts and Euler–Maruyama
Write the VP and VE marginals, the reverse-SDE and probability-flow drifts, and an Euler–Maruyama step; check the PF-ODE on Gaussian data.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Reverse sampling usually stops at…
  2. 2. Likelihoods from the PF-ODE need…
  3. 3. The official implementations are called…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Forward marginals. For the linear SDE dx = −½β x dt + √β dw, the mean obeys dm/dt = −½β m and the variance dv/dt = −β v + β, so m = x0 e^(−½∫β) and v = 1 − e^(−∫β) (starting from v = 0). VP keeps unit-variance data at unit variance; VE has m = x0 and v = σ(t)² − σ(0)².

Reverse SDE. Anderson (1982): if dx = f dt + g dw has marginals p_t, then the process run backwards satisfies dx = [f − g² ∇ log p_t] dt + g dw̄. One way to see it: the time-reversed density must satisfy the Fokker–Planck equation backwards, and adding g² ∇ log p_t to the drift turns the diffusion term into an anti-diffusion of exactly the right size.

Probability-flow ODE. Fokker–Planck for the forward SDE: ∂p/∂t = −∇·(f p) + ½ g² Δp. Since Δp = ∇·(p ∇ log p), this equals −∇·[(f − ½ g² ∇ log p) p], the continuity equation of the deterministic velocity f − ½ g² ∇ log p_t. So particles moved by that ODE have the same densities p_t at every t.

Likelihoods. The PF-ODE is a continuous normalizing flow (h5-7), so log p_0(x0) = log p_1(x(1)) + ∫₀¹ ∇·f̃(x(t), t) dt with f̃ the ODE drift; FFJORD-style Hutchinson estimators make the divergence affordable in high dimension.

Discretisations. Euler–Maruyama on the VP SDE is DDPM’s ancestral sampler up to O(β²); NCSN’s annealed Langevin dynamics is a corrector-only PC sampler for the VE SDE; DDIM (h7-4) is Euler on the PF-ODE in a reparameterised time.

Exercise check. With the exact score, the RK4-solved PF-ODE gave a mean NLL of 1.0874 against the exact 1.0877 (seed 0), and the largest single-point error was 0.028 nats.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The PF-ODE follows from rewriting the Fokker–Planck equation using…
  2. 2. DDPM's ancestral sampler is approximately…
  3. 3. NCSN's annealed Langevin dynamics corresponds to…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Do the SDE and ODE samplers agree, how many steps do they need, and is the PF-ODE likelihood exact?

History.

  • B. D. O. Anderson, “Reverse-time diffusion equation models” (Stochastic Processes and their Applications, 1982).
  • Y. Song, J. Sohl-Dickstein, D. P. Kingma, A. Kumar, S. Ermon and B. Poole, “Score-based generative modeling through stochastic differential equations” (ICLR 2021, outstanding paper award).
  • Y. Song and S. Ermon, “Generative modeling by estimating gradients of the data distribution” (NeurIPS 2019): NCSN.
  • Y. Song, C. Durkan, I. Murray and S. Ermon, “Maximum likelihood training of score-based diffusion models” (NeurIPS 2021).

Exercise 2.

  • Data and score: the 8-mode ring (std 0.15, radius 2) with its exact score at every noise level (Tweedie: score = (mean factor · E[x0 | x_t] − x_t)/variance). No network is trained: the exercise isolates the samplers.
  • SDEs: VP with β from 0.1 to 20; VE with σ from 0.01 to 20. Reverse SDE by Euler–Maruyama and PF-ODE by Euler, N uniform steps from t = 1 to 0.001, 1000 samples each.
  • Likelihood: VP PF-ODE solved by RK4 (50 steps, central-difference divergence) from 200 test points at t = 0.001 to t = 1, then the round trip back.
  • Metrics: spread = RMS distance to the nearest mode / (0.15·√2), 1 = data-like; off-mode = share farther than 4 std from every mode.
measure seed 0 seed 1 seed 2
VP, 10 steps: SDE / ODE spread (off-mode) 3.212 (47.2%) / 1.484 (7.4%) 3.277 (51.3%) / 1.426 (6.1%) 3.340 (50.3%) / 1.464 (7.8%)
VP, 30 steps: SDE / ODE spread 1.244 / 0.982 1.229 / 0.974 1.241 / 1.022
VP, 100 steps: SDE / ODE spread 1.035 / 1.017 1.053 / 0.989 1.077 / 0.988
VE, 10 steps: SDE / ODE spread (off-mode) 1.169 (0.1%) / 1.519 (3.2%) 1.146 (0.1%) / 1.568 (3.3%) 1.182 (1.0%) / 1.524 (2.7%)
VE, 100 steps: SDE / ODE spread 1.019 / 0.936 1.004 / 0.932 1.011 / 0.942
NLL via PF-ODE / exact 1.0874 / 1.0877 1.1769 / 1.1772 1.0427 / 1.0432
largest per-point log-likelihood error 0.028 0.034 0.036
encoded latents: max mean / variance 0.069 / 1.027 0.100 / 1.033 0.035 / 0.893
round-trip error (× 10⁻⁴) 1.94 2.11 2.71
  • Same distribution, different discretisation errors: at 100 steps all four samplers matched the data, as the theory says. At 10 steps the VP SDE broke down: with β(1) = 20 and Δt = 0.1, an Euler–Maruyama step overshoots, and half the samples landed between modes. The VP ODE was much more robust. For VE the order flipped: the SDE’s injected noise corrected the ODE’s discretisation bias.
  • A persistent bias: the VE ODE with uniform Euler steps stayed too sharp (spread 0.93–0.94) even at 100 steps; time discretisation matters, which is the starting point of EDM (h7-5).
  • Exact likelihood: the PF-ODE reproduced the exact log-likelihood to 0.0003–0.0005 nats on average; encoded latents look standard normal and decoding recovers the input to about 2 × 10⁻⁴: the ODE really is an invertible flow.
🐍 Exercise 2 (research): do the SDE and ODE samplers agree, how many steps do they need, and is the PF-ODE likelihood exact?
Sample the 8-mode ring with the exact score using reverse SDEs and probability-flow ODEs (VP and VE) at 10, 30 and 100 steps, and compute PF-ODE likelihoods and round trips.

Score SDEs in AI, robotics and aerospace

  • Inverse problems: score-based priors solve inpainting, compressed-sensing MRI and CT reconstruction by adding data consistency to the reverse SDE (Song et al., 2022; Chung et al., DPS, 2023), and are used for deblurring and cloud removal in satellite imagery.
  • Likelihoods and anomaly detection: PF-ODE likelihoods score how typical a sensor frame or trajectory is.
  • Weather and climate: generative forecast and downscaling models (h8-2) are trained and sampled in the SDE framework, often with EDM-style solvers.
  • Molecules: equivariant score SDEs generate 3-D conformations and docking poses (e.g. DiffDock, 2023).

References

  1. B. D. O. Anderson (1982). “Reverse-time diffusion equation models.” Stochastic Processes and their Applications 12(3).
  2. Y. Song, J. Sohl-Dickstein, D. P. Kingma, A. Kumar, S. Ermon and B. Poole (2021). “Score-based generative modeling through stochastic differential equations.” ICLR.
  3. Y. Song and S. Ermon (2019). “Generative modeling by estimating gradients of the data distribution.” NeurIPS.
  4. Y. Song, C. Durkan, I. Murray and S. Ermon (2021). “Maximum likelihood training of score-based diffusion models.” NeurIPS.
  5. H. Chung, J. Kim, M. T. Mccann, M. L. Klasky and J. C. Ye (2023). “Diffusion posterior sampling for general noisy inverse problems.” ICLR.