A hands-on, build-it-yourself course on diffusion models for text.
Train a real (tiny) language diffusion model in seconds โ every line of math tied to the paper it comes from.
๐ด Live interactive demo โ watch text denoise, then train a real model in your browser.
Most explanations of language diffusion are either a wall of equations or a black-box library call. This course sits in between: small, runnable notebooks that build each idea from scratch, so you can see the forward process corrupt text, watch the model denoise it back, and read the exact equation each block of code implements.
Autoregressive (GPT-style) models write left-to-right, one token at a time. Diffusion models start from noise and refine the whole sequence in parallel over a chosen number of steps โ giving native infilling/editing, controllable compute, and a different scaling story. Recent work (LLaDA, 2025) shows the approach is competitive with autoregressive LLMs.
Youโll build the masking models behind MDLM and LLaDA, the general D3PM framework, embedding-space diffusion (Diffusion-LM), a scaled-up conditional diffusion LLM, and score-entropy diffusion (SEDD) โ all from scratch.
Before the math, get the feel: โถ open the live interactive playground. Watch a sentence dissolve into noise and re-form, scrub the noise level, toggle the noise kernel (mask vs. random), step through the reverse-trajectory filmstrip, compare confidence-first vs. random unmasking, poke a live transition matrix โ and then train a real masked-diffusion model right in your browser (hand-written backprop, live loss plot, and generation). An intuition-first tour of Lessons 1, 2 & 4. (The animation above is the real page.)
Runs entirely client-side โ no install, no server. Or clone the repo and open
interactive/index.htmlin any browser.
# 1. Install (CPU build shown; for GPU use the matching CUDA wheel from pytorch.org)
pip install torch --index-url https://download.pytorch.org/whl/cpu
pip install -r requirements.txt
# 2. Open the first lesson
jupyter notebook 01_foundations_masked_diffusion.ipynb
Prefer zero install? Every notebook has an badge at the top โ click it to run the lesson on a free GPU in your browser, nothing to install.
The models are intentionally tiny โ everything runs on a laptop CPU in a minute
or two. The notebooks auto-detect a GPU (device = "cuda" if torch.cuda.is_available() else "cpu") and use it when present.
Every push is checked by CI: the two math oracles run, all six notebooks are rebuilt from their tools/build_lesson*.py scripts, and each is smoke-tested end-to-end.
| # | Notebook | What you build | Papers |
|---|---|---|---|
| 1 | Foundations: Masked Diffusion | A working masked diffusion LM โ forward masking, bidirectional denoiser, ELBOโweighted-CE, sampling, infilling | MDLM, LLaDA |
| 2 | D3PM: General Transition Kernels | Uniform vs. absorbing kernels with validated closed forms, the term-by-term ELBO, the hybrid loss, a kernel bake-off | D3PM |
| 3 | Diffusion-LM: Continuous Diffusion | Gaussian diffusion in embedding space + rounding; DDPM sampling; gradient-guided controllable generation | Diffusion-LM, DDPM |
| 4 | Making Masked Diffusion Practical | LLaDA-style: generalization (grammar corpus), classifier-free guidance, confidence-based sampling + the stepsโquality trade-off | LLaDA, MaskGIT |
| 5 | SEDD: Score-Entropy Diffusion | Learn the distributionโs ratios (concrete score) via score-entropy; the learned score is verified to match the true ratioโs shape (corr โ 0.99) | SEDD |
| 6 | Unmasking Strategies | (Part II) The decoding zoo: confidence/entropy/margin orders vs. random, and remasking / self-correction โ one fixed model, big quality swings | MaskGIT, LLaDA |
๐ See CONTENT.md for the full syllabus, learning path, and concept map.
๐งฎ See reference/D3PM_MATH.md for the worked, numerically-validated D3PM derivations.
tools/d3pm_reference_check.py) cross-checks the matrix math. If the math were wrong, the notebook would fail..
โโโ README.md โ you are here
โโโ CONTENT.md โ full syllabus & learning path
โโโ 01_foundations_masked_diffusion.ipynb โ Lesson 1
โโโ 02_d3pm_transition_kernels.ipynb โ Lesson 2
โโโ 03_diffusion_lm_embedding_space.ipynb โ Lesson 3
โโโ 04_scaling_diffusion_llm.ipynb โ Lesson 4
โโโ 05_sedd_score_entropy.ipynb โ Lesson 5
โโโ 06_unmasking_strategies.ipynb โ Lesson 6 (Part II)
โโโ interactive/ โ self-contained interactive playground (index.html)
โโโ papers/ โ the cited papers (PDFs) + a per-lesson index
โโโ reference/
โ โโโ D3PM_MATH.md โ worked, validated D3PM math
โโโ tools/
โ โโโ build_lesson1.py โฆ build_lesson6.py โ regenerate each lesson's notebook
โ โโโ d3pm_reference_check.py โ numerical oracle for the D3PM math (Lesson 2)
โ โโโ sedd_reference_check.py โ numerical oracle for the SEDD math (Lesson 5)
โโโ requirements.txt
โโโ LICENSE
The notebooks are authored from the
tools/build_lesson*.pyscripts (cell sources live as plain Python, which is easier to review and diff than notebook JSON). Run a build script to regenerate its.ipynb; you never need to โ the committed notebooks are ready to open.
This is a learning resource โ issues, corrections, and suggestions are very welcome. Found a clearer explanation, a bug, or a typo in the math? Open an issue or a PR.
MIT โ free to use, learn from, and build on.
This is a from-scratch teaching re-implementation. All credit for the ideas,
methods, and results belongs to the authors below โ please cite the original
papers, not this course. PDFs are collected in papers/ (see
papers/README.md for the per-lesson mapping and a note on
redistribution).
Foundations
Discrete / masked diffusion
Continuous / embedding-space diffusion
Sampling / generation