Denoising Diffusion Implicit Models: Accelerated Sampling

3340 words
17 min read
Python Week 1: the first filter for runtime behavior
Visual companion
Python
Type and operator map

Python Week 1: the first filter for runtime behavior

View
Revision summary

What this note is really saying

Short form

# Denoising Diffusion Implicit Models: Accelerated Sampling ## ๐ŸŽฏ Learning Objectives - Understand the limitations of DDPM sampling speed and how DDIM addresses them - Derive the non-Markovian forward process that makes DDIM work - Implement accelerated sampling with arbitrary step sizes - Use DDIM for deterministic...

Denoising Diffusion Implicit Models: Accelerated Sampling

๐ŸŽฏ Learning Objectives

  • Understand the limitations of DDPM sampling speed and how DDIM addresses them
  • Derive the non-Markovian forward process that makes DDIM work
  • Implement accelerated sampling with arbitrary step sizes
  • Use DDIM for deterministic encoding (inversion) of real images
  • Compare DDIM's latent space properties with DDPM

๐Ÿ“‹ Prerequisites

  • Diffusion Models (DDPM) (Week 4): Forward/reverse processes, noise scheduling
  • Probability basics: Conditional distributions, Markov chains
  • Linear algebra: Matrix operations, eigendecomposition

1. ๐Ÿ“– Core Content

1.1 Intuition: Why DDIM?

DDPM generates high-quality images but requires 1000 sequential denoising steps to produce a single sample. Each step runs the full U-Net. This makes sampling:
  • Slow: A single 1024ร—1024 image can take 10-60 seconds on a high-end GPU
  • Impractical for real-time applications: Chat-based image generation, interactive editing
  • Energy inefficient: 1000 forward passes through a large neural network DDIM asks: Do we really need all 1000 steps? Can we skip most of them? The key insight: DDPM's forward process is Markovian (each step depends only on the previous step). But the reverse process doesn't need to be Markovian too! By designing a non-Markovian forward process that has the same marginal distributions as DDPM, we can use a shortened reverse sampling path โ€” e.g., 50 steps instead of 1000. Even more surprisingly, DDIM makes the reverse process deterministic (no random noise during sampling), which means:
  1. The mapping from noise zz to image x0x_0 is one-to-one (injective)
  2. We can invert the process: encode a real image back into its latent noise vector
  3. Latent space becomes smooth for interpolation

1.2 Review: DDPM's Markovian Forward Process

In DDPM, the forward process adds Gaussian noise step by step:
q(xtโˆฃxtโˆ’1)=N(xt;1โˆ’ฮฒtxtโˆ’1,ฮฒtI)q(x_t | x_{t-1}) = \mathcal{N}(x_t; \sqrt{1-\beta_t} x_{t-1}, \beta_t I)
Because it's Markovian, we can also write the marginal distribution at any timestep directly:
q(xtโˆฃx0)=N(xt;ฮฑห‰tx0,(1โˆ’ฮฑห‰t)I)q(x_t | x_0) = \mathcal{N}(x_t; \sqrt{\bar{\alpha}_t} x_0, (1-\bar{\alpha}_t)I)
where ฮฑห‰t=โˆi=1t(1โˆ’ฮฒi)\bar{\alpha}_t = \prod_{i=1}^t (1-\beta_i). The reverse process in DDPM is also Markovian:
pฮธ(xtโˆ’1โˆฃxt)=N(xtโˆ’1;ฮผฮธ(xt,t),ฯƒt2I)p_\theta(x_{t-1} | x_t) = \mathcal{N}(x_{t-1}; \mu_\theta(x_t, t), \sigma_t^2 I)
And sampling requires iterating through every timestep T,Tโˆ’1,...,1T, T-1, ..., 1.

1.3 The DDIM Non-Markovian Forward Process

