language-diffusion-models

๐ŸŒ€ Language Diffusion Models โ€” from scratch

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.

Python PyTorch License Status CI

๐Ÿ”ด  Live interactive demo  โ€”  watch text denoise, then train a real model in your browser.


Why this course?

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.

๐ŸŽฎ Try it first โ€” interactive playground

A sentence dissolving into noise and re-forming โ€” the interactive diffusion playground

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.html in any browser.

Quickstart

# 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 Open In Colab 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.

The lessons

# 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.

What makes these notebooks different

Repository layout

.
โ”œโ”€โ”€ 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*.py scripts (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.

Contributing & feedback

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.

License

MIT โ€” free to use, learn from, and build on.

References & Acknowledgments

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