You cannot add Gaussian noise to a word. So a diffusion language model erases words into [MASK] and learns to fill the holes. It can write a 12-word sentence in 4 passes, where a left-to-right model needs 12.
Pick a writer and how many passes, then press Write. Watch which words appear in each pass. Then we build, piece by piece, the jumps, the rate matrix, the masking path, the classifier and the sampler behind it.
You need what a probability is and a little Python. Lectures 1 to 4 help but are not required. We build the rest from zero.
Based on Lecture 5 of MIT 6.S184: Introduction to Flow Matching and Diffusion Models (IAP 2026), taught by Peter Holderrieth and Ron Shprints. Slides: Lecture 5 PDF. Course notes: Holderrieth and Erives, An Introduction to Flow Matching and Diffusion Models. It follows Lecture 4, Architectures and Latent Spaces.
Illustrative toy. The denoiser's confidence for each word is made up, and the diffusion writer reveals its most confident words first. A real pass runs a full transformer over all 12 positions. Chapter 8 shows the cost, and what goes wrong with too few passes.
Chapter 0
See why the noise of Lectures 1 to 4 breaks on text, and what replaces it
Take one grey pixel with brightness 0.62. Add a little Gaussian noise and you get 0.70: a slightly lighter grey. Small noise makes a small change. Every flow and diffusion model so far stands on that fact.
Now take the word "cat". A computer stores it as a token: an integer index into a fixed list of words. Say "cat" is token 2.
Add noise and you get 3.4. There is no token 3.4. Round it and you get token 3, which might be "off". The numbers 2 and 3 are labels, not positions, so being close as numbers means nothing.
Top: a pixel's brightness on a line. Bottom: eight words in token order. Drag the noise and press New noise.
Illustrative toy. The word order is arbitrary, as in a real tokenizer. The word's noise is scaled by 4 because token ids span a wider range than a brightness.
The slides say it plainly: there is no SDE and no ODE on a discrete space. What survives is the recipe behind flow matching. Build a path from noise to data, then learn to run it backwards. The tool that carries it to text is the continuous-time Markov chain (CTMC). A CTMC sits on one state and jumps to another at random moments.
Think of a frog on lily pads. It cannot drift halfway between pads. It sits, then hops. We choose how often it hops in each direction. Generation becomes: set the hop rates so that a frog that starts on "noise" lands on a real sentence.
The symptom: a model that adds Gaussian noise to token ids makes nonsense, even at small noise. To measure it, add noise of 0.1 and count how often the rounded token changes meaning. The fix is not smaller noise. It is a different kind of corruption, one that never invents a word between words.
Chapter 1
Count every possible sentence and see why no model can list them
A continuous sample was a point with d real numbers. A text sample is a row of d slots, each holding one token. Two numbers set the size of the world. The vocabulary is the fixed list of allowed tokens; call its size V. The sequence length is the number of slots; call it d.
A sentence is x = (x1, ..., xd), and each xj is one token. The set of every possible sentence is the state space, S. How big is it?
The size of S equals V to the power d.
Slot 1 can hold any of V tokens. So can slot 2, and every slot up to d. The choices multiply, so the count is V multiplied by itself d times.
Toy alphabet A, B, C, so V = 3, and d = 4: 3 × 3 × 3 × 3 = 81 sentences, such as ABCA. With V = 50,000 and d = 1,024, the count is a number with 4,812 digits.
Each bar shows how many digits a number has. Pick a vocabulary, then drag the length.
Exact counts. "Factorized" is the d × V output that Chapter 3 introduces; it is what a transformer produces per pass.
The data distribution, pdata, gives each of those sentences a probability. Grammatical sentences get a lot; keyboard mash gets almost none. We never see pdata. We see a corpus of samples from it.
We add one extra token, [MASK], which means "this slot was erased". A half-erased sentence looks like A [MASK] C [MASK]. With the mask, the toy has 44 = 256 states. This one token is the noise of a masked diffusion model.
The symptom: any method that stores one number per whole sentence runs out of memory at once. Measure it with the bar above: the digits of Vd. The fix is to make the model output a few numbers per slot, never per sentence. Chapter 3 builds that.
Chapter 2
Watch a continuous-time Markov chain hop between states and take one Euler step
An ODE path is a smooth curve. A CTMC path is a staircase: flat while the process sits on a state, then a sudden jump. "Continuous time" means a jump can happen at any moment, not on a clock tick. "Markov" means the next jump depends only on the current state, not on the history.
The staircase is one random path over time 0 to 1. Raise the rate and press New path. Between jumps, nothing moves.
Simulated exactly: the wait before a jump is drawn from an exponential distribution with the total leaving rate. The frog picks either other pad with equal chance.
The flow map of Lecture 1 had no formula, so we stepped the vector field. Here too we need a local rule. The rate from state x to state y at time t is the number of jumps from x to y per unit of time. Call it Qt(y | x). Over a short time h, the chance of a jump is rate times time.
p of y given x, from t to t plus h, is about delta y-equals-x, plus h times Q-t of y given x.
For each other state y, the chance to jump there in the step is h times its rate Q-t of y given x. Whatever chance is left is the chance to stay at x. Draw the next state from these chances.
With made-up numbers: the frog sits on A. The rate to B is 2, the rate to C is 1, and h = 0.1. Jump to B: 0.1 × 2 = 0.2. Jump to C: 0.1 × 1 = 0.1. Stay on A: 1 − 0.3 = 0.7.
That single line is the discrete Euler step. It plays the part of x + h · ut(x) from Lecture 1. Every sampler in this lesson is a loop of it.
The symptom: the sampler crashes with a negative probability. Measure the stay chance, 1 minus h times the total leaving rate. With rates 2 and 1 and h = 0.5, it is 1 − 1.5 = −0.5. The fix: keep h below 1 divided by the total leaving rate, here below 1/3.
Chapter 3
Set the diagonal so probability is kept, and shrink the matrix to one slot at a time
Put every rate in a table: one column per state you leave, one row per state you land on. This is the rate matrix, or generator: the discrete vector field. Rates off the diagonal are zero or more. One rule sets the diagonal.
Q-t of x given x equals minus the sum, over every y not equal to x, of Q-t of y given x.
Add up every rate of leaving x. Write minus that total on the diagonal. Then the column for x sums to zero, and the Euler step's chances sum to 1.
Rates from A: 2 to B, 1 to C. So Q-t of A given A = −3, and with h = 0.1 the stay chance is 0.7. Then 0.7 + 0.2 + 0.1 = 1.0.
Why does this keep the total at 1? Call the probability of state y at time t pt(y). It changes by what flows in minus what flows out.
d by d-t of p-t of y equals the sum over x of Q-t of y given x times p-t of x.
For each state x, multiply its mass p-t of x by its rate into y and add. The term for x = y is negative and takes away what leaves y. This is the Kolmogorov forward equation, the discrete version of the continuity equation.
All mass on A, rates 2 to B and 1 to C. The changes are −3 for A, +2 for B, +1 for C. They add to 0, so the total stays 1. After h = 0.1 the masses are 0.7, 0.2, 0.1.
Bars show the chances after one step from A. Drag the rates and h. Then turn off the diagonal and watch the total.
Exact arithmetic of one Euler step. Rates are made up.
A full rate matrix has one entry per pair of sentences: 81 × 81 = 6,561 for the toy, and far beyond memory for text. So real models are factorized: a jump may change only one slot. Two sentences are neighbours if they differ in at most one slot. AB and AC are neighbours; AB and CC are not.
The model then outputs one rate per slot and per token: d × V numbers, 12 for the toy. In a short step, two slots rarely change together (the chance shrinks like h squared). So the sampler treats slots as independent. Chapter 8 shows the price.
Now code it and break the diagonal.
The symptom: the total drifts away from 1 over many steps. Measure the column sums of Q; each must be 0. The fix is the diagonal rule above.
Chapter 4
Build the forward path that turns a clean sentence into all masks, token by token
A diffusion model needs a path between noise and data. For text the simplest path erases tokens. At time 0 every slot is [MASK]. At time 1 every slot shows its clean token. In between, each slot shows its clean token or a mask.
A scheduler sets how much survives. Call the chance that a slot shows its clean token at time t κt ("kappa-t"). It rises from κ0 = 0 to κ1 = 1. Call the clean sentence z, and its token in slot j zj.
p-t of x-j given z-j equals kappa-t times delta z-j of x-j, plus one minus kappa-t times delta MASK of x-j.
For each slot j, flip a coin that lands heads with chance kappa-t. Heads: show the clean token z-j. Tails: show [MASK]. Each slot gets its own coin.
Linear scheduler, kappa-t = t. A 12-word sentence at t = 0.25: each word survives with chance 0.25, so on average 12 × 0.75 = 9 words are masked. At t = 0.75, 12 × 0.25 = 3 are masked.
Each row is the same 12-word sentence with its own coins. Drag the time and press Re-mask. Compare the average count with 12 × (1 − κ).
Illustrative sentence; the coin flips are real random draws.
Compare this with Gaussian noise. A Gaussian path moves mass through space: the cloud drifts and spreads. The masking path moves nothing. It fades out the clean token and fades in the mask. The slides call it teleporting mass, and this path the factorized mixture path. The mask is an absorbing state: once a slot is masked on the way to noise, it stays masked.
The endpoints check out. At t = 1, κ = 1 and every slot shows z. At t = 0, κ = 0 and every slot is a mask, the same start for every sentence. Now build the forward process in code.
The symptom: the model never learns to write from all masks. Measure the masked fraction at t = 0 over many samples; it must be 1. The fix: make the mask chance 1 − κt, not a constant.
Chapter 5
Find the rate that follows the masking path, then replace the unknown answer with a guess
To generate, we need rates that make the CTMC follow the path from all masks to a sentence. Lecture 2's recipe: solve the case where z is known, then average over what is not.
Fix z. A masked slot jumps straight to its clean token. A slot that is already right never moves. Call the speed of the scheduler, how fast κ rises, κ̇t ("kappa-dot-t").
Q-t-z of y-j given x-j equals kappa-dot-t over one minus kappa-t, times delta z-j of y-j minus delta x-j of y-j.
If slot j already shows z-j, the bracket is 1 − 1 = 0 and nothing happens. If it shows [MASK], it jumps to z-j at the speed factor.
Linear scheduler, so kappa-dot = 1. At t = 0.5 the speed factor is 1 / 0.5 = 2; with h = 0.1 a masked slot un-masks with chance 0.2. At t = 0.9 the factor is 10, so the chance is 1.0: every mask left goes in the last step.
The slides say the rates explode at t = 1. That is how the path ends on a full sentence.
At generation time z is unknown. The trick replaces z with a belief about z, given the half-masked sentence x. Call that belief the posterior, p1|t(zj = v | x): the chance that the clean token in slot j is v.
Q-t of y-j given x equals kappa-dot over one minus kappa, times p-one-given-t of z-j equals y-j given x, minus delta x-j of y-j.
A masked slot jumps to each token y-j at the speed factor times the posterior chance of y-j. A slot that shows a real token has posterior 1 on that token, so its rate is zero.
In the corpus below, x = [M] cat [M] [M]. Slot 1 has posterior "the" 2/3 and "a" 1/3. At t = 0.5 the factor is 2, so the rates are 1.33 and 0.67. With h = 0.1: "the" 0.133, "a" 0.067, stay masked 0.8.
Tap a slot to cycle it through [MASK] and its two words. The lit sentences are the ones that match. Bars under each masked slot are the posterior; yellow numbers are the rates at time t.
Illustrative six-sentence corpus, each sentence equally likely. The posterior here is exact: count the matching sentences. A real model learns to estimate it.
So the posterior is the only unknown. It answers a plain question: what word was in each hole? That is a classifier per slot, a softmax over V tokens.
The symptom: the sampler rewrites words it placed. Measure the posterior at a filled slot; it must be 1. The fix: copy visible tokens through and predict only masked slots.
Chapter 6
Mask a sentence, ask for the hidden words, and score the guesses with cross-entropy
The network takes a half-masked sentence x and the time t. Its output is a table of logits, d rows by V columns: one score per slot per token. A softmax per row turns each row into a posterior. Call the network's posterior pθ1|t, where θ ("theta") is its weights.
Training needs no simulation. Take a clean sentence, mask it at a random time, and ask for the hidden words. The score is the cross-entropy: minus the log of the chance the network gave the true word.
L of theta equals the average, over z, t and x, of the sum over j of minus log p-theta-one-given-t of z-j given x.
Pick a sentence z and a time t. Mask it into x. For each masked slot j, read the chance the network gave the true word z-j, take minus its log, and add. Average over many such draws and step the weights downhill.
With made-up numbers: two slots are masked. The network gives the true words 0.5 and 0.8. The loss is 0.693 + 0.223 = 0.916 nats.
Top row: the clean sentence. Middle row: the masked copy at time t. Bars: what a perfect denoiser would guess. Press New example, or drag t.
The same six-sentence toy corpus. The "perfect denoiser" counts matching sentences, so its loss is the smallest any model can reach.
Try [M] cat [M] [M] with the clean sentence "the cat ran off". Each hidden word has posterior 2/3, so even a perfect model pays 3 × 0.405 = 1.22 nats. That floor is the real ambiguity of the corpus, not an error.
Real systems add one detail. MDLM and LLaDA score only the masked slots and multiply each example by the speed factor. With κt = t that factor is 1 divided by the masked share, the "1/t" in LLaDA's paper, whose t is the masked share. The weight turns the loss into a bound on the model's log-likelihood.
python# One discrete flow matching step. z: LongTensor (B, d) of token ids. t = torch.rand(B, 1) # (B, 1) random times keep = torch.rand(B, d) < kappa(t) # (B, d) True = survives x = torch.where(keep, z, MASK) # (B, d) half-masked input logits = net(x, t) # (B, d, V) one softmax per slot ce = F.cross_entropy(logits.transpose(1, 2), z, reduction="none") # (B, d) loss = (ce * ~keep).sum(1).mean() # score masked slots only loss.backward(); opt.step()
The symptom: training loss is tiny but samples are poor. Measure the loss at small t (mostly masked) and at large t separately. If most examples have few masks, the model rarely practices from scratch. The fix: draw t uniformly and weight by the speed factor.
Chapter 7
Run the Euler step with the learned rate and watch a sentence fill in, out of order
Start with every slot masked. Split time 0 to 1 into n steps, each of length h = 1/n. Each step does three things.
The jump chance comes from Chapter 5. A masked slot un-masks with chance h times the speed factor. With κt = t, that is h/(1 − t). The masked count at time t is about d × (1 − t), so the expected reveals per step are:
With d = 12 and n = 4, that is 3 words per step on average. Every slot is updated from one network pass, at the same time. A left-to-right model makes one pass per word: 12.
Press Step to run one Euler step. Choose random un-masking (the CTMC rule) or most confident first. The bars count reveals per step against d / n.
Illustrative toy denoiser that always knows the right word; only the order and timing are simulated. Confidences are made up.
Real samplers often use the second rule. LLaDA's paper, for example, re-masks its least confident predictions at each step, so the surest words are kept first. Easy words settle early and give context to the hard ones. The order emerges; it is not left to right.
The same loop also edits. Fix some tokens, mask the rest, and let the model fill only the holes. Now write the confident-first sampler yourself.
The symptom: the sentence never finishes. Measure the masks left after the last step. The fix: make the last step un-mask everything; h/(1 − t) is 1 when t = 1 − h. And in this basic sampler, never re-mask a committed token.
Chapter 8
Sample thousands of sentences, find the price of parallel un-masking, and weigh it against left to right
Parallel un-masking draws each slot from its own posterior. Each draw is right on its own. But two slots drawn in the same step do not see each other. In our corpus, "sat" always goes with "down" and "ran" with "off". Un-mask both slots at once and you can get "sat off", which no sentence contains.
The curve shows the share of samples that are real corpus sentences, for 1 to 16 passes. Drag the passes and press Sample to see examples.
The six-sentence corpus with a perfect denoiser and the random Euler rule. If no corpus sentence matches the visible words, the toy denoiser uses the sentences that match the most. Numbers are fresh random draws.
With 1 pass, all four slots are drawn from their own posteriors at once. Each pair of words is 50/50, so 2 × 2 × 2 × 2 = 16 sentences are equally likely, and only 6 are real: 37.5%. With 2 passes about 70% are real, with 4 about 86%, and with 64 about 99%. More passes give each slot more context. A left-to-right model with the true conditional is right 100% of the time, in 4 passes.
The slides close with an honest list of pros and cons.
| Left to right (GPT) | Masked diffusion (MDLM) | |
|---|---|---|
| Order | Fixed, left to right | Any order, often confident first |
| Tokens per pass | One | d / n, your choice |
| Editing and in-fill | Not native | Native: mask and re-fill |
| KV cache | Yes: past keys and values are reused | No: each pass re-reads the whole sentence |
| What it must learn | One order, which fits how text is written | Every order, which is harder |
The KV cache row matters most for speed. A left-to-right pass processes one new token and reuses the rest. A diffusion pass processes all d slots.
So fewer passes do not mean less compute. With d = 12 and n = 4, diffusion runs 4 full passes of 12 slots: 48 slot-passes. Left to right runs 12 cached passes of one token. Whether diffusion wins depends on the hardware and on how few passes still give good text.
The symptom: fluent words, broken phrases ("sat off"). Measure the share of outputs that pass a grammar or corpus check as you lower n. The fix: more passes, confident-first un-masking, or semi-autoregressive blocks that fill a short span at a time.
Chapter 9
Keep the terms, the numbers, the code and the links on one page
Text has no directions, so we replace drift with jumps. A rate matrix whose columns sum to zero plays the vector field. The masking path fades a sentence into [MASK]. The marginalization trick shows that the rate needs one unknown, the posterior over each clean word. So training is classification, and sampling un-masks many slots per pass.
| Term | What it is | Number to remember |
|---|---|---|
| State space S | every sentence of d tokens from V | Vd: 81 for V = 3, d = 4 |
| CTMC | a process that sits, then jumps at random times | jump chance = rate × h |
| Rate matrix Q | rates of every jump; diagonal = minus the total leaving | rates 2, 1, h = 0.1: chances 0.2, 0.1, 0.7 |
| Factorized | one slot changes per jump | d × V outputs: 12 for the toy |
| Scheduler κt | chance a slot shows its clean token | masked on average: d × (1 − κt) |
| Speed factor | κ̇ / (1 − κ) | 2 at t = 0.5, 10 at t = 0.9 for κ = t |
| Posterior p1|t | the guess of each clean token, the only learned part | d × V softmax per pass |
| DFM loss | cross-entropy on the masked slots | guesses 0.5, 0.8: 0.916 nats |
| Sampler | predict, jump, commit, repeat n times | d / n reveals per step |
pythonimport numpy as np def sample(net, d, n, MASK, rng=np.random.default_rng(0)): x = np.full(d, MASK) # X_0: every slot masked h = 1.0 / n for i in range(n): t = i * h post = net(x, t) # (d, V) posterior over clean tokens p_jump = h / (1.0 - t) # kappa_t = t: speed factor times h for j in range(d): # every slot from the same pass if x[j] == MASK and rng.random() < p_jump: x[j] = rng.choice(len(post[j]), p=post[j]) # commit return x # X_1: a sentence
The slides end on why the recipe carried over so cleanly. Flow matching is not tied to flows. It works for any Markov process, a result called Generator Matching (Holderrieth et al., 2024). The vector field and the rate matrix are two cases of one object, the generator.
| Continuous (Lectures 1 to 4) | Discrete (this lesson) |
|---|---|
| ODE or SDE | CTMC |
| Vector field ut(x) | Rate matrix Qt(y | x) |
| Euler: x + h · u | Jump chance h · Q |
| Gaussian start | All [MASK] start |
| Regression loss | Classification loss |
Now press Present or Teach. Explain from memory: no noise on words, the zero column sums, the masking path, the classifier and the d / n sampler. Then go back to the hero and write the sentence again, and predict which words come first.
This lesson re-teaches Lecture 5, "Discrete diffusion models and discrete flow matching", of MIT 6.S184 (IAP 2026). Peter Holderrieth and Ron Shprints teach it, with Tommi Jaakkola as sponsor. Course site: diffusion.csail.mit.edu. Slides: the Lecture 5 PDF. Notes: Holderrieth and Erives, An Introduction to Flow Matching and Diffusion Models. The explanations, toy corpus and devices here are our own.