DDIM defines a different forward process that has the same marginal distributions as DDPM but is no longer Markovian. Key idea: Instead of q(xtโˆฃxtโˆ’1)q(x_t | x_{t-1}), define q(xtโˆ’1โˆฃxt,x0)q(x_{t-1} | x_t, x_0) directly. From DDPM's joint distribution, we can derive (using Bayes rule) the reverse conditional distribution given x0x_0:
q(xtโˆ’1โˆฃxt,x0)=N(xtโˆ’1;ฮผ~t(xt,x0),ฮฒ~tI)q(x_{t-1} | x_t, x_0) = \mathcal{N}\left(x_{t-1}; \tilde{\mu}_t(x_t, x_0), \tilde{\beta}_t I\right)
where:
ฮผ~t(xt,x0)=ฮฑห‰tโˆ’1ฮฒt1โˆ’ฮฑห‰tx0+1โˆ’ฮฒt(1โˆ’ฮฑห‰tโˆ’1)1โˆ’ฮฑห‰txt\tilde{\mu}_t(x_t, x_0) = \frac{\sqrt{\bar{\alpha}_{t-1}} \beta_t}{1-\bar{\alpha}_t} x_0 + \frac{\sqrt{1-\beta_t}(1-\bar{\alpha}_{t-1})}{1-\bar{\alpha}_t} x_t ฮฒ~t=1โˆ’ฮฑห‰tโˆ’11โˆ’ฮฑห‰tฮฒt\tilde{\beta}_t = \frac{1-\bar{\alpha}_{t-1}}{1-\bar{\alpha}_t} \beta_t
(These come from the standard DDPM derivation โ€” we derived them in Week 4.) DDIM's innovation: Generalize this distribution by introducing a parameter ฯƒโ‰ฅ0\sigma \geq 0 that controls the stochasticity of the reverse step:
qฯƒ(xtโˆ’1โˆฃxt,x0)=N(xtโˆ’1;ฮฑห‰tโˆ’1x0+1โˆ’ฮฑห‰tโˆ’1โˆ’ฯƒt2โ‹…xtโˆ’ฮฑห‰tx01โˆ’ฮฑห‰t,ฯƒt2I)q_\sigma(x_{t-1} | x_t, x_0) = \mathcal{N}\left(x_{t-1}; \sqrt{\bar{\alpha}_{t-1}} x_0 + \sqrt{1-\bar{\alpha}_{t-1} - \sigma_t^2} \cdot \frac{x_t - \sqrt{\bar{\alpha}_t} x_0}{\sqrt{1-\bar{\alpha}_t}}, \sigma_t^2 I\right)
When ฯƒt2=ฮฒ~t\sigma_t^2 = \tilde{\beta}_t, this reduces to DDPM. When ฯƒt=0\sigma_t = 0, this becomes DDIM โ€” a fully deterministic reverse process.

1.4 The DDIM Sampling Equation

For DDIM (ฯƒt=0\sigma_t = 0), the reverse step simplifies to:
xtโˆ’1=ฮฑห‰tโˆ’1โ‹…(xtโˆ’1โˆ’ฮฑห‰tฯตฮธ(t)(xt)ฮฑห‰t)โŸpredictedย x^0+1โˆ’ฮฑห‰tโˆ’1โ‹…ฯตฮธ(t)(xt)x_{t-1} = \sqrt{\bar{\alpha}_{t-1}} \cdot \underbrace{\left(\frac{x_t - \sqrt{1-\bar{\alpha}_t} \epsilon_\theta^{(t)}(x_t)}{\sqrt{\bar{\alpha}_t}}\right)}_{\text{predicted } \hat{x}_0} + \sqrt{1-\bar{\alpha}_{t-1}} \cdot \epsilon_\theta^{(t)}(x_t)
Wait, that's not fully simplified. Let me derive it properly. DDIM reverse step derivation: We have the predicted x0x_0 from the current xtx_t and predicted noise ฯตฮธ\epsilon_\theta:
x^0=xtโˆ’1โˆ’ฮฑห‰tฯตฮธ(xt,t)ฮฑห‰t\hat{x}_0 = \frac{x_t - \sqrt{1-\bar{\alpha}_t} \epsilon_\theta(x_t, t)}{\sqrt{\bar{\alpha}_t}}
The DDIM sampling equation (Song et al., 2021) is:
xtโˆ’1=ฮฑห‰tโˆ’1x^0+1โˆ’ฮฑห‰tโˆ’1ฯตฮธ(xt,t)x_{t-1} = \sqrt{\bar{\alpha}_{t-1}} \hat{x}_0 + \sqrt{1-\bar{\alpha}_{t-1}} \epsilon_\theta(x_t, t)
Or equivalently:
xtโˆ’1=ฮฑห‰tโˆ’1โ‹…xtโˆ’1โˆ’ฮฑห‰tฯตฮธ(xt,t)ฮฑห‰t+1โˆ’ฮฑห‰tโˆ’1ฯตฮธ(xt,t)x_{t-1} = \sqrt{\bar{\alpha}_{t-1}} \cdot \frac{x_t - \sqrt{1-\bar{\alpha}_t} \epsilon_\theta(x_t, t)}{\sqrt{\bar{\alpha}_t}} + \sqrt{1-\bar{\alpha}_{t-1}} \epsilon_\theta(x_t, t)
Notice: There is NO noise term. The process is deterministic given xtx_t and ฯตฮธ\epsilon_\theta.

