I will derive the training loss function of the masked diffusion language model based on the paper Simple and Effective Masked Diffusion Language Models. The derivation will be done from two perspectives:

  1. Discrete-time ELBO Perspective:

    • Following Appendix A and B of the paper, I will derive the ELBO loss rigorously.
    • I will ensure to derive the posterior distribution explicitly using the approach outlined in Discrete Diffusion Modeling by Estimating the Ratios of the Data Distribution.
  2. Discrete Score Matching Perspective:

    • Based on Structured Denoising Diffusion Models in Discrete State-Spaces and Appendix C of the paper, I will derive the loss function using score matching.
    • This will include explicit mathematical steps and explanations to ensure clarity.

1. Discrete-Time ELBO Perspective

1.1 Diffusion Model Setup and ELBO Definition. We consider a discrete diffusion process on text sequences. Let be the vocabulary of tokens and let denote a special absorbing “mask” token. A data sample is a sequence with tokens . The forward diffusion is a discrete-time Markov chain where is the corrupted sequence at time . Each token evolves independently: at each step , unmasked tokens may randomly convert to the mask. Formally, for each position and step , we define the one-step transition probabilities (with and ):

  • If (token is unmasked at ): ,
  • If (token is already masked):

where is a noise schedule (the probability of masking a token at step ). In words, an unmasked token either stays the same with probability or becomes with probability , and once masked, it remains forever (absorbing state). For a given schedule , define as the probability that a token remains unmasked after steps. One can show (with and for sufficiently large ). Equivalently, is the marginal probability a token is still intact at time . Then is the probability it has been masked by time . The forward (corruption) process can be summarized as: for any token , and () (). Moreover, by independence, , which is a mixture distribution: each token is either still the original (with probability ) or has become (with probability ). The prior as concentrates on the fully-masked sequence (all tokens ). We assume the generative model’s prior matches this (i.e. ), so that the KL divergence at is zero ().

A diffusion language model defines a reverse Markov chain to gradually denoise . Here (at time ) should be a high-quality sample from the data distribution . At each step , the model specifies to predict the less corrupted state given . We train by maximizing the data likelihood . Since direct likelihood is intractable, we use the standard variational diffusion ELBO (evidence lower bound) (). Using the forward process as a variational posterior, the discrete-time ELBO can be written as ():

Expanding the joint probabilities and using , this becomes:

where the last terms cancel out since we set (). Rearranging terms yields a sum of KL-divergences for each diffusion step () ():

This is the diffusion training loss: a sum of KL terms between the true posterior and the model at each step () (). Intuitively, at each time we ask the model to retrodict the previous state given the current corrupted state , and we penalize the KL divergence from the true Bayesian posterior. We will derive an explicit form of this loss for the masked diffusion (absorbing state) case.

1.2 Posterior via Data Distribution Ratios. To compute the KL in (*), we need the conditional . By Bayes’ rule one can derive this from known forward quantities. In general, for any and any states , ()

This formula expresses the posterior in terms of data distribution ratios, i.e. the marginals and , and the forward kernel . The approach of Lou et al. (Discrete Diffusion Modeling by Estimating the Ratios of the Data …) () emphasizes learning such ratios for efficiency. In our case, the forward transition is simple enough to derive in closed form. We focus on a single token position (the chain factorizes over positions). There are two cases for a token at time :

  • Case 1: is unmasked (). This means the token survived steps without masking. In the absorbing model, if is still , it must have been at the previous time as well (it could not unmask if it were already ). Thus, and . In other words, when is unchanged, the posterior puts all mass on “no change” at . (This can be confirmed from the general formula above: if , then , , and , so since .) Thus:

  • Case 2: is masked (). Now the token at time is , which could happen in two ways: either it just became masked at step , or it was already masked at . Accordingly, could be either the original token or . The posterior will assign some probability to (meaning the token was up until and then got masked at step ) and the rest to (meaning it was already masked by ). Using the ratio formula with , , , we get ():

Here we used and , and . Likewise,

Thus in Case 2 the posterior is a Bernoulli (or 2-category) distribution on :

q

These results match Appendix A.2.1 of the MDLM paper () (), where (21) and (22) give the same two conditional probabilities. Intuitively, is the probability the token was only just masked at step (because ), whereas is the probability it was masked earlier (masked by ).

