---
title: "DDIM from Scratch, Part 3: The Loss-Equivalence Proof"
date: 2026-07-01
tags: ["Diffusion Models", "Generative Models", "Deep Learning"]
author: "Yiyang Yu"
---

> Part 3 of the DDIM series. In [Part 2](ddim-02-removing-markov-and-sampling.md) we removed the
> Markov shackle and derived DDIM's step-skipping sampler under two free-loading requirements. But
> "it *looks* like the noising process is untouched" isn't a proof. Here we prove it rigorously:
> under DDIM's setup, the loss is **equivalent** to DDPM's — which is what licenses free-loading a
> pretrained DDPM model.

## What "equivalent" means — Theorem 1

The DDIM paper states this as **Theorem 1**:

> **Theorem 1.** For all $\sigma > 0$, there exists $\gamma \in \mathbb{R}^T_{>0}$ and
> $C \in \mathbb{R}$ such that $J_\sigma = L_\gamma + C$.

Decoding it:

- $J_\sigma$ — the **DDIM** loss (under DDIM's sampling setup, variance vector $\sigma$).
- $L_\gamma$ — the **DDPM** loss (the $\gamma$-weighted noise-prediction MSE we derived in Part 1 /
  the DDPM series).
- They differ by only a **constant** $C$. (And note: $\sigma > 0$ **strictly** — remember this, it
  matters at the end.)

Two losses that differ by a constant (and a constant scale) have the **same gradient direction**, so
optimizing one optimizes the other. That's the whole point: train once with DDPM, sample with DDIM.

---

## Which process does DDIM actually change?

DDPM's loss derivation starts from two joint distributions:

$$
q(x_{1:T}\mid x_0) = \prod_{t=1}^{T} q(x_t \mid x_{t-1}), \tag{17}
$$
$$
p_\theta(x_{0:T}) = p(x_T)\prod_{t=1}^{T} p_\theta(x_{t-1}\mid x_t). \tag{18}
$$

Formula 17 is the **forward (noising)** joint (Markov chain). Formula 18 is the **reverse
(denoising)** joint — what the neural network $p_\theta$ models.

**Quiz:** DDIM changes *one* of these. Which — the forward (17) or the reverse (18)?

**Answer (surprising):** DDIM changes the **forward** process, Formula 17.

Why? In Part 2, the term $q(x_t \mid x_{t-1})$ in the Bayes expansion *would* normally have an $x_0$
that Markov lets us drop. But DDIM is **non-Markovian**, so $x_0$ **cannot** be dropped — the forward
transition is really $q(x_t \mid x_{t-1}, x_0)$. That extra $x_0$ is exactly what changes Formula 17.

Picture the DDIM graphical model: $x_t$ is determined **jointly** by $x_{t-1}$ *and* $x_0$ (arrows
into $x_t$ from both). So:

> **DDIM modifies the forward noising joint distribution** $q(x_{1:T}\mid x_0)$. The reverse model
> $p_\theta$ is left untouched — to free-load, you change $q$, not $p_\theta$.

---

## Rebuilding the forward joint (Formula 19)

So each factor in Formula 17 picks up an $x_0$: $q(x_t \mid x_{t-1}) \to q(x_t \mid x_{t-1}, x_0)$.
The paper then "shows off" by jumping straight to the answer — but the honest path is Bayes plus a
telescoping product:

$$
q(x_{1:T}\mid x_0) = \prod_{t=1}^{T} q(x_t \mid x_{t-1}, x_0)
= \prod_{t=1}^{T} \frac{q(x_{t-1}\mid x_t, x_0)\,q(x_t \mid x_0)}{q(x_{t-1}\mid x_0)}.
$$

(That first step is just Bayes applied to each factor — note the swap of $x_t$ and $x_{t-1}$ between
condition and main variable, the tell-tale sign of Bayes.) Split the product:

$$= \prod_{t=1}^{T} q(x_{t-1}\mid x_t, x_0)\ \cdot\ \prod_{t=1}^{T}\frac{q(x_t\mid x_0)}{q(x_{t-1}\mid x_0)}.$$

The second product **telescopes** — write out $t = 1, 2, 3, \dots, T$ and watch the middle factors
cancel:

$$
\prod_{t=1}^{T}\frac{q(x_t\mid x_0)}{q(x_{t-1}\mid x_0)}
= \frac{q(x_1\mid x_0)}{q(x_0\mid x_0)}\cdot\frac{q(x_2\mid x_0)}{q(x_1\mid x_0)}\cdots\frac{q(x_T\mid x_0)}{q(x_{T-1}\mid x_0)}
= \frac{q(x_T\mid x_0)}{q(x_0\mid x_0)}.
$$

By the **Kunkun rule** $q(x_0\mid x_0) = 1$ (*"given Kunkun practiced two and a half years, the
probability he practiced two and a half years is 1"*), the denominator is $1$. Hence:

$$
q(x_{1:T}\mid x_0) = q(x_T\mid x_0)\prod_{t=1}^{T} q(x_{t-1}\mid x_t, x_0). \tag{19}
$$

Finally, tag on the human-set variance subscript $\sigma$ (the distribution is controlled by the
variance), giving the form of Eq. 6 in the *original DDIM paper* (an external reference — not our
lecture-numbered Eq 6):

$$
q_\sigma(x_{1:T}\mid x_0) = q_\sigma(x_T\mid x_0)\prod_{t=1}^{T} q_\sigma(x_{t-1}\mid x_t, x_0). \tag{20}
$$

> **Why does the product start at $t=1$, not $t=2$?** Just like DDPM, the $t=1$ term can be folded
> into the $t \ge 2$ terms (Kunkun rule again), so we *don't* split out $t=1$ as a special case —
> handling one fewer special term makes the rest of the derivation much cleaner.

> **Why does a "forward" joint look like a sum of "reverse" conditionals
> $q(x_{t-1}\mid x_t, x_0)$?** Because Formula 20 is the result *after* applying Bayes. It looks
> incongruous (one side forward-noising, the other reverse-conditioned), but that's exactly the
> Bayes-rewritten form — and crucially, this DDIM forward step relates $t$ to its *previous step*
> $t-1$ (which need not be the strictly-adjacent DDPM timestep) **plus** an $x_0$. The price of
> non-Markovianity is carrying that $x_0$ along.

---

## Deriving $J_\sigma$ and reducing it

The DDIM loss $J_\sigma$ follows the DDPM recipe: **maximum likelihood**, then $\max \to \min$ with a
$\log$ and a minus sign. Using Formula 20 for $q_\sigma$ and Formula 18 for $p_\theta$:

$$
J_\sigma = \mathbb{E}_{q(x_{0:T})}\!\left[-\log\frac{p_\theta(x_{0:T})}{q_\sigma(x_{1:T}\mid x_0)}\right]
= \mathbb{E}_{q(x_{0:T})}\!\left[-\log\frac{p(x_T)\prod_{t=1}^T p_\theta(x_{t-1}\mid x_t)}{q_\sigma(x_T\mid x_0)\prod_{t=1}^T q_\sigma(x_{t-1}\mid x_t, x_0)}\right].
$$

Flip the minus sign into the ratio (invert numerator/denominator) and turn products into sums of
logs:

$$
= \mathbb{E}_{q(x_{0:T})}\!\left[\underbrace{\log\frac{q_\sigma(x_T\mid x_0)}{p(x_T)}}_{\text{model-free, ignore}}
+ \sum_{t=1}^{T}\log\frac{q_\sigma(x_{t-1}\mid x_t, x_0)}{p_\theta(x_{t-1}\mid x_t)}\right].
$$

- The first term involves only $q_\sigma(x_T\mid x_0)$ (known: $x_0$ is data, the endpoint is the
  approximate standard normal) and $p(x_T)$ — **no network**, so it's a constant we drop.
- Each summand is a $\log$ ratio under an expectation → a **KL divergence**:

$$
= \mathbb{E}_{q(x_{0:T})}\!\left[\sum_{t=1}^{T} D_{\mathrm{KL}}\big(q_\sigma(x_{t-1}\mid x_t, x_0)\,\|\,p_\theta(x_{t-1}\mid x_t)\big)\right]. \tag{21}
$$

### Why the outer $\mathbb{E}$ survives the KL

A natural worry: doesn't $\mathbb{E}[\log\frac{q}{p}]$ *become* the KL, consuming the expectation?
Not fully. The $\log$ ratio concerns the $x_{t-1}$ conditional, but the outer expectation is over the
**full joint** $q(x_{0:T})$. Writing it out:

$$
\mathbb{E}_{q(x_{0:T})}\!\left[\log\frac{q_\sigma(x_{t-1}\mid x_t, x_0)}{p_\theta(x_{t-1}\mid x_t)}\right]
= \int \log(\cdots)\, q(x_{0:T})\, dx_{0:T}.
$$

Forming the KL only "uses up" the single $x_{t-1}$ conditional pulled from the joint — the marginals
over the *other* latents ($x_0, x_1, \dots$ except $x_{t-1}$) are still being integrated. So the
outer $\mathbb{E}_{q(x_{0:T})}$ **cannot** be cancelled; it persists. (Recall the KL identity
$D_{\mathrm{KL}}(P\|Q) = \int p(x)\log\frac{p(x)}{q(x)}dx = \mathbb{E}_P[\log\frac{p}{q}]$.)

### Why we may write $p_\theta$ as $q_\sigma$

Next subtlety: in the KL we replace $p_\theta(x_{t-1}\mid x_t)$ with $q_\sigma(x_{t-1}\mid x_t, \hat x_0)$. Is that legal? Yes — because the **functional form is already fixed**. In DDPM, the model
$p_\theta$ predicts $\hat x_0$ (equivalently $\hat\epsilon$ — the two are interchangeable), and the
reverse distribution is a Gaussian with a known mean form (a weighted combo of $x_0, x_t$) and a
fixed variance. In DDIM we worked hard in Part 2 to pin down exactly that $q_\sigma$ form. So
$p_\theta$ *is* a $q_\sigma$ — the **only** difference is that its $x_0$ is the model's estimate
$\hat x_0$ rather than the dataset's true $x_0$:

$$
J_\sigma \doteq \mathbb{E}_{q(x_{0:T})}\!\left[\sum_{t=1}^{T} D_{\mathrm{KL}}\big(q_\sigma(x_{t-1}\mid x_t, x_0)\,\|\,q_\sigma(x_{t-1}\mid x_t, \hat x_0)\big)\right],
\quad \hat x_0 = \text{model estimate}.
$$

Here "$\doteq$" means **equal up to constant terms**.

---

## Collapsing the Gaussian KL to MSE

Both arguments of the KL are the *same* $q_\sigma$ Gaussian, differing only in $x_0$ vs. $\hat x_0$.
Plug into the two-Gaussian KL formula (derived in the DDPM loss post):

$$D_{\mathrm{KL}} = \log\frac{\sigma_2}{\sigma_1} + \frac{\sigma_1^2 + (\mu_1 - \mu_2)^2}{2\sigma_2^2} - \frac12.$$

Drop all constant terms:

- $\log\frac{\sigma_2}{\sigma_1} = \log 1 = 0$ — both variances are the *same human-specified*
  $\sigma_t$.
- $\frac{\sigma_1^2}{2\sigma_2^2} = \frac12$ — cancels the $-\frac12$.

Only the **mean-difference** survives. The two means differ only through $x_0$ vs $\hat x_0$ (the
$kx_0 + mx_t$ form, same $k,m,x_t$), so $(\mu_1 - \mu_2)^2 \propto \|x_0 - \hat x_0\|^2$:

$$J_\sigma \doteq \mathbb{E}_{q(x_0)}\!\left[\sum_{t=1}^{T} \frac{\|x_0 - \hat x_0\|_2^2}{2\sigma_t^2}\right].$$

Now substitute $x_0 = \dfrac{x_t - \sqrt{1-\alpha_t}\,\epsilon_t}{\sqrt{\alpha_t}}$ (invert Formula 2)
for both the true and predicted $x_0$, and fold all the constants into a per-step coefficient
$\gamma_t$:

$$
J_\sigma = \sum_{t=1}^{T}\mathbb{E}_{\epsilon_t\sim\mathcal{N}(\mathbf{0},\mathbf{I})}\!\left[
\gamma_t\,\Big\|\epsilon_t - \epsilon_\theta\big(\sqrt{\alpha_t}\,x_0 + \sqrt{1-\alpha_t}\,\epsilon_t,\ t\big)\Big\|_2^2\right] \doteq L_\gamma.
$$

This is **identical in form to DDPM's loss (Formula 8)** — same $\gamma_t$ out front, same
noise-prediction MSE. The two $\gamma_t$'s might differ, but we may **force them equal**; and even if
they didn't match, a constant scale is absorbed by the learning rate ($L$ tripled ⇔ learning rate
$\times \tfrac13$). To be rigorous the paper sets the $\gamma_t$ in $J_\sigma$ equal to DDPM's,
yielding exactly

$$\boxed{\,J_\sigma = L_\gamma + C.\,}$$

QED. **DDIM's change to the forward process does not change the gradient of the DDPM training loss
— so a DDPM-trained model can be free-loaded by DDIM's sampler.**

---

## The fine print: $\sigma > 0$ vs. DDIM's $\sigma = 0$

One catch: Theorem 1 requires $\sigma > 0$ **strictly**. Why? Look at the $\frac{1}{2\sigma_t^2}$
denominators — at $\sigma_t = 0$ they blow up (division by zero). But DDIM's headline trick is
exactly $\sigma_t = 0$ (the deterministic, Implicit sampler from Part 2). The paper patches this by
**continuity**: the equality holds as $\sigma \to 0$ (i.e. for arbitrarily small $\sigma$), so we may
treat $\sigma = 0$ as the limiting case where Theorem 1 still holds.

---

## Summary

- **Theorem 1:** $J_\sigma = L_\gamma + C$ — DDIM's loss equals DDPM's up to a constant, so their
  gradients agree and the pretrained DDPM model is reusable.
- DDIM changes the **forward** joint $q(x_{1:T}\mid x_0)$ (Formula 17 → 19/20), *not* the reverse
  model $p_\theta$ — because non-Markovianity forbids dropping $x_0$ from $q(x_t\mid x_{t-1}, x_0)$.
- Rebuild the forward joint via Bayes + a **telescoping** product + the Kunkun rule (Eq 20).
- Derive $J_\sigma$ by max-likelihood → drop the model-free $x_T$ term → recognize **KL
  divergences** (the outer $\mathbb{E}$ survives; $p_\theta$ rewrites as $q_\sigma$ with $\hat x_0$).
- The Gaussian KL collapses (equal variances, constants dropped) to the **noise-prediction MSE** —
  identical to DDPM's loss.
- Theorem 1 needs $\sigma > 0$; DDIM's $\sigma = 0$ is justified as the $\sigma \to 0$ limit.

This closes the loop opened in Part 2: changing the **forward noising** process to allow
step-skipping is precisely what lets the **reverse sampling** skip steps — for free.

---

*Notes taken from [a video tutorial](https://www.bilibili.com/video/BV1v2pUeLE2v/) on DDIM (Part 3 of the series).*


---

## References

- Song, J., Meng, C., & Ermon, S. (2021). *Denoising Diffusion Implicit Models.* ICLR 2021. [arXiv:2010.02502](https://arxiv.org/abs/2010.02502)
- Ho, J., Jain, A., & Abbeel, P. (2020). *Denoising Diffusion Probabilistic Models.* NeurIPS 2020. [arXiv:2006.11239](https://arxiv.org/abs/2006.11239)