1.5 Accelerated Sampling: Skipping Steps

For DDPM, we must sample xT,xTโˆ’1,...,x1,x0x_T, x_{T-1}, ..., x_1, x_0 โ€” all TT steps. For DDIM, we can sample a subsequence {xฯ„S,xฯ„Sโˆ’1,...,xฯ„1,x0}\{x_{\tau_S}, x_{\tau_{S-1}}, ..., x_{\tau_1}, x_0\} where Sโ‰ชTS \ll T and ฯ„\tau is an increasing subsequence of [1,...,T][1, ..., T]. For example, with T=1000T = 1000 and S=50S = 50, we use steps ฯ„=[1,20,40,...,980,1000]\tau = [1, 20, 40, ..., 980, 1000] โ€” only 50 steps. The sampling equation for a subsequence step xฯ„iโˆ’1x_{\tau_{i-1}} from xฯ„ix_{\tau_i}:
xฯ„iโˆ’1=ฮฑห‰ฯ„iโˆ’1โ‹…xฯ„iโˆ’1โˆ’ฮฑห‰ฯ„iฯตฮธ(xฯ„i,ฯ„i)ฮฑห‰ฯ„i+1โˆ’ฮฑห‰ฯ„iโˆ’1ฯตฮธ(xฯ„i,ฯ„i)x_{\tau_{i-1}} = \sqrt{\bar{\alpha}_{\tau_{i-1}}} \cdot \frac{x_{\tau_i} - \sqrt{1-\bar{\alpha}_{\tau_i}} \epsilon_\theta(x_{\tau_i}, \tau_i)}{\sqrt{\bar{\alpha}_{\tau_i}}} + \sqrt{1-\bar{\alpha}_{\tau_{i-1}}} \epsilon_\theta(x_{\tau_i}, \tau_i)
The model ฯตฮธ\epsilon_\theta was trained on the original 1000-step schedule, but we can evaluate it at any timestep ฯ„i\tau_i because the noise level ฮฑห‰ฯ„i\bar{\alpha}_{\tau_i} is well-defined.

Worked Example 1: DDIM Sampling with 10 Steps

Suppose we have a trained DDPM with T=1000T=1000 and ฮฑห‰t=cosโก2(tTโ‹…ฯ€2)\bar{\alpha}_t = \cos^2\left(\frac{t}{T} \cdot \frac{\pi}{2}\right) (a cosine schedule). We want to sample using only 10 DDIM steps. Step 1: Choose the subsequence. A common choice is uniformly spaced:
ฯ„i=โŒŠiSโ‹…TโŒ‹forย i=1,...,S\tau_i = \left\lfloor \frac{i}{S} \cdot T \right\rfloor \quad \text{for } i = 1, ..., S
With S=10S=10, T=1000T=1000: ฯ„=[100,200,300,...,1000]\tau = [100, 200, 300, ..., 1000]. Step 2: Start with pure noise x1000โˆผN(0,I)x_{1000} \sim \mathcal{N}(0, I). Step 3: For i=10,9,...,1i = 10, 9, ..., 1 (i.e., t=1000,900,...,100t = 1000, 900, ..., 100):
xtโˆ’100=ฮฑห‰tโˆ’100โ‹…xtโˆ’1โˆ’ฮฑห‰tฯตฮธ(xt,t)ฮฑห‰t+1โˆ’ฮฑห‰tโˆ’100ฯตฮธ(xt,t)x_{t-100} = \sqrt{\bar{\alpha}_{t-100}} \cdot \frac{x_t - \sqrt{1-\bar{\alpha}_t} \epsilon_\theta(x_t, t)}{\sqrt{\bar{\alpha}_t}} + \sqrt{1-\bar{\alpha}_{t-100}} \epsilon_\theta(x_t, t)
Step 4: After 10 steps, x0x_0 is the generated image.

Worked Example 2: Comparing DDPM (1000 steps) vs DDIM (50 steps) Quality

