Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 4

Vocabulary and Tokenization in NLP Models

Recommended resources

Welcome to the fourth lesson in our series on Large Language Models. In our previous session, we delved into the mechanics of Byte Pair Encoding (BPE), observing how it iteratively constructs a set of subword tokens from a raw text corpus. This process gave us a method for turning any text into a sequence of integer IDs.

Today, we will formalize and expand on that concept. This lesson is centered on the vocabulary: the complete, finite dictionary of tokens that a language model understands. We will define what a vocabulary is and, crucially, explore its dual role as the bridge between raw text and the model's input, and between the model's internal computations and its final, human-readable output. Understanding this concept is fundamental, as the vocabulary's structure and size have profound implications for a model's architecture, memory footprint, and computational cost.

What is a Vocabulary?

In the context of NLP, a vocabulary is the complete, predefined set of all unique tokens a model can process. Think of it as a definitive dictionary. Each token—be it a word, a character, or, as we saw with BPE, a subword—is mapped to a unique integer ID.

This vocabulary is not an inherent property of a language; it is a learned artifact, created during the tokenizer training phase. The vocab_size parameter you might see in tokenizer configurations is a hyperparameter that dictates how many merge operations BPE (or a similar algorithm) will perform, thereby determining the final size of this dictionary.

A typical vocabulary for a modern LLM contains tens of thousands of tokens, including:

  • Individual characters or bytes to handle any possible string.
  • Commonly occurring subwords (e.g., ing, tion, pre).
  • Frequent whole words (e.g., the, and, is).
  • Special control tokens (e.g., <|endoftext|>, [CLS], [SEP]).

The tokenization process, therefore, is the act of consulting this vocabulary to convert an input string into a sequence of corresponding integer IDs.

This diagram shows the flow from raw input text to a sequence of integer token IDs. The tokenizer uses its pre-compiled vocabulary to map chunks of text like "for" to their unique ID, in this case, 329.

This diagram shows the flow from raw input text to a sequence of integer token IDs. The tokenizer uses its pre-compiled vocabulary to map chunks of text like "for" to their unique ID, in this case, 329.


The Vocabulary's Role at the Model's Input

As established, the vocabulary is the cornerstone of converting text to numbers. But its influence extends directly into the model's architecture. The first layer of a Transformer, the embedding layer, is fundamentally shaped by the vocabulary.

This layer's job is to map the discrete, one-dimensional token IDs into a high-dimensional vector space. It does this using an embedding matrix, a large table of weights. If our vocabulary has a size of and our model's hidden dimension is , the embedding matrix will have dimensions . Each row of this matrix corresponds to a token in the vocabulary and contains its unique vector representation, or "embedding."

8 Transformers

The textbook chapter "Transformers" by Jurafsky and Martin provides a formal description of how the input embedding matrix is structured and used.

Please read the section titled The Input: Embeddings. Pay close attention to the explanation of the embedding matrix E and how indexing (or multiplication by a one-hot vector) is used to look up the vector for a specific token ID. This directly links the vocabulary size |V| to the dimensions of the first weight matrix in the model.

The size of this embedding matrix is a significant factor in the total parameter count of a model. For a model like GPT-3 with a vocabulary of ~50,000 tokens and a hidden dimension of 12,288, this single matrix contains over 600 million parameters. This direct relationship between vocabulary size and model size is a critical architectural consideration.


The Vocabulary's Role at the Model's Output

The vocabulary's influence is just as profound at the end of the model as it is at the beginning. An autoregressive language model's fundamental task is to predict the next token in a sequence. This prediction is not a single token but a probability distribution over the entire vocabulary.

To achieve this, the output from the final Transformer layer, which is a vector of dimension , must be converted into a vector of dimension , where each element represents the likelihood of a corresponding token in the vocabulary. This is accomplished by the language model head, which typically consists of two components:

  1. A Linear Layer (Unembedding Layer): This layer projects the final token's output vector from the hidden dimension () up to the vocabulary dimension (). This is done by multiplying the vector by an unembedding matrix of shape . The resulting vector of size contains a raw, unnormalized score for each token in the vocabulary. These scores are called logits.
  2. The Softmax Function: The logits are then passed through a softmax function. This function normalizes the scores, converting them into a valid probability distribution where all values are between 0 and 1, and the sum of all values is exactly 1.
