Simple Algorithm
Deep Learning · Representation · 24 min

Diffusion Models

Learning to undo noise, one small step at a time.

Destroying an image takes no skill: add a little Gaussian noise, then a little more, a thousand times over. A diffusion model is trained to undo one of those small steps and nothing else. Chain a thousand undone steps and pure noise turns into a new image, and the single regression loss that trains the network turns out to estimate the gradient of the log-density at every noise level.

What you’ll learn

  • Derive the forward-process marginal q(xt∣x0)=N(αˉt x0,(1−αˉt)I)q(\mathbf x_t\mid\mathbf x_0)=\mathcal N\big(\sqrt{\bar\alpha_t}\,\mathbf x_0,(1-\bar\alpha_t)I\big) from the single steps, and read a noise schedule through αˉt\bar\alpha_t and the signal-to-noise ratio.
  • Implement the noise-prediction loss ∥ϵ−ϵθ(xt,t)∥2\norm{\boldsymbol\epsilon-\boldsymbol\epsilon_{\boldsymbol\theta}(\mathbf x_t,t)}^2 and prove that its minimizer is the scaled score −1−αˉt ∇xlog⁡qt(xt)-\sqrt{1-\bar\alpha_t}\,\nabla_{\mathbf x}\log q_t(\mathbf x_t).
  • Write the DDPM sampling loop, explain why it takes many small steps, and show how DDIM and the probability-flow ODE sample deterministically in far fewer.
  • Explain how class labels and text enter the denoiser, what classifier-free guidance and latent diffusion change, and how U-Net and transformer backbones are built.

The forward process: noise on a schedule

Start with data x0∼pdata\mathbf x_0\sim p_{\text{data}}, say images with pixel values scaled to [−1,1][-1,1]. A diffusion model fixes a noise schedule β1,…,βT∈(0,1)\beta_1,\dots,\beta_T\in(0,1) and corrupts the data with a Markov chain, the forward process:

q(xt∣xt−1)=N(1−βt xt−1, βtI),t=1,…,T.q(\mathbf x_t\mid\mathbf x_{t-1})=\mathcal N\big(\sqrt{1-\beta_t}\,\mathbf x_{t-1},\ \beta_tI\big),\qquad t=1,\dots,T.

Throughout this lesson tt counts discrete steps and TT is the number of steps, not a sequence length. Each step shrinks the current image slightly and adds a little fresh Gaussian noise. The shrinking keeps the scale under control: if every coordinate of xt−1\mathbf x_{t-1} has unit variance, then xt\mathbf x_t has variance (1−βt)+βt=1(1-\beta_t)+\beta_t=1 as well, which is why this chain is called variance preserving. Nothing in it is learned.

