Concepts / Tokenization and Token Embeddings in LLMs

Tokenization and Token Embeddings in LLMs

The dimensionality of a word embedding is the number of values in the vector that represents one word, and it ranges from one to thousands in practice.

  • Programming

Asking How Big an Embedding Is

Suppose someone hands you a word embedding and asks how big it is. You might expect the answer to describe the word itself, but an embedding is simply a list of numbers attached to that word. The size of that list is what we call the dimensionality of the embedding. Word embeddings can have anywhere from one dimension to thousands in practice.

What do you think happens?

If every word is described by a list of numbers, what do you think changes when that list gets longer?

  • The words themselves change meaning

  • The number of values stored per word changes, while the words stay the same

  • The words get longer as text

Reveal answer
Answer:

The number of values stored per word changes, while the words stay the same

Dimensionality changes only how many values each word carries. The words are the same; the vector describing each word has more or fewer entries.

Keep one question in mind as you read: if each word is described by a list of numbers, what happens to the meaning captured by those numbers when the list gets longer or shorter? The rest of this article traces that question step by step, from a single number per word up to the very large vectors used in GPT models.

Tracing One Word as Its Vector Grows

The clearest way to see what dimensionality counts is to watch a single word while only the length of its vector changes. Nothing else about the word is altered. Only the number of values in its list is allowed to vary.

Worked example

Tracing the embedding of one word at increasing dimensionality

Follow a single word through three embeddings of different sizes and identify what each size gives that word.

  1. 1
    One dimension

    Each word is represented by a single number. The word has exactly one value to position it, so every word lives somewhere on a line.

  2. 2
    Two dimensions

    The same word is now a pair of numbers. One number sets a horizontal position and the other sets a vertical position, so the word can be drawn as a point on a flat plane.

  3. 3
    Hundreds of dimensions

    The word is a list with hundreds of values. The word is unchanged, but it now has far more slots in which to store information about how it differs from other words.

Result

Dimensionality is the count of values in the vector for one word. Moving from one to two to hundreds changes how much room each word has, not what the word is.

slot 0one numberslot 1one numberslot 2one numberand so oncontinuesslot 767one numbernext positionnext positionnext positionlast position
What does a 768-dimensional embedding look like as a structure? Each position in the vector holds exactly one stored number.

Read the diagram as a row of boxes. A 768-dimensional embedding has 768 such boxes, numbered from zero, and each box holds one number. The index of a box tells you which dimension it belongs to, and the number inside is that dimension's value for the word. The word has the same identity in every case; only the count of boxes changes.

Why Extra Dimensions Matter for Meaning

A two-dimensional embedding can be drawn directly. Each word becomes a point on a flat plane, with one number setting its horizontal position and the other setting its vertical position. Once words are points, we can look at which points sit close together. When a technique such as Word2Vec is used, words for similar concepts tend to land near one another in the embedding space.

The plotted example places several kinds of birds in one region of the plane, and a country paired with its capital city in another region. Comparative forms of the word long form a third group. Each cluster is a neighborhood of similar points, and the grouping is what the two-dimensional picture communicates.

Two numbers per word are enough for a picture, but they leave little room to represent the many ways words relate to one another. Each extra dimension is another value the embedding can use to separate words that differ in subtle ways. Two words might be close on one feature, such as being animals, yet still differ on another, such as size or habitat. A richer vector has more room to record those fine distinctions.

The Tradeoff Between Nuance and Cost

Dimensionality is a tradeoff rather than a setting where bigger is always better. The gain is nuance; the price is computation. The two effects grow together, so a designer has to decide how much of each to accept.

Comparing the Two Sides of the Tradeoff

DimensionalityWhat it gives a wordWhat it costs
Small (for example one or two values)Simple placement; easy to plot and inspectLittle room for subtle distinctions
Large (hundreds to thousands of values)More room to separate words that differ in subtle waysMore memory per word and more arithmetic on every vector

The same dimensionality choice cuts in both directions

EASY

A designer doubles the length of every word vector in a system. Using only the tradeoff described above, explain what the designer gains and what the designer pays.

Hints
  • Think about what each extra dimension gives a word, and separately what each extra value must be stored and multiplied
  • Keep the words themselves the same; only the number of values per word changed

Why High-Dimensional Embeddings Resist Drawing

The two-dimensional plane worked because two dimensions fit on a page. Human senses and ordinary graphs are limited to three dimensions or fewer, so a vector with hundreds of values cannot be drawn as points in one picture. The earlier bird-and-capital example used a two-dimensional embedding precisely because that is the only size that can be plotted without loss.