1.3 Simplified Training Loss Derivation. We now plug these posteriors into the KL term in (*). Because each position diffuses independently, the KL for step factorizes over token positions. It suffices to derive the loss for a single token, which we then sum over all tokens. Consider one token with original value . At step , there are two sub-cases corresponding to the posterior cases above:

  • If (token is unmasked at ): Then and the model’s prediction can also be designed to deterministically output (since no change is needed). In this case, .
  • If (token is masked at ): Then is given by Posterior Case 2 above, which is a Bernoulli distribution on . The model will likewise produce some probability of unmasking the token (guessing ), and of leaving it masked. We denote for brevity. The single-step KL can be written explicitly as the binary KL between the distributions in Posterior Case 2 and the model’s distribution:

This is the standard cross-entropy expression: it equals the expected negative log-likelihood of the “true” previous-state under the model. In particular, the two terms correspond to two “labels” for when : with probability the correct label is (the token should have been before it got masked at ), and with probability the correct label is (the token was already masked). The model’s probability of label is and of label is . Thus the KL is the entropy of plus the cross-entropy from to . Since the entropy of does not depend on , minimizing the KL is equivalent to minimizing the cross-entropy loss:

This term will be zero if and only if equals the true posterior probability . In particular, the optimal model is , i.e. exactly matching the posterior (often called the “Bayes optimal” reconstruction). MDLM follows a parameterization that makes it easy for the network to achieve this: instead of predicting the binary label directly, the model learns to predict the original token from the masked context, and then uses that prediction to parametrize . In fact, the paper uses a “SUBS” parameterization () (): the network outputs a distribution over vocabulary tokens (an estimate of the original given the corrupted ), and then is constructed such that and () (). This choice (inspired by a similar derivation in Austin et al. ([2107.03006] Structured Denoising Diffusion Models in Discrete State-Spaces) ()) ensures the model’s optimal output is achieved when places all probability on the true token (so that and indeed equals the posterior). In practice, this means the model’s training reduces to predicting the original masked token. Each term in the loss is thus a weighted token prediction error.

Now we assemble the total loss by summing over all time steps to and all token positions. As argued above, only the case contributes nonzero loss. For a given token , let be the (random) step at which it first gets masked (or if it never does). Then the token incurs a loss only at time . Given the data , the probability is (since ). At that time , the expected negative log-likelihood (from the cross-entropy above) for token is (because given it is first masked at , the ground truth label for will surely be ). Therefore, summing over and all tokens :

Using and the above discussion, this simplifies to

where denotes the model’s probability of the original token in the position(s) that are masked in . This is our final training loss function. It has a clear interpretation: at each diffusion time , an additional fraction $(\alpha{t-1}-\alphat)$ of tokens become masked, and the model is asked to predict those tokens from the partially masked sequence . The term is just a standard masked language modeling (MLM) loss (cross-entropy to predict the masked tokens) for corruption level . Thus is a weighted mixture of MLM losses for different mask ratios (). In practice, one can sample a time and apply a mask to each token independently with probability (so that the expected mask proportion is ) and then train the model to predict the original tokens from this partially masked input. MDLM indeed implements training by sampling a random continuous time and masking tokens i.i.d. with probability , then taking a gradient step on the loss (). Here is the continuous-time limit of the weight (see below). In summary, the ELBO perspective shows that masked diffusion training reduces to a series of masked token prediction tasks with different mask percentages. This “simple form” is exactly the Rao-Blackwellized objective derived in MDLM (). It is a principled variational bound that coincides with a mixture of classical MLM losses.

2. Discrete Score-Matching Perspective

2.1 Continuous-Time Diffusion as a CTMC. Alternatively, we can derive the same loss by taking a score-matching view, following ideas from continuous diffusion models. In continuous-state diffusion (DDPM), the ELBO can be reinterpreted as a weighted denoising score-matching objective, where the network is trained to match the gradient of the log-density at each noise level (). An analogous view exists for discrete diffusion. Instead of an SDE, the forward process is a continuous-time Markov chain (CTMC) on a finite state-space. In the limit of large , we can imagine time being continuous. The state of a token follows a CTMC that jumps from to at some rate. In fact, one can show (Appendix C of MDLM) that under the schedule above, the instantaneous hazard rate of masking is () (). For example, if (linear schedule), then , which diverges as ensuring the process hits by . No reverse jumps () occur since is absorbing (so ). This CTMC view lets us derive a continuous-time ELBO. Prior works () () show that as , the sum in (*) converges to an integral. In particular, the discrete KL at step divided by becomes an infinitesimal KL at time , and the sum becomes . Mathematically, one finds:

