Causal language model

A causal language model predicts each next token using only the tokens that come before it, never those after, which is the training setup behind GPT-style text generators.

Hugging Face's documentation defines causal language modelling as predicting the next token in a sequence, where the model can only attend to tokens on the left and cannot see future ones. Google's machine learning glossary treats the term as a synonym for a unidirectional language model. The word causal refers to the direction of the sequence, earlier tokens influencing later ones, not to cause and effect in the scientific sense. A causal language model has no special grasp of causation.

The restriction is enforced inside the model by a mask on the attention mechanism, which blocks each position from looking at anything after it. This has a useful side effect in training. Because no position can peek ahead, the model can learn from every position of a passage at once: for each token, the target is simply the next token. No human labels are needed, since, as Hugging Face's guide puts it, the next word is the label.

The main alternative is masked language modelling, used by BERT, which hides random words and predicts them from context on both sides. Masked models build richer representations for understanding tasks but generate text poorly. Causal models are natural generators: give them the start of something and they continue it, one token at a time. GPT-2 is the example Hugging Face uses, and today's widely used chat and coding models are built the same way.

Three terms often appear together and describe the same family from different angles. Causal language model describes the training objective. Decoder-only describes the architecture that usually implements it. Autoregressive describes how such a model generates, feeding each output back in as input. In Hugging Face's widely used Transformers library, these models are loaded through a class called AutoModelForCausalLM.

Common questions

Why is it called a causal language model?

Because each prediction may depend only on what came earlier in the sequence, just as a cause must come before its effect. The name describes this one-way flow of information through the text. It does not mean the model understands causation or reasons about causes.

What is the difference between causal and masked language modelling?

A causal language model predicts the next token using only the tokens before it. A masked language model, such as BERT, hides some tokens and predicts them using the words on both sides. Causal models suit text generation; masked models suit tasks such as classification where the whole passage is available.

Is GPT a causal language model?

Yes. GPT models are trained to predict the next token from the tokens before it, which is the definition of causal language modelling. Hugging Face uses GPT-2 as its standard example, and most current chat and coding models follow the same approach.

Do causal language models need labelled data?

No. The text supplies its own targets: at every position, the correct answer is the token that actually comes next. That is why these models can be pre-trained on enormous amounts of raw text without anyone labelling it.

Finding more

Search for "causal language modeling", "causal attention mask" or "causal vs masked language model" to see how the objective works in practice. The Hugging Face documentation and course, Google's machine learning glossary and the original model papers on arXiv are reliable starting points. Note the American spelling "modeling" is far more common in technical sources, so include it in searches.

Working out what this means for your business?

Aileron Shift builds the systems behind this site: AI newsrooms that research, write and publish on their own, marketing automation and WordPress hosting for companies that need to move faster than their competitors.