This diagram illustrates the language model head. The `Decoder stack output` (a vector of size \(d_{\text{model}}\)) is passed through a `Linear` layer (the unembedding matrix) to produce `logits` (a vector of size \(|V|\)). The `Softmax` function then converts these logits into `log_probs` (or probabilities), from which the next token can be sampled.

This diagram illustrates the language model head. The Decoder stack output (a vector of size ) is passed through a Linear layer (the unembedding matrix) to produce logits (a vector of size ). The Softmax function then converts these logits into log_probs (or probabilities), from which the next token can be sampled.

The following video provides an excellent visual and conceptual walkthrough of this entire output process.

Transformers, the tech behind LLMs | Deep Learning Chapter 5

In "Transformers, the tech behind LLMs," 3Blue1Brown masterfully explains the final stages of the Transformer model.

First, watch the segment explaining the unembedding matrix. This will clarify how the model maps its final internal representation back to the vocabulary space. Notice the symmetry with the input embedding matrix. Next, watch the detailed explanation of the softmax function. This part is crucial for understanding how the raw logit scores are transformed into a usable probability distribution. The discussion of "temperature" is also a valuable preview of decoding strategies we will cover later.

To formalize this, let's turn back to the Jurafsky and Martin text, which provides a precise definition and diagram of the language model head.

8 Transformers

This resource provides the academic definition of the language model head.

Read section 8.5 The Language Modeling Head. Focus on Figure 8.15, which visualizes the flow from the final transformer block's output (h_N^L) through the unembedding layer (E^T) and softmax to produce the final probability vector y. The concept of weight tying, where the unembedding matrix is simply the transpose of the input embedding matrix, is an important implementation detail that reduces the model's total parameter count.


The Vocabulary Size Trade-off

Given that the vocabulary size directly determines the size of the input embedding and final output projection matrices, choosing is a critical engineering decision with significant trade-offs.

  • A smaller vocabulary results in smaller embedding matrices, reducing the model's memory footprint. However, it forces the tokenizer to break words into more, smaller pieces. This increases the average sequence length () for a given text, which in turn increases the computational cost of the attention mechanism (which scales with ).
  • A larger vocabulary allows more words to be represented as single tokens, leading to shorter average sequence lengths and reducing the cost of attention. However, it dramatically increases the size of the embedding and unembedding matrices and increases the computational cost of the final softmax layer, which must compute a probability for every token in the vocabulary.

As a computer science student with a background in systems, you can appreciate that this is a classic space-time trade-off, manifesting in the context of neural network architecture.

Vocabulary Size Selection Trade-offs - ApX Machine Learning

This article from ApX Machine Learning provides an excellent, practical overview of the trade-offs involved in selecting a vocabulary size.

Please read the entire article, paying special attention to: Impact on Sequence Length: How vocabulary size affects the number of tokens per sentence. Impact on Model Size: The provided Python snippet offers a concrete calculation of how vocabulary size impacts memory usage, which should be very intuitive for you. Impact on Computation: How the cost of the final prediction step scales with |V|.


Conclusion

In this lesson, we have established the central role of the vocabulary in a language model's architecture. It is far more than just a list of tokens; it is a fundamental component that defines the interface between text and the model's numerical world.

Here are the key takeaways:

  • The vocabulary is a finite, fixed dictionary mapping all known tokens to unique integer IDs. It is the output of a training process like BPE.
  • At the input, the vocabulary size dictates the number of rows in the input embedding matrix (), a major contributor to the model's total size.
  • At the output, the model produces a probability distribution over the entire vocabulary. This requires a language model head consisting of a linear (unembedding) layer to produce logits of size and a softmax function to normalize them into probabilities.
  • The choice of vocabulary size involves a critical trade-off between the model's memory/parameter count and the computational cost associated with sequence length and the final softmax calculation.

We have now seen how text is converted to IDs using a vocabulary. The next logical step is to understand what happens in that very first layer of the network. In our next lesson, we will explore token embeddings in detail, examining how these static, high-dimensional vectors are learned and how they manage to capture the rich semantic meaning of language.