Concepts / Masked and Autoregressive Language Models

Masked and Autoregressive Language Models

A language model encodes statistical information about one or more languages, which tells us how likely a word is to appear in a given context.

  • Programming

A Blank That Almost Fills Itself

Read the sentence 'My favorite color is ___' and try to fill the blank. Most people would answer with a color such as blue, and almost no one would answer with a word like car. That everyday judgment is what a language model is built to capture. A language model is a program that learns how likely words are to appear in a given context, so that it can judge which words are probable in a particular position.

This article answers one core question: how does a program come to know which words are likely to appear in a given context? Along the way it introduces the basic unit a model works with, called a token, and the two main families of language models, masked and autoregressive.

What a Language Model Encodes

Definition

A language model encodes statistical information about one or more languages, which tells us how likely a word is to appear in a given context.

The word statistical matters. A language model does not store a dictionary of meanings to be looked up. It stores patterns of likelihood. When it is given some context, it can judge which words are probable next. For the context 'My favorite color is __', a model that encodes English should give 'blue' a higher likelihood than 'car'. The model's skill is this ranking of probable continuations, not any single fixed answer.

The idea that language has statistical structure is older than computers. In the 1905 story 'The Adventure of the Dancing Men', Sherlock Holmes decodes a sequence of mysterious stick figures using simple statistics of English. Because the most common letter in English is E, he reasons that the most common stick figure must stand for E. The same kind of reasoning, applied with more sophisticated statistics, appears later in the work of Claude Shannon, who used it to decipher enemy messages during the Second World War.

Shannon's 1951 paper 'Prediction and Entropy of Printed English' described how to model English statistically. Many concepts from that paper, including entropy, are still used for language modeling today. Early language models usually covered a single language. Today a language model can involve multiple languages.

Breaking Text into Tokens

A language model does not read text as a stream of whole words the way people do. Its basic unit is called a token. Depending on the model, a token can be a character, a word, or a part of a word, such as the suffix -tion. The process of breaking the original text into tokens is called tokenization.

The source gives a concrete case. GPT-4, the model behind ChatGPT, breaks the phrase 'I can't wait to build AI applications' into nine tokens. Notice how the contraction is handled: can't does not survive as one piece. It is split into can and a second piece made of the apostrophe and t. Tokens are therefore not the same thing as the words a person would count on a page.

can'tone word on the pagecanfirst token'tapostrophe and tsplit intosplit into
Which pieces does the word can't become when a model tokenizes it?

A natural question is why a model should use tokens at all, when it could use whole words or single characters. The source answers with a balance. Characters give a very small set of units, but they carry little meaning on their own. Whole words carry meaning, but there are very many of them, and a made-up word can be unknown to the model entirely. Tokens sit between the two: there are fewer units than words, yet each unit still carries more meaning than a single character.

Vocabulary and Token Capacity

Definition

A model's vocabulary is the set of all tokens it can work with. Its size varies between models: the source gives 32,000 for Mixtral 8x7B and 100,256 for GPT-4.

A small set of tokens can build a large number of distinct words, much as a few letters of the alphabet can be combined into many words. The vocabulary is therefore a compact toolkit, not a list of every word the model could ever meet. The vocabulary size is the number of entries in that toolkit, so it sets how many distinct tokens the model can represent. These two figures belong to specific models and show that vocabulary size is not the same across models.

Token counts and word counts are also related through an average. The source states that for GPT-4 an average token is approximately three quarters of a word, so 100 tokens are approximately 75 words. Working backwards, a passage of 400 tokens would be roughly 300 words. This is an average, so any particular passage can deviate from it.

Worked example

Estimating the words in a passage

A GPT-4 passage is 400 tokens long. Roughly how many words does it contain?

  1. 1
    Recall the average

    An average GPT-4 token is about three quarters of a word.

  2. 2
    Scale the count

    Multiply the token count by three quarters: 400 times 0.75.

  3. 3
    Read the result

    The passage is roughly 300 words, but a specific passage may differ from this average.

Result

About 300 words.

Tracing a Prediction Step by Step

Before naming the two model types, follow one prediction through a model. Start with a partial sentence and ask what the model is allowed to see when it fills the next position. Think about this for a moment before reading the answer.

