Move through the deck with (i) the arrow keys, (ii) a presentation clicker, or (iii) the buttons at the bottom left. The gear at the bottom right opens the slide settings.
How does a model calculate which permitted token representations contribute and the contribution from each one?
Answer: it refers to glass.
Attention can incorporate information from the value vector at glass into the representation at it.
An RNN updates its hidden state once per token. Information from token 1 reaches token 6 through five consecutive state updates.
| sequence computation | path from token 1 to token 6 | training schedule within one layer |
|---|---|---|
| RNN | 1\to2\to3\to4\to5\to6 | step 6 waits for steps 1 through 5 |
| self attention | token 6 scores token 1 directly | all query rows use matrix multiplication together |
Each Transformer layer updates the token representations, so the stacked layers revise those representations repeatedly. Within one self attention layer, every permitted pair of positions has a direct scoring path.
One attention head, a single copy of the calculation, contains five operations.
In a search system, a request is compared with index labels, and a successful match retrieves content. Attention uses the standard terms (i) query, (ii) key, and (iii) value for analogous roles.
| standard term | question for one token position | mathematical job |
|---|---|---|
| query, q_i | What information would help position i? | compared with every permitted key |
| key, k_j | What kind of information does position j offer? | produces a compatibility score with a query |
| value, v_j | What content can position j contribute? | enters the weighted output sum |
One hidden row x_i produces all three vectors through three learned projection matrices.
The illustrative token window contains (i) red, (ii) ball, and (iii) rolls. Each input row has three entries. The head produces (i) a query with one entry, (ii) a key with one entry, and (iii) a value with two entries.
| symbol | worked value | meaning |
|---|---|---|
| n | 3 | three token positions |
| d_{model} | 3 | three entries in each input row |
| d_k | 1 | one entry in each query and key |
| d_v | 2 | two entries in each value |
With these dimensions, every multiplication can be written explicitly.
For the illustrative window red ball rolls, use a representation with three slots: each token receives a row containing (i) one entry equal to 1 and (ii) two entries equal to 0.
| position | token | input row x_i |
|---|---|---|
| 1 | red | (1,0,0) |
| 2 | ball | (0,1,0) |
| 3 | rolls | (0,0,1) |
In a trained model, hidden rows are dense vectors computed from tokens and earlier layers. Multiplication by the identity matrix I_3 leaves a matrix unchanged, which exposes the projection arithmetic.
| token row | query q_i | key k_i | value v_i |
|---|---|---|---|
| red | 0 | 0 | (1,0) |
| ball | 0 | \ln2 | (0,2) |
| rolls | 1 | 0 | (1,1) |
The number \ln2\approx0.693 satisfies e^{\ln2}=2.
Because each query and key has one entry, every compatibility score is one multiplication: s_{ij}=q_i k_j.
The query from rolls is 1. Compare it with the three keys, (0,\ln2,0), one at a time.
The head has d_k=1, so division by \sqrt{d_k}=1 leaves these scores unchanged.
Matrix multiplication computes every query row at once. Matrix rows represent queries, and matrix columns represent keys.
The entry in row 3, column 2 records the score from the rolls query to the ball key.
A model that predicts the next token may use its current position and earlier positions. The causal mask assigns -\infty to every future column before softmax.
The symbol -\infty represents an extremely negative score. Because e^{-\infty}=0, softmax assigns zero weight to every masked entry.
| query | red | ball | rolls |
|---|---|---|---|
| red | allowed | future | future |
| ball | allowed | allowed | future |
| rolls | allowed | allowed | allowed |
Decoders that predict the next token use this triangular mask. In their 2020 Vision Transformer manuscript, Dosovitskiy et al. allowed every patch token to attend to every patch token.
The last query, from rolls, may use all three columns. Applying softmax to these scores produces positive weights whose sum is 1.
The coefficient for ball is one half; the coefficients for red and rolls are each one quarter.
| query row | permitted exponentiated scores | divide by their total | weight row |
|---|---|---|---|
| red | (1) | (1)/1 | (1,0,0) |
| ball | (1,1) | (1,1)/2 | (1/2,1/2,0) |
| rolls | (1,2,1) | (1,2,1)/4 | (1/4,1/2,1/4) |
The output for rolls is the weighted sum of three value rows, each with two entries.
| source token | weight | value | weighted contribution |
|---|---|---|---|
| red | 1/4 | (1,0) | (1/4,0) |
| ball | 1/2 | (0,2) | (0,1) |
| rolls | 1/4 | (1,1) | (1/4,1/4) |
The head's output representation for rolls is (0.5,1.25). A second head can produce another output row for the same token.
Matrix multiplication performs the three weighted sums together. Row i of A supplies the coefficients for the permitted value rows in output row i of O.
The largest score in the rolls row belongs to ball. Keeping that value alone would produce v_{\text{ball}}=(0,2).
Scaled dot product attention applies softmax and retains every permitted value with its calculated weight.
The standard result is the weighted sum (0.5,1.25).
When one softmax weight lies very near 1 and the other weights lie very near 0.
| array | shape | meaning |
|---|---|---|
| X | n\times d_{model} | n token rows |
| Q,K | n\times d_k | query and key rows |
| V | n\times d_v | value rows |
| S,M,A | n\times n | one entry per token pair |
| O | n\times d_v | one output row per token |
| calculation | score for key 1 | score for key 2 | softmax weights |
|---|---|---|---|
| raw dot products | 4 | 2 | (0.881,0.119) |
| divide by \sqrt4=2 | 2 | 1 | (0.731,0.269) |
Each score adds d_k products of corresponding query and key entries. As d_k grows, raw score gaps often widen. Scaling keeps several weights able to contribute.
Division by \sqrt{d_k} reduces the effect of head width on the typical score spread.
Because this head has d_k=1, division by \sqrt{d_k}=1 leaves every score unchanged.
Head 1 produced o^{(1)}_{\text{rolls}}=(1/2,5/4). Suppose that a second illustrative head assigns the permitted weight row a^{(2)}_{\text{rolls}}=(1/2,1/4,1/4).
| source token | head 2 weight | value | weighted contribution |
|---|---|---|---|
| red | 1/2 | (1,0) | (1/2,0) |
| ball | 1/4 | (0,2) | (0,1/2) |
| rolls | 1/4 | (1,1) | (1/4,1/4) |
The two heads now provide four numbers for the rolls row: (1/2,5/4) and (3/4,3/4).
Each head has separate projection matrices, so multiplying the same input rows by those matrices produces several learned coordinate representations.
A learned head may become useful for a recurring pattern. A simple human label summarizes observed examples. The same head can exhibit another pattern for another input.
Concatenation places the two head outputs beside each other.
Choose this illustrative output projection, whose shape is 4\times3:
Multiplication by W_O maps four concatenated entries back to d_{model}=3 entries.
A residual connection adds the input row x_{\text{rolls}}=(0,0,1).
The output projection restores the model width, which makes the residual addition possible.
In 2017, Vaswani et al. arranged attention inside each Transformer layer with (i) residual connections, (ii) layer normalization, and (iii) a positionwise feedforward network.
| step | calculation for the token rows | purpose |
|---|---|---|
| 1 | U=\operatorname{MultiHead}(X) | combine information from permitted positions |
| 2 | R=\operatorname{LayerNorm}(X+U) | add the residual input and normalize each row |
| 3 | F=\operatorname{FFN}(R) | apply the same feedforward network, which contains two linear layers, to every token row |
| 4 | Y=\operatorname{LayerNorm}(R+F) | add the second residual input and normalize |
The positionwise feedforward network contains two linear layers with an activation function between them. It updates each token row inside every Transformer layer.
During training, a final linear layer maps every final token row to logits. During generation, the model uses the logits from the newest row.
| import numpy as np rng = np.random.default_rng(9) d_model, d_k, d_v, H = 3, 1, 2, 2 | After importing NumPy and initializing its generator with seed 9, set the dimensions for the two worked heads. |
| def scaled_normal(rows, cols): sd = np.sqrt(2.0 / (rows + cols)) return rng.normal(0.0, sd, size=(rows, cols)) | After computing a standard deviation from the input and output widths, draw every entry. |
| Wq = [scaled_normal(d_model, d_k) for _ in range(H)] Wk = [scaled_normal(d_model, d_k) for _ in range(H)] Wv = [scaled_normal(d_model, d_v) for _ in range(H)] | Initialize (i) a query projection, (ii) a key projection, and (iii) a value projection for each head. |
| Wo = scaled_normal(H * d_v, d_model) | Initialize the output projection with shape 4\times3. |
The scale \sqrt{2/(\mathrm{rows}+\mathrm{cols})} decreases as the matrix dimensions grow.
The matrices in the red ball rolls example use entries chosen by hand so that every result remains exact. A training program can create initial parameter arrays with a pseudorandom generator.
Seed 9 reproduces the same draws when (i) the NumPy version, (ii) the generator type, (iii) the array shapes, and (iv) the generator call order also match.
Because both sentences contain the same token identities, their positions distinguish the grammatical roles. The additive method in the 2017 Transformer included position information before attention.
| symbol | meaning | source of its entries |
|---|---|---|
| e_i | token embedding at position i | the model's learned token embedding table |
| p_i | additive position encoding | a fixed sine and cosine encoding or a learned position embedding |
Vaswani et al. used sinusoidal position encodings in 2017 and reported similar results with learned position embeddings. In 2018, Peter Shaw, Jakob Uszkoreit, and Ashish Vaswani placed relative position representations inside the attention calculation.
| NumPy code | shape and worked value |
|---|---|
| import numpy as np | Import the NumPy library. |
| X = np.eye(3) | 3\times3 identity matrix for the three token rows. |
| Wq = np.array([[0.], [0.], [1.]]) | 3\times1 query matrix chosen by hand. |
| Wk = np.array([[0.], [np.log(2.)], [0.]]) | 3\times1 key matrix containing \ln2. |
| Wv = np.array([[1., 0.], [0., 2.], [1., 1.]]) | 3\times2 value matrix. |
| Q, K, V = X @ Wq, X @ Wk, X @ Wv | The @ operator means matrix multiplication. The output shapes are (i) 3\times1 for Q, (ii) 3\times1 for K, and (iii) 3\times2 for V. |
| scores = Q @ K.T / np.sqrt(Q.shape[1]) | After .T transposes K, matrix multiplication produces the 3\times3 score matrix. |
The code expresses the same row calculations as arithmetic by hand. NumPy performs all query rows together.
| NumPy code | mathematical action |
|---|---|
| allowed = np.tril(np.ones_like( scores, dtype=bool)) | Create a lower triangular Boolean mask in which current and earlier columns are true. |
| scores = np.where(allowed, scores, -np.inf) | Keep permitted scores and assign negative infinity to future columns. |
| shifted = scores - np.max( scores, axis=-1, keepdims=True) | Subtracting each row maximum leaves the softmax weights unchanged and makes the largest shifted score 0, which prevents unnecessarily large exponential values. |
| A = np.exp(shifted) A = A / A.sum(axis=-1, keepdims=True) | Exponentiate each score and divide every row by its own total. |
| O = A @ V | Multiplying A by V computes weighted sums of the value rows and produces an output with shape 3\times2. |
| setting | available token rows | attention computation | next action |
|---|---|---|---|
| training | the full training sequence is stored | the causal mask permits all query rows to run together | compute losses for the predicted positions |
| generation | the prompt and previously chosen tokens are stored | the program compares the newest query with the permitted keys and uses the corresponding values | after the program chooses one token, it appends that token and repeats the calculation |
A generation implementation can keep earlier key and value rows in a cache of keys and values. After the program generates a new token, it appends (i) one new key row and (ii) one new value row.
Training exposes parallel query rows. Autoregressive generation remains sequential because the token chosen at step t becomes part of the input at step t+1.
With n token positions, each of the n queries scores n keys. One full attention head therefore forms n^2 score entries.
Doubling the sequence length multiplies the score count by four.
| tokens n | scores per head n^2 |
|---|---|
| 4 | 16 |
| 8 | 64 |
| 128 | 16,384 |
| 1,024 | 1,048,576 |
These counts describe score entries for one head and one example. Full implementations also store or recompute other intermediate arrays.
An entry A_{ij} is the coefficient for value row v_j in the weighted sum that produces output row o_i for (i) one head, (ii) one layer, and (iii) one forward pass.
In 2019, Jain and Wallace produced similar predictions from attention distributions that differed greatly across several language tasks. Wiegreffe and Pinter argued later that year that conclusions depend on the definition of explanation and the test being used.
Historical precision: the 2017 paper defined (i) encoder self attention, (ii) causal self attention in the decoder, and (iii) attention from the decoder to the encoder. Language models that contain only a decoder use causal self attention to predict the next token.
1. In a causal window with three tokens, which key columns may the query at position 2 use?
Columns 1 and 2. Column 3 is a future position.
2. Compute the softmax weights for the score row (0,\ln2,0).
Exponentiation gives (1,2,1). Because the total is 4, the softmax weights are (1/4,1/2,1/4).
3. Use those weights with values (i) (1,0), (ii) (0,2), and (iii) (1,1).
(1/4,0)+(0,1)+(1/4,1/4)=(1/2,5/4).
4. If (i) n=5, (ii) d_k=2, and (iii) d_v=3, what are the shapes of the score matrix and the head output?
The score matrix is 5\times5; the output is 5\times3.
A diffusion model learns to reverse gradual corruption.