Discrete Diffusion

Dive into Deep Learning · §17.9

Discrete and Masked Diffusion Models
transition matrices Q_t · absorbing (masking) kernel · weighted cross-entropy ELBO · parallel unmasking & independence error · BERT vs. GPT

Why Discrete Tokens Need a Non-Gaussian Corruption

Motivation

  • No continuous vector addition: Adding 0.3\,\boldsymbol\epsilon to token index 412 (“cat”) is meaningless—categorical IDs have no Euclidean metric structure.
  • Sequential bottleneck of GPT: Autoregressive decoding of L = 1{,}024 tokens requires 1{,}024 sequential calls, each a cached pass over one new token.
  • Discrete diffusion (Austin et al., 2021; Sahoo et al., 2024): Corrupt tokens into masks [\textrm{M}] via a Markov chain and train a bidirectional denoiser to unmask tokens in parallel.

Discrete Forward Chains via Transition Matrices

Markov Chains on a Finite Vocabulary

q(\mathbf{x}_t \mid \mathbf{x}_0) = \mathrm{Cat}\big(\mathbf{x}_t;\; \mathbf{x}_0\bar{\mathbf{Q}}_t\big), \qquad \bar{\mathbf{Q}}_t = \mathbf{Q}_1\mathbf{Q}_2\cdots\mathbf{Q}_t

  • Row-stochastic transitions: For one-hot row vector \mathbf{x} \in \{0, 1\}^K, [\mathbf{Q}_t]_{ij} = P(i \to j) corrupts each position independently.
  • Exact Bayes posterior: Given (\mathbf{x}_0, \mathbf{x}_t), the predecessor distribution is an elementwise product (\odot) of backward and forward probabilities: q(\mathbf{x}_{t-1} \mid \mathbf{x}_t, \mathbf{x}_0) = \mathrm{Cat}\!\left(\frac{\mathbf{x}_t\mathbf{Q}_t^\top \odot \mathbf{x}_0\bar{\mathbf{Q}}_{t-1}}{\mathbf{x}_0\bar{\mathbf{Q}}_t\mathbf{x}_t^\top}\right)

Worked Example: Two-Step Binary Flip Posterior

Evaluating the Discrete Forward Posterior by Hand

  • Symmetric 10\% binary flip (\mathbf{Q}_1 = \mathbf{Q}_2): Starting at \mathbf{x}_0 = (1, 0) and observing \mathbf{x}_2 = (0, 1) after two steps means exactly one flip occurred.
  • Forward prior \odot backward likelihood: \mathbf{x}_0\bar{\mathbf{Q}}_1 = (0.9, 0.1) and \mathbf{x}_2\mathbf{Q}_2^\top = (0.1, 0.9) multiply elementwise to (0.09, 0.09) with marginal evidence 0.18.
  • Normalized posterior (0.50, 0.50): The single flip is equally likely to have occurred at step 1 (\mathbf{x}_1 = 1) or step 2 (\mathbf{x}_1 = 0).

The Absorbing (Masking) Kernel

Noise as Token Erasure

On text and discrete image codes, the absorbing kernel (Austin et al., 2021) augments the vocabulary with a mask token [\textrm{M}] (index K). At step t, each unmasked token is independently replaced by [\textrm{M}] with probability \beta_t, while masked tokens stay masked forever:

\mathbf{Q}_t = (1 - \beta_t)\,\mathbf{I} + \beta_t\,\mathbf{1}\mathbf{e}_{\textrm{M}}^\top

  • Idempotent projection collapse: Because \mathbf{P} = \mathbf{1}\mathbf{e}_{\textrm{M}}^\top satisfies \mathbf{P}^2 = \mathbf{P}, the t-step product \bar{\mathbf{Q}}_t collapses analytically with survival probability \bar\alpha_t = \prod_{s=1}^t (1 - \beta_s): \bar{\mathbf{Q}}_t = \bar\alpha_t\,\mathbf{I} + (1 - \bar\alpha_t)\,\mathbf{1}\mathbf{e}_{\textrm{M}}^\top
  • Pure-mask terminal state (\bar\alpha_T = 0): At t = T, every token is [\textrm{M}] with probability 1.

Two-Point Posterior of the Absorbing Chain

Why Visible Tokens Never Change in Reverse

