Skip to main content
Open In Colab

Deterministic sampling

In the DDPM example you trained a noise predictor ϵθ(xt,t)\epsilon_\theta(x_t, t) and generated samples with the stochastic reverse chain. That chain has two practical drawbacks. It is slow, because it visits every timestep, and with T=500T = 500 that means 500 network calls per sample. It is also random. Even from a fixed starting noise xTx_T the chain adds fresh noise at each step, so you cannot reproduce a sample or interpolate between two of them. DDIM (Song et al., 2020) addresses both. It defines a non-Markovian forward process with the same marginals q(xt∣x0)q(x_t \mid x_0) as DDPM (Ho et al., 2020), so the same trained ϵθ\epsilon_\theta works with a different sampler. The sampler can skip timesteps, for example 50 steps instead of 500. A noise level η∈[0,1]\eta \in [0, 1] moves it between a chain close to DDPM at η=1\eta = 1 and a deterministic update at η=0\eta = 0. The deterministic case turns the model into a map from Gaussian noise to data. With a finite number of steps this map is only approximately invertible, and it becomes exactly invertible as the step size shrinks. That near-invertibility is what makes latent-space arithmetic and image editing possible. The schedule is the one from the DDPM example: 500 steps with β\beta linear from 10−410^{-4} to 0.020.02. In the clock tk=−12log⁡αˉkt_k = -\tfrac12 \log \bar\alpha_k of the Fokker-Planck section, the forward chain is the Ornstein-Uhlenbeck process observed at discrete times, with at=αˉka_t = \sqrt{\bar\alpha_k} and bt=1−αˉkb_t = \sqrt{1 - \bar\alpha_k}. The last step sits at tK≈2.53t_K \approx 2.53. The sampler starts from N(0,I)\mathcal{N}(0, I), which is close to the last forward marginal but not equal to it. The stochastic DDPM chain is Gaussian by construction, while the true finite-step reverse transitions of a mixture are not, so that chain only approximates them. DDIM with η=0\eta = 0 integrates the probability flow ODE, which the score-based section also uses. Each update is an Euler step, but not in the time tt. After the change of variables y=x/aty = x / a_t and ρ=bt/at\rho = b_t / a_t, the right side of the ODE is the noise prediction alone, and one Euler step in ρ\rho is exactly the DDIM update. The section before the update rule derives this. Each sample moves along a velocity field, as in flow matching, so the same starting noise always gives the same sample.

Same target distribution as the DDPM tutorial

The 3-cluster MoG, seed 42, 300 points. We then train the same MLP noise predictor with the same DDPM objective.
Output from cell 2

Forward (noising) process

Same fixed Markov noising chain as DDPM: q(xt∣x0)=N ⁣(xt;αˉt x0,(1−αˉt)I)q(x_t \mid x_0) = \mathcal{N}\!\left(x_t;\sqrt{\bar\alpha_t}\,x_0,(1-\bar\alpha_t)I\right). We visualize a few specific timesteps so the noise schedule is concrete before training.
Output from cell 4

Train the noise predictor (same architecture and objective as DDPM)

DDIM and DDPM share the training step, only the sampler differs.

The deterministic update as an Euler step

Write ata_t and btb_t for the coefficients in xt=atx0+btϵx_t = a_t x_0 + b_t \epsilon, with tt the OU time. Step kk sits at tkt_k, where atk=αˉk=e−tka_{t_k} = \sqrt{\bar\alpha_k} = e^{-t_k} and btk=1−αˉkb_{t_k} = \sqrt{1 - \bar\alpha_k}. So at=e−ta_t = e^{-t} for every tt, and the prediction table gives the score st=−ϵθ/bts_t = -\epsilon_\theta / b_t. The probability flow ODE from the Fokker-Planck section then reads dxdt=−x−st(x)=−x+ϵθ(x,t)bt.\frac{dx}{dt} = -x - s_t(x) = -x + \frac{\epsilon_\theta(x, t)}{b_t}. The right side has a linear part and a part that depends on the network. Change variables to the rescaled position and the noise-to-signal ratio, y=xat,ρ=btat.y = \frac{x}{a_t}, \qquad \rho = \frac{b_t}{a_t}. With a˙t=−at\dot a_t = -a_t and b˙t=at2/bt\dot b_t = a_t^2 / b_t, the time derivatives of the new variables are dydt=1atdxdt+xat=ϵθ(x,t)atbt,dρdt=b˙tat−bta˙tat2=1atbt.\begin{aligned} \frac{dy}{dt} &= \frac{1}{a_t}\frac{dx}{dt} + \frac{x}{a_t} = \frac{\epsilon_\theta(x, t)}{a_t b_t}, \\ \frac{d\rho}{dt} &= \frac{\dot b_t a_t - b_t \dot a_t}{a_t^2} = \frac{1}{a_t b_t}. \end{aligned} Dividing the first by the second removes the factor 1/(atbt)1 / (a_t b_t) and the linear part with it: dydρ=ϵθ(aty,t).\frac{dy}{d\rho} = \epsilon_\theta(a_t y, t). One Euler step in ρ\rho from tt to an earlier rr gives yr=yt+(ρr−ρt) ϵθ(xt,t)y_r = y_t + (\rho_r - \rho_t)\,\epsilon_\theta(x_t, t). Multiplying by ara_r returns to xx: xr=arat xt+(br−arbtat) ϵθ(xt,t).x_r = \frac{a_r}{a_t}\, x_t + \Big(b_r - \frac{a_r b_t}{a_t}\Big)\, \epsilon_\theta(x_t, t). With x^0=(xt−bt ϵθ)/at\hat x_0 = (x_t - b_t\,\epsilon_\theta)/a_t the same step reads xr=arx^0+br ϵθx_r = a_r \hat x_0 + b_r\,\epsilon_\theta, the η=0\eta = 0 case of the update rule below. DDIM is therefore an Euler integrator in (ρ,y)(\rho, y), not in OU time. Its error comes from how much ϵθ\epsilon_\theta changes over the step. If the noise prediction were constant along the path, a single step would be exact.