DerivationJumping to step t in one draw
  1. xt=αt xt−1+1−αt ϵt,ϵt∼N(0,I),αt=1−βt,αˉt=∏s=1tαs\mathbf x_t=\sqrt{\alpha_t}\,\mathbf x_{t-1}+\sqrt{1-\alpha_t}\,\boldsymbol\epsilon_t,\qquad\boldsymbol\epsilon_t\sim\mathcal N(\mathbf 0,I),\qquad\alpha_t=1-\beta_t,\quad\bar\alpha_t=\prod_{s=1}^{t}\alpha_s

    Give each step its own fresh noise and introduce the shorthand αt\alpha_t and its running product αˉt\bar\alpha_t. The claim to prove is xt=αˉt x0+1−αˉt ϵ\mathbf x_t=\sqrt{\bar\alpha_t}\,\mathbf x_0+\sqrt{1-\bar\alpha_t}\,\boldsymbol\epsilon for a single standard normal ϵ\boldsymbol\epsilon; at t=1t=1 it is the definition.

  2. xt=αtαˉt−1 x0+αt(1−αˉt−1) ϵ′+1−αt ϵt⏟variance αt(1−αˉt−1)+(1−αt)\mathbf x_t=\sqrt{\alpha_t\bar\alpha_{t-1}}\,\mathbf x_0+\underbrace{\sqrt{\alpha_t(1-\bar\alpha_{t-1})}\,\boldsymbol\epsilon'+\sqrt{1-\alpha_t}\,\boldsymbol\epsilon_t}_{\text{variance }\alpha_t(1-\bar\alpha_{t-1})+(1-\alpha_t)}

    Assume the claim at step t−1t-1, with its own noise ϵ′\boldsymbol\epsilon', and substitute. Two independent zero-mean Gaussian noise terms appear:

  3. q(xt∣x0)=N(αˉt x0, (1−αˉt) I)q(\mathbf x_t\mid\mathbf x_0)=\mathcal N\big(\sqrt{\bar\alpha_t}\,\mathbf x_0,\ (1-\bar\alpha_t)\,I\big)

    Variances of independent Gaussians add, and αt(1−αˉt−1)+1−αt=1−αtαˉt−1=1−αˉt\alpha_t(1-\bar\alpha_{t-1})+1-\alpha_t=1-\alpha_t\bar\alpha_{t-1}=1-\bar\alpha_t. The noise is one Gaussian of variance 1−αˉt1-\bar\alpha_t, which completes the induction:

  4. Training never has to simulate the chain: draw tt and ϵ\boldsymbol\epsilon, and form xt\mathbf x_t in one line. As αˉt→0\bar\alpha_t\to0 the data are forgotten and q(xT∣x0)≈N(0,I)q(\mathbf x_T\mid\mathbf x_0)\approx\mathcal N(\mathbf 0,I), the distribution that sampling starts from.

A schedule is easiest to read through αˉt\bar\alpha_t, the fraction of the data's variance still present at step tt, or through the signal-to-noise ratio SNR(t)=αˉt/(1−αˉt)\mathrm{SNR}(t)=\bar\alpha_t/(1-\bar\alpha_t), which falls monotonically from very large to nearly zero. DDPM (Ho, Jain & Abbeel, 2020) used T=1000T=1000 steps with βt\beta_t rising linearly from 10−410^{-4} to 0.020.02. That gives αˉT≈4×10−5\bar\alpha_T\approx4\times10^{-5}, and Ho et al. measured the mismatch DKL(q(xT∣x0) ∥ N(0,I))\KL\big(q(\mathbf x_T\mid\mathbf x_0)\,\|\,\mathcal N(\mathbf 0,I)\big) at about 10−510^{-5} bits per dimension: the end of the chain is, for all practical purposes, pure noise.

Predict before you look

With DDPM's linear schedule (T=1000T=1000, βt\beta_t from 10−410^{-4} to 0.020.02), at which step tt does αˉt\bar\alpha_t first fall below 12\tfrac12, so that noise carries more than half of the variance?

Show the answer

260 step

log⁡αˉt=∑s≤tlog⁡(1−βs)≈−∑s≤tβs≈−(10−4t+10−5t2)\log\bar\alpha_t=\sum_{s\le t}\log(1-\beta_s)\approx-\sum_{s\le t}\beta_s\approx-\big(10^{-4}t+10^{-5}t^2\big), which reaches −log⁡2≈−0.69-\log2\approx-0.69 near t≈259t\approx259; the exact product first drops below 12\tfrac12 at t=260t=260. A quarter of the way through the chain, half of the signal is gone, and from t=674t=674 on less than 1% remains: the last third of the chain is nearly pure noise. That imbalance is one reason later work replaced the linear schedule (The Design Space of Diffusion Models).

What the network is trained to predict

The forward process destroys information; the reverse process must restore it. The network that does so is surprisingly simple to train: show it a noisy xt\mathbf x_t together with its step tt, and ask for the noise that was added. Ho et al.'s simplified objective is plain regression:

Lsimple(θ)=Et∼U{1,…,T}, x0∼pdata, ϵ∼N(0,I)[∥ϵ−ϵθ(αˉt x0+1−αˉt ϵ, t)∥2]\loss_{\text{simple}}(\boldsymbol\theta)=\E_{t\sim\mathcal U\{1,\dots,T\},\ \mathbf x_0\sim p_{\text{data}},\ \boldsymbol\epsilon\sim\mathcal N(\mathbf 0,I)}\Big[\norm{\boldsymbol\epsilon-\boldsymbol\epsilon_{\boldsymbol\theta}\big(\sqrt{\bar\alpha_t}\,\mathbf x_0+\sqrt{1-\bar\alpha_t}\,\boldsymbol\epsilon,\ t\big)}^2\Big]
One network serves every step, with tt as an input. Because xt\mathbf x_t is a known mix of x0\mathbf x_0 and ϵ\boldsymbol\epsilon, a noise prediction is also a clean-image prediction, x^0=(xt−1−αˉt ϵ^)/αˉt\hat{\mathbf x}_0=\big(\mathbf x_t-\sqrt{1-\bar\alpha_t}\,\hat{\boldsymbol\epsilon}\big)/\sqrt{\bar\alpha_t}; which of the two the network outputs is a design choice that The Design Space of Diffusion Models examines.

Where does this loss come from? Viewed as a hierarchical VAE whose encoder is the fixed forward chain, a DDPM has an ELBO that splits into one Gaussian KL term per step, each a weighted noise-prediction error; the Emergence lesson Very Deep Hierarchical VAEs writes it out. Lsimple\loss_{\text{simple}} drops the weights, which shifts emphasis toward the noisier, harder steps, and it gave better samples: a state-of-the-art unconditional CIFAR-10 FID of 3.17 at the time. Training is then a few lines:

python
import torch
import torch.nn.functional as F

T = 1000
betas = torch.linspace(1e-4, 0.02, T)              # DDPM's linear schedule
alpha_bar = torch.cumprod(1 - betas, dim=0)         # alpha_bar[t-1] is the text's alpha-bar_t

def diffusion_loss(model, x0):
    t = torch.randint(0, T, (x0.shape[0],))         # one random step per example (0-based)
    eps = torch.randn_like(x0)
    ab = alpha_bar[t].view(-1, *[1] * (x0.dim() - 1))
    xt = ab.sqrt() * x0 + (1 - ab).sqrt() * eps     # jump straight to step t
    return F.mse_loss(model(xt, t), eps)            # regress the noise

for x0 in loader:                                   # e.g. images scaled to [-1, 1]
    loss = diffusion_loss(model, x0)
    opt.zero_grad(); loss.backward(); opt.step()

model is any network that maps (xt,t)(\mathbf x_t,t) to an array shaped like xt\mathbf x_t. Each time an example is seen it is reused at a fresh random step with fresh noise, so the data are effectively augmented without limit.

DerivationProof
  1. ϵ∗(xt,t)=arg min⁡e E[∥ϵ−e∥2 ∣ xt]=E[ϵ∣xt]\boldsymbol\epsilon^*(\mathbf x_t,t)=\argmin_{\mathbf e}\ \E\big[\norm{\boldsymbol\epsilon-\mathbf e}^2\,\big|\,\mathbf x_t\big]=\E[\boldsymbol\epsilon\mid\mathbf x_t]

    A squared error is minimized pointwise by a conditional mean. Each tt has its own term, so for an unrestricted function the weighting over tt is irrelevant:

  2. ∇xlog⁡qt(x)=∫q(x∣x0) pdata(x0)qt(x) ∇xlog⁡q(x∣x0) dx0=E[∇xlog⁡q(xt∣x0) ∣ xt=x]\nabla_{\mathbf x}\log q_t(\mathbf x)=\int\frac{q(\mathbf x\mid\mathbf x_0)\,p_{\text{data}}(\mathbf x_0)}{q_t(\mathbf x)}\,\nabla_{\mathbf x}\log q(\mathbf x\mid\mathbf x_0)\,d\mathbf x_0=\E\big[\nabla_{\mathbf x}\log q(\mathbf x_t\mid\mathbf x_0)\,\big|\,\mathbf x_t=\mathbf x\big]

    The noised marginal is a mixture of Gaussians, one per clean image: qt(x)=∫q(x∣x0) pdata(x0) dx0q_t(\mathbf x)=\int q(\mathbf x\mid\mathbf x_0)\,p_{\text{data}}(\mathbf x_0)\,d\mathbf x_0. Differentiate under the integral and divide by qt(x)q_t(\mathbf x): the score of a mixture is the posterior average of its components' scores.

  3. ∇xlog⁡q(xt∣x0)=−xt−αˉt x01−αˉt=−ϵ1−αˉt\nabla_{\mathbf x}\log q(\mathbf x_t\mid\mathbf x_0)=-\frac{\mathbf x_t-\sqrt{\bar\alpha_t}\,\mathbf x_0}{1-\bar\alpha_t}=-\frac{\boldsymbol\epsilon}{\sqrt{1-\bar\alpha_t}}

    Each component's score is explicit, and at a training pair it is the noise in disguise:

  4. Combining, ∇xlog⁡qt(xt)=−E[ϵ∣xt]/1−αˉt\nabla_{\mathbf x}\log q_t(\mathbf x_t)=-\E[\boldsymbol\epsilon\mid\mathbf x_t]/\sqrt{1-\bar\alpha_t}, the first claim. Writing x0=(xt−1−αˉt ϵ)/αˉt\mathbf x_0=(\mathbf x_t-\sqrt{1-\bar\alpha_t}\,\boldsymbol\epsilon)/\sqrt{\bar\alpha_t} and taking E[ ⋅∣xt]\E[\,\cdot\mid\mathbf x_t] gives the second. ■\blacksquare

So a noise predictor is a score estimator in disguise, and training it is denoising score matching (Vincent, 2011) at every noise level at once; the Emergence lesson Energy-Based Models derives score matching from scratch. Two consequences shape everything that follows.

  • No normalizing constant. The score ∇xlog⁡qt\nabla_{\mathbf x}\log q_t ignores any constant factor in qtq_t, so the network never has to compute a probability.
  • Many noise levels. At low noise the score can be learned only near the training points; between them, where a sampler starting from noise would begin, there is almost no training signal. Heavy noise spreads every training point over the whole space and makes the score learnable everywhere, and a decreasing ladder of noise levels leads samples from there back to the data (Song & Ermon, 2019).
Check your understanding

A well-trained network receives xt\mathbf x_t at a step where αˉt≈10−4\bar\alpha_t\approx10^{-4}, and its implied clean image x^0\hat{\mathbf x}_0 is close to the best possible estimate E[x0∣xt]\E[\mathbf x_0\mid\mathbf x_t]. What does x^0\hat{\mathbf x}_0 look like?

  1. A sharp, realistic image from the data distribution
  2. The training image that was noised to produce xt\mathbf x_t
  3. Close to the average of the training data: a featureless blur
  4. Pure noise, since xt\mathbf x_t is almost pure noise
Show the answer

C. Close to the average of the training data: a featureless blur

At αˉt≈10−4\bar\alpha_t\approx10^{-4}, xt\mathbf x_t carries almost no information about x0\mathbf x_0, so the posterior over clean images is nearly the data distribution itself, and its mean is nearly the dataset average. This is why a single denoising step from pure noise cannot generate: sampling needs many steps, each committing to a little more detail as the noise level falls.

Running the chain backward

To sample you need the reverse transitions q(xt−1∣xt)q(\mathbf x_{t-1}\mid\mathbf x_t), which depend on the unknown data distribution. Two facts make a learned approximation work. First, if the clean image were known, the reverse step would be an exact Gaussian, by Gaussian conditioning on the forward process:

q(xt−1∣xt,x0)=N(μ~t, β~tI),μ~t=αˉt−1 βt1−αˉt x0+αt (1−αˉt−1)1−αˉt xt,β~t=1−αˉt−11−αˉt βtq(\mathbf x_{t-1}\mid\mathbf x_t,\mathbf x_0)=\mathcal N\big(\tilde{\boldsymbol\mu}_t,\ \tilde\beta_tI\big),\qquad\tilde{\boldsymbol\mu}_t=\frac{\sqrt{\bar\alpha_{t-1}}\,\beta_t}{1-\bar\alpha_t}\,\mathbf x_0+\frac{\sqrt{\alpha_t}\,(1-\bar\alpha_{t-1})}{1-\bar\alpha_t}\,\mathbf x_t,\qquad\tilde\beta_t=\frac{1-\bar\alpha_{t-1}}{1-\bar\alpha_t}\,\beta_t
The mean mixes the clean image with the current noisy one, and the variance β~t\tilde\beta_t is slightly smaller than βt\beta_t.

Second, when βt\beta_t is small the true reverse step q(xt−1∣xt)q(\mathbf x_{t-1}\mid\mathbf x_t) is itself close to Gaussian (Sohl-Dickstein et al., 2015), so a Gaussian with a learned mean loses little. DDPM plugs the network's x^0\hat{\mathbf x}_0 into μ~t\tilde{\boldsymbol\mu}_t. Written with the predicted noise the mean simplifies, and written with the implied score s^θ=−ϵθ/1−αˉt\hat{\mathbf s}_{\boldsymbol\theta}=-\boldsymbol\epsilon_{\boldsymbol\theta}/\sqrt{1-\bar\alpha_t} it is a step uphill on the log-density of the current noise level, followed by a slight rescaling:

xt−1=1αt(xt−βt1−αˉt ϵθ(xt,t))⏟= (xt+βt s^θ(xt,t))/αt+ σtz,z∼N(0,I)\mathbf x_{t-1}=\underbrace{\frac{1}{\sqrt{\alpha_t}}\Big(\mathbf x_t-\frac{\beta_t}{\sqrt{1-\bar\alpha_t}}\,\boldsymbol\epsilon_{\boldsymbol\theta}(\mathbf x_t,t)\Big)}_{=\ (\mathbf x_t+\beta_t\,\hat{\mathbf s}_{\boldsymbol\theta}(\mathbf x_t,t))/\sqrt{\alpha_t}}+\ \sigma_t\mathbf z,\qquad\mathbf z\sim\mathcal N(\mathbf 0,I)
Ho et al. tried σt2=βt\sigma_t^2=\beta_t and σt2=β~t\sigma_t^2=\tilde\beta_t and found similar results. A score step plus fresh noise is the pattern of Langevin dynamics, here with a target distribution that sharpens as tt falls.
python
@torch.no_grad()
def sample(model, shape):
    x = torch.randn(shape)                                     # x_T ~ N(0, I)
    for t in reversed(range(T)):                               # 0-based: T-1, ..., 0
        eps = model(x, torch.full((shape[0],), t))
        ab = alpha_bar[t]
        x = (x - betas[t] / (1 - ab).sqrt() * eps) / (1 - betas[t]).sqrt()   # the mean
        if t > 0:                                              # no noise on the last step
            var = betas[t] * (1 - alpha_bar[t - 1]) / (1 - ab)             # tilde-beta_t
            x = x + var.sqrt() * torch.randn_like(x)
    return x

One network evaluation per step, T=1000T=1000 in all: the price of DDPM sampling. Generation is inherently sequential, because each step needs the previous step's output.

Deterministic sampling: DDIM and the probability-flow ODE

A thousand evaluations per sample is expensive, and skipping steps of the DDPM chain makes each Gaussian step too crude. DDIM (Song, Meng & Ermon, 2021) starts from an observation about the loss: it involves only the marginals q(xt∣x0)q(\mathbf x_t\mid\mathbf x_0), never the step-to-step transitions. Every forward process with the same marginals, Markov or not, has the same training objective, so one trained network can be sampled by any of them. The deterministic member gives the update

xt′=αˉt′  x^0+1−αˉt′  ϵθ(xt,t),x^0=xt−1−αˉt ϵθ(xt,t)αˉt,t′<t\mathbf x_{t'}=\sqrt{\bar\alpha_{t'}}\;\hat{\mathbf x}_0+\sqrt{1-\bar\alpha_{t'}}\;\boldsymbol\epsilon_{\boldsymbol\theta}(\mathbf x_t,t),\qquad\hat{\mathbf x}_0=\frac{\mathbf x_t-\sqrt{1-\bar\alpha_t}\,\boldsymbol\epsilon_{\boldsymbol\theta}(\mathbf x_t,t)}{\sqrt{\bar\alpha_t}},\qquad t'<t
Denoise to the current guess of the clean image, then re-noise it to an earlier level t′t' with the predicted noise instead of fresh noise. Since t′t' may be many steps before tt, a 1,000-step model can be sampled on a short subsequence of its steps: Song et al. reported quality comparable to 1,000-step sampling within 20 to 100 steps, a 10- to 50-fold speedup.

With no fresh noise, the output is a deterministic function of the starting noise xT\mathbf x_T, and running the map in reverse encodes a real image into noise, which makes interpolation and editing possible. The continuous-time picture explains why this works. Song et al. (2021) showed that as the steps shrink, the DDPM chain becomes, in continuous time t∈[0,1]t\in[0,1], a stochastic differential equation dx=f(x,t) dt+g(t) dwd\mathbf x=\mathbf f(\mathbf x,t)\,dt+g(t)\,d\mathbf w, and that the probability-flow ODE

dxdt=f(x,t)−12 g(t)2 ∇xlog⁡qt(x)\frac{d\mathbf x}{dt}=\mathbf f(\mathbf x,t)-\tfrac12\,g(t)^2\,\nabla_{\mathbf x}\log q_t(\mathbf x)

transports a cloud of points so that it has exactly the SDE's marginal qtq_t at every time. Integrated backward from noise with the learned score, it generates data with no randomness after the first draw. Deterministic DDIM is a first-order discretization of this ODE, and higher-order solvers need fewer steps still (The Design Space of Diffusion Models). Its close relative, flow matching, trains the velocity field of such an ODE directly, by regression onto the velocities of straight lines drawn between noise samples and data samples (Continuous Flows & Flow Matching).

Check your understanding

Why can a network trained with the DDPM loss be sampled with DDIM, whose forward process is different and not even Markov, without retraining?

  1. DDIM fine-tunes the network's last layer on the fly
  2. The loss depends only on the marginals q(xt∣x0)q(\mathbf x_t\mid\mathbf x_0), which DDIM's processes share with DDPM's, so the same network is optimal for all of them
  3. DDIM and DDPM have identical step-to-step transitions
  4. With fewer steps, the network's errors cancel out
Show the answer

B. The loss depends only on the marginals q(xt∣x0)q(\mathbf x_t\mid\mathbf x_0), which DDIM's processes share with DDPM's, so the same network is optimal for all of them

Lsimple\loss_{\text{simple}} samples xt\mathbf x_t directly from q(xt∣x0)q(\mathbf x_t\mid\mathbf x_0) and never touches q(xt∣xt−1)q(\mathbf x_t\mid\mathbf x_{t-1}). Any family with those marginals defines the same training problem, and DDIM picks the member whose reverse process is deterministic and can skip steps.

Backbones, conditioning and latents

Backbones. The denoiser maps an image-shaped input to an image-shaped output, and the standard choice since DDPM has been a U-Net (Ronneberger, Fischer & Brox, 2015): downsampling and upsampling paths joined by skip connections at every resolution, so the output keeps fine detail while the deepest layers see the whole image. DDPM's version stacked residual blocks with group normalization, used self-attention at 16×1616\times16, and added a sinusoidal embedding of tt inside every residual block. Diffusion transformers (DiT; Peebles & Xie, 2023) replace the U-Net with a vision transformer over small patches of a latent image, injecting tt and the class through adaptive layer normalization; their quality improved steadily with compute per forward pass (The Design Space of Diffusion Models).

Conditioning. For a class label, add a learned class embedding to the step embedding, so every block sees both. For text, an encoder (a CLIP or T5 transformer, frozen or trained) turns the prompt cc into MM token vectors τ(c)∈RM×dτ\tau(c)\in\R^{M\times d_\tau}, and cross-attention layers inside the U-Net let every spatial position read from them (Rombach et al., 2022):

CrossAttn⁡(Φ,τ(c))=softmax⁡ ⁣(QK⊤dk)V,Q=ΦWQ,K=τ(c) WK,V=τ(c) WV\operatorname{CrossAttn}\big(\Phi,\tau(c)\big)=\softmax\!\Big(\frac{QK^\top}{\sqrt{d_k}}\Big)V,\qquad Q=\Phi W_Q,\quad K=\tau(c)\,W_K,\quad V=\tau(c)\,W_V
Φ∈RN×dϕ\Phi\in\R^{N\times d_\phi} holds the NN spatial positions of a feature map, flattened. Queries come from the image and keys and values from the text, so each position mixes in the words relevant to it and the output keeps the image's shape. Imagen (Saharia et al., 2022) found that scaling up the frozen text encoder improved results much more than scaling up the image model.

Latent diffusion. Pixels are expensive: a 512×512512\times512 RGB image has 786,432 values, and the network processes all of them at every step. Latent diffusion (Rombach et al., 2022) first trains an autoencoder that shrinks the image 4- to 16-fold along each side (8-fold in Stable Diffusion) into a small latent grid, runs the whole diffusion process on the latents, and decodes once at the end (Latent Spaces for Generative Models designs that autoencoder).

Three systems of 2022 brought text-to-image diffusion into wide use: DALL·E 2 (Ramesh et al.), which generates a CLIP image embedding from the caption and then decodes it to pixels with a diffusion model; Imagen (Saharia et al.), a frozen T5 text encoder driving a 64×6464\times64 pixel-space diffusion model and two diffusion super-resolution stages up to 1024×10241024\times1024; and Stable Diffusion, latent diffusion with publicly released weights.

SamplerNetwork evaluations per sampleWhere
DDPM ancestral sampling1,000, one per training stepHo et al. (2020)
DDIM on a subsequence of steps20–100 for quality comparable to 1,000 DDPM stepsSong, Meng & Ermon (2021)
Heun's second-order ODE solver (EDM)35 on CIFAR-10The Design Space of Diffusion Models
DPM-Solver (exponential integrator)10–20The Design Space of Diffusion Models
Distilled students: progressive distillation, consistency models, adversarial distillation1–4Few-Step Generation
Typical counts from the papers, each on its own benchmarks. Classifier-free guidance doubles every count, and the cost of one evaluation depends on the backbone and on whether it runs on pixels or latents.

Key takeaways

  • The forward process q(xt∣xt−1)=N(1−βt xt−1,βtI)q(\mathbf x_t\mid\mathbf x_{t-1})=\mathcal N(\sqrt{1-\beta_t}\,\mathbf x_{t-1},\beta_tI) has the closed-form marginal xt=αˉt x0+1−αˉt ϵ\mathbf x_t=\sqrt{\bar\alpha_t}\,\mathbf x_0+\sqrt{1-\bar\alpha_t}\,\boldsymbol\epsilon, so training samples any step in one draw; a schedule is read through αˉt\bar\alpha_t or SNR(t)=αˉt/(1−αˉt)\mathrm{SNR}(t)=\bar\alpha_t/(1-\bar\alpha_t).
  • The network regresses the added noise, ∥ϵ−ϵθ(xt,t)∥2\norm{\boldsymbol\epsilon-\boldsymbol\epsilon_{\boldsymbol\theta}(\mathbf x_t,t)}^2; its optimum is −1−αˉt ∇log⁡qt-\sqrt{1-\bar\alpha_t}\,\nabla\log q_t, so denoising at many noise levels is score estimation at many noise levels.
  • DDPM samples with TT small Gaussian reverse steps, accurate only because each step is small; DDIM reuses the same network on a deterministic, step-skipping process that discretizes the probability-flow ODE.
  • Class labels join the step embedding, text enters through cross-attention, and classifier-free guidance extrapolates from the unconditional prediction toward the conditional one at two evaluations per step.
  • Latent diffusion runs the whole process in an autoencoder's latent space; U-Nets and diffusion transformers are the two standard backbones.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

At some step αˉt=0.25\bar\alpha_t=0.25. What is the distribution of xt\mathbf x_t given x0\mathbf x_0?

  1. N(0.25 x0, 0.75 I)\mathcal N(0.25\,\mathbf x_0,\ 0.75\,I)
  2. N(0.5 x0, 0.75 I)\mathcal N(0.5\,\mathbf x_0,\ 0.75\,I)
  3. N(0.5 x0, 0.866 I)\mathcal N(0.5\,\mathbf x_0,\ 0.866\,I)
  4. N(0.25 x0, 0.25 I)\mathcal N(0.25\,\mathbf x_0,\ 0.25\,I)
Show the answer

B. N(0.5 x0, 0.75 I)\mathcal N(0.5\,\mathbf x_0,\ 0.75\,I)

xt=αˉt x0+1−αˉt ϵ\mathbf x_t=\sqrt{\bar\alpha_t}\,\mathbf x_0+\sqrt{1-\bar\alpha_t}\,\boldsymbol\epsilon, so the mean is 0.25 x0=0.5 x0\sqrt{0.25}\,\mathbf x_0=0.5\,\mathbf x_0 and the covariance is (1−0.25)I=0.75 I(1-0.25)I=0.75\,I. The noise's standard deviation is 0.75≈0.866\sqrt{0.75}\approx0.866.

Question 2

Over all functions, what does the noise-prediction loss's minimizer ϵ∗(xt,t)\boldsymbol\epsilon^*(\mathbf x_t,t) equal?

  1. +1−αˉt ∇xlog⁡qt(xt)+\sqrt{1-\bar\alpha_t}\,\nabla_{\mathbf x}\log q_t(\mathbf x_t)
  2. The exact noise ϵ\boldsymbol\epsilon that was added to x0\mathbf x_0
  3. −∇xlog⁡pdata(xt)-\nabla_{\mathbf x}\log p_{\text{data}}(\mathbf x_t)
  4. −1−αˉt ∇xlog⁡qt(xt)-\sqrt{1-\bar\alpha_t}\,\nabla_{\mathbf x}\log q_t(\mathbf x_t), where qtq_t is the distribution of noised data at step tt
Show the answer

D. −1−αˉt ∇xlog⁡qt(xt)-\sqrt{1-\bar\alpha_t}\,\nabla_{\mathbf x}\log q_t(\mathbf x_t), where qtq_t is the distribution of noised data at step tt

The minimizer is E[ϵ∣xt]\E[\boldsymbol\epsilon\mid\mathbf x_t], and the score of the noised marginal is the posterior average of −ϵ/1−αˉt-\boldsymbol\epsilon/\sqrt{1-\bar\alpha_t}. The noise points away from the data, so predicting it means pointing down the density: hence the minus sign.

Question 3

You train with the same loss but a schedule of only T=10T=10 large steps, and sample with DDPM's Gaussian reverse steps. What goes wrong?

  1. Training fails, because the loss is undefined for large βt\beta_t
  2. Each true reverse transition q(xt−1∣xt)q(\mathbf x_{t-1}\mid\mathbf x_t) is now broad and often multimodal, so a single Gaussian step with a learned mean approximates it badly and samples land between modes
  3. Nothing: ten steps give the same samples ten to a hundred times faster
  4. αˉT\bar\alpha_T can no longer approach zero, so sampling cannot start from noise
Show the answer

B. Each true reverse transition q(xt−1∣xt)q(\mathbf x_{t-1}\mid\mathbf x_t) is now broad and often multimodal, so a single Gaussian step with a learned mean approximates it badly and samples land between modes

Gaussian reverse steps are accurate only when each forward step adds a little noise. With big steps the true reverse conditional must choose among very different cleaner images, which a Gaussian centered at their average cannot represent.

Question 4

In a text-conditioned U-Net's cross-attention layer, where do the queries, keys and values come from?

  1. Queries from the text tokens; keys and values from the image features
  2. Queries, keys and values all from the image features
  3. Queries from the image features; keys and values from the text encoder's token vectors
  4. Queries, keys and values all from the text tokens
Show the answer

C. Queries from the image features; keys and values from the text encoder's token vectors

Each spatial position of the feature map asks a question (its query) and reads from the prompt's tokens (their keys and values), so the output at every position is a mixture of word information relevant to it, and it keeps the image's shape.

Go deeper

Further reading

  • J. Ho, A. Jain & P. Abbeel, "Denoising diffusion probabilistic models," NeurIPS (2020).
  • Y. Song, J. Sohl-Dickstein, D. P. Kingma, A. Kumar, S. Ermon & B. Poole, "Score-based generative modeling through stochastic differential equations," ICLR (2021).
  • R. Rombach, A. Blattmann, D. Lorenz, P. Esser & B. Ommer, "High-resolution image synthesis with latent diffusion models," CVPR (2022).
  • S. J. D. Prince, Understanding Deep Learning, ch. 18 "Diffusion models" (MIT Press, 2023).