What is the difference between the masked language modeling objective and the causal next-token prediction objective?
Last updated
MLM corrupts some tokens and predicts them from both sides (bidirectional context). Causal LM predicts every token from only the tokens before it. MLM yields strong representations; causal LM yields a true generative model.
Masked language modeling (BERT):
About 15% of tokens are selected. Of those, 80% become
[MASK], 10% become a random token, and 10% stay unchanged. The model predicts the originals.There is no attention mask, so each prediction uses left and right context.
Loss only covers the selected tokens, so there is less signal per sequence.
Pretrain/finetune mismatch:
[MASK]never appears downstream. The 80/10/10 split mitigates this.
Causal next-token prediction (GPT):
Inputs are shifted by one, and position t predicts token t+1.
A lower-triangular attention mask enforces that only the left context is visible.
Every position contributes to the loss, giving a dense signal.
It defines a full joint distribution, so you can sample from it directly.
Downside: exposure bias. The model trains on ground-truth prefixes but generates from its own outputs.
Consequences:
MLM suits classification, tagging, and embeddings.
Causal LM suits generation, prompting, and in-context learning.
Hybrids exist: T5 span corruption, prefix LM, and UL2 mixtures.
[lang:python]
# Causal LM: shift labels, loss on every position
logits = model(input_ids[:, :-1])
loss = F.cross_entropy(logits.flatten(0, 1), input_ids[:, 1:].flatten())
# MLM: loss only where tokens were masked (others labeled -100)
labels = torch.where(mask, input_ids, -100)
loss = F.cross_entropy(model(corrupted_ids).flatten(0, 1), labels.flatten(), ignore_index=-100)