AnyLearn
All lessons
AIadvanced

The Mathematics of Discrete Diffusion

The formal machinery under masked text generation: categorical transition matrices, the kernel choices that were tried and discarded, the variational bound, and the two parameterizations that made the objective trainable at scale.

Updated · AI-authored, review-gated · how lessons are made

Not signed in: your progress and quiz score won't be saved.
Progress1 / 9

What a corruption process has to provide

A companion lesson introduced diffusion language models at the level of mechanism: mask tokens, predict them in parallel, unmask iteratively. This path takes the machinery apart, and the right place to start is what a diffusion process must supply before any of that works.

Continuous diffusion leans on three properties of Gaussian noise. You can jump to any corruption level in closed form without simulating the intermediate steps. The endpoint is a distribution you can sample from trivially. And the reverse of a small noising step is itself approximately Gaussian, so a network predicting a mean is enough.

Discrete data has none of this for free. Adding noise to token 17,432 is undefined, because token IDs are labels, not quantities. Any discrete process has to be built so that the three properties hold.

The construction that works treats corruption as a Markov chain over the vocabulary, and once you write it that way, the entire apparatus of diffusion follows from linear algebra on categorical distributions.

Full lesson text

All 9 steps on one page, for reading, reference, and search.

Show

1. What a corruption process has to provide

A companion lesson introduced diffusion language models at the level of mechanism: mask tokens, predict them in parallel, unmask iteratively. This path takes the machinery apart, and the right place to start is what a diffusion process must supply before any of that works.

Continuous diffusion leans on three properties of Gaussian noise. You can jump to any corruption level in closed form without simulating the intermediate steps. The endpoint is a distribution you can sample from trivially. And the reverse of a small noising step is itself approximately Gaussian, so a network predicting a mean is enough.

Discrete data has none of this for free. Adding noise to token 17,432 is undefined, because token IDs are labels, not quantities. Any discrete process has to be built so that the three properties hold.

The construction that works treats corruption as a Markov chain over the vocabulary, and once you write it that way, the entire apparatus of diffusion follows from linear algebra on categorical distributions.

2. Corruption as a transition matrix

Jacob Austin and colleagues formalised this in "Structured Denoising Diffusion Models in Discrete State-Spaces" (NeurIPS 2021), the paper that named D3PM and set the framework everything since has used.

Represent a token as a one-hot row vector xx over a vocabulary of size VV. Define the corruption at step tt by a V×VV \times V transition matrix QtQ_t, where entry [Qt]ij[Q_t]_{ij} is the probability that token ii becomes token jj:

q(xtxt1)=Cat(xt;p=xt1Qt)q(x_t \mid x_{t-1}) = \text{Cat}(x_t; \, p = x_{t-1} Q_t)

The crucial consequence is that composing steps is just matrix multiplication. Define the cumulative matrix Qˉt=Q1Q2Qt\bar{Q}_t = Q_1 Q_2 \cdots Q_t, and you get the marginal at any level in one shot:

q(xtx0)=Cat(xt;p=x0Qˉt)q(x_t \mid x_0) = \text{Cat}(x_t; \, p = x_0 \bar{Q}_t)

That recovers the first required property. During training you sample a random tt, corrupt directly to that level, and never simulate the chain.

Everything now hinges on one design decision: what to put in QtQ_t.

3. The kernels that were tried

D3PM catalogued several structured choices, and the differences between them are substantive rather than cosmetic.

Uniform. With some probability, replace the token with one drawn uniformly from the vocabulary. This is the discrete analogue of isotropic noise and was the earlier multinomial diffusion approach. Its weakness is that the model cannot tell a corrupted token from a genuine one, so it must second-guess every position at every step.

Absorbing. With some probability, replace the token with a special [MASK] state that has no outgoing transitions. Once masked, always masked.

Ordinal or Gaussian-like. Concentrate transition probability on nearby token indices, mimicking a Gaussian kernel. Sensible for quantised images where index order means something, meaningless for text where token 500 and token 501 are unrelated.

