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
Embed the token sequence . Check the RMS norm of each column. The output is column , column , then column again: , , . The two copies of token get the same vector, since the table has no context. The column RMS norms are , , and . All are close to , as the lecture's rule asks.
(Medium) Tying and the Factor
A model has and , with tied weights . The entries of follow the de-embedding rule.
What RMS norm does a one-hot input have? What RMS norm does the tied embedding layer output? What factor restores the RMS norm of ? A one-hot vector of length has RMS norm . The de-embedding rule gives entries of size . A column of is a row of , so its entries are also about . The embedding output therefore has RMS norm about . Multiplying it by brings the RMS norm to . This is the factor of the original transformer.
(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.
Compute the share of the parameters held by the tables in each model, with biases ignored. What does tying save in the small model? weights. Each table has , and the two tables together hold . The total is , and the tables hold of it. Tying removes one table and leaves parameters, of which the table is .
The small model’s blocks have
GPT-3’s table has entries. Its blocks have weights. The table is of the block weights. The table grows like , but the blocks grow like , so the table matters less as models grow.
See Also
- 7.3.1. Tokenization, which produces the token indices.
- 7.3.2. Word2vec, an older way to learn static vectors for words.
- 7.2. Transformer Architecture, the stack between the two tables.
- 7.5. Encoder and Decoder Models, for contextual embeddings from BERT.
- 4.3. Maximal Update Parameterization, for the RMS-to-RMS view of layer scales.
- 4.3a. Softmax Regression, the softmax at the output.