Pre-training Objectives: Causal LM, Masked LM, and Beyond
2136 words
11 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Pre-training Objectives: Causal LM, Masked LM, and Beyond ## 🎯 Learning Objectives - Distinguish between Causal LM and Masked LM objectives - Understand why different architectures use different objectives - Implement the training loop for both CLM and MLM - Explain Next Sentence Prediction and other auxiliary ob...

Pre-training Objectives: Causal LM, Masked LM, and Beyond
🎯 Learning Objectives
- Distinguish between Causal LM and Masked LM objectives
- Understand why different architectures use different objectives
- Implement the training loop for both CLM and MLM
- Explain Next Sentence Prediction and other auxiliary objectives
- Compare autoregressive vs autoencoding models
📋 Prerequisites
- Transformer encoder-decoder architecture
- Cross-entropy loss
- Self-attention and masking
1. 📖 Core Content
1.1 What is Pre-training?
Pre-training is the process of training a large neural network on a self-supervised objective using vast amounts of unlabeled text data. The model learns general language understanding — grammar, syntax, semantics, world knowledge — without needing human-annotated labels.
Why self-supervised? Text data is abundant on the internet. We can create training examples from raw text by:
- Predicting the next word (Causal LM)
- Predicting masked/filled words (Masked LM)
- Predicting if two sentences are consecutive (NSP)
1.2 Causal Language Modeling (CLM)
Intuition
CLM is the simplest pre-training objective: given a sequence of tokens, predict the next token. This is exactly what language modeling has always been — estimating P(xt∣x<t).
The model sees "The cat sat on the" and must predict "mat."
Formal Definition
Given a sequence of tokens x1,x2,...,xT:
Where θ represents model parameters.
Architecture: Decoder-Only
CLM uses a decoder-only Transformer with causal masking. GPT models use this approach.
(Diagram)
Worked Example
Sequence: "I love learning" Token IDs: [5, 12, 8]
| Position | Input | Target | Model predicts P(target) |
|---|---|---|---|
| 0 | [BOS] | 5 (I) | P(I |
| 1 | [BOS, I] | 12 (love) | P(love |
| 2 | [BOS, I, love] | 8 (learning) | P(learning |
Loss = -[log P(I|BOS) + log P(love|BOS, I) + log P(learning|BOS, I, love)]
1.3 Masked Language Modeling (MLM)
Intuition
In MLM, we randomly mask some tokens in the input and train the model to predict them using bidirectional context. The model sees "The [MASK] sat on the mat" and must predict "cat."
Unlike CLM which processes left-to-right, MLM processes the entire sequence and uses both left and right context.
Formal Definition
Given a sequence x, we create a corrupted version x^ by masking 15% of tokens:
Where M is the set of masked positions.
The 15% Masking Strategy
BERT uses a specific strategy for the 15% of tokens selected for masking:
| Action | % of Selected | What happens |
|---|---|---|
| Replace with [MASK] | 80% | "The cat sat" → "The [MASK] sat" |
| Replace with random token | 10% | "The cat sat" → "The apple sat" |
| Keep unchanged | 10% | "The cat sat" → "The cat sat" |
Why not 100% [MASK]? If the model only sees [MASK] during training, it won't learn to handle non-masked tokens during fine-tuning. The mixed strategy forces the model to maintain contextual representations for all tokens.
python# runnable import numpy as np def apply_bert_mask(tokens, mask_token_id, vocab_size, mask_prob=0.15): """ Apply BERT-style masking to a token sequence Args: tokens: List of token IDs mask_token_id: ID of [MASK] token vocab_size: Size of vocabulary mask_prob: Probability of masking each token Returns: masked_tokens: Tokens after masking labels: Original tokens (with -100 for non-masked positions) """ seq_len = len(tokens) masked_tokens = tokens.copy() labels = [-100] * seq_len # -100 positions are ignored in loss for i in range(seq_len): if np.random.random() < mask_prob: labels[i] = tokens[i] # Store original token for loss r = np.random.random() if r < 0.8: masked_tokens[i] = mask_token_id # 80%: [MASK] elif r < 0.9: masked_tokens[i] = np.random.randint(0, vocab_size) # 10%: random # else 10%: keep unchanged (labels still has original) return masked_tokens, labels # Example np.random.seed(42) tokens = [5, 12, 8, 23, 45, 67, 89, 34, 56, 78] mask_token_id = 0 vocab_size = 1000 masked, labels = apply_bert_mask(tokens, mask_token_id, vocab_size) print("Original:", tokens) print("Masked: ", masked) print("Labels: ", labels) print("Masked positions:", [i for i, l in enumerate(labels) if l != -100])
1.4 CLM vs MLM: Comparison
| Aspect | CLM (GPT) | MLM (BERT) |
|---|---|---|
| Direction | Left-to-right | Bidirectional |
| Architecture | Decoder-only | Encoder-only |
| Masking | Causal | Random [MASK] |
| Training efficiency | All tokens | Only masked tokens |
| Perplexity | Measures LM quality | Not applicable |
| Generation | Natural (autoregressive) | Needs separate decoder |
| Understanding | Good, but misses right context | Excellent (bidirectional) |
(Diagram)
1.5 Next Sentence Prediction (NSP)
BERT uses an additional pre-training objective: Next Sentence Prediction (NSP).
Given two sentences A and B, predict whether B follows A in the original text.
- Positive example: A = "The cat sat on the mat." B = "It was very comfortable." (B follows A)
- Negative example: A = "The cat sat on the mat." B = "The capital of France is Paris." (random pair)
python# runnable import numpy as np def create_nsp_example(sentence_a_tokens, sentence_b_tokens, is_next): """ Create an NSP training example Args: sentence_a_tokens: Token IDs for sentence A sentence_b_tokens: Token IDs for sentence B is_next: 1 if B follows A, 0 otherwise Returns: input_ids: [CLS] + A_tokens + [SEP] + B_tokens + [SEP] segment_ids: 0 for A, 1 for B label: is_next (1 or 0) """ cls_id = 101 # [CLS] sep_id = 102 # [SEP] input_ids = [cls_id] + sentence_a_tokens + [sep_id] + sentence_b_tokens + [sep_id] segment_ids = [0] + [0]*len(sentence_a_tokens) + [0] + [1]*len(sentence_b_tokens) + [1] label = 1 if is_next else 0 return input_ids, segment_ids, label # Example sent_a = [12, 34, 56] # "The cat sat" sent_b_pos = [78, 90, 23] # "on the mat" (positive) sent_b_neg = [45, 67, 89] # "Paris is capital" (negative) pos_ids, pos_seg, pos_label = create_nsp_example(sent_a, sent_b_pos, True) neg_ids, neg_seg, neg_label = create_nsp_example(sent_a, sent_b_neg, False) print(f"Positive example - Input: {pos_ids}") print(f"Segment IDs: {pos_seg}") print(f"Label: {pos_label}")
1.6 Other Pre-training Objectives
| Objective | Description | Used In |
|---|---|---|
| Span Corruption | Mask contiguous spans of tokens | T5 |
| Prefix LM | Bidirectional on prefix, causal on continuation | UniLM |
| Denoising Autoencoding | Reorder/corrupt text, reconstruct original | BART |
| Replaced Token Detection | Discriminate real vs generated tokens | ELECTRA |
| Sentence Order Prediction | Predict if sentences are in correct order | ALBERT |
1.7 Why This Matters
The choice of pre-training objective determines:
- What the model learns: CLM excels at generation, MLM excels at understanding
- Architecture: Decoder-only for CLM, encoder-only for MLM
- Downstream performance: Different objectives suit different tasks
- Computational cost: CLM trains on all tokens, MLM only on 15%
4. 📐 Key Formulas / Concepts
| Objective | Loss Function | Architecture | Example Model |
|---|---|---|---|
| Causal LM | $-\sum_t \log P(x_t\ | x_{<t})$ | Decoder-only |
| Masked LM | $-\sum_{i \in \mathcal{M}} \log P(x_i\ | \hat{x})$ | Encoder-only |
| Span Corruption | $-\sum_{s \in \mathcal{S}} \log P(x_s\ | \hat{x})$ | Encoder-Decoder |
| Denoising | $-\log P(x\ | \tilde{x})$ | Encoder-Decoder |
| RTD | $-\sum_t \log D(x_t\ | \tilde{x})$ | Encoder + Discriminator |
5. ⚠️ Common Pitfalls
Pitfall 1: Thinking MLM and CLM are interchangeable
The mistake: Assuming a model pre-trained with CLM can do BERT-style MLM fine-tuning.
Correction: CLM models (GPT) use causal masking and can only attend left-to-right. MLM requires bidirectional attention. A CLM model cannot compute MLM because it can't see "right context." You'd need to pre-train from scratch or use a different architecture.
Pitfall 2: Confusing "pre-training" with "training from scratch"
The mistake: Thinking every model training is "pre-training."
Correction:
- Pre-training: Self-supervised learning on large unlabeled data (objective: LM/MLM)
- Fine-tuning: Supervised learning on labeled downstream data (objective: task-specific)
- Training from scratch: Supervised on task data only (no pre-training) Pre-training is the expensive first stage that provides general language understanding.
Pitfall 3: Ignoring the 15% masking cost
The mistake: Thinking MLM trains on 100% of tokens effectively.
Correction: MLM only computes loss on masked tokens (15%). To process the entire sequence, you need ~6.7× more steps than CLM for the same per-token signal. This is why MLM models are typically trained for more steps.
6. 📝 Practice Questions
Q1: For a sentence of 100 tokens with 15% masking, how many tokens contribute to the MLM loss?15 tokens contribute to the loss (100 × 0.15 = 15). The remaining 85 tokens serve only as context. This is why MLM is less data-efficient per training step than CLM (where all 100 tokens contribute).However, MLM provides richer training signal per token because it uses bidirectional context. Q2: Why does BERT use [MASK] only 80% of the time, not 100%?If [MASK] was used 100% of the time:
- The model would never learn to process non-masked tokens correctly
- During fine-tuning, there are no [MASK] tokens, creating a distribution mismatch
- The model would be confused by real tokens
The 80-10-10 split forces the model to maintain good representations for all tokens, not just masked ones. Q3: Calculate the CLM loss for a 3-token sequence [2, 5, 3] if the model outputs logits as follows: P(2|1)=0.7, P(5|[1,2])=0.5, P(3|[1,2,5])=0.8Loss = -[log(0.7) + log(0.5) + log(0.8)] = -[-0.357 + -0.693 + -0.223] = -[-1.273] = 1.273Lower is better. If the model was perfect (all probabilities = 1.0), loss would be 0. Q4: Why is T5's span corruption objective more efficient than BERT's MLM?BERT masks individual tokens (15% of tokens), treating each independently. T5 masks contiguous spans (average span length ~3 tokens, masking ~15% of text).Span corruption is more efficient because:
- Each span requires one prediction instead of multiple token predictions
- The model learns to reconstruct coherent phrases, not isolated tokens
- Fewer masked positions for the same amount of corruption
T5's approach is particularly beneficial for generation tasks where output is naturally structured in spans. Q5: GPT-3 has 175B parameters and was trained on 570GB of text. If each token is processed once per epoch, and 10 epochs were used, how many forward passes were performed?Assuming ~0.75 bytes per token on average:
- 570GB = 570 × 10⁹ bytes
- Tokens ≈ 570 × 10⁹ / 0.75 ≈ 7.6 × 10¹¹ tokens
- 10 epochs = 7.6 × 10¹² tokens processed
- Each token requires one forward pass in CLM
- Total forward passes: ~7.6 trillion
This gives a sense of the scale of LLM pre-training. Q6: RoBERTa showed that removing NSP improves performance. Why might NSP be unnecessary?RoBERTa found that NSP doesn't add value when training with longer sequences and more data. Possible reasons:
- NSP is too easy — the model can exploit shallow features (topic shift) to solve it
- The single-sentence training in BERT's NSP setup reduces effective sequence length
- MLM alone with longer sequences captures sufficient sentence-level understanding
RoBERTa uses full-length sequences packed from multiple documents without the NSP task. Q7: Design a pre-training objective for a multimodal model (text + images).Possibilities:
- Masked modality modeling: Mask image patches or text tokens, predict from the other modality
- Image-text matching: Predict if an image and caption match (like NSP but cross-modal)
- Contrastive learning: Maximize similarity between matched image-text pairs, minimize for mismatched pairs
- Prefix generation: Use image as prefix, generate text description
These approaches are used in models like CLIP, ALIGN, and Flamingo. Q8: Calculate the perplexity of a language model that assigns probability 0.25 to each token in a 10-token sequence.Perplexity (PPL) = exp(Loss) Loss = -(10 × log(0.25)) = -(10 × -1.386) = 13.86 PPL = exp(13.86 / 10) = exp(1.386) = 4.0Alternatively: PPL = 1 / average probability = 1 / 0.25 = 4.0A perplexity of 4 means the model is as confused as if it had to choose uniformly among 4 tokens. Q9: What happens if you train BERT with 50% masking instead of 15%?With 50% masking:
- Too little context: The model has insufficient information to predict masked tokens
- Unnatural task: Real text doesn't have half the words missing
- Poor representations: The model doesn't learn good representations for non-masked tokens
- Higher variance: Fewer training examples per sequence
- Performance drops: Empirical studies show 15% is optimal for BERT-style models
Higher masking rates work better for T5's span corruption because contiguous spans provide more context for reconstruction. Q10: How does ELECTRA's Replaced Token Detection (RTD) achieve better sample efficiency than MLM?ELECTRA uses a two-model approach:
- Generator (small MLM): Predicts masked tokens
- Discriminator (main model): Predicts whether each token is original or replaced
RTD is more efficient because:
- All tokens contribute to the loss (discriminator evaluates every position)
- The task (detect replacement) is harder than MLM prediction
- The discriminator learns from all positions, not just 15%
- ELECTRA achieves BERT-level performance with 1/4 the compute
Loss is computed over all N tokens, not just 0.15N masked ones.
7. 🔗 Cross-References
- Next: GPT Architecture (Week 4)
- Next: BERT Architecture (Week 5)
- Video: BSDA5004 Week 3-4 transcripts Join Discord PreviousLayer Norm & ResidualNextGPT Architecture