DDIM (Denoising Diffusion Implicit Models) is a method that reuses a trained DDPM as is and generates images quickly by skipping generation steps. Training is exactly the same as DDPM; only the way of generating changes. Following just 20-100 of the 1000 steps still produces images of quality close to a 1000-step DDPM.
It was introduced in 2020 by Song, Meng, and Ermon in the paper "Denoising Diffusion Implicit Models" and accepted at ICLR 2021. The motivation was the slow generation of DDPM. According to the paper, on a single NVIDIA 2080 Ti GPU, DDPM takes about 20 hours to generate 50,000 images of 32 Γ 32, while a GAN takes less than a minute. For 50,000 images of 256 Γ 256, it can take nearly 1000 hours.
DDIM rests on two ideas.
The DDPM loss depends only on "the distribution that produces xtβ from x0β": changing how xtβ1β is connected to xtβ step by step gives the same loss. So instead of a Markov chain that advances one step at a time, the authors considered a non-Markovian forward process and built a new generation procedure that matches it.
Deterministic generation without added noise is possible: the new generation procedure lets the noise scale be chosen freely. Setting the noise to 0 means that once the initial noise xTβ is fixed, the image x0β is uniquely determined. This is DDIM.
Being deterministic made the following possible.
Fast generation by skipping steps: images of nearly the same quality, 10-50 times faster.
Interpolation in latent space: smoothly connecting two xTβ makes the images change smoothly in a meaningful way as well.
Encoding and reconstructing images: xTβ can be computed back from an image x0β, and the image can be recovered almost exactly from it.
Theory
This article uses the same notation as the DDPM article and writes Ξ±Λtβ=βs=1tβ(1βΞ²sβ). The paper writes this quantity as Ξ±tβ, so translate accordingly when reading the paper.
Starting point: the DDPM loss depends only on the marginals
The DDPM forward process can produce xtβ from x0β at any step in the following form (Eq. 4 of the paper).
The DDPM training loss (squared error of the noise) uses only this q(xtββ£x0β). It does not depend on how x1β,β¦,xTβ are connected to each other.
A non-Markovian forward process
Using Οtββ₯0 for the noise scale, the paper considers the following family of forward processes (Eqs. 6 and 7). Since xtβ1β depends not only on xtβ but also on x0β, it is not a Markov chain.
This mean is chosen so that qΟβ(xtββ£x0β) is the same as in DDPM for any choice of Οtβ. The paper then shows that the variational objective for this forward process equals the DDPM loss up to a constant and weighting (Theorem 1). In other words, a network trained with DDPM can be used as is for generation with any Οtβ.
One generation step
During generation x0β is unknown, so it is estimated from the noise predicted by the network ϡθβ and plugged into the formula above. One update step is the following (Eq. 12).
Ξ·=1: the forward process becomes a Markov chain again, and generation is the same as DDPM.
Ξ·=0: the noise term vanishes and generation becomes deterministic. This is DDIM. The "implicit" in the name indicates that, like a GAN, it is a model that produces data from latent variables by a fixed procedure (an implicit probabilistic model).
Skipping steps
Since the loss depends only on q(xtββ£x0β), the same network can be used even if the forward process is defined not on all steps 1,β¦,T but only on a subset of steps Ο1β<Ο2β<β―<ΟSβ (Section 4.2). Generation follows ΟSβ,ΟSβ1β,β¦,Ο1β in that order, replacing t and tβ1 in the formula above with Οiβ and Οiβ1β.
Two ways of choosing the subsequence Ο are used (Appendix D.2).
c is chosen so that the last ΟSβ is close to T.
Generation procedure
Choose the number of steps S, the subsequence Ο, and the noise scale Ξ· (DDIM uses Ξ·=0).
Draw xΟSβββΌN(0,I).
For i=S,β¦,1, predict the noise ϡθβ(xΟiββ,Οiβ) with the network and estimate x0β from it.
Compute xΟiβ1ββ with Eq. 12 (Ο0β=0, Ξ±Λ0β=1).
Output the final x0β.
DDIM changes only the computation at generation time. The network architecture and training are the same as DDPM. The paper's experiments also use DDPM's trained models (CIFAR-10 and LSUN) as is.
Why steps can be skipped
The reasons DDIM produces good images with few steps can be summarized in three points.
It re-estimates the "destination x0β" at every step: one step of Eq. 12 first estimates the finished image x0β from the current xtβ, then re-mixes it to match the noise level of Οiβ1β. Even with large jumps, it reviews the finished picture each time before moving on, so it is less likely to stray. It is like walking a mountain trail with long strides while checking the direction of the summit each time.
No added noise, so noise does not pile up: DDPM adds random noise at every step. With few steps, the noise added per step becomes large and graininess remains in the image. DDIM adds no noise, so images come out clean even with few steps.
It is equivalent to solving an ordinary differential equation: one DDIM step has the same form as one Euler step for numerically solving an ordinary differential equation (ODE) (Eqs. 13 and 14). Reducing the number of steps corresponds to solving the ODE with a coarse step size, and finer steps approach the ODE solution. This ODE is the same as a special case of the "probability flow ODE" of Score SDE (Proposition 1).
Because generation is deterministic, the high-level features of the image are determined by xTβ alone. Generating from the same xTβ with different numbers of steps gave images with consistent high-level features at both 20 and 1000 steps (Figure 5). More steps only raise the quality of the details.
In DDPM, even starting from the same xTβ, the noise added along the way leads to very different images. In DDIM, xTβ and the image correspond nearly one-to-one, so xTβ can be treated like the latent variable of a GAN.
Experimental results
The paper used the same trained models (T=1000) on CIFAR-10 (32 Γ 32), CelebA (64 Γ 64), and LSUN Bedroom and Church (256 Γ 256), and compared them by changing only the number of generation steps S and Ξ·.
Number of steps and image quality (Table 1; FID, lower is better; Ξ·=1 is DDPM, Ξ·=0 is DDIM; Ο^ is the DDPM with larger noise used in the DDPM paper on CIFAR-10)
Dataset
Method
S=10
20
50
100
1000
CIFAR-10
DDIM (Ξ·=0)
13.36
6.84
4.67
4.16
4.04
CIFAR-10
DDPM (Ξ·=1)
41.07
18.36
8.01
5.78
4.73
CIFAR-10
DDPM (Ο^)
367.43
133.37
32.72
9.99
3.17
CelebA
DDIM (Ξ·=0)
17.33
13.73
9.17
6.53
3.51
CelebA
DDPM (Ξ·=1)
33.12
26.03
18.48
13.93
5.98
CelebA
DDPM (Ο^)
299.71
183.83
71.71
45.20
3.26
The fewer the steps, the larger the gap between DDIM and DDPM. At 1000 steps, the DDPM with larger noise came out slightly ahead. The paper states that DDIM reaches quality close to the 1000-step model with 20-100 steps, which is 10-50 times faster in wall-clock time than the original DDPM. On CelebA, the FID of 100-step DDPM and 20-step DDIM were about the same. Generation time is roughly proportional to the number of steps (Figure 4).
256 Γ 256 images (Table 3, FID; 1000-step DDPM gives 6.36 on Bedroom and 7.89 on Church)
Dataset
Method
S=10
20
50
100
Bedroom
DDIM
16.95
8.89
6.75
6.62
Bedroom
DDPM
42.78
22.77
10.81
6.81
Church
DDIM
19.45
12.47
10.84
10.58
Church
DDPM
51.56
23.37
11.16
8.27
Encoding and reconstruction (Table 2, CIFAR-10 test data; per-dimension squared error for x0ββxTββx0β, solving the ODE backward and then forward; pixels in 0-1)
S
10
20
50
100
200
500
1000
Error
0.014
0.0065
0.0023
0.0009
0.0004
0.0001
0.0001
The more steps, the more exactly the original image is recovered. DDPM adds random noise, so this kind of encoding is not possible.
The paper also showed that connecting two xTβ by spherical linear interpolation makes the generated images change smoothly in a meaningful way (Figure 6).
Practical notes
Keep the following points in mind when using DDIM.
The paper's Ξ±tβ is DDPM's Ξ±Λtβ: the DDIM paper writes the accumulated noise schedule as Ξ±tβ. It is not the same as Ξ±tβ=1βΞ²tβ in the DDPM paper, so do not mix them up when implementing.
The choice of subsequence Ο changes the FID: the paper uses quadratic spacing on CIFAR-10 and linear spacing on the other datasets, getting slightly better FID with each.
Number of steps and image quality are a trade-off: fewer steps are faster but lower the quality. In the paper's tables, 10 steps give a much worse FID than 1000 steps, and 50-100 steps come close to 1000 steps.
The DDPM with larger noise breaks down with fewer steps: generation with Ο^ is the best at 1000 steps, but its FID worsens sharply as steps are reduced. When reducing steps, use Ξ·=0.
DDIM is a method for speeding up generation and does not reduce training time. Also, the fewer the steps, the wider the gap from 1000-step quality, so if you need high quality with few steps, check with FID or similar metrics while varying the number of steps before using it.
Comparison with other methods
The following compares DDIM with methods whose generation procedures are similar, from the same viewpoints.
To use a trained DDPM faster without retraining: DDIM is a good fit. Start with 50-100 steps and check the balance between speed and quality.
To get the best quality even if it takes 1000 steps: DDPM's own generation is fine.
To interpolate between generated images or encode images into latent variables: the deterministic DDIM is a good fit.
To generate high-resolution images with even less computation: combine it with a method that diffuses in latent space, such as Latent Diffusion. The Latent Diffusion paper also uses DDIM for generation in many experiments.
PyTorch example
βΆShow code
python
import math
import torch
"""
DDIM generation from Song et al. (2021) (Eqs. 12 and 16, Appendices D.2 and D.3).
Training is the same as DDPM. eps_model is the noise-predicting network eps_model(x_t, t) trained with DDPM.
Notation follows DDPM and uses alpha_bar_t (alpha_t in the paper)
"""
T = 1000
betas = torch.linspace(1e-4, 0.02, T) # same beta schedule as DDPM
alpha_bars = torch.cumprod(1.0 - betas, dim=0) # alpha_bar_1, ..., alpha_bar_T
def make_tau(S, kind="linear"):
"""Choose S steps from 1..T. Linear: tau_i = floor(c i), quadratic: tau_i = floor(c i^2)"""
i = torch.arange(1, S + 1, dtype=torch.float64)
if kind == "linear":
tau = torch.floor(i * (T / S))
else:
tau = torch.floor(i ** 2 * (T / S ** 2))
return tau.clamp(1, T).long().unique() # choose c so that the last step is close to T
@torch.no_grad()
def ddim_sample(eps_model, shape, S=50, eta=0.0, kind="linear", device="cpu"):
"""eta = 0 gives DDIM (deterministic), eta = 1 gives the same generation as DDPM"""
tau = make_tau(S, kind).tolist()
ab = alpha_bars.to(device)
x = torch.randn(shape, device=device) # x_{tau_S} ~ N(0, I)
for i in reversed(range(len(tau))):
t = tau[i]
a_t = ab[t - 1]
a_prev = ab[tau[i - 1] - 1] if i > 0 else torch.tensor(1.0, device=device) # alpha_bar_0 = 1
eps = eps_model(x, torch.full((shape[0],), t, device=device, dtype=torch.long))
x0_pred = (x - (1 - a_t).sqrt() * eps) / a_t.sqrt() # predicted x_0 (Eq. 9)
sigma = eta * ((1 - a_prev) / (1 - a_t)).sqrt() * (1 - a_t / a_prev).sqrt() # Eq. 16
direction = (1 - a_prev - sigma ** 2).clamp(min=0).sqrt() * eps # direction pointing to x_t
noise = sigma * torch.randn_like(x) if eta > 0 else 0.0 # random noise (0 for DDIM)
x = a_prev.sqrt() * x0_pred + direction + noise # Eq. 12
return x.clamp(-1, 1)
@torch.no_grad()
def ddim_encode(eps_model, x0, S=50, device="cpu"):
"""Run the eta = 0 update in reverse to encode an image x_0 into x_T (solve the ODE backward)"""
tau = [0] + make_tau(S).tolist()
ab = torch.cat([torch.ones(1), alpha_bars]).to(device) # ab[0] = alpha_bar_0 = 1
x = x0
for i in range(len(tau) - 1):
t, t_next = tau[i], tau[i + 1]
eps = eps_model(x, torch.full((x.shape[0],), max(t, 1), device=device, dtype=torch.long))
x0_pred = (x - (1 - ab[t]).sqrt() * eps) / ab[t].sqrt()
x = ab[t_next].sqrt() * x0_pred + (1 - ab[t_next]).sqrt() * eps
return x
# Usage (pass a model trained with DDPM, put in eval() mode, as eps_model)
# eps_model.eval()
# images = ddim_sample(eps_model, (16, 3, 32, 32), S=50, eta=0.0, kind="quadratic") # quadratic spacing for CIFAR-10
FAQ
Q. Does using DDIM require retraining the model?
A.
No. The training objective of DDIM is exactly the same as DDPM, so a noise-prediction network trained with DDPM can be used as is. Only the update formula at generation time and the number of steps followed change. The paper's experiments also use trained DDPM models.
Q. What does Ξ· in DDIM control?
A.
It sets the scale Οtβ of the random noise added at each generation step, as a multiple Ξ·. With Ξ·=0, generation is deterministic with no added noise (DDIM); with Ξ·=1, it is the same as DDPM. In the paper's tables, the fewer the steps, the better the FID of Ξ·=0 was.
Q. How many steps should be used for generation?
A.
In the paper, 20-100 steps give quality close to the 1000-step model. For example, the CIFAR-10 FID was 4.04 for 1000-step DDIM, 4.16 for 100 steps, 4.67 for 50 steps, and 13.36 for 10 steps. If speed matters, try 20-50 steps; if quality matters, start around 100 steps, and check with FID or similar metrics.