q\big(\mathbf{x}_{t-1} = \mathbf{x}_0 \,\big|\, [\textrm{M}], \mathbf{x}_0\big) = \frac{\bar\alpha_{t-1} - \bar\alpha_t}{1 - \bar\alpha_t} =: w_t, \quad q\big(\mathbf{x}_{t-1} = [\textrm{M}] \,\big|\, [\textrm{M}], \mathbf{x}_0\big) = 1 - w_t

  • If \mathbf{x}_t^i \neq [\textrm{M}] (visible): Never corrupted, so \mathbf{x}_{t-1}^i = \mathbf{x}_t^i with probability 1 (revealed tokens are never revised).
  • If \mathbf{x}_t^i = [\textrm{M}] (masked): Only two predecessors exist—stay [\textrm{M}] with probability 1 - w_t, or unmask \mathbf{x}_{t-1}^i \sim p_{\boldsymbol\theta}(\cdot \mid \mathbf{x}_t) with probability w_t.

The Masked Diffusion ELBO Is Weighted Cross-Entropy

Simplifying the Variational Bound (Sahoo et al., 2024; Shi et al., 2024)

L_{\textrm{VLB}}(\mathbf{x}_0) = \sum_{t=1}^T w_t\;\mathbb{E}_{q(\mathbf{x}_t \mid \mathbf{x}_0)}\!\left[\sum_{i \,:\, \mathbf{x}_t^i = [\textrm{M}]} -\log p_{\boldsymbol\theta}\big(\mathbf{x}_0^i \mid \mathbf{x}_t\big)\right]

  • Exact cancellation inside D_{\textrm{KL}}: Unmasked divergences vanish (0), while (1-w_t)\log\frac{1-w_t}{1-w_t} = 0 leaves -w_t\log p_{\boldsymbol\theta}(\mathbf{x}_0^i \mid \mathbf{x}_t) on masked sites.
  • Linear schedule (\bar\alpha_t = 1 - t/T): A fraction t/T of tokens are masked with weight w_t = 1/t, giving constant expected weighted mask count L/T at every step.

Schedule Invariance and Time-Agnostic Optimality

Continuous-Time CTMC Limit (T → ∞)