What do you think happens?

When a model predicts the word at a hidden position, which words does it use as context?

  • Only the words that come before the hidden position

  • Only the words that come after the hidden position

  • Words on both sides of the hidden position

Reveal answer
Answer:

It depends on the model type: a masked model can use words on both sides of the hidden position, while an autoregressive model uses only the words that come before the position it is predicting.

This difference in what each model may look at is the distinction between the two main families of language models.

Masked Versus Autoregressive Models

Language models come in two main types: masked language models and autoregressive language models. Both are language models in the sense defined earlier, so both encode statistical information about language. The two are distinguished by the information each can use when predicting a token. The difference lies in what they are allowed to look at when they make a prediction.

words left of theblankhidden wordthe targetearlier wordsnext wordthe targetwords right of theblankcontextcontextcontext
Which words can the model see when it predicts a target word in a masked model versus an autoregressive model?
FeatureMasked language modelAutoregressive language model
Shared basisEncodes statistical information about languageEncodes statistical information about language
What it predictsA token at a hidden positionThe next token in the sequence
Context it can useWords on both sides of the hidden positionWords that come before the position being predicted
What distinguishes itAccess to context on both sidesAccess only to the sequence built so far

The two families share a statistical foundation and differ in what context they may use.

How Generation Repeats the Prediction

An autoregressive model generates text by repeating one step. It predicts a next word, appends that word to the sequence, and then feeds the longer sequence back in to predict the word after it. The loop below follows a short generated example that starts from the words 'The cat sat'.

The cat satstarting contextpredict: onnext tokenThe cat sat onappendedpredict: thenext tokenThe cat sat on theappendedfeed inappendfeed inappend
How does an autoregressive model repeatedly append its predicted word and feed the growing sequence back in to predict the next word?

Each pass uses a slightly longer sequence than the last, because every predicted token is added to the context before the next prediction. This is why the word autoregressive fits: each new output is regressed on what the model has already produced.

Mistakes Beginners Make

  • Common mistake

    Treating a token as a word

    What it looks like

    Counting the words in 'I can't wait to build AI applications' and expecting the same number of tokens.

    Why it fails

    A token can be a character, a word, or a part of a word, and the contraction can't is split into can and a piece made of apostrophe and t.

    Do this instead

    Count tokens separately from words, and remember that a token is the model's basic unit.

  • Common mistake

    Assuming vocabulary size is fixed across models

    What it looks like

    Believing every model stores the same number of tokens.

    Why it fails

    The source gives 32,000 for Mixtral 8x7B and 100,256 for GPT-4, showing that vocabulary size varies between models.

    Do this instead

    Look up the vocabulary size of the specific model you are studying.

  • Common mistake

    Treating the token-to-word ratio as exact

    What it looks like

    Concluding that a 400-token passage must contain exactly 300 words.

    Why it fails

    Three quarters of a word per token is an average, so a particular passage can deviate from it.

    Do this instead

    Use the ratio only as a rough estimate.

  • Common mistake

    Confusing a language model with a dictionary that looks up meanings

Practice Before the Summary

EASY

A model is shown the context 'My favorite color is' with a blank after it. Which model type would use the words after the blank, and which would not? Explain your answer in terms of what each model may look at.

Hints
  • Recall that a masked model predicts a hidden position using context on both sides.
  • Recall that an autoregressive model predicts the next token from the tokens before it.
MEDIUM

A passage contains 250 GPT-4 tokens. Using the average from the source, estimate how many words it contains, and state one reason the estimate might be off for a particular passage.

Hints
  • Multiply the token count by three quarters.
  • The source describes the ratio as an average.

At a glance

  • A language model encodes statistical information about language, which lets it rank how likely each word is in a given context.
  • Tokenization breaks text into tokens, which can be characters, words, or parts of words, so tokens are not the same as words on a page.
  • A model's vocabulary is the set of tokens it can work with, and its size varies between models such as Mixtral 8x7B and GPT-4.
  • Masked language models predict a hidden position using context on both sides, while autoregressive language models predict the next token from the tokens before it.

Related concepts

More concepts starting with M