Embedding-nearest-neighbour. Transition preferentially to tokens that are close in embedding space, which injects semantic structure into the corruption.

Text converged on the absorbing kernel, and it is worth being precise about why, because the reason is structural rather than empirical taste.

4. Why the absorbing kernel wins

The absorbing kernel has a property no other choice offers: corruption is self-identifying. A [MASK] symbol announces that this position is noise; every other position is known-clean. The model never wastes capacity deciding what is signal.

Its cumulative matrix is also trivially simple. Under a schedule where the mask probability at time tt is αt\alpha_t, a token has either survived untouched or been absorbed:

q(xtx0)=(1αt)δx0+αtδ[MASK]q(x_t \mid x_0) = (1 - \alpha_t)\,\delta_{x_0} + \alpha_t\,\delta_{\texttt{[MASK]}}

No matrix powers, no vocabulary-sized products. Sampling a corrupted sequence is one Bernoulli draw per position.

The reverse process inherits the same simplicity. Because masking is irreversible in the forward direction, the reverse only ever needs to answer one question: given the visible context, what was this masked token? Unmasked positions require no decision at all.

The uniform kernel, by contrast, forces the reverse model to consider revising every position at every step, a strictly harder problem with no compensating benefit for text. The empirical gap follows from the structural one.

5. Building a discrete diffusion model

Every discrete diffusion model is assembled from the same four decisions. Fixing the kernel to absorbing collapses most of the downstream complexity, which is why the masked branch of this tree became the mainstream path.

flowchart TD
  A["Choose a transition matrix family"] --> B["Uniform: model must detect corruption"]
  A --> C["Absorbing: mask is self-identifying"]
  C --> D["Closed-form marginal from one Bernoulli draw"]
  D --> E["Reverse model predicts only masked positions"]
  E --> F["Bound reduces to weighted masked-token loss"]
  B --> G["Reverse model must revise every position"]

6. The training objective

Like every diffusion model, a discrete one is trained by maximising a variational bound on the log-likelihood. The generic D3PM bound is a sum over timesteps of KL divergences between the true reverse posterior q(xt1xt,x0)q(x_{t-1} \mid x_t, x_0) and the model's pθ(xt1xt)p_\theta(x_{t-1} \mid x_t), plus a reconstruction term.

For the absorbing kernel this collapses dramatically. Because an unmasked token is known with certainty and a masked one is predicted directly from context, each KL term reduces to a cross-entropy on masked positions, weighted by the noise schedule. In continuous time the bound becomes