L_{\textrm{VLB}}(\mathbf{x}_0) \xrightarrow{T \to \infty} \int_0^1 \frac{-\bar\alpha'(\tau)}{1 - \bar\alpha(\tau)}\,\mathbb{E}\big[\ell(\mathbf{x}_0, \mathbf{x}_\tau)\big]\,d\tau = \int_0^1 \frac{1}{u}\,\mathbb{E}_u\big[\ell(\mathbf{x}_0, \mathbf{x})\big]\,du

  • Schedule invariance: Changing variables to the masking rate u(\tau) = 1 - \bar\alpha(\tau) \in [0, 1] eliminates the schedule \bar\alpha(\tau) completely.
  • Time-agnostic optimality (Ou et al., 2025): Once \mathbf{x}_t reveals which positions are masked, the step index t carries zero extra information about \mathbf{x}_0, so p^\star(\mathbf{x}_0^i \mid \mathbf{x}_t) does not need t.

Implementing the Masked Diffusion Loss and Sampler

Vectorized Cross-Entropy and Jump Unmasking

Because composing absorbing kernels yields another absorbing kernel, unmask can jump across any coarse grid T = t_0 > t_1 > \cdots > t_S = 0 in S evaluations by revealing each masked token with probability (\bar\alpha_s - \bar\alpha_t)/(1 - \bar\alpha_t):

def masked_loss(model, x0, t, alpha_bar, mask_id):
    """One-draw estimate of the masked bound per sequence, in nats."""
    T = len(alpha_bar) - 1
    shape = (-1,) + (1,) * (x0.dim() - 1)      # broadcast over positions
    masked = (torch.rand(x0.shape, device=x0.device)
              < (1 - alpha_bar[t]).view(shape))  # with prob. 1 - abar_t
    logits = model(torch.where(masked, mask_id, x0), t / T)
    nll = -torch.log_softmax(logits, -1).gather(-1, x0[..., None])[..., 0]
    w = (alpha_bar[t - 1] - alpha_bar[t]) / (1 - alpha_bar[t])  # 1/t if linear
    return (T * w.view(shape) * nll * masked).flatten(1).sum(1)
  • Telescoping reveal probabilities: Across S jumps, each position i is unmasked in jump k with probability \Delta_k = \bar\alpha_{t_{k+1}} - \bar\alpha_{t_k} (1/S on an even linear grid), independently of all other positions.

The Parallel Independence Error on Two Tokens

Analytical Derivation on the Quantized 32×32 Mixture

q_S = \left(1 - \frac{1}{S}\right)p + \frac{1}{S}\,p^1 \otimes p^2 \quad \Longrightarrow \quad \mathrm{TV}(q_S, p) = \frac{\mathrm{TV}(p^1 \otimes p^2, p)}{S} = \frac{0.314}{S}

  • Sequential reveal (prob 1 - 1/S): X^1 and X^2 unmask in different jumps (marginal then conditional), drawing an exact sample from p.
  • Simultaneous collision (prob S \cdot (1/S)^2 = 1/S): Both unmask in the same jump from ([\textrm{M}], [\textrm{M}]), drawing independently from p^1 \otimes p^2 and creating phantom modes.

Phantom Modes in the Product of Marginals

Verifying the 0.314 / S Total Variation Law

Because two of our three Gaussian modes share the same vertical mean (y = -1.5), the marginal p^1 has 3 peaks while p^2 has 2 peaks—so the independent product p^1 \otimes p^2 creates 3 \times 2 = 6 blobs (3 real modes + 3 phantom modes carrying 0.32 of the mass):

entropy 4.840 nats, mutual information 0.479 nats
 1 steps, exact network: TV 0.314
 2 steps, exact network: TV 0.157
 8 steps, exact network: TV 0.039
32 steps, exact network: TV 0.010

Random Order vs. Confidence-Ordered Remasking

Why MaskGIT / LLaDA Confidence Ordering Seeks Modes

In practice, MaskGIT (Chang et al., 2022) and LLaDA (Nie et al., 2025) draw candidate tokens at all masked sites and keep the most confident draw (highest predicted marginal probability), remasking the rest. Testing this on our exact 2-token table reveals a hidden bias:

           order     TV  entropy  mass near the modes
        one step  0.314    5.319  0.482 0.321 0.197
two random jumps  0.157    5.216  0.491 0.310 0.199
     fixed order  0.000    4.840  0.500 0.300 0.200
      confidence  0.179    4.406  0.606 0.322 0.072
  • Fixed order (1 token/step): Zero independence error (\mathrm{TV} = 0.000), recovering the exact entropy 4.840 nats and mode weights [0.500, 0.300, 0.200].
  • Confidence remasking (\mathrm{TV} = 0.179, worse than 2 random jumps at 0.157): Because draws from the lightest mode (\pi_3 = 0.20) have lower marginal probability, they lose the confidence contest and get overwritten—collapsing mode 3’s weight from 0.200 to 0.068 and dropping entropy to 4.41 nats.

The ELBO Floor of Any Factorized Denoiser

Why Finite-T Discrete ELBO Exceeds True Entropy by I / T

We train MaskedMLP (K = 32, T = 32) for 6{,}000 updates on quantized mixture pairs and evaluate the exact discrete ELBO summed over all T = 32 steps and mask patterns:

exact bound 4.856 nats; entropy 4.840, floor 4.855
  • Analytical derivation of the H(X^1, X^2) + I(X^1; X^2)/T floor:
    • When X^1 and X^2 are unmasked at different steps (probability 1 - 1/T), their expected cross-entropy is H(X^1) + H(X^2 \mid X^1) = H(X^1, X^2).
    • When both are unmasked in the same step (probability 1/T), predicting them independently from ([\textrm{M}], [\textrm{M}]) costs H(X^1) + H(X^2) = H(X^1, X^2) + I(X^1; X^2).
    • Thus no factorized reverse network can score below H + I/T = 4.840 + 0.479/32 = \mathbf{4.855\text{ nats}} (our trained MLP hits 4.856\text{ nats}, within 0.001 nats of the theoretical floor).

Trained vs. Exact Unmasking Across Step Budgets

Separating Parallel Independence Error from Network Error

Summing the exact output distribution of unmask for our trained pair_model vs. the exact table oracle across S \in \{32, 8, 2, 1\} steps separates the 1/S parallel error from the network’s approximation error:

TV to the table: trained and exact network; TV between the two
32 steps: trained 0.020, exact 0.010, between 0.013
 8 steps: trained 0.045, exact 0.039, between 0.013
 2 steps: trained 0.159, exact 0.157, between 0.012
 1 steps: trained 0.316, exact 0.314, between 0.012
  • 1 step (\mathrm{TV} = 0.315): Dominated entirely by the parallel independence error (0.314); the scatter plot visibly populates the three phantom modes where mode columns and rows cross.
  • 32 steps (\mathrm{TV} = 0.020): Parallel error drops 32\times to 0.010, leaving the network’s fixed \approx 0.010 approximation error.

Scaling to 784-Token Binary Fashion-MNIST Garments

Bits-per-Pixel Bound and 100-Step Unmasking

Thresholding Fashion-MNIST to binary pixels \{0, 1\} with mask token [\textrm{M}] = 2 turns each image into L = 784 tokens. We train a 3-channel-in, 2-channel-out MaskedUNet (T = 100) for 6{,}000 updates:

bound 0.170 bits per pixel, independent pixels 0.702 bits per pixel
  • 4\times compression over independent pixels: The variational bound reaches \approx 0.171\text{ bits/pixel} (\approx 134 bits per garment image) vs. 0.697\text{ bits/pixel} (\approx 546 bits) for independent pixel marginals.
  • 100-step generation (\approx 8 pixels unmasked per jump): Produces coherent binary garment silhouettes.

Diagnosing Parallel Unmasking Failure on Images

Why 4 Jumps Shatter Garments into Speckles

Unmasking L = 784 binary pixels in S = 4 jumps (\approx 196 per jump) vs. S = 100 or 784 jumps (\approx 1 per jump):

     data: speckles 0.0037, standard error over the 1,000 images 0.00016
784 steps: speckles 0.0047, standard error over the 1,000 images 0.00016
100 steps: speckles 0.0075, standard error over the 1,000 images 0.00021
  4 steps: speckles 0.0330, standard error over the 1,000 images 0.00027
  • 4 steps (9\times more speckles than real data): In jump 1, \approx 196 pixels are drawn independently from the all-mask marginal, creating uncorrelated salt-and-pepper noise that subsequent jumps cannot erase.
  • 100 vs. 784 steps (10\,\mathrm{SE} drop in speckles): Refining from 100 to 784 steps eliminates 70\% of the excess speckles using the exact same trained U-Net—proving the 100-step artifacts were parallel independence error.

Unifying GPT, Order-Agnostic AR, and BERT

Sequence Models as Special Cases of Masked Diffusion

  • Autoregressive (GPT) & Order-Agnostic AR: Unmask 1 token/call (L calls total, \mathrm{TV} = 0) along either a causal left-to-right prefix or a random permutation \sigma trained across u \sim U(0, 1) with weight 1/u.
  • Masked Diffusion LM (MDLM / LLaDA): Trained across u \sim U(0, 1) with weight 1/u, but reveals \approx L/S tokens/call in parallel—trading \propto 1/S independence error for L/S\times fewer sequential calls (each a full pass over all L positions, with no KV cache).
  • Masked LM (BERT): Single fixed masking slice u = 0.15; cannot generate from the u = 1.0 all-mask start.

Chapter 17 in Retrospect: Strengths and Open Frontiers

Comparing Diffusion & Flow Models Across the Design Space

Across all nine sections, one principle drove every continuous and discrete generator—regress onto the clean point across all corruption levels, then integrate backward from pure noise/mask:

Generative Capability How Diffusion / Flow Matching Achieves It Inherent Engineering Trade-Off & Open Frontier
Stable single-network training Least-squares regression onto \hat{\mathbf{x}}_0, \hat{\boldsymbol\epsilon}, \mathbf{u}_t, or \log p(\mathbf{x}_0^i \mid \mathbf{x}_t) Eliminates GAN minimax instability, but requires multi-step integration at inference
Exact likelihoods & mode coverage Continuous ELBO (w(\lambda) \equiv 1) and ODE change of variables Best likelihood weighting (w \equiv 1) differs from best perceptual sample weighting (e^{-\lambda/2})
Fast 20–50 step ODE sampling Deterministic DDIM, 2nd-order Heun, and multistep solvers Still 20–50\times slower than 1-step generators without secondary flow-map distillation
High-resolution scaling (512^2+) Latent diffusion (VAE) + adaLN-Zero Diffusion Transformers Adds a two-stage pipeline where autoencoder modelability bottlenecks sample FD
Parallel discrete token generation Absorbing masked diffusion (w_t = (\bar\alpha_{t-1}-\bar\alpha_t)/(1-\bar\alpha_t)) Simultaneous reveals incur \propto 1/S independence error and cannot revise committed tokens

Concept Check: Absorbing Posteriors & Parallel Error

Check Your Understanding

Test your mastery of discrete and masked diffusion mechanics:

  1. Absorbing vs. uniform kernel visibility: Under a uniform corruption kernel \mathbf{Q}_t = (1 - \beta_t)\mathbf{I} + (\beta_t/K)\mathbf{1}\mathbf{1}^\top, every token value has positive posterior probability even at unmasked positions. What question must a network answer at every visible token under the uniform kernel that it never has to answer under the absorbing kernel?
  2. Linear-schedule ELBO weights: For a pair of tokens (L = 2) under a linear schedule \bar\alpha_t = 1 - t/T with T = 32 steps, what are the step weights w_1 and w_{32}, and what is the expected weighted count of masked tokens at any step t?

Answers:

  1. Under the uniform kernel, the network must infer whether each visible token is genuine clean data or was randomly resampled by noise; under the absorbing kernel, any visible token (\neq [\textrm{M}]) is guaranteed to be 100\% clean.
  2. Since w_t = 1/t, we have w_1 = 1 and w_{32} = 1/32. At step t, the expected number of masked tokens is L(t/T) = 2t/32, so the expected weighted count is (2t/32) \cdot (1/t) = \mathbf{1/16 = L/T} at every step t.

Concept Check: Absorbing Posteriors & Parallel Error (cont.)

Check Your Understanding

  1. Splitting parallel error from network error: On our quantized mixture pair, an exact oracle sampler has total variation error 0.314 / S, while our trained pair_model has \mathrm{TV} = 0.020 at S = 32 steps. Will increasing the sampler budget to S = 1{,}024 steps push the trained network’s TV error below 0.005?

Answer:

  1. No: At S = 32, the parallel independence error is 0.314/32 \approx 0.010 and the trained network’s own conditional approximation error is \approx 0.010. Taking S = 1{,}024 eliminates the 0.314/S term, but the \approx 0.010 network error remains fixed regardless of step count.

Summary: Discrete and Masked Diffusion Models

Key Takeaways

Concept / Formula Mathematical Expression Key Property / Scaling Role in Discrete Diffusion
Absorbing Forward Chain \bar{\mathbf{Q}}_t = \bar\alpha_t\mathbf{I} + (1 - \bar\alpha_t)\mathbf{1}\mathbf{e}_{\textrm{M}}^\top Token survives t steps with prob \bar\alpha_t = \prod_{s \le t}(1-\beta_s) Maps any sequence to all-mask [\textrm{M}]^L at \bar\alpha_T = 0
Absorbing Step Posterior q(\mathbf{x}_{t-1} = \mathbf{x}_0 \mid [\textrm{M}], \mathbf{x}_0) = w_t = \frac{\bar\alpha_{t-1}-\bar\alpha_t}{1-\bar\alpha_t} Unmasked tokens copy (\mathbf{x}_t); masks revert with prob w_t Concentrates all KL divergence onto masked positions
Masked Diffusion ELBO \sum_{t=1}^T w_t\,\mathbb{E}_q\!\big[\sum_{i:\mathbf{x}_t^i=[\textrm{M}]} -\log p_{\boldsymbol\theta}(\mathbf{x}_0^i \mid \mathbf{x}_t)\big] Continuous limit \int_0^1 \frac{1}{u}\mathbb{E}_u[\ell]\,du; w_t = 1/t if linear Schedule-invariant bound; optimal predictor is time-agnostic
Factorized ELBO Floor H(X^1, X^2) + \frac{1}{T}I(X^1; X^2) Excess over entropy vanishes as 1/T \to 0 Trained 2-token MLP matches theoretical floor within 0.001 nats
Parallel Independence Error q_S = (1 - \frac{1}{S})p + \frac{1}{S}p^1 \otimes p^2 \mathrm{TV}(q_S, p) = \mathrm{TV}(p^1 \otimes p^2, p)/S \propto 1/S Simultaneous reveals draw from product of marginals (speckles/phantoms)