Denoising Diffusion Implicit Models: Accelerated Sampling
3340 words
17 min read
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:
- The mapping from noise z to image x0โ is one-to-one (injective)
- We can invert the process: encode a real image back into its latent noise vector
- 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:
Because it's Markovian, we can also write the marginal distribution at any timestep directly:
where ฮฑหtโ=โi=1tโ(1โฮฒiโ).
The reverse process in DDPM is also Markovian:
And sampling requires iterating through every timestep T,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โ), define q(xtโ1โโฃxtโ,x0โ) directly.
From DDPM's joint distribution, we can derive (using Bayes rule) the reverse conditional distribution given x0โ:
where:
(These come from the standard DDPM derivation โ we derived them in Week 4.)
DDIM's innovation: Generalize this distribution by introducing a parameter ฯโฅ0 that controls the stochasticity of the reverse step:
When ฯt2โ=ฮฒ~โtโ, this reduces to DDPM. When ฯtโ=0, this becomes DDIM โ a fully deterministic reverse process.
1.4 The DDIM Sampling Equation
For DDIM (ฯtโ=0), the reverse step simplifies to:
Wait, that's not fully simplified. Let me derive it properly.
DDIM reverse step derivation:
We have the predicted x0โ from the current xtโ and predicted noise ฯตฮธโ:
The DDIM sampling equation (Song et al., 2021) is:
Or equivalently:
Notice: There is NO noise term. The process is deterministic given xtโ and ฯตฮธโ.
1.5 Accelerated Sampling: Skipping Steps
For DDPM, we must sample xTโ,xTโ1โ,...,x1โ,x0โ โ all T steps.
For DDIM, we can sample a subsequence {xฯSโโ,xฯSโ1โโ,...,xฯ1โโ,x0โ} where SโชT and ฯ is an increasing subsequence of [1,...,T].
For example, with T=1000 and S=50, we use steps ฯ=[1,20,40,...,980,1000] โ only 50 steps.
The sampling equation for a subsequence step xฯiโ1โโ from xฯiโโ:
The model ฯตฮธโ was trained on the original 1000-step schedule, but we can evaluate it at any timestep ฯiโ because the noise level ฮฑหฯiโโ is well-defined.
Worked Example 1: DDIM Sampling with 10 Steps
Suppose we have a trained DDPM with T=1000 and ฮฑหtโ=cos2(Ttโโ
2ฯโ) (a cosine schedule). We want to sample using only 10 DDIM steps.
Step 1: Choose the subsequence. A common choice is uniformly spaced:
With S=10, T=1000: ฯ=[100,200,300,...,1000].
Step 2: Start with pure noise x1000โโผN(0,I).
Step 3: For i=10,9,...,1 (i.e., t=1000,900,...,100):
Step 4: After 10 steps, x0โ 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 x0โ, the forward process in DDPM gives:
In DDPM reverse, at step t we predict ฯตฮธโ(xtโ,t) and sample:
The stochastic term ฯtโz means even with a perfect noise predictor (ฯตฮธโ=ฯต), DDPM's reverse is not purely deterministic โ the noise z accumulates.
In DDIM with ฯtโ=0, the reverse step has no noise. If ฯตฮธโ perfectly predicts the noise, then:
which is exactly the forward process equation at timestep tโ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 x0โ, we can find the latent noise vector xTโ that would generate it.
Forward DDIM inversion:
This is the reverse of the sampling equation. We start from x0โ and iterate t=0,1,...,Tโ1 to obtain xTโ.
Important caveat: This inversion is approximate because ฯตฮธโ isn't perfect. However, it works well enough for real applications.
Why Inversion Matters
- Image editing: Encode an image, edit the latent, decode back
- Style mixing: Combine latents from different images
- Interpolation: Smoothly morph between two images by interpolating their latents
1.7 DDIM vs DDPM: Comparison
| Property | DDPM | DDIM |
|---|---|---|
| Forward process | Markovian | Non-Markovian |
| Reverse process | Stochastic | Deterministic ( ฯ=0 ) |
| Sampling steps required | All T (e.g., 1000) | Any SโชT (e.g., 10-100) |
| Sample quality at few steps | Poor | Good |
| Sample quality at full steps | Excellent | Comparable (slightly worse) |
| Deterministic encoding | No (stochastic) | Yes (invertible) |
| Latent space structure | Non-smooth | Smooth for interpolation |
| Training changes | None (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
| Concept | Formula | Description |
|---|---|---|
| DDIM reverse step | xtโ1โ=ฮฑหtโ1โโx^0โ+1โฮฑหtโ1โโฯตฮธโ(xtโ,t) | Deterministic denoising |
| Predicted x0โ | x^0โ=ฮฑหtโโxtโโ1โฮฑหtโโฯตฮธโ(xtโ,t)โ | Denoised estimate from current step |
| DDIM general | $q_\sigma(x_{t-1}\ | x_t,x_0)with\sigma \geq 0$ |
| Accelerated schedule | ฯ=subsequenceย ofย [1,...,T] | Sample only SโชT steps |
| DDIM inversion | xt+1โ=ฮฑหt+1โโx^0โ+1โฮฑหt+1โโฯตฮธโ(xtโ,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โ) and then using DDIM with a different schedule without re-normalizing.
Why: DDIM assumes the noise schedule ฮฑหtโ matches what the model was trained on.
Correct approach: DDIM sampling uses the noise predictor ฯตฮธโ 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 ฯตฮธโ 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ร in terms of neural network evaluations. However, DDIM's per-step computation is identical to DDPM's (both evaluate ฯตฮธโ once per step), so the actual speedup is indeed 20ร.In practice, the speedup may be slightly less due to overhead (data transfers between CPU/GPU), but it's approximately 20ร faster. Q2: Show that when ฯtโ=ฮฒ~โtโโ, the DDIM general process reduces to DDPM.The general DDIM reverse distribution is:qฯโ(xtโ1โโฃxtโ,x0โ)=N(xtโ1โ;ฮฑหtโ1โโx0โ+1โฮฑหtโ1โโฯt2โโโ 1โฮฑหtโโxtโโฮฑหtโโx0โโ,ฯt2โI)When ฯt2โ=ฮฒ~โtโ=1โฮฑหtโ1โฮฑหtโ1โโฮฒtโ:The mean becomes:ฮผ=ฮฑหtโ1โโx0โ+1โฮฑหtโ1โโ1โฮฑหtโ1โฮฑหtโ1โโฮฒtโโโ 1โฮฑหtโโxtโโฮฑหtโโx0โโUsing ฮฒtโ=1โฮฑtโ=1โฮฑหtโ1โฮฑหtโโ:After simplification (shown in DDPM derivation), this becomes:ฮผ~โtโ(xtโ,x0โ)=1โฮฑหtโฮฑหtโ1โโฮฒtโโx0โ+1โฮฑหtโฮฑtโโ(1โฮฑหtโ1โ)โxtโwhich is exactly the DDPM posterior mean. And the variance ฯt2โI=ฮฒ~โtโI is DDPM's posterior variance. Q3: For DDIM, if you start from the same noise vector xTโ and run sampling twice, do you get the same image?Yes โ DDIM is deterministic. Given the same xTโ and the same trained model ฯตฮธโ, the sequence of xTโ1โ,xTโ2โ,...,x0โ 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 xTโ.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โ=ฮฑtโโ1โ(xtโโ1โฮฑหtโโฮฒtโโฯตฮธโ(xtโ,t))+ฯtโzEven if we know every ฯตฮธโ(xtโ,t) exactly, the random zโผN(0,I) at each step means:
- Starting from the same x0โ, the forward process gives a distribution over xTโ (not a unique xTโ)
- Starting from xTโ, the reverse gives a distribution over x0โ
There's no deterministic mapping between x0โ and xTโ in either direction. DDIM removes the stochasticity, creating a bijection. Q6: For DDIM with T=1000 and S=20, propose a sampling schedule ฯ and explain why you chose it.A common choice is:ฯiโ=โS2i2โโ Tโforย i=1,...,SThis is a quadratic schedule (compared to uniform). The reasoning:
- Early steps (high noise, near T): 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=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]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โ=โSiโโ Tโย forย i=1,...,SWith S=20, T=1000: ฯ=[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โโ), DDIM's iterative equation becomes:dtdxโ=fฮธโ(x,t)โ โxโlogptโ(x)where fฮธโ is derived from the noise prediction network.This connection is important because:
- It justifies using ODE solvers (RK45, DOPRI) for even faster sampling
- It enables likelihood computation via the probability flow ODE
- 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 ฯตฮธโ on T timesteps with a Markovian forward process
- DDIM sampling: Uses the same trained model ฯตฮธโ 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]"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 x0โ and you run DDIM inversion to get xTโ. Then you run DDIM sampling from xTโ back to x0โฒโ. Under what conditions would x0โ=x0โฒโ?Perfect reconstruction (x0โ=x0โฒโ) would require:
- Perfect denoiser: ฯตฮธโ(xtโ,t)=ฯต for all t, where ฯต is the actual noise added at step t
- Consistent inversion path: The inversion path from x0โ to xTโ must use the same steps as the generation path from xTโ to x0โ
- No numerical error: Floating-point precision doesn't cause drift
In practice, none of these hold perfectly:
- ฯตฮธโ 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 SpaceStep 1: Take two real images x0(A)โ and x0(B)โ.Step 2: Encode both to latents xT(A)โ and xT(B)โ using DDIM inversion.Step 3: For ฮฑ in {0,0.1,0.2,...,1.0}, compute interpolated latent:xT(ฮฑ)โ=ฮฑโ xT(A)โ+(1โฮฑ)โ xT(B)โStep 4: Decode each xT(ฮฑ)โ with DDIM sampling to get x0(ฮฑ)โ.Expected result: For ฮฑ=0, x0(0)โโx0(B)โ. For ฮฑ=1, x0(1)โโx0(A)โ. For intermediate ฮฑ, x0(ฮฑ)โ should be a smooth semantic blend โ e.g., if A is a photo of a cat and B 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=1) gives a poor result.With S=1, ฯ=[1000], so we jump directly from t=1000 to t=0:x0โ=ฮฑห0โโโ ฮฑหTโโxTโโ1โฮฑหTโโฯตฮธโ(xTโ,T)โ+1โฮฑห0โโฯตฮธโ(xTโ,T)Since ฮฑห0โ=1 and ฮฑหTโโ0:x0โโ1โ 0xTโโ1โ ฯตฮธโ(xTโ,T)โ+0โ ฯตฮธโ(xTโ,T)This is problematic because ฮฑหTโโโ0 causes division by near-zero. In practice, implementations clamp ฮฑหtโ to avoid this, but the single-step reconstruction is just x^0โ=ฮฑหTโโxTโโโ 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:
Shared noise structure: Generate a base latent xTโ for the first frame, then for subsequent frames, use small perturbations of xTโ. Since DDIM deterministically maps latent โ image, small latent changes โ small image changes. Interpolation between frames: Generate latents for keyframes, then linearly interpolate intermediate latents. DDIM's smooth latent space ensures smooth visual transitions. 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
- Previous: Diffusion Models (DDPM) โ Foundation for DDIM
- Next: Autoregressive Models โ Alternative generative paradigm
- Related: Conditional Generation โ Using DDIM with guidance
- External: Song et al., "Denoising Diffusion Implicit Models" (ICLR 2021) Join Discord PreviousDiffusion Models (DDPM)NextAutoregressive Models