A minimal implementation of masked text diffusion models, inspired by nanoGPT's simplicity. This implements discrete diffusion for text generation using a transformer architecture.
- Model: Bidirectional transformer (6 layers, 6 heads, 384 dim)
- Diffusion: Progressive token masking with cosine schedule
- Training: Predict original tokens from partially masked sequences
- Generation: Iterative unmasking based on model confidence
pip install -r requirements.txtpython train.pyThis will:
- Download Shakespeare dataset automatically
- Train for 5000 iterations
- Save best model checkpoint
- Show sample generations during training
python generate.pyThis will:
- Load the trained model
- Visualize the noise schedule
- Show the denoising process step-by-step
- Generate sample text
config.py: All hyperparameters in one placemodel.py: Transformer architecture with timestep conditioningdiffusion.py: Noise schedule and diffusion utilitiesdata.py: Character-level tokenizer and data loadingtrain.py: Training loop with validationgenerate.py: Generation and visualization
- Forward Process: Gradually mask tokens according to cosine schedule
- Training: Learn to predict original tokens from masked sequences + timestep
- Sampling: Start with all masks, iteratively unmask based on model confidence
Adjust hyperparameters in config.py:
n_layer,n_head,n_embd: Model sizediffusion_steps: Number of denoising stepslearning_rate,batch_size: Training settingsblock_size: Maximum sequence length