Let's trace the reconstruction error at each step. For a given x0x_0, the forward process in DDPM gives:
xt=ฮฑห‰tx0+1โˆ’ฮฑห‰tฯต,ฯตโˆผN(0,I)x_t = \sqrt{\bar{\alpha}_t} x_0 + \sqrt{1-\bar{\alpha}_t} \epsilon, \quad \epsilon \sim \mathcal{N}(0, I)
In DDPM reverse, at step tt we predict ฯตฮธ(xt,t)\epsilon_\theta(x_t, t) and sample:
xtโˆ’1=1ฮฑt(xtโˆ’ฮฒt1โˆ’ฮฑห‰tฯตฮธ(xt,t))+ฯƒtz,zโˆผN(0,I)x_{t-1} = \frac{1}{\sqrt{\alpha_t}} \left(x_t - \frac{\beta_t}{\sqrt{1-\bar{\alpha}_t}} \epsilon_\theta(x_t, t)\right) + \sigma_t z, \quad z \sim \mathcal{N}(0, I)
The stochastic term ฯƒtz\sigma_t z means even with a perfect noise predictor (ฯตฮธ=ฯต\epsilon_\theta = \epsilon), DDPM's reverse is not purely deterministic โ€” the noise zz accumulates. In DDIM with ฯƒt=0\sigma_t = 0, the reverse step has no noise. If ฯตฮธ\epsilon_\theta perfectly predicts the noise, then:
xtโˆ’1=ฮฑห‰tโˆ’1x0+1โˆ’ฮฑห‰tโˆ’1ฯตx_{t-1} = \sqrt{\bar{\alpha}_{t-1}} x_0 + \sqrt{1-\bar{\alpha}_{t-1}} \epsilon
which is exactly the forward process equation at timestep tโˆ’1t-1. This means DDIM perfectly inverts the forward process in a deterministic manner.

1.6 Deterministic Inversion (Encoding)

Because DDIM is deterministic, we can invert the sampling process. Starting from a real image x0x_0, we can find the latent noise vector xTx_T that would generate it. Forward DDIM inversion:
xt+1=ฮฑห‰t+1โ‹…xtโˆ’1โˆ’ฮฑห‰tฯตฮธ(xt,t)ฮฑห‰t+1โˆ’ฮฑห‰t+1ฯตฮธ(xt,t)x_{t+1} = \sqrt{\bar{\alpha}_{t+1}} \cdot \frac{x_t - \sqrt{1-\bar{\alpha}_t} \epsilon_\theta(x_t, t)}{\sqrt{\bar{\alpha}_t}} + \sqrt{1-\bar{\alpha}_{t+1}} \epsilon_\theta(x_t, t)
This is the reverse of the sampling equation. We start from x0x_0 and iterate t=0,1,...,Tโˆ’1t = 0, 1, ..., T-1 to obtain xTx_T. Important caveat: This inversion is approximate because ฯตฮธ\epsilon_\theta isn't perfect. However, it works well enough for real applications.

Why Inversion Matters

  1. Image editing: Encode an image, edit the latent, decode back
  2. Style mixing: Combine latents from different images
  3. Interpolation: Smoothly morph between two images by interpolating their latents

1.7 DDIM vs DDPM: Comparison

PropertyDDPMDDIM
Forward processMarkovianNon-Markovian
Reverse processStochasticDeterministic ( ฯƒ=0\sigma=0 )
Sampling steps requiredAll TT (e.g., 1000)Any Sโ‰ชTS \ll T (e.g., 10-100)
Sample quality at few stepsPoorGood
Sample quality at full stepsExcellentComparable (slightly worse)
Deterministic encodingNo (stochastic)Yes (invertible)
Latent space structureNon-smoothSmooth for interpolation
Training changesNone (uses DDPM training)None (uses DDPM training)

1.8 Edge Cases & Gotchas

  • Very few steps (1-5): DDIM quality degrades significantly. At 1 step, it just denoises once but doesn't resolve details well.
  • Optimal step count: Empirically, 20-50 DDIM steps give comparable quality to 1000 DDPM steps for most datasets.
  • Noise scheduling matters: The original schedule (linear, cosine, sigmoid) affects DDIM performance. Cosine schedules generally work better with DDIM.
  • Deterministic inversion accumulates error: Long inversion chains (e.g., 1000 steps) can drift from the true path.
  • CFG (Classifier-Free Guidance) interaction: DDIM's determinism breaks with CFG scaling > 1, but accelerated sampling still works.

1.9 Why This Matters

DDIM is used in virtually every modern diffusion-based system:
  • Stable Diffusion uses DDIM schedulers for fast sampling (typically 20-50 steps)
  • Image-to-image translation relies on DDIM inversion
  • Latent space manipulation (e.g., finding directions for specific attributes) works because DDIM provides a smooth latent space
  • Video generation benefits from DDIM's consistency across frames when using shared noise