The DDIM update rule

Given the trained ϵθ\epsilon_\theta, define a strictly increasing sub-sequence τ=(τ1<τ2<⋯<τS)⊆{1,…,T}\tau = (\tau_1 < \tau_2 < \dots < \tau_S) \subseteq \{1, \dots, T\}. The DDIM reverse step from xτix_{\tau_i} to xτi−1x_{\tau_{i-1}} is xτi−1=αˉτi−1 x^0  +  1−αˉτi−1−στi2  ϵθ(xτi,τi)  +  στi zτi,zτi∼N(0,I)x_{\tau_{i-1}} = \sqrt{\bar\alpha_{\tau_{i-1}}} \,\hat x_0 \;+\; \sqrt{1 - \bar\alpha_{\tau_{i-1}} - \sigma_{\tau_i}^2}\; \epsilon_\theta(x_{\tau_i}, \tau_i) \;+\; \sigma_{\tau_i}\, z_{\tau_i}, \quad z_{\tau_i} \sim \mathcal{N}(0, I) with the predicted clean sample x^0=xτi−1−αˉτi ϵθ(xτi,τi)αˉτi\hat x_0 = \frac{x_{\tau_i} - \sqrt{1-\bar\alpha_{\tau_i}}\, \epsilon_\theta(x_{\tau_i}, \tau_i)}{\sqrt{\bar\alpha_{\tau_i}}} and the per-step noise scale στi  =  η 1−αˉτi−11−αˉτi 1−αˉτiαˉτi−1\sigma_{\tau_i} \;=\; \eta \, \sqrt{\frac{1-\bar\alpha_{\tau_{i-1}}}{1-\bar\alpha_{\tau_i}}} \, \sqrt{1 - \frac{\bar\alpha_{\tau_i}}{\bar\alpha_{\tau_{i-1}}}} Two regimes:
  • η=1\eta = 1, full noise. The sampler becomes equivalent to DDPM (after accounting for sub-sequence corrections).
  • η=0\eta = 0, deterministic. The noise term zτiz_{\tau_i} disappears and the step reduces to the Euler step in (ρ,y)(\rho, y) from the previous section, so the same xTx_T always produces the same x0x_0.
Because the model was trained on the marginals q(xt∣x0)q(x_t \mid x_0) (not the chain transitions), it does not depend on which sampler you use at inference time.
The Euler step in (ρ,y)(\rho, y) and one step of the sampler above should agree to floating-point precision. The check moves 256 noise points from step 400 to step 300 both ways and compares the results.

DDIM in 50 steps versus DDPM-equivalent in 500 steps

Three samplers, same model, 1000 generated points each:
  • DDIM η=0, 50 steps, fast, deterministic.
  • DDIM η=1, 500 steps, equivalent to DDPM, sanity check.
  • DDIM η=0, 500 steps, deterministic with the full timestep grid.
Output from cell 9

Determinism: same noise, same sample

With η=0\eta = 0 the sampler is a deterministic function of the initial noise xTx_T. Run it twice with the same xTx_T and you get bit-identical outputs, regardless of how many steps you take.
Output from cell 11

Latent interpolation: slerp on xTx_T

Because η=0\eta = 0 DDIM is a deterministic map that is approximately invertible (exactly so only as the step size shrinks), you can pick two endpoints in noise space and walk a path between them; each intermediate noise decodes to a coherent point in data space. The right path on the unit sphere is spherical linear interpolation (slerp), which preserves the Gaussian magnitude.
Output from cell 13

Connections to other concepts

  • Sampler decoupling. Training trains; sampling samples. DDIM proves you can swap samplers freely as long as marginals are preserved. Modern stacks ship many samplers (DDIM, DPM-Solver, Euler, Heun, …) on top of the same DDPM-trained network.
  • Step count is a knob, not a constant. Image-generation pipelines routinely use 20-50 DDIM steps in production where DDPM would take 1000.
  • Deterministic, approximately invertible. η=0\eta = 0 DDIM gives you a noise-to-data map you can run forwards and backwards. The round trip is exact only as the step size shrinks, and it is accurate enough for editing and interpolation.
  • Bridge to flows and ODEs. Deterministic DDIM is the discretization of a probability-flow ODE. That connection underlies most of the recent diffusion-model speedup work (consistency models, rectified flow, …).
  • Score-based unification. The score mixture example trains a score network sθ(x,t)s_\theta(x, t) on the same mixture under the same Ornstein-Uhlenbeck process, then samples with the reverse SDE and the probability flow ODE (Song et al., 2020). Deterministic DDIM is an Euler discretization of that ODE in rescaled coordinates.

PyTorch reference

References

  • Ho, J., Jain, A., Abbeel, P. (2020). Denoising Diffusion Probabilistic Models. arXiv [cs.LG]. DOI
  • Song, J., Meng, C., Ermon, S. (2020). Denoising Diffusion Implicit Models. arXiv [cs.LG]. DOI
  • Song, Y., Sohl-Dickstein, J., Kingma, D., Kumar, A., Ermon, S., et al. (2020). Score-based generative modeling through stochastic differential equations. arXiv [cs.LG]. DOI

Found an error on this page? Report an issue.