high-dimensionalembeddinghundreds or thousands ofvalueshuman perceptionlimitthree dimensions or fewerlost detailnot every value is shownreduced 2D or 3D viewa projection of the vectorexceedsmust be reduced toprojection drops
How can a space with hundreds or thousands of axes be reduced to a 2D or 3D picture, and what is lost in that reduction?

When we move to large language models, the dimensionality is far higher than anything we can plot. Visualizing such an embedding directly is therefore not practical, and a learner should not expect to see the full vector of a large model as a picture. Any picture of such an embedding has to be a reduced view, and that reduction is a separate step from the embedding itself. A reduced view shows some structure while leaving the rest of the vector out of sight.

Embedding Sizes in GPT-2 and GPT-3

The GPT family makes the idea concrete. In GPT models the embedding size varies by variant and size, and the embedding size is often called the dimensionality of the model's hidden states. Larger models generally use larger embeddings, and the choice is framed as a tradeoff between performance and efficiency.

smallest GPT-2 model768 dimensionslargest GPT-3 model12,288 dimensions
How do the embedding sizes of GPT-2 models compare with those of GPT-3 models, and how large is the jump between them?

Between the smallest GPT-2 model at 768 dimensions and the largest GPT-3 model at 12,288 dimensions, the embedding length grows by a factor of 16. Every word in the largest GPT-3 model is therefore described by sixteen times as many values as a word in the smallest GPT-2 model. This is the same tradeoff at scale: more room for nuance, bought with more storage and arithmetic per word.

Where Dimensionality Is Set in the Pipeline

Dimensionality is chosen as part of how an LLM turns text into numbers. Text is split into words, those words are converted into tokens, and the tokens are turned into embedding vectors. The embedding size is the length of the final vector produced in that last step, so it sets the shape that every later stage of the model must handle.

Mistakes Learners Make About Dimensionality

The ideas in this article are easy to misread. The three mistakes below come up often.

  • Common mistake

    Assuming a larger dimensionality is always better

    What it looks like

    Choosing the largest embedding available for every task without considering cost

    Why it fails

    Higher dimensionality may capture more nuance, but it costs computational efficiency, so the size is a tradeoff

    Do this instead

    Weigh how much nuance the task needs against the memory and arithmetic the design can afford

  • Common mistake

    Reading the axes of a plot as labeled features

    What it looks like

    Trying to say what the horizontal axis of the bird-and-capital plot means

    Why it fails

    The axes do not carry a simple label you can read off; position encodes similarity

    Do this instead

    Read the arrangement of points and which points sit close together

  • Common mistake

    Expecting to see the full vector of a large model as a picture

    What it looks like

    Trying to plot a GPT-3 embedding with 12,288 values as points

    Why it fails

    Human senses and ordinary graphs are limited to three dimensions or fewer

    Do this instead

    Treat any picture of a large embedding as a reduced view, separate from the embedding itself

Predicting the Cost of a Larger Embedding

What do you think happens?

Two words are stored, one with a 768-dimensional embedding and one with a 12,288-dimensional embedding. Which one needs more memory to store?

  • The 768-dimensional embedding

  • The 12,288-dimensional embedding

  • They need the same memory

Reveal answer
Answer:

The 12,288-dimensional embedding

A larger dimensionality means each word carries more numbers, so storing the embeddings takes more memory. The 12,288-value vector holds sixteen times as many values as the 768-value vector.

MEDIUM

A two-dimensional embedding places each word as a point on a plane. Explain in your own words why two words that are close on that plane might still differ in ways that a two-dimensional embedding cannot record, and what additional dimensions would contribute.

Hints
  • Two numbers per word leave little room to represent the many ways words relate to one another
  • Each extra dimension is another value that can separate words that differ in subtle ways

Key Takeaways

  • The dimensionality of a word embedding is the number of values in the vector that represents one word, and it ranges from one to thousands in practice.
  • Higher dimensionality may capture more nuanced relationships between words, but it costs computational efficiency, so the size is a tradeoff.
  • Two-dimensional embeddings can be plotted, and position encodes similarity; high-dimensional embeddings cannot be visualized directly because of human perceptual and graphical limits.
  • In GPT models the embedding size ranges from 768 dimensions in the smallest GPT-2 models to 12,288 dimensions in the largest GPT-3 model.
  • The embedding size is set late in tokenization, as the length of the final token vector, so it shapes every downstream stage of the model.

Related concepts

More concepts starting with T