L=Et,xt[αtαti:xti=[MASK]logpθ(x0ixt)]\mathcal{L} = \mathbb{E}_{t,\,x_t}\left[\frac{\alpha_t'}{\alpha_t} \sum_{i \,:\, x_t^i = \texttt{[MASK]}} \log p_\theta(x_0^i \mid x_t)\right]

Read that carefully, because it is the punchline of the whole framework: the loss is masked language modelling with a schedule-dependent weight. BERT's objective, generalised over all masking ratios instead of a fixed 15 percent, and reweighted so it forms a genuine bound on sequence likelihood.

That single observation is why diffusion language models could be trained with existing infrastructure almost immediately.

7. The objective in code

Stripped to its essentials, a training step is short enough to read in full:

def mdlm_loss(model, x0, mask_id):
    B, L = x0.shape

    # 1. sample a corruption level per sequence, uniform in (0, 1]
    t = torch.rand(B, 1, device=x0.device).clamp_min(1e-3)

    # 2. corrupt: mask each token independently with probability t
    masked = torch.rand(B, L, device=x0.device) < t
    xt = torch.where(masked, mask_id, x0)

    # 3. predict the originals at every position
    logits = model(xt)                      # bidirectional, no causal mask

    # 4. cross-entropy on masked positions only, weighted by 1/t
    ce = F.cross_entropy(logits.transpose(1, 2), x0, reduction="none")
    return ((ce * masked) / t).sum() / masked.sum().clamp_min(1)

Three details carry the weight. The masking ratio is resampled every batch rather than fixed, so the model learns to denoise from any corruption level. The loss is computed only on masked positions, since unmasked ones are given. And the 1/t1/t factor is the schedule weighting that turns a heuristic into a likelihood bound: lightly masked examples are easy and get downweighted.

8. Two parameterizations that made it work

The bound above is correct but was not immediately competitive. Two papers closed most of the remaining gap by changing how the reverse model is parameterized, not by changing the underlying process.

MDLM, from Subham Sahoo and colleagues (NeurIPS 2024), introduced the SUBS parameterization, built on two constraints that are true by construction and so should be hard-coded rather than learned. Zero masking probability: the model should never predict [MASK] as an original token. Carry-over unmasking: an already-revealed token stays fixed and is never re-predicted. Enforcing both simplifies the absorbing-state bound into a clean weighted mixture of masked-language-modelling losses, and a Rao-Blackwellised continuous-time version cuts gradient variance further. The result set state-of-the-art perplexity among diffusion models on standard benchmarks.

SEDD, from Aaron Lou, Chenlin Meng, and Stefano Ermon (ICML 2024), took a different route: rather than predicting tokens, estimate ratios between probabilities of neighbouring sequences, the discrete analogue of a score. Its score-entropy objective is the closest thing to score matching that discrete spaces admit, and it substantially narrowed the perplexity gap to autoregressive models.

9. Comparing the formulations

The three formulations that matter, judged on what they actually give you:

D3PM (absorbing)MDLMSEDD
PredictsOriginal tokensOriginal tokensProbability ratios
Key ideaTransition matricesSUBS constraintsScore entropy
ObjectiveDiscrete-time boundContinuous-time, Rao-BlackwellisedScore-matching analogue
Gradient varianceHigherLowerModerate
ImplementationStraightforwardStraightforwardMore involved

One gotcha deserves emphasis before the next lesson, because it recurs in every published comparison. These models report bounds on log-likelihood, not exact values. An autoregressive model factorises the sequence probability exactly and reports true perplexity.

So a diffusion model reporting perplexity 20 against an autoregressive model reporting 18 is not straightforwardly worse: the diffusion number is an upper bound on its true perplexity, and the size of the gap between bound and truth is unknown. Comparisons in this literature are apples to oranges unless the evaluation protocol is stated, and this is the single most common way results here get over-read.

Check your understanding

The lesson ends with a 5-question quiz. Take it in the player above to see your score.

  1. In the D3PM framework, what makes it possible to corrupt a sequence to any noise level without simulating intermediate steps?
    • The reverse model predicts the corruption level directly
    • The cumulative transition matrix is the product of per-step matrices, giving a closed-form marginal
    • Token embeddings are Gaussian by construction
    • The vocabulary is sorted so that corruption is monotone
  2. Why did text-based discrete diffusion converge on the absorbing (masking) kernel rather than the uniform kernel?
    • It allows the use of Gaussian noise on token embeddings
    • It produces exact likelihoods rather than a bound
    • It requires fewer parameters in the reverse model
    • Masked positions are self-identifying, so the reverse model only predicts known-corrupted positions
  3. What does the absorbing-state variational bound reduce to in practice?
    • Masked language modelling loss over all masking ratios, weighted by the noise schedule
    • Standard next-token prediction with a causal mask
    • Contrastive loss between clean and corrupted sequences
    • Mean squared error between predicted and true embeddings
  4. What are the two constraints behind MDLM's SUBS parameterization?
    • Fixed masking rate and causal attention
    • Uniform noise schedule and shared encoder-decoder weights
    • The model never predicts [MASK] as an original token, and already-revealed tokens stay fixed
    • Tokens are unmasked strictly left to right and never revised
  5. Why is comparing a diffusion language model's perplexity to an autoregressive model's misleading?
    • Diffusion models use a different tokenizer by necessity
    • Diffusion models report an upper bound on log-likelihood while autoregressive models factorize exactly
    • Perplexity is undefined for bidirectional models
    • Autoregressive perplexity is measured only on held-out prompts

Related lessons