Concepts / Vocabulary Size and Model Efficiency

Vocabulary Size and Model Efficiency

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

Guessing the Next Word

Read the beginning of this sentence: 'My favorite color is' followed by a blank. Most people would fill the blank with a color such as blue, and almost never with a word like car. That everyday judgment is what a language model is built to capture. The central question of this article is how a program comes to know which words are likely to appear in a given context.

What do you think happens?

After 'My favorite color is', which word should a model that encodes English rank as more likely: blue or car?

  • blue

  • car

Reveal answer
Answer:

blue

A model that encodes English should give 'blue' a higher likelihood than 'car' in this context. The model's skill is ranking probable continuations, not producing one fixed answer.

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 here. A language model does not store a dictionary of meanings to be looked up. It stores patterns of likelihood, so that when it is given some context, it can judge which words are probable next. Given the context 'My favorite color is __', a model that encodes English should give 'blue' a higher likelihood than 'car'. The model is therefore best understood as a ranker of probable continuations.

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 introduced in 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.

Tokens as the Basic Unit

A language model does not read text as a stream of whole words in 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.

Worked example

Tokenizing 'I can't wait to build AI applications'

The source gives GPT-4 as breaking the phrase 'I can't wait to build AI applications' into nine tokens. Identify the tokens in order and explain the split in can't.

  1. 1
    Start with the words

    The phrase has six words when counted the way a person would count them on a page: I, can't, wait, to, build, AI, applications. That is seven words.

  2. 2
    Split the contraction

    The word can't does not survive as one piece. It is split into can and a second piece made of the apostrophe and t, so it contributes two tokens.

  3. 3
    List the tokens in order

    The tokens are: I, can, apostrophe, t, wait, to, build, AI, applications. Counting them gives nine, matching the source.

  4. 4
    Compare with word count

    Seven words become nine tokens, which shows that tokens are not the same thing as the words a person would count.

Result

Nine tokens: I, can, ', t, wait, to, build, AI, applications.

Original phraseI can't wait to build AIapplicationsItoken 1cantoken 2'token 3ttoken 4waittoken 5totoken 6buildtoken 7AItoken 8applicationstoken 9tokenize
What happens to the phrase 'I can't wait to build AI applications', step by step, as it is broken into tokens?

Read the diagram from left to right. The original phrase enters tokenization and comes out as an ordered sequence of nine tokens. The two changed tokens, the apostrophe and t, are the pieces that came from splitting can't. Every other word in the phrase maps to a single token.

Vocabulary and Vocabulary Size

Definition

A model's vocabulary is the set of all tokens it can work with. Its size varies between models.

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 rather than a list of every word the model could ever meet. The source gives two reference points: the Mixtral 8x7B model has a vocabulary size of 32,000, and GPT-4 has a vocabulary size of 100,256. These numbers belong to specific models and show that vocabulary size differs from one model to another.

Mixtral 8x7B32,000 tokensGPT-4100,256 tokens
How large is the vocabulary of each of the two models the source names?

Vocabulary size is the count of tokens in this set. It is a property of the model, not of the text being read. A model with a larger vocabulary has more distinct units available to represent text, and a model with a smaller vocabulary must build the same text from a different set of pieces.

Converting Between Tokens and Words

The source gives a rough size relationship for GPT-4: an average token is approximately three quarters of a word, so 100 tokens are approximately 75 words. Because the figure is an average, any particular passage can deviate from it.

Worked example

Estimating a passage length from its token count

Using the average relationship that 100 tokens are about 75 words, estimate how many words a 400-token passage contains.

  1. 1
    Find the words per token

    Each token is about three quarters of a word, so the ratio of words to tokens is about 0.75.

  2. 2
    Apply the ratio

    Multiply the token count by 0.75: 400 tokens times 0.75 gives 300.

  3. 3
    State the estimate with its limit

    The estimate is roughly 300 words. The actual count can differ for a specific passage, because the three quarters figure is only an average.

Result

A 400-token passage is roughly 300 words.