L∞  =  ∫01Ex0∼q, zt∼q(⋅∣x0)DKL ⁣(q(zt−dt∣zt,x0) ∥ pθ(zt−dt∣zt)) dt .\mathcal{L}{\infty} ;=; \int{0}^{1} \mathbb{E}{x_0\sim q,,z_t\sim q(\cdot\mid x_0)} D{\mathrm{KL}}!\Big(q(z{t-dt}\mid z_t,x_0),\Big|,p\theta(z_{t-dt}\mid z_t)\Big),dt,.

For our absorbing process, is an infinitesimal time step, during which a token either remains in its current state or has an infinitesimal probability of transitioning . Following the derivation in (Austin et al., 2021) and MDLM Appendix C () (), the leading term in the KL above comes from the event of a single jump in . This yields a similar expression to the discrete case, except with and . Thus the weight is replaced by . Dividing by (since the KL ) gives a density w.r.t. time. In this way, one finds the continuous-time objective () ():

This is exactly the integral form of the loss we derived above. The factor serves as a continuous weight emphasizing later timesteps (when is large, is smaller in magnitude; when is small, masking events are rare but each carries more weight) (). We can discretize () by the trapezoidal rule or Riemann sum to recover the discrete formula. For instance, using and , equation () yields

which indeed equals the loss from Section 1.3 above. MDLM’s Appendix C verifies that the continuous-time ELBO matches their simplified objective () (). In their notation, equation (47) of MDLM () corresponds to our (**) (up to sign convention), and Algorithm 1 of their paper explicitly trains the network with this weighting ().

2.2 Interpretation as Score Matching. The form (**) can be understood as a type of score matching. In continuous diffusion, one shows that minimizing is equivalent to the ELBO, with the score (grad-log-density) predictor (). In discrete diffusion, we lack a true spatial gradient, but the “score” information is contained in the ratio of probabilities for staying masked vs. revealing the token. In fact, the model’s logits for “reveal ” vs “keep ” at time correspond to an estimate of the instantaneous time-derivative of the log-probability that the token is still masked. One can show that the optimal predictor satisfies:

i.e. the hazard rate is exactly the ratio of the probability flux to the remaining “masked” probability mass. This quantity is what the network effectively learns to match. Instead of directly regressing this ratio, the network learns it by classification: it outputs the probability of vs (which implicitly encodes the ratio), and the weighting in () arises naturally from the score-matching derivation () (). Minimizing () forces to approach the true posterior for every . In the limit, learns the optimal score function at all times, which is equivalent to learning the correct conditional (hence producing perfect samples). In summary, the score-matching perspective rederives the same weighted sum-of-cross-entropy loss. This perspective, introduced by (Austin et al., 2021) and expanded in MDLM Appendix C, provides further insight that the particular weighting is optimal and that no explicit schedule-dependent tuning is required () ().

2.3 Equivalence of Perspectives. We have derived the masked diffusion LM training loss from two angles: as a discrete ELBO and as a continuous score-matching objective. Both lead to the simple cross-entropy loss that trains the model to predict masked tokens at randomly chosen time steps, with an importance weight for each step. In practice, one can implement this by sampling a time and a mask pattern with marginal , and computing the negative log-likelihood of the original tokens (the same procedure as BERT-style masked language modeling, but with a random mask rate each time). The final loss can be succinctly written (for one token) as

which in discrete time corresponds to , as derived above () (). This loss is a valid variational bound () and has been empirically shown to be simple and effective for diffusion language models. In conclusion, both perspectives are consistent and yield an objective that is a weighted mixture of masked language modeling losses, training the diffusion model by minimizing the expected negative log-likelihood of original tokens given partially masked inputs. This rigorously links the masked diffusion model to familiar MLM training, providing intuition and theoretical justification for the loss function.