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 σtz 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−1x^0+1−αˉt−1ϵθ(xt,t) | Deterministic denoising |
| Predicted x0 | x^0=αˉtxt−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+1x^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−1x0+1−αˉt−1−σt2⋅1−αˉtxt−αˉtx0,σt2I)When σt2=β~t=1−αˉt1−αˉt−1βt:The mean becomes:μ=αˉt−1x0+1−αˉt−1−1−αˉt1−αˉt−1βt⋅1−αˉtxt−αˉtx0Using βt=1−αt=1−αˉt−1αˉt:After simplification (shown in DDPM derivation), this becomes:μ~t(xt,x0)=1−αˉtαˉt−1βtx0+1−αˉtαt(1−αˉt−1)xtwhich is exactly the DDPM posterior mean. And the variance σt2I=β~tI 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=αt1(xt−1−αˉtβtϵθ(xt,t))+σtzEven 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)⋅∇xlogpt(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⋅αˉTxT−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=αˉTxT≈ 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