Causal Masking

LLMs interview question · Medium difficulty

What is causal masking in GPT-style models, and why does it matter?medium

Status
✓
Type
conceptual
Topic
causal-masking
Frequency
common
Tags
causal-masking, GPT, decoder-only, autoregressive
Answer

Causal masking is a triangular attention mask that prevents a token from attending to future tokens, so token N can only attend to tokens 1 through N. It's what forces GPT-style decoder-only models to predict each token using only the tokens that came before it, preserving left-to-right, autoregressive generation.

Explanation

Without causal masking, a model training on "The capital of India is New Delhi" could cheat while predicting "India" by looking ahead and seeing "New Delhi" already sitting in the sequence — the loss would look artificially low during training but the model would have learned nothing useful about actually predicting the next token from scratch. The mask enforces: token 1 attends to token 1; token 2 attends to tokens 1-2; token 3 attends to tokens 1-3; and so on, so every prediction is made honestly from only the preceding context. This is specific to decoder-only, autoregressive architectures — encoder-only models like BERT are trained bidirectionally and don't use causal masking, since they aren't generating text left to right.

Follow-upWhat would happen to training loss if you removed causal masking from a GPT-style model?
Follow-upWhy doesn't an encoder-only model like BERT need causal masking?
Follow-upDoes causal masking apply during inference, or only during training?