Aditya Dutt Pandey

Build · 2025

Published · 7 min read

I trained a language model that writes by filling in blanks

Most language models write one token at a time, from left to right. I trained a small masked diffusion model on TinyStories that starts from a page of blanks and fills them over several passes. It learned the sound of a children's story, but not how to keep one going. Here's what worked, what didn't, and what I still need to test.

Most language models write the way we read: one word after another, each new choice depending on everything already written. I wanted to try a different bargain. What if a model could look at the whole sentence, start with every word missing, and fill in the easy parts first?

So I built a small masked diffusion language model, trained it on TinyStories on my laptop, and watched it learn the sound of a children's story before losing track of the story itself. This is a learning project, not a claim that diffusion beats the language models people use every day. The samples and limitations matter more than the idea on paper.

Start with a sentence of blanks

A GPT-style model predicts the next token from the tokens to its left. This model is an encoder-only transformer: it can attend to tokens on both sides of every position. At training time, I hide some of a real text sequence and teach the model to recover the original tokens. At generation time, I give it a sequence of [MASK] tokens and ask it to fill in the missing text over several passes.

A simplified example:

[MASK] [MASK] [MASK] [MASK] [MASK] [MASK] [MASK] [MASK]
[MASK] upon  [MASK] [MASK] [MASK] [MASK] [MASK] [MASK]
once  upon  [MASK] [MASK] there  [MASK] [MASK] [MASK]
once  upon  a      time   there  was    a      mouse

The real sampler does not simply keep the most confident predictions each time. The run behind these samples used the random reverse process: each masked position has a schedule-dependent chance of staying masked, and otherwise gets sampled from the model's predictions. There is also a confidence-based mode that reveals the positions the model is most sure about first. Either way, several tokens can be committed in one pass without seeing each other's new choices. That is one of the central tradeoffs, not a minor implementation detail.

This is the text version of a diffusion idea used in image generation: start from corruption and gradually turn it into a sample. Images can be nudged through small changes; text cannot. A token is either mouse or it isn't, so the corruption here is discrete: a token is visible or replaced by [MASK].

The model and the run

The checkpoint behind the results is the legacy, BERT-like version of the model: 41,175,552 parameters, eight transformer layers, width 512, eight attention heads, and tied input and output embeddings. It uses learned position embeddings and was trained on fixed 128-token chunks. The tokenizer is BERT WordPiece, which lowercases text; that is why generated samples are all lowercase and sometimes decode with awkward spaces.

Training uses TinyStories, a dataset of short stories for children. Stories are tokenized, concatenated into a stream, then divided into 128-token chunks. For each chunk, the model draws a random diffusion timestep and masks tokens according to a cosine schedule. It predicts the original token at every position, using cross-entropy loss.

I trained locally on an Apple M4 Pro with 24 GB of unified memory, using PyTorch's MPS backend and bf16 autocast. The tinystories_fast run took 3 hours 41 minutes: 50,000 micro-batches, or 8,333 optimizer updates, about 154 million tokens and 0.57 of an epoch. It used the full TinyStories training split, with 0.5% held out for validation. No cloud cluster.

One choice in the objective had a big effect on how to read the results: the loss averages over all positions, not just masked ones. The unmasked tokens are already visible and easy to copy, so they dilute the loss. That means the validation number is not ordinary language-model perplexity, or even masked-token perplexity. It is useful for comparing runs with this same setup, but not for comparing against published language models.

What the model learned

Two earlier 50,000-step runs did not learn much beyond word frequencies. Their validation score stayed around 200–217, and their samples were word salad. The successful run changed two things: it moved to all-position loss, and it reduced the diffusion horizon from 1,000 to 100. Its score fell from around 210 to 9.2 at step 7,500, and kept falling.

I did not run a clean ablation that changes one of those choices at a time. So I cannot say which change made training work. That uncertainty is part of the result.

The successful run's validation number reached 3.793 at step 45,000. Because the score includes many unmasked positions, it makes the model look better than masked-token prediction alone would. The average masking rate is roughly 36%; dividing the loss by that fraction suggests a rough upper estimate near 40 perplexity on masked tokens, but I did not measure that directly. Treat 3.793 as a within-this-setup training signal, not a headline benchmark.

Here are unedited samples from step 45,000, generated in random mode with 32 steps:

once upon a time there was a mouse. the rat had very and he once upon a timethere was a mouse in his gray house. one day, it found something on the ground. he ran scared. the mouse decided to climb....

so sarah loved to stay to her. when she found her mom and smiled. she was about the garden. suddenly once upon a time, there was a girl named sarah. every morning she thought she had a flashlight an...

ben said, " but please want the bunny., i do you you want to do. you can do? " the bunny said. " i need you know. i know, i want to help. i can ' t find me," mia. ". but as they ran with the bunny. s...

The model has picked up the TinyStories register: familiar names, short declarative sentences, quoted dialogue, and the "once upon a time" opening. For a sentence or two, the text sounds like the right kind of story. Then it restarts, wanders or repeats itself. In the last sample, "i do you you want to do" is exactly the kind of local collision parallel sampling can create.

What went wrong along the way

The run's learning-rate schedule also did not behave as intended. The training loop counts micro-batches, while the schedule was configured in optimizer updates. With six accumulation steps, a warmup of 2,000 optimizer steps became 12,000 micro-batches. The run ended with the learning rate still near its 3e-4 peak, so the cosine decay barely started. I have not changed this in the code used for the reported run; fixing it would make a new run, not reproduce the old one.

The newer model variant in the working tree adds adaLN-Zero conditioning and rotary position embeddings (RoPE). The timestep can condition each transformer block, and RoPE replaces the learned position table. But this variant has not been trained, and the training model builder does not pass through the RoPE settings. A checkpoint can therefore save a config that claims RoPE while the trained model used learned positions. It is a wiring bug, not evidence that RoPE works or fails here.

The limits are the story

The model has not learned to hold a plot. It trains on fixed 128-token windows, so a generated sample can begin or end mid-thought; the legacy model also has a learned position table of exactly that length. The BERT tokenizer lowercases text and decodes punctuation awkwardly. And with 32 passes, multiple positions are filled at once without conditioning on one another's newly sampled tokens.

There are important measurements I do not have: no matched autoregressive baseline with the same parameter count and token budget, no human ratings, no repetition or diversity scores, and no generation-speed comparison. More denoising steps might improve the text, but I have not measured the quality-versus-time tradeoff. Parallel decoding gives a possible speed lever; this project has not demonstrated a speed win.

What I would try next

First, fix the RoPE wiring and the learning-rate schedule so the newer configuration actually matches the model being trained and the schedule counts optimizer updates. Then run controlled comparisons: all-position versus masked-only loss at the same diffusion horizon, and the newer tokenizer and architecture against the existing checkpoint with matched prompts and seeds.

After that, measure quality across different numbers of sampling steps, including repetition and distinct n-gram scores, and train an autoregressive baseline on the same data and token budget. Until those comparisons exist, the honest conclusion is small: masked diffusion can learn story-like local patterns on a laptop, but this run does not show that it can tell a coherent story, run faster, or compete with autoregressive models.

I built this to understand what left-to-right generation makes easy and what it makes awkward. Filling in a whole sentence at once is an intriguing alternative. The hard part is not making the blanks disappear; it is making the words agree with each other after they do.

By Aditya Dutt Pandey, CTO of tunyt

By , CTO of tunyt

← All writing