2. ๐Ÿ“ Key Formulas / Concepts

ConceptFormulaDescription
DDIM reverse stepxtโˆ’1=ฮฑห‰tโˆ’1x^0+1โˆ’ฮฑห‰tโˆ’1ฯตฮธ(xt,t)x_{t-1} = \sqrt{\bar{\alpha}_{t-1}} \hat{x}_0 + \sqrt{1-\bar{\alpha}_{t-1}} \epsilon_\theta(x_t, t)Deterministic denoising
Predicted x0x_0x^0=xtโˆ’1โˆ’ฮฑห‰tฯตฮธ(xt,t)ฮฑห‰t\hat{x}_0 = \frac{x_t - \sqrt{1-\bar{\alpha}_t} \epsilon_\theta(x_t, t)}{\sqrt{\bar{\alpha}_t}}Denoised estimate from current step
DDIM general$q_\sigma(x_{t-1}\x_t,x_0)withwith\sigma \geq 0$
Accelerated scheduleฯ„=subsequenceย ofย [1,...,T]\tau = \text{subsequence of } [1,...,T]Sample only Sโ‰ชTS \ll T steps
DDIM inversionxt+1=ฮฑห‰t+1x^0+1โˆ’ฮฑห‰t+1ฯตฮธ(xt,t)x_{t+1} = \sqrt{\bar{\alpha}_{t+1}} \hat{x}_0 + \sqrt{1-\bar{\alpha}_{t+1}} \epsilon_\theta(x_t, t)Encoding real images to latent

3. โš ๏ธ Common Pitfalls

Pitfall 1: Using DDPM Training but DDIM Sampling Without Adjusting the Noise Schedule

Mistake: Training a model with a specific noise schedule (e.g., linear ฮฒt\beta_t) and then using DDIM with a different schedule without re-normalizing. Why: DDIM assumes the noise schedule ฮฑห‰t\bar{\alpha}_t matches what the model was trained on. Correct approach: DDIM sampling uses the noise predictor ฯตฮธ\epsilon_\theta trained with DDPM's schedule. You don't change the schedule โ€” you just skip intermediate timesteps during sampling. The model was trained on all timesteps, so evaluating it at a subset works.

Pitfall 2: Expecting DDIM to Replace All DDPM Sampling

Mistake: Assuming DDIM is strictly better and should always be used. Why: At full step count (1000 steps), DDPM often produces slightly higher quality samples because the stochasticity helps correct accumulation errors. Correct approach: Use DDIM for speed (10-50 steps), use DDPM when quality is paramount and speed doesn't matter.

Pitfall 3: Assuming DDIM Inversion Is Perfect

Mistake: Encoding an image with DDIM inversion and expecting to reconstruct it exactly. Why: The inversion relies on the model's ฯตฮธ\epsilon_\theta being the optimal denoiser at every step, which it isn't. Small errors accumulate. Correct approach: Use the inverted latent as a starting point, accept minor reconstruction artifacts, and consider regularizing the inversion (e.g., adding noise to improve reconstruction).

4. ๐Ÿ“ Practice Questions

