Understanding DDIM (Denoising Diffusion Implicit Models)
Junyoung Park · 2023-04-20 · 12 min
Introduction
Unlike GANs, whose adversarial training does not allow a latent prior to be specified directly, DDPM emerged as a promising new kind of generative model because it avoids mode collapse and can be trained stably.
During training, however, DDPM adds tiny Gaussian noise values to individual pixels over many iterations (T=1000), and sampling is based on the noised input (xt) at each step. It therefore cannot escape a Markov process.
A Markov process is a useful way to construct a stable prior p(z), but every advantage comes with a cost: DDPM sampling is remarkably slow. In a Markov process, each next state depends on the preceding one in a cascade, which cannot be parallelized on a hardware accelerator such as a GPU. In practice, generating images at the same resolution requires a thousand times more computation than an implicit model such as a GAN. For example, a GAN can generate 50k images of size 32×32 in under a minute, whereas DDPM takes 20 hours. For more detail on the equations and diffusion itself, see the introductory DDPM post and the post deriving the DDPM equations.
This paper focuses on the excessively slow DDPM sampling process and proposes a new method that retains the same objective as DDPM while enabling a non-Markovian process during sampling. In contrast to DDPM’s fully stochastic sampling, this process is deterministic—conditioned on the original image x0—and creates a one-to-one mapping trajectory like a neural-network decoder. The authors call it a DDIM (Denoising Diffusion Implicit Model).
Overview
The idea behind DDPM was to train the reverse process to follow the posterior q(xt−1∣xt,x0), which makes the Markovian forward process tractable. DDIM assumes a non-Markovian process and thus enables deterministic sampling. By explicitly stating that the noised input at time t originates from x0, as shown below, it escapes the approach that depended only on the preceding state.
In other words, we can ensure that the distribution followed by the original pθ(xt−1∣xt) is non-Markovian rather than Markovian. The diffusion process can thereby remain implicitly aware, during sampling, of the image x0 that it must recover.
Most importantly, this change does not alter the objective function. There is no need to retrain with a different objective: a pretrained DDPM can be used as-is by applying only the sampling procedure derived in the figure above. A further advantage is that, because deterministic sampling is aware of x0, the reverse process need not traverse all T=1000 steps used by the forward process to create a noised sample. It can instead follow a subsequence Tsub=[τn,τn−1,⋯,τ0] and sample without a large loss of quality.
Finally, because every noised image xt(t<T) in the sampling process is conditioned on x0, interpolation in the latent space can produce meaningful semantic interpolation.
Image editing with GANs was comparatively straightforward—for example, through GAN inversion—because the mapping from latent z through a style mapper F and image synthesizer G to the implicitly generated x=F(G(z)) nonlinearly warped the information in the spaces of z and x. To some degree, interpolating the two latents z1 and z2 that produce x1 and x2 as α⋅z1+(1−α)⋅z2 could therefore have an effect in image space analogous to β⋅x1+(1−β)x2. Because the mapping is nonlinear, this affine expression is not exact, but it enables the intuitive semantic operation of producing, for instance, a brunette between a blonde woman and a woman with black hair.
Stochasticity prevented such intuitive sampling in a diffusion process. DDIM offers one way to overcome that limitation.
Deriving the equations
DDIM uses notation different from DDPM, which can be confusing. I prefer the DDPM notation, so I will rewrite the entire DDIM derivation using DDPM notation.
q(x1:T∣x0):=t=1∏Tq(xt∣xt−1), where q(xt∣xt−1):=N(αtxt−1,(1−αt)I)
Here αt is one minus βt, the variance scheduled for each time step t. As discussed in the equation-derivation post, adding noise through this forward process produces a Gaussian distribution while keeping the variance at 1. Accumulating noise in this way gives the following expression for q(xt∣x0), where αˉt=∏τ=1tατ.
As in DDPM, the actual implementation uses a reparameterization trick rather than sampling directly from the stochastic process above.
xt=αˉtx0+1−αˉtϵ, where ϵ∼N(0,I)
Remarkably, the DDIM equation can be derived directly from this expression. The proof is based on Equation 2.115 in Bishop’s Pattern Recognition—the formula for marginalizing a particular variable from a Gaussian conditional distribution. In brief, suppose we have
p(x)=p(y∣x)=N(x∣μ,Λ−1)N(y∣Ax+b,L−1)
Then the distribution of y after projecting out x is
p(y)=N(y∣Aμ+b,L−1+AΛ−1A⊤)
The conditional mean was parameterized by x; after marginalization it uses the prior mean, while the variance acquires the additional term at the end. Returning to DDIM, we want to inductively learn the “posterior” of a non-Markovian process satisfying the following constraint:
qσ(xt−1∣xt,x0)
If this looks familiar, in the DDPM derivation it appeared when the original forward distribution q(xt−1∣xt) was conditioned on x0 for every time step t>1 and expanded with Bayes’ rule while retaining the Markov process:
This time, however, we seek a new non-Markovian distribution qσ(xt∣xt−1,x0) with the same marginal q(xt∣x0) at every time step t. The inductive proof proceeds as follows.
For t=1, it is clear that q(x1∣x0)=qσ(x1∣x0).
At t=τ, suppose q(xτ−1∣x0)=∫qσ(xτ−1∣xτ,x0)dxτ holds (τ≥1).
If we can also make q(xτ∣x0)=∫qσ(xτ∣xτ+1,x0)dxτ+1 hold for t=τ+1, every marginal of the DDIM forward process can be matched to DDPM for all t≥1.
Keeping Bishop’s equation firmly in mind, consider the marginal that must be matched:
q(xt−1∣x0)=N(xt−1;αˉt−1x0,(1−αˉt−1)I)
Conditioning this expression on xt gives the following. Shown together with the distribution q(xt∣x0), it is
Here σt can be regarded as an arbitrary standard deviation of any Gaussian distribution satisfying the same marginal. The constant C is introduced into the mean for the following reason. According to Equation 2.115 in Bishop,
Therefore, the expression αˉt−1x0+C⋅(xt−αˉtx0) was intentionally constructed so that substituting αˉtx0 for xt produces αˉt−1x0. Since we introduced the arbitrary constant C, we solve for it by applying the variance identity in the same way:
To emphasize once more, this equation has a completely different meaning from the posterior derived for DDPM. Converting the non-Markovian posterior above into a likelihood gives
We have thus found a non-Markovian forward process qσ with the same marginals as the Markovian forward process q, which is exactly what the paper set out to do.
From a non-Markovian posterior to the DDPM loss
Now let us verify why optimizing the q defined above yields the same loss as DDPM. Only the q term changes relative to the DDPM loss, so we can express the result as follows by replacing the forward process in DDPM’s lower bound with the non-Markovian one.
The conversion to a posterior and the rest of the algebra were already covered for DDPM, so we can skip directly to the result. The variational-inference loss is
The only change is that the distribution q representing the forward process now carries σ, the standard deviation of the non-Markovian posterior. As in DDPM, we discard the first term and express the training of the diffusion model on the remaining terms in terms of the reverse process pθ:
Here x0(xt,ϵθ) denotes x0=(xt−1−αˉt⋅ϵθ(t)(xt))/αˉt, reconstructed from the noise ϵθ(t) that the network predicts was added to each xt. Looking only at the case t>1, the KL divergence between Gaussian distributions simplifies because their exponential terms cancel:
This proves that the non-Markovian posterior introduced for DDIM sampling indeed converges to the DDPM loss.
Sampling with DDIM
Using the predicted ϵθ in the non-Markovian forward process defined above yields the following sampling equation:
xt−1=αˉt−1predicted x0(αˉtxt−1−αˉtϵθ(t)(xt))+direction pointing to xt1−αˉt−1−σt2⋅ϵθ(t)(xt)+random noiseσtz,z∼N(0,I)
The equation first predicts x0 and then reconstructs xt. There is no strict need to predict the sample at t−1; predicting the sample at t−2 is also reasonable. DDIM sampling can therefore use only selected values of t rather than consuming all T time steps. For example, given a subsequence [xτ1,⋯,xτS](S<T) of the full length-T time sequence [1,2,⋯,T], we can redefine the non-Markovian forward process as q(xτi∣x0)=N(αˉτix0,(1−αˉτi)I). Because it no longer depends only on the preceding state, dependence on a contiguous sequence disappears as well.
Experiments
The sampling equation contains a new hyperparameter, σ:
xt−1=αˉt−1predicted x0(αˉtxt−1−αˉtϵθ(t)(xt))+direction pointing to xt1−αˉt−1−σt2⋅ϵθ(t)(xt)+random noiseσtz,z∼N(0,I)
Computing the variance separately gives the sum of the squared coefficients attached to epsilon:
Thus, σ controls the transition between DDPM’s stochasticity and DDIM’s determinism without changing the actual sampling variance. Recall the DDPM sampling equation:
xt−1=αt1(xt−1−αˉtβtϵθ(xt,t))+σtz
In deriving this equation, the standard deviation of the tractable probability distribution q(xt−1∣xt,x0) for the Markovian forward process was
σ~t2=βt×(1−αˉt1−αˉt−1)
Remarkably, when DDIM’s σt takes this form, its sampling equation becomes exactly the DDPM equation. Defining η as a hyperparameter therefore lets us interpolate between DDPM and DDIM sampling:
σt=η(1−αt)×(1−αˉt1−αˉt−1)
η=1 gives pure DDPM, while η=0 gives pure DDIM.
The figure reports FID after sampling with fewer than T=1000 steps for DDPM (η=1), DDIM (η=0), and intermediate values. DDPM performance deteriorates sharply as the number of steps decreases, while DDIM preserves its quality.
Unsurprisingly, more steps yield better sampling performance—but the sampling time becomes excessive.
Roughly 20∼100 steps provide generation quality comparable to 1000 steps, corresponding by a simple calculation to a 10-to-50-fold speedup.
Best of all, latent interpolation now has a meaningful relationship with the semantics of the generated images. It is hilarious how the Shutterstock watermark slowly appears along the way—maybe that is just me, though.
Neural ODEs with DDIM
Although DDIM and DDPM are diffusion models, their discrete treatment of diffusion makes it difficult to call them differential equations. DDIM sampling, however, has a form that admits an Euler approximation. Recall the sampling equation:
xt−1=αˉt−1predicted x0(αˉtxt−1−αˉtϵθ(t)(xt))+direction pointing to xt1−αˉt−1−σt2⋅ϵθ(t)(xt)+random noiseσtz,z∼N(0,I)
There it is. For DDIM, as discussed above, we simply set σ=0.
Substituting xt/αˉt=xˉt and αˉt1−αˉt=σ gives
dxˉ(t)=ϵθ(t)(σ2+1xˉ(t))dσ(t)
This is a differential equation. Compared with DDPM, the continuous diffusion process formulated through an SDE presents x(T) as the solution to a Neural ODE, allowing a deterministic one-to-one mapping.
Because an ODE is available, x0 can be encoded directly into xT or reconstructed back into x0. The trajectory error depends on Δ, so a larger S—a shorter time interval—brings the result closer to the true solution of the differential equation. The figure above shows reconstruction error on CIFAR-10.