Token Embeddings

A transformer works with vectors, but text arrives as a string. Two steps connect them:

  • Tokenization splits the string into tokens from a fixed vocabulary of entries and replaces each token by its index. 7.3.1. Tokenization covers how the vocabulary is built.
  • Embedding maps each index to a learned vector in .

The embedding layer is a linear layer applied to one-hot vectors. Token becomes the one-hot vector , and the layer computes with . The product picks out column of . The layer is therefore a lookup table with one learned vector per token. Gradient descent trains it like any other weight. The lecture’s example has and , so the table holds million numbers.

At the output, each final token vector must become a probability for every token in the vocabulary. A second linear layer, the de-embedding layer , computes scores . A softmax turns them into probabilities .

The lecture sets the scale of both layers with RMS norms. The vectors entering the attention blocks should have RMS norm :

  • Embedding: the output is column itself. Each column of should therefore have RMS norm , with entries of size about .
  • De-embedding: the input has RMS norm . For scores of size about , each row of should have Euclidean norm . Its entries are then about . For that is about .

Intuition

A one-hot vector already identifies a token, so the network could read it directly. One-hot vectors make every pair of tokens equally different, though. The Fall 2022 lecture 19 notes use “actual”, “actually” and “banana” as the example. In one-hot form, “actual” is as far from “actually” as from “banana”. A learned table can place related tokens close together. A network is a continuous function, so close inputs give close outputs. A small mistake in an input vector then causes only a small change in the prediction.

The table holds one vector per token, the same in every sentence. “Bank” gets one vector, whether the sentence is about rivers or money. The transformer blocks then change each vector according to its context. The vector for “bank” leaving the last block depends on the whole sentence. These output vectors are contextual embeddings, and BERT uses them as features. The table’s vectors are static embeddings, like those of 7.3.2. Word2vec.

The RMS rules come from the same reasoning as in 4.3. Maximal Update Parameterization. A one-hot input has RMS norm , about for . The embedding layer must raise this to , the scale that the attention blocks expect. The output layer must map a vector of RMS norm to scores that are neither tiny nor huge.

Properties

(A Lookup Is a Matrix Product) Multiplying by a one-hot vector returns one column, so the layer is computed by indexing.

No multiplication is needed in practice. The layer reads column from memory. The gradient also touches only the columns of tokens that appear in the batch. The column of a token that never appears in the training data stays at its initial value. The tokenizer runs on the CPU before training starts, and the table lives on the GPU with the other weights.

(Scale of the Embedding) Each column of should have RMS norm , because the layer’s output is a column.

The lecture asks for an RMS norm of at the input to the attention heads. The input has RMS norm . The output is the column , so its RMS norm is the column’s own. Entries of size about give . In the RMS-to-RMS view, the layer multiplies the RMS norm of its input by about .

(Scale of the De-embedding) Each row of should have Euclidean norm about , so that the scores stay of size about .

Score is the inner product of row with . The lecture asks each row to satisfy , which gives for equal entries, so . If and the row point in unrelated directions, the score is then about , which is . Entries of size would multiply all scores by . The softmax would then be nearly one-hot from the start, with gradients near .

(Weight Tying) Many models use one matrix for both layers, setting .

Not Taught in Lecture

Weight tying follows Press and Wolf (2017) and Vaswani et al. (2017).

Tying saves parameters, which matters for small models. It also links the two jobs. A token’s input vector and its output row are the same vector. The two layers want different scales, though. The de-embedding rows need entries near , while the embedding columns need entries near . The original transformer ties the weights and multiplies the embedding output by . This factor is exactly the gap between the two RMS rules.

(Where the Table Comes From) The table is usually trained with the rest of the model, but it can start from embeddings learned separately.

The embedding table gets its gradient from the model’s loss, like every other weight. Some models start the table from word2vec vectors. The Fall 2022 lecture 20 notes point out a mismatch. Word2vec learns one vector per word, while a tokenizer may split a rare word into several tokens. Those fragments have no word2vec vectors.

Examples

(Easy) A Lookup

A vocabulary has tokens, indexed to , and . The embedding matrix is

(Medium) Tying and the Factor

A model has and , with tied weights . The entries of follow the de-embedding rule.

(Hard) How Much of a Model Is Embeddings

A small model has , and blocks with weights each, and separate input and output tables. GPT-3 has , and blocks, with one table.

See Also