Q1: If DDPM requires 1000 steps and DDIM achieves comparable quality with 50 steps, what's the speedup factor?
The speedup appears to be 1000/50=20ร—1000/50 = 20\times in terms of neural network evaluations. However, DDIM's per-step computation is identical to DDPM's (both evaluate ฯตฮธ\epsilon_\theta once per step), so the actual speedup is indeed 20ร—20\times.
In practice, the speedup may be slightly less due to overhead (data transfers between CPU/GPU), but it's approximately 20ร—20\times faster. Q2: Show that when ฯƒt=ฮฒ~t\sigma_t = \sqrt{\tilde{\beta}_t}, the DDIM general process reduces to DDPM.
The general DDIM reverse distribution is:
qฯƒ(xtโˆ’1โˆฃxt,x0)=N(xtโˆ’1;ฮฑห‰tโˆ’1x0+1โˆ’ฮฑห‰tโˆ’1โˆ’ฯƒt2โ‹…xtโˆ’ฮฑห‰tx01โˆ’ฮฑห‰t,ฯƒt2I)q_\sigma(x_{t-1} | x_t, x_0) = \mathcal{N}\left(x_{t-1}; \sqrt{\bar{\alpha}_{t-1}} x_0 + \sqrt{1-\bar{\alpha}_{t-1} - \sigma_t^2} \cdot \frac{x_t - \sqrt{\bar{\alpha}_t} x_0}{\sqrt{1-\bar{\alpha}_t}}, \sigma_t^2 I\right)
When ฯƒt2=ฮฒ~t=1โˆ’ฮฑห‰tโˆ’11โˆ’ฮฑห‰tฮฒt\sigma_t^2 = \tilde{\beta}_t = \frac{1-\bar{\alpha}_{t-1}}{1-\bar{\alpha}_t} \beta_t:
The mean becomes:
ฮผ=ฮฑห‰tโˆ’1x0+1โˆ’ฮฑห‰tโˆ’1โˆ’1โˆ’ฮฑห‰tโˆ’11โˆ’ฮฑห‰tฮฒtโ‹…xtโˆ’ฮฑห‰tx01โˆ’ฮฑห‰t\mu = \sqrt{\bar{\alpha}_{t-1}} x_0 + \sqrt{1-\bar{\alpha}_{t-1} - \frac{1-\bar{\alpha}_{t-1}}{1-\bar{\alpha}_t} \beta_t} \cdot \frac{x_t - \sqrt{\bar{\alpha}_t} x_0}{\sqrt{1-\bar{\alpha}_t}}
Using ฮฒt=1โˆ’ฮฑt=1โˆ’ฮฑห‰tฮฑห‰tโˆ’1\beta_t = 1 - \alpha_t = 1 - \frac{\bar{\alpha}_t}{\bar{\alpha}_{t-1}}:
After simplification (shown in DDPM derivation), this becomes:
ฮผ~t(xt,x0)=ฮฑห‰tโˆ’1ฮฒt1โˆ’ฮฑห‰tx0+ฮฑt(1โˆ’ฮฑห‰tโˆ’1)1โˆ’ฮฑห‰txt\tilde{\mu}_t(x_t, x_0) = \frac{\sqrt{\bar{\alpha}_{t-1}} \beta_t}{1-\bar{\alpha}_t} x_0 + \frac{\sqrt{\alpha_t}(1-\bar{\alpha}_{t-1})}{1-\bar{\alpha}_t} x_t
which is exactly the DDPM posterior mean. And the variance ฯƒt2I=ฮฒ~tI\sigma_t^2 I = \tilde{\beta}_t I is DDPM's posterior variance. Q3: For DDIM, if you start from the same noise vector xTx_T and run sampling twice, do you get the same image?
Yes โ€” DDIM is deterministic. Given the same xTx_T and the same trained model ฯตฮธ\epsilon_\theta, the sequence of xTโˆ’1,xTโˆ’2,...,x0x_{T-1}, x_{T-2}, ..., x_0 is fully determined. There's no stochastic term in the reverse step.
This is fundamentally different from DDPM, where each sample path has random noise injections and would produce different images from the same starting xTx_T.
This determinism is what enables DDIM inversion and interpolation applications. Q4: You want to generate 16 images for a storyboard. You have a batch of 16 noise vectors. With DDPM you need 16 ร— 1000 = 16000 model evaluations. How many with DDIM using 50 steps?
16 ร— 50 = 800 model evaluations. That's a 20ร— reduction.
However, note that modern implementations process the entire batch in parallel (batch size = 16), so it's actually 50 batch evaluations vs 1000 batch evaluations. The wall-clock speedup is still approximately 20ร—. Q5: Why can't DDPM be used for deterministic image encoding?
DDPM's reverse process includes a stochastic noise term at each step:
xtโˆ’1=1ฮฑt(xtโˆ’ฮฒt1โˆ’ฮฑห‰tฯตฮธ(xt,t))+ฯƒtzx_{t-1} = \frac{1}{\sqrt{\alpha_t}} \left(x_t - \frac{\beta_t}{\sqrt{1-\bar{\alpha}_t}} \epsilon_\theta(x_t, t)\right) + \sigma_t z
Even if we know every ฯตฮธ(xt,t)\epsilon_\theta(x_t, t) exactly, the random zโˆผN(0,I)z \sim \mathcal{N}(0, I) at each step means:
  1. Starting from the same x0x_0, the forward process gives a distribution over xTx_T (not a unique xTx_T)
  2. Starting from xTx_T, the reverse gives a distribution over x0x_0
