Day 14: Self-Attention in Transformers
Agenda
- 3:45-3:55pm: Assignment debrief
- 3:55-4:15pm: Walkthrough of attention by hand problems
- 4:15-4:35pm: Overview of first-half of NanoGPT
- 4:35-5:25pm: Start on next assignment(s)
Assignment Debrief
With people around you, given an input sentence, describe each of the computations performed to arrive at the self-attention matrix (i.e., how much token i attends to token j).
Walkthrough of attention by hand problems
Let’s use a toy problem to make sure we have a handle on the mechanics of self-attention. Instead of words, let’s think of individual letters as our tokens (again, sorry for this sleight-of-hand. We are doing this to make the problem as simple as possible to highlight the important bits of self-attention. We’ll also be using a resource called NanoGPT that will implement a GPT, at first, on the character level). Let’s imagine that we want our attention head to take in a sequence of letters and compute for each token whether a consonant has occurred at any point up to and including the current token. Here are some examples.
- Input text: “eaeia”, our attention head should output no, no, no, no, no (none of our token have the property that they are or are preceded by a consonant).
- Input text: “ccrs”, our attention head should output yes, yes, yes, yes (all tokens either are or are preceded by a consonant)
- Input text: “aeri”, our attention head should output no, no, yes, yes (starting with the third token, “r”, we have at least one consonant).
We haven’t quite defined how the responses “no” and “yes” will be represented as vectors, but we will get to that shortly.
Let’s use a tokenization scheme where each letter is mapped to its position in the alphabet (starting with $a \rightarrow 0$ and ending with $z \rightarrow 25$).
Part A
Explain what each of the features (the rows) of the input tokens (the columns) in the embedding matrix $\mathbf{W_E}$ captures.
$$ \mathbf{W_E} = \begin{bmatrix} 1 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 \\ 0 & 1& 1 & 1 & 0 & 1 & 1 & 1 & 0 & 1 & 1 & 1 & 1 & 1 & 0 & 1 & 1 & 1 & 1 & 1 & 0 & 1 & 1 & 1 & 1 & 1 \end{bmatrix} $$
Part B
Define a query ($\mathbf{W_q}$) and key ($\mathbf{W_k}$) matrix pair that causes all letters to attend to consonants.
$\mathbf{W_q}$ and $\mathbf{W_k}$ are both matrices with $n_{q}$ rows and $n_{e}$ columns, where $n_q$ is the query dimension (you can choose this) and $n_e$ is the dimensionality our embeddings (in this example, 2).
Hint 1: You should be able to solve the problem with $n_{q} = 1$ (that is, the key and query matrices are both 1 row and 2 columns).
Hint 2: The key equation you’ll want to use is that the degree to which token $i$ attends to token $j$ can be computed from the embeddings $\mathbf{r}_i$ and $\mathbf{r}_j$ (these would be found in the appropriate column of $\mathbf{W_E}$) of tokens $i$ and $j$ respectively using the following formula.
$$ \begin{aligned} attention &= (\mathbf{W_q} \mathbf{r}_i) \cdot (\mathbf{W_k} \mathbf{r}_j) \end{aligned} $$
Part C
Come up with a short sequence of characters, $s$, consisting of some vowels and some consonants (keep the length pretty small). Compute the matrix of all queries corresponding to your sequence, $\mathbf{Q}$, where the number of rows of $\mathbf{Q}$ is equal to the number of tokens (the length of $s$) and the number of columns is equal to the query dimension. Compute the matrix of all keys corresponding to your sequence, $\mathbf{K}$, where the number of rows of $\mathbf{K}$ is equal to the number of tokens (the length of $s$) and the number of columns is equal to the query dimension. Compute the (pre-masking) attention of each token to each other token using the formula $\mathbf{Q} \mathbf{K}^\top$. Apply masking to ensure that keys (columns) corresponding to later tokens do not influence earlier queries (rows). Note: that the visualization in the 3B1B video (at this time stamp) has this matrix laid out with query tokens as columns and the keys as rows (we wanted to let you know to minimize confusion). Apply a softmax across each row (as before, this is shown on columns in the 3B1B video) to determine a weight for each token and show the resultant matrix.
Part D
Define the value for the $i$th token as $\mathbf{W_V} \mathbf{r}_i$ where $\mathbf{W_V}$ is the identity matrix and $\mathbf{r}_i$ is the embedding of the token. Construct the matrix $\mathbf{V}$ by computing the values of each token using the formula $\mathbf{W_V} \mathbf{r}_i$ and then transforming each value to a row of a matrix. Show that taking your attention matrix from Part C and multiplying it on the right by $\mathbf{V}$ computes the output of the attention head which will give a vector close to $\begin{bmatrix} 1 \ 0 \end{bmatrix}$ if no consonants preceded a token and $\begin{bmatrix} 0 \ 1 \end{bmatrix}$ if at least one consonant preceded a token.
Part E
Suppose you wanted the attention head to determine the proportion of consonants that precede (rather than just whether a consonant precedes a word or not). How would you modify $\mathbf{W_Q}$ and $\mathbf{W_K}$ to achieve this result? You should not need to change $\mathbf{V}$.
Next, let’s see how a position embedding might help us.
Suppose we want our attention head to take in a sequence of letters and output the vector \(\begin{bmatrix} 1 \\ 0 \end{bmatrix}\) if there is a consonant at position 1 (where 1 is the first position in the sequence) and \(\begin{bmatrix} 0 \\ 0 \end{bmatrix}\) otherwise.
- Input text: “eacia”, our attention head should output \(\begin{bmatrix} 0 \\ 0 \end{bmatrix}\), \(\begin{bmatrix} 0 \\ 0 \end{bmatrix}\), \(\begin{bmatrix} 0 \\ 0 \end{bmatrix}\), \(\begin{bmatrix} 0 \\ 0 \end{bmatrix}\), \(\begin{bmatrix} 0 \\ 0 \end{bmatrix}\) (token 1 is a vowel).
- Input text: “ccrs”, our attention head should output \(\begin{bmatrix} 1 \\ 0 \end{bmatrix}\), \(\begin{bmatrix} 1 \\ 0 \end{bmatrix}\), \(\begin{bmatrix} 1 \\ 0 \end{bmatrix}\), \(\begin{bmatrix} 1 \\ 0 \end{bmatrix}\) (the first token is a consonant).
Let’s use the same tokenization scheme as in the previous exercise. That is, each letter is mapped to its position in the alphabet (starting with $a \rightarrow 0$ and ending with $z \rightarrow 25$).
Part A
Explain what each of the features (the rows) of the input tokens (the columns) in the embedding matrix $\mathbf{W_E}$ captures.
We can also specify our position embeddings for each token position (we’ll stop at position $8$ since the pattern should be obvious). Explain what the positional embedding matrix is representing.
Part B
Define a query ($\mathbf{W_q}$) and key ($\mathbf{W_k}$) matrix pair that causes all letters to attend to only the first position in the sequence. In this example, each key might emit the same query (no matter if it is a consonant or value), but the key would only match in the case where the key corresponds to the first token in the sequence.
$\mathbf{W_q}$ and $\mathbf{W_k}$ are both matrices with $n_{q}$ rows and $n_{e}$ columns, where $n_q$ is the query dimension (you can choose this) and $n_e$ is the dimensionality our embeddings (in this example, 3).
Hint 1: You should be able to solve the problem with $n_{q} = 1$ (that is, the key and query matrices are both 1 row and 2 columns).
Hint 2: The key equation you’ll want to use is that the degree to which token $i$ attends to token $j$ can be computed from the embeddings (both position and token embedding) $\mathbf{r}_i$ and $\mathbf{r}_j$ (these would be found in the appropriate columns of $\mathbf{W_E}$ and $\mathbf{W_P}$) of tokens $i$ and $j$ respectively using the following formula.
Part C
Come up with a short sequence of characters, $s$, consisting of some vowels and some consonants (keep the length pretty small). Compute the matrix of all queries corresponding to your sequence, $\mathbf{Q}$, where the number of rows of $\mathbf{Q}$ is equal to the number of tokens (the length of $s$) and the number of columns is equal to the query dimension. Compute the matrix of all keys corresponding to your sequence, $\mathbf{K}$, where the number of rows of $\mathbf{K}$ is equal to the number of tokens (the length of $s$) and the number of columns is equal to the query dimension. Compute the (pre-masking) attention of each token to each other token using the formula $\mathbf{Q} \mathbf{K}^\top$. Apply masking to ensure that keys (columns) corresponding to later tokens do not influence earlier queries (rows). Note: that the visualization in the 3B1B video (at this time stamp) has this matrix laid out with query tokens as columns and the keys as rows (we wanted to let you know to minimize confusion). Apply a softmax across each row (as before, this is shown on columns in the 3B1B video) to determine a weight for each token and show the resultant matrix.
Part D
Determine $\mathbf{W_V}$ to compute the value of each token as $\mathbf{W_V} \mathbf{r}_i$. $\mathbf{V}$ will be formed by laying out each of these values as a row of the matrix. Show that taking your attention matrix from Part C and multiplying it on the right by $\mathbf{V}$ computes the output of the attention head which will give a vector close to $\begin{bmatrix} 1 \ 0 \end{bmatrix}$ if the first token is a consonant and close to $\begin{bmatrix} 0 \ 0 \end{bmatrix}$ otherwise.
Hint: you’ll want to construct $\mathbf{V}$ so consonants are mapped to the vector \(\begin{bmatrix} 1 \\ 0 \end{bmatrix}\) and vowels are mapped to the vector \(\begin{bmatrix} 0 \\ 0 \end{bmatrix}\).
Part E
Why was it important to have a position embedding in order to get this attention head to behave (i.e., have the output) the way we wanted it to?
Overview of first-half of NanoGPT
Let’s go over the code checkpoint from the halfway point of the Karpathy video. I hope it will be helpful to talk through some of the main ideas with you all (and answer some questions).
Upcoming Assignments
- Assignment 13, which is due tomorrow, involves reading a paper on trust
and trustworthiness in machine learning
systems. If you can’t read every word of the paper, please at least familiarize yourself with the contents.
we’ll be discussing some of the key themes in class on Thursday. - Assignment 14, involves finishing up the NanoGPT video and the final
3B1B. You’ll also learn about the idea of ablation experiments as a way to understand machine learning models.
This assignment is due after Spring break.