Next-Word Prediction Step by Step

Prediction starts with context. The preceding words are the input, and the model's stored statistical information about language turns that context into a judgment about which words are likely to come next. The output is not one forced answer. It is a ranking in which some candidates are more probable than others.

My favorite coloriscontextLanguage modelencoded statisticsbluehigher likelihoodcarlower likelihoodcontext inranksranks
How do the preceding words flow into the model so that it judges each possible next word?
  1. 1
    Read the context: the words that come before the blank, here 'My favorite color is'.
  2. 2
    Pass the context to the model, which holds statistical information about how words are likely to appear.
  3. 3
    For each candidate next word, the model judges its likelihood in this context.
  4. 4
    The candidates are ranked, so that a word such as blue scores higher than a word such as car.

Two Types of Language 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 each is allowed to look at when it makes a prediction.

PropertyMasked language modelAutoregressive language model
Encodes statistical information about languageYesYes
What distinguishes itThe information it can use when predicting a tokenThe information it can use when predicting a token
Difference between the twoSet by what it is allowed to look atSet by what it is allowed to look at

What the two model types share and what sets them apart, as stated in the source

Common Misreadings of Tokens and Vocabulary

  • Common mistake

    Treating tokens and words as the same thing

    What it looks like

    Assuming that the phrase 'I can't wait to build AI applications' has seven tokens because it has seven words.

    Why it fails

    The word can't is split into can and a second piece of an apostrophe and t, so the phrase produces nine tokens.

    Do this instead

    Count tokens after tokenization, not words on the page.

  • Common mistake

    Thinking the vocabulary is a list of every word the model can ever meet

    What it looks like

    Believing that a model with a vocabulary of 32,000 can recognize only 32,000 words.

    Why it fails

    A small set of tokens can build a large number of distinct words, so the vocabulary is a compact toolkit.

    Do this instead

    Read vocabulary size as the number of tokens in the set, which can combine into many more words.

  • Common mistake

    Assuming every model has the same vocabulary size

    What it looks like

    Using the GPT-4 figure of 100,256 when describing Mixtral 8x7B.

    Why it fails

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

    Do this instead

    Quote the vocabulary size for the specific model being discussed.

  • Common mistake

    Treating the 75 percent figure as exact

    What it looks like

    Concluding that a 400-token passage is exactly 300 words.

    Why it fails

    The three quarters ratio is an average, and any particular passage can deviate from it.

    Do this instead

    Use the ratio as an estimate, and state that it is approximate.

  • Common mistake

    Expecting a model to store a dictionary of meanings to look up, when the source describes it as storing patterns of likelihood

Practice: Tokens and Vocabulary

EASY

Using the source's ratio that 100 tokens are about 75 words, estimate how many words are in a 200-token passage, and explain why your answer is an estimate rather than an exact count.

Hints
  • Work out the words per token first: three quarters of a word for each token.
  • Multiply the token count by 0.75.
  • Remember that the ratio is an average, so a particular passage can differ.
MEDIUM

Tokenize the phrase 'I can't wait' by listing each token in order. Then state how many words a person would count and how many tokens the model produces.

Hints
  • Split can't into can and a second piece made of the apostrophe and t.
  • A person counts three words here.

Key Takeaways

  • A language model encodes statistical information about language, which tells us how likely a word is to appear in a given context, and it ranks probable continuations rather than looking up fixed meanings.
  • A token is the basic unit of a language model. It can be a character, a word, or part of a word, and tokenization is the process of breaking text into tokens; GPT-4 splits 'I can't wait to build AI applications' into nine tokens.
  • A model's vocabulary is the set of all tokens it can work with. Its size varies between models, such as 32,000 for Mixtral 8x7B and 100,256 for GPT-4.
  • An average GPT-4 token is about three quarters of a word, so 100 tokens are about 75 words, which is an average and not an exact count.
  • The two main types of language models are masked and autoregressive models, and they are distinguished by the information each can use when predicting a token.

Related concepts

More concepts starting with V