There's no deterministic mapping between x0x_0 and xTx_T in either direction. DDIM removes the stochasticity, creating a bijection. Q6: For DDIM with T=1000T=1000 and S=20S=20, propose a sampling schedule ฯ„\tau and explain why you chose it.
A common choice is:
ฯ„i=โŒŠi2S2โ‹…TโŒ‹forย i=1,...,S\tau_i = \left\lfloor \frac{i^2}{S^2} \cdot T \right\rfloor \quad \text{for } i = 1, ..., S
This is a quadratic schedule (compared to uniform). The reasoning:
  • Early steps (high noise, near TT): Larger changes happen, so we want more steps when noise is high
  • Late steps (low noise, near 0): Finer details are adjusted, but changes are smaller, so we can take larger jumps
With S=20S=20, the schedule might be: ฯ„=[1,3,6,10,15,21,28,36,45,55,66,78,91,105,120,136,153,171,190,210,231,253,276,300,325,351,378,406,435,465,496,528,561,595,630,666,703,741,780,820,861,903,946,990,1000]\tau = [1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 276, 300, 325, 351, 378, 406, 435, 465, 496, 528, 561, 595, 630, 666, 703, 741, 780, 820, 861, 903, 946, 990, 1000]
Wait, that's more than 20. Let me adjust: the quadratic spacing places more steps at the end.
Actually, a simpler and commonly used approach is linear spacing:
ฯ„i=โŒŠiSโ‹…TโŒ‹ย forย i=1,...,S\tau_i = \left\lfloor \frac{i}{S} \cdot T \right\rfloor \text{ for } i = 1, ..., S
With S=20S=20, T=1000T=1000: ฯ„=[50,100,150,200,250,300,350,400,450,500,550,600,650,700,750,800,850,900,950,1000]\tau = [50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950, 1000].
This is simple and works well in practice. Q7: Explain the relationship between DDIM and neural ODEs.
DDIM's deterministic reverse process can be viewed as an Euler discretization of a neural ordinary differential equation (ODE).
In the continuous-time limit (as the number of steps Tโ†’โˆžT \to \infty), DDIM's iterative equation becomes:
dxdt=fฮธ(x,t)โ‹…โˆ‡xlogโกpt(x)\frac{dx}{dt} = f_\theta(x, t) \cdot \nabla_x \log p_t(x)
where fฮธf_\theta is derived from the noise prediction network.
This connection is important because:
  1. It justifies using ODE solvers (RK45, DOPRI) for even faster sampling
  2. It enables likelihood computation via the probability flow ODE
  3. It connects diffusion models to score-based generative modeling and normalizing flows
The DDIM ODE perspective is explored further in the Score-Based Generative Modeling literature (Song et al., 2021). Q8: A student claims "DDIM is just DDPM with fewer training steps." Is this correct? Why or why not?
Not correct. DDIM and DDPM have different claims:
  • DDPM training: Trains ฯตฮธ\epsilon_\theta on TT timesteps with a Markovian forward process
  • DDIM sampling: Uses the same trained model ฯตฮธ\epsilon_\theta but with a different (non-Markovian) reverse process
DDIM does not change training at all. It's purely a sampling-time modification. The model is still trained with the DDPM objective:
L=Et,x0,ฯต[โˆฅฯตโˆ’ฯตฮธ(xt,t)โˆฅ2]\mathcal{L} = \mathbb{E}_{t, x_0, \epsilon} \left[ \|\epsilon - \epsilon_\theta(x_t, t)\|^2 \right]
"Fewer training steps" refers to something different (training the model for fewer iterations), which would make the model worse regardless of the sampling method. Q9: You have a real image x0x_0 and you run DDIM inversion to get xTx_T. Then you run DDIM sampling from xTx_T back to x0โ€ฒx_0'. Under what conditions would x0=x0โ€ฒx_0 = x_0'?
Perfect reconstruction (x0=x0โ€ฒx_0 = x_0') would require:
  1. Perfect denoiser: ฯตฮธ(xt,t)=ฯต\epsilon_\theta(x_t, t) = \epsilon for all tt, where ฯต\epsilon is the actual noise added at step tt
  2. Consistent inversion path: The inversion path from x0x_0 to xTx_T must use the same steps as the generation path from xTx_T to x0x_0
  3. No numerical error: Floating-point precision doesn't cause drift
In practice, none of these hold perfectly:
  • ฯตฮธ\epsilon_\theta is an imperfect estimator
  • There's discretization error from the finite step count
  • Floating-point errors accumulate
However, DDIM reconstruction is remarkably good โ€” with 1000 steps, pixel-wise reconstruction error is typically < 5% for natural images, and with 10000 steps it's nearly perfect (for models that can handle the extended schedule). Q10: Design an experiment to verify DDIM's latent space smoothness.
Experiment: Image Interpolation in DDIM Latent Space
Step 1: Take two real images x0(A)x_0^{(A)} and x0(B)x_0^{(B)}.
Step 2: Encode both to latents xT(A)x_T^{(A)} and xT(B)x_T^{(B)} using DDIM inversion.
Step 3: For ฮฑ\alpha in {0,0.1,0.2,...,1.0}\{0, 0.1, 0.2, ..., 1.0\}, compute interpolated latent:
xT(ฮฑ)=ฮฑโ‹…xT(A)+(1โˆ’ฮฑ)โ‹…xT(B)x_T^{(\alpha)} = \alpha \cdot x_T^{(A)} + (1-\alpha) \cdot x_T^{(B)}
Step 4: Decode each xT(ฮฑ)x_T^{(\alpha)} with DDIM sampling to get x0(ฮฑ)x_0^{(\alpha)}.
Expected result: For ฮฑ=0\alpha = 0, x0(0)โ‰ˆx0(B)x_0^{(0)} \approx x_0^{(B)}. For ฮฑ=1\alpha = 1, x0(1)โ‰ˆx0(A)x_0^{(1)} \approx x_0^{(A)}. For intermediate ฮฑ\alpha, x0(ฮฑ)x_0^{(\alpha)} should be a smooth semantic blend โ€” e.g., if AA is a photo of a cat and BB is a photo of a dog, intermediate values morph continuously between cat and dog.
This would not work with DDPM latents because the stochastic reverse process destroys the linear interpolation structure. Q11: With DDIM, explain why using 1 sampling step (S=1S=1) gives a poor result.
With S=1S=1, ฯ„=[1000]\tau = [1000], so we jump directly from t=1000t=1000 to t=0t=0:
x0=ฮฑห‰0โ‹…xTโˆ’1โˆ’ฮฑห‰Tฯตฮธ(xT,T)ฮฑห‰T+1โˆ’ฮฑห‰0ฯตฮธ(xT,T)x_0 = \sqrt{\bar{\alpha}_0} \cdot \frac{x_T - \sqrt{1-\bar{\alpha}_T} \epsilon_\theta(x_T, T)}{\sqrt{\bar{\alpha}_T}} + \sqrt{1-\bar{\alpha}_0} \epsilon_\theta(x_T, T)
Since ฮฑห‰0=1\bar{\alpha}_0 = 1 and ฮฑห‰Tโ‰ˆ0\bar{\alpha}_T \approx 0:
x0โ‰ˆ1โ‹…xTโˆ’1โ‹…ฯตฮธ(xT,T)0+0โ‹…ฯตฮธ(xT,T)x_0 \approx 1 \cdot \frac{x_T - 1 \cdot \epsilon_\theta(x_T, T)}{0} + 0 \cdot \epsilon_\theta(x_T, T)
This is problematic because ฮฑห‰Tโ‰ˆ0\sqrt{\bar{\alpha}_T} \approx 0 causes division by near-zero. In practice, implementations clamp ฮฑห‰t\bar{\alpha}_t to avoid this, but the single-step reconstruction is just x^0=xTฮฑห‰Tโ‰ˆ\hat{x}_0 = \frac{x_T}{\sqrt{\bar{\alpha}_T}} \approx a scaled noise vector, which contains no meaningful structure.
A single denoising step can only remove a fixed amount of noise โ€” it can't separate structure from noise in one shot. Multiple steps are needed for the iterative refinement that diffusion models rely on. Q12: Suppose you want to use DDIM for video generation where temporal consistency is crucial. How would DDIM's properties help?
For video generation, we want consecutive frames to be consistent (no flickering). DDIM helps because:
  1. Shared noise structure: Generate a base latent xTx_T for the first frame, then for subsequent frames, use small perturbations of xTx_T. Since DDIM deterministically maps latent โ†’ image, small latent changes โ†’ small image changes.
  2. Interpolation between frames: Generate latents for keyframes, then linearly interpolate intermediate latents. DDIM's smooth latent space ensures smooth visual transitions.
  3. Consistent inversion: If editing a video, invert each frame independently with DDIM, edit the latents consistently, and decode. The edits will be temporally coherent.
This is why most diffusion-based video models (e.g., Stable Video Diffusion) build on DDIM-like schedulers.

5. ๐Ÿ”— Cross-References

Document outline

Keep your place and jump directly to a heading.

Table of contents

No note selected

Open a chapter to see its outline, backlinks, and sources here.