Understanding Transformer Architecture
Understanding Transformer Architecture
All
rights reserved. Draft of January 6, 2026.
CHAPTER
8 Transformers
In this chapter we introduce the transformer, the standard architecture for build-
ing large language models. As we discussed in the prior chapter, transformer-based
large language models have completely changed the field of speech and language
processing. Indeed, every subsequent chapter in this textbook will make use of them.
As with the previous chapter, we’ll focus for this chapter on the use of transformers
to model left-to-right (sometimes called causal or autoregressive) language model-
ing, in which we are given a sequence of input tokens and predict output tokens one
by one by conditioning on the prior context.
Softmax
Unembedding
+
Feedforward
Layer Norm
residual N times
stream +
MultiHead
Attention
Layer Norm
+ Positional
Embedding
input token
Figure 8.1 A transformer decoder for language modeling, showing the residual stream for
processing an input token. A single token is embedded and passed forward in the network,
with the feedforward and attention components adding information. The multihead attention
layer takes inputs (not shown in detail) from the neighboring token streams. This is thus one
column of an autoregressive transformer language model, taking an input token and outputting
a distribution over next tokens.
Transformers also have a special mechanism for encoding the position/index of the
token in the input string, which is simply added to the embedding. The resulting
embedding represents both the word and its position. and is then passed through a
set of N transformer blocks.
It’s common to think of each of these transformer blocks as part of a stream in
which the input embedding is directly passed up to the output, while simultaneously
being enriched by the application of various processing modules: the multi-head
attention layer, feedforward networks and the layer normalization. The value of the
stream at any layer is the sum of the original embedding and all the outputs from all
the previous layers and blocks.
The core intuition of the transformer, and the component that distinguishes it
from the feedforward layers we saw in Chapter 6, is this multi-head attention layer,
also called a self-attention layer. Attention can be thought of as a way to build
contextual representations of a token’s meaning by attending to and integrating
information from surrounding tokens, helping the model learn how tokens relate to
each other over large spans. It can also be thought of as a way to move information
from one residual stream to another, augmenting the stream at one token position
with information from another token position.
After the N transformer blocks we take the output embedding that is produced
by the final transformer block, pass it through an linear unembedding matrix U
and then a softmax over the vocabulary to generate a distribution over possible next
tokens. These last two components (the unembedding matrix and the softmax) are
sometimes called the language modeling head. In the rest of this chatper we’ll
introduce attention and the rest of these modules in more detail.
Fig. ?? shows the transformer architecture applied to a context window with
the words So long and thanks for, showing at each token position what is the
most likely token to be generated. In this full figure, the set of N blocks maps an
entire context window of input vectors (x1 , ..., xn ) to a window of output vectors
(h1 , ..., hn ) of the same length. A column might contain from 12 to 96 or more
stacked blocks. The arrows in the figure shows how information from the hidden
representations of preceding tokens is incorporated into the transformer block.
Transformer-based language models are complex, and so the details will unfold
over this chapter and the next few chapters. Chapter 7 already discussed how lan-
guage models are pretrained, and how tokens are generated via sampling. In the
rest of this chapter we’ll introduce multi-head attention, the rest of the transformer
block, and the input encoding and language modeling head components of the trans-
former. Chapter 9 introduces masked language modeling and the BERT family of
bidirectional transformer encoder models. Chapter 10 shows how to instruction-
tune language models to perform NLP tasks, and how to align the model with hu-
man preferences. Chapter 12 will introduce machine translation with the encoder-
decoder architecture. And we’ll see application of the transformer to speech recog-
nition, as well as further use of the encoder-decoder architecture, in Chapter 15.
8.1 Attention
Recall from Chapter 5 that for word2vec and other static embeddings, the repre-
sentation of a word’s meaning is always the same vector irrespective of the context:
the word chicken, for example, is always represented by the same fixed vector. So
a static vector for the word it might somehow encode that this is a pronoun used
8.1 • ATTENTION 3
Language
Modeling
logits logits logits logits logits …
Head U U U U U
Stacked
… … … … …
Transformer …
Blocks
x1 x2 x3 x4 x5 …
+ 1 + 2 + 3 + 4 + 5
Input
Encoding E E E E E
…
for animals and inanimate entities. But in context it has a much richer meaning.
Consider it in one of these two sentences:
(8.1) The chicken didn’t cross the road because it was too tired.
(8.2) The chicken didn’t cross the road because it was too wide.
In (8.1) it is the chicken (i.e., the reader knows that the chicken was tired), while
in (8.2) it is the road (and the reader knows that the road was wide).1 That is, if
we are to compute the meaning of this sentence, we’ll need the meaning of it to be
associated with the chicken in the first sentence and associated with the road in
the second one, sensitive to the context.
Furthermore, consider reading left to right like a causal language model, pro-
cessing the sentence up to the word it:
(8.3) The chicken didn’t cross the road because it
At this point we don’t yet know which thing it is going to end up referring to! So a
representation of it at this point might have aspects of both chicken and road as
the reader is trying to guess what happens next.
This fact that words have rich linguistic relationships with other words that may
be far away pervades language. Consider two more examples:
(8.4) The keys to the cabinet are on the table.
(8.5) I walked along the pond, and noticed one of the trees along the bank.
In (8.4), the phrase The keys is the subject of the sentence, and in English and many
languages, must agree in grammatical number with the verb are; in this case both are
plural. In English we can’t use a singular verb like is with a plural subject like keys
(we’ll discuss agreement more in Chapter 18). In (8.5), we know that bank refers
to the side of a pond or river and not a financial institution because of the context,
including words like pond. (We’ll discuss word senses more in Chapter 9.)
1 We say that in the first example it corefers with the chicken, and in the second it corefers with the
road; we’ll return to this in Chapter 23.
4 C HAPTER 8 • T RANSFORMERS
The point of all these examples is that these contextual words that help us com-
pute the meaning of words in context can be quite far away in the sentence or para-
graph. Transformers can build contextual representations of word meaning, contex-
contextual
embeddings tual embeddings, by integrating the meaning of these helpful contextual words. In a
transformer, layer by layer, we build up richer and richer contextualized representa-
tions of the meanings of input tokens. At each layer, we compute the representation
of a token i by combining information about i from the previous layer with infor-
mation about the neighboring tokens to produce a contextualized representation for
each word at each position.
Attention is the mechanism in the transformer that weighs and combines the
representations from appropriate other tokens in the context from layer k to build
the representation for tokens in layer k + 1.
because
didn’t
cross
tired
Layer k+1
road
The
the
was
too
it
self-attention distribution
chicken
because
didn’t
cross
tired
Layer k
road
The
the
was
too
it
Figure 8.3 The self-attention weight distribution α that is part of the computation of the
representation for the word it at layer k + 1. In computing the representation for it, we attend
differently to the various words at layer k, with darker shades indicating higher self-attention
values. Note that the transformer is attending highly to the columns corresponding to the
tokens chicken and road , a sensible result, since at the point where it occurs, it could plausibly
corefer with the chicken or the road, and hence we’d like the representation for it to draw on
the representation for these earlier words. Figure adapted from Uszkoreit (2017).
a1 a2 a3 a4 a5
x1 x2 x3 x4 x5
Figure 8.4 Information flow in causal self-attention. When processing each input xi , the
model attends to all the inputs up to, and including xi .
Each αi j is a scalar used for weighing the value of input x j when summing up
the inputs to compute ai . How shall we compute this α weighting? In attention we
weight each prior embedding proportionally to how similar it is to the current token
i. So the output of attention is a sum of the embeddings of prior tokens weighted
by their similarity with the current token embedding. We compute similarity scores
via dot product, which maps two vectors into a scalar value ranging from −∞ to
∞. The larger the score, the more similar the vectors that are being compared. We’ll
normalize these scores with a softmax to create the vector of weights αi j , j ≤ i.
Simplified Version: score(xi , x j ) = xi · x j (8.7)
αi j = softmax(score(xi , x j )) ∀ j ≤ i (8.8)
into a probability distribution used to weight the sum of the prior vectors. But now
we’re ready to remove the simplifications.
A single attention head using query, key, and value matrices Now that we’ve
attention head seen a simple intuition of attention, let’s introduce the actual attention head, the
head version of attention that’s used in transformers. (The word head is often used in
transformers to refer to specific structured layers). The attention head allows us to
distinctly represent three different roles that each input embedding plays during the
course of the attention process:
• As the current element being compared to the preceding inputs. We’ll refer to
query this role as a query.
• In its role as a preceding input that is being compared to the current element
key to determine a similarity weight. We’ll refer to this role as a key.
value • And finally, as a value of a preceding element that gets weighted and summed
up to compute the output for the current element.
To capture these three different roles, transformers introduce weight matrices
WQ , WK , and WV . These weights will project each input vector xi into a represen-
tation of its role as a query, key, or value:
qi = xi WQ ; ki = xi WK ; vi = xi WV (8.9)
Given these projections, when we are computing the similarity of the current ele-
ment xi with some prior element x j , we’ll use the dot product between the current
element’s query vector qi and the preceding element’s key vector k j . Furthermore,
the result of a dot product can be an arbitrarily large (positive or negative) value, and
exponentiating large values can lead to numerical issues and loss of gradients during
training. To avoid this, we scale the dot product by a factor related to the size of the
embeddings, via dividing by the square root of the dimensionality of the query and
key vectors (dk ). We thus replace the simplified Eq. 8.7 with Eq. 8.11. The ensuing
softmax calculation resulting in αi j remains the same, but the output calculation for
headi is now based on a weighted sum over the value vectors v (Eq. 8.13).
Here’s a final set of equations for computing self-attention for a single self-
attention output vector ai from a single input vector xi . This version of attention
computes ai by summing the values of the prior elements, each weighted by the
similarity of its key to the query from the current element:
qi = xi WQ ; k j = x j WK ; v j = x j WV (8.10)
qi · k j
score(xi , x j ) = √ (8.11)
dk
αi j = softmax(score(xi , x j )) ∀ j ≤ i (8.12)
X
headi = αi j v j (8.13)
j≤i
ai = headi WO (8.14)
We illustrate this in Fig. 8.5 for the case of calculating the value of the third output
a3 in a sequence.
Note that we’ve also introduced one more matrix, WO , which is left-multiplied
by the attention head. This is necessary to reshape the output of the head. The input
to attention xi and the output from attention ai both have the same dimensionality
[1 × d]. We often call d the model dimensionality, and indeed as we’ll discuss in
8.1 • ATTENTION 7
8. Output of self-attention a3 [1 × d]
7. Reshape to [1 x d] WO [dv × d]
[1 × dv]
6. Sum the weighted
value vectors
×
×
4. Turn into 𝛼i,j weights via softmax
1. Generate k q v k q v k q v
key, query, value WK WQ WV WK WQ WV WK WQ WV
vectors
x1 x2 x3
[1 × d] [1 × d] [1 × d]
Figure 8.5 Calculating the value of a3 , the third element of a sequence using causal (left-
to-right) self-attention.
Section 8.2 the output hi of each transformer block, as well as the intermediate vec-
tors inside the transformer block also have the same dimensionality [1 × d]. Having
everything be the same dimensionality makes the transformer very modular.
So let’s talk shapes. How do we get from [1 × d] at the input to [1 × d] at the
output? Let’s look at all the internal shapes. We’ll have a dimension dk for the
query and key vectors. The query vector and the key vector are both dimensionality
[1 × dk ], so we can take their dot product qi · k j to produce a scalar. We’ll have a
separate dimension dv for the value vectors. The transform matrix WQ has shape
[d × dk ], WK is [d × dk ], and WV is [d × dv ]. So the output of headi in equation
Eq. 8.13 is of shape [1 × dv ]. To get the desired output shape [1 × d] we’ll need to
reshape the head output, and so WO is of shape [dv × d]. In the original transformer
work (Vaswani et al., 2017), d was 512, dk and dv were both 64.
Multi-head Attention Equations 8.11-8.13 describe a single attention head. But
actually, transformers use multiple attention heads. The intuition is that each head
might be attending to the context for different purposes: heads might be special-
ized to represent different linguistic relationships between context elements and the
current token, or to look for particular kinds of patterns in the context.
multi-head So in multi-head attention we have A separate attention heads that reside in
attention
parallel layers at the same depth in a model, each with its own set of parameters that
allows the head to model different aspects of the relationships among inputs. Thus
each head i in a self-attention layer has its own set of query, key, and value matrices:
WQi , WKi , and WVi . These are used to project the inputs into separate query, key,
and value embeddings for each head.
When using multiple heads the model dimension d is still used for the input
and output, the query and key embeddings have dimensionality dk , and the value
embeddings are of dimensionality dv (again, in the original transformer paper dk =
8 C HAPTER 8 • T RANSFORMERS
dv = 64, A = 8, and d = 512). Thus for each head i, we have weight layers WQi of
shape [d × dk ], WKi of shape [d × dk ], and WVi of shape [d × dv ].
Below are the equations for attention augmented with multiple heads; Fig. 8.6
shows an intuition.
ai
[1 x d]
[Adv x d]
Project to final representation WO usually dv=d/A, hence [d x d]
[1 x dv ] [1 x dv ] [1 x dv ] [1 x dv ]
Each head
attends differently Head 1 Head 2 Head 3 Head 4
K4
to context WK1 WV1 WQ1 WK2 WV2 WQ2 WK3 WV3W
WQ3 WK3 WV4 WQ4
hi-2 hi-1 hi
+
Feedforward
Layer Norm
… +
MultiHead
Attention
Layer Norm
xi-2 xi-1 xi
Figure 8.7 The architecture of a transformer block showing the residual stream, showing
how most information flows up through the residual stream, and only the attention module
is sensitive to information from other streams at prior token positions. In this figure and
throughout the chapter, we use the prenorm version of the architecture, in which the layer
norms happen before the attention and feedforward layers rather than after. The first
feedforward layer, and the output of those is added back into the residual, and we’ll
use hi to refer to the resulting output of the transformer block for token i.
We’ve already seen the attention layer, so let’s now introduce the feedforward
and layer norm computations in the context of processing a single input xi at token
10 C HAPTER 8 • T RANSFORMERS
position i.
Feedforward layer The feedforward layer is a fully-connected 2-layer network,
i.e., one hidden layer, two weight matrices, as introduced in Chapter 6. The weights
are the same for each token position i, but are different from layer to layer. It is com-
mon to make the dimensionality dff of the hidden layer of the feedforward network
be larger than the model dimensionality d. (For example in the original transformer
model, d = 512 and dff = 2048.)
FFN(xi ) = ReLU(xi W1 + b1 )W2 + b2 (8.21)
Layer Norm At two stages in the transformer block we normalize the vector (Ba
layer norm et al., 2016). This process, called layer norm (short for layer normalization), is one
of many forms of normalization that can be used to improve training performance
in deep neural networks by keeping the values of a hidden layer in a range that
facilitates gradient-based training.
Layer norm is a variation of the z-score from statistics, applied to a single vec-
tor in a hidden layer. That is, the term layer norm is a bit confusing; layer norm
is not applied to an entire transformer layer, but just to the embedding vector of a
single token. Thus the input to layer norm is a single vector of dimensionality d
and the output is that vector normalized, again of dimensionality d. The first step in
layer normalization is to calculate the mean, µ, and standard deviation, σ , over the
elements of the vector to be normalized. Given an embedding vector x of dimen-
sionality d, these values are calculated as follows.
d
1X
µ = xi (8.22)
d
i=1
v
u d
u1 X
σ = t (xi − µ)2 (8.23)
d
i=1
Given these values, the vector components are normalized by subtracting the mean
from each and dividing by the standard deviation. The result of this computation is
a new vector with zero mean and a standard deviation of one.
(x − µ)
x̂ = (8.24)
σ
Finally, in the standard implementation of layer normalization, two learnable param-
eters, γ and β , representing gain and offset values, are introduced.
(x − µ)
LayerNorm(x) = γ +β (8.25)
σ
Putting it all together The function computed by a transformer block can be ex-
pressed by breaking it down with one equation for each component computation,
using t (of shape [1 × d]) to stand for transformer and superscripts to demarcate
each computation inside the block:
t1i = LayerNorm(xi ) (8.26)
t2i = MultiHeadAttention(t1i , t11 , · · · , t1N )
(8.27)
3 2
ti = ti + xi (8.28)
ti = LayerNorm(ti )
4 3
(8.29)
ti = FFN(ti )
5 4
(8.30)
hi = t5i + t3i (8.31)
8.2 • T RANSFORMER B LOCKS 11
Notice that the only component that takes as input information from other tokens
(other residual streams) is multi-head attention, which (as we see from Eq. 8.27)
looks at all the neighboring tokens in the context. The output from attention, how-
ever, is then added into this token’s embedding stream. In fact, Elhage et al. (2021)
show that we can view attention heads as literally moving information from the
residual stream of a neighboring token into the current stream. The high-dimensional
embedding space at each position thus contains information about the current to-
ken and about neighboring tokens, albeit in different subspaces of the vector space.
Fig. 8.8 shows a visualization of this movement. We therefore call the attention func-
token-mixing tion the token-mixing component of the architecture, because it mixes information
from neighboring token streams into the current stream.
Token A Token B
residual residual
stream stream
Figure 8.8 An attention head can move information from token A’s residual stream into
token B’s residual stream.
Crucially, the input and output dimensions of transformer blocks are matched so
they can be stacked. Each token vector xi at the input to the block has dimensionality
d, and the output hi also has dimensionality d. Transformers for large language
models stack many of these blocks, from 12 layers (used for the T5 or GPT-3-small
language models) to 96 layers (used for GPT-3 large), to even more for more recent
models. We’ll come back to this issue of stacking in a bit.
Equation 8.26 and following are just the equation for a single transformer block,
but the residual stream metaphor goes through all the transformer layers, from the
first transformer blocks to the 12th, in a 12-layer transformer. At the earlier trans-
former blocks, the residual stream is representing the current token. At the highest
transformer blocks, the residual stream is usually representing the following token,
since at the very end it’s being trained to predict the next token.
Once we stack many blocks, there is one more requirement: at the very end of
the last (highest) transformer block, there is a single extra layer norm that is run on
the last hi of each token stream (just below the language model head layer that we
will define soon). 2
2 Note that we are using the most common current transformer architecture, which is called the prenorm
architecture. The original definition of the transformer in Vaswani et al. (2017) used an alternative archi-
tecture called the postnorm transformer in which the layer norm happens after the attention and FFN
layers; it turns out moving the layer norm beforehand works better, but does require this one extra layer
at the end.
12 C HAPTER 8 • T RANSFORMERS
Given these matrices we can compute all the requisite query-key comparisons simul-
taneously by multiplying Q and K| in a single matrix multiplication. The product is
of shape N × N, visualized in Fig. 8.9.
Figure 8.9 The N × N QK| matrix showing how it computes all qi · k j comparisons in a
single matrix multiple.
Once we have this QK| matrix, we can very efficiently scale these scores, take
the softmax, and then multiply the result by V resulting in a matrix of shape N × d:
a vector embedding representation for each token in the input. We’ve reduced the
entire self-attention step for an entire sequence of N tokens for one head to the
following computation:
QK|
head = softmax mask √ V (8.33)
dk
A = head WO (8.34)
8.3 • PARALLELIZING COMPUTATION USING A SINGLE MATRIX X 13
Masking out the future You may have noticed that we introduced a mask function
in Eq. 8.34 above. This is because the self-attention computation as we’ve described
it has a problem: the calculation of QK| results in a score for each query value to
every key value, including those that follow the query. This is inappropriate in the
setting of language modeling: guessing the next word is pretty simple if you already
know it! To fix this, the elements in the upper-triangular portion of the matrix are set
to −∞, which the softmax will turn to zero, thus eliminating any knowledge of words
that follow in the sequence. This is done in practice by adding a mask matrix M in
which Mi j = −∞ ∀ j > i (i.e. for the upper-triangular portion) and Mi j = 0 otherwise.
Fig. 8.10 shows the resulting masked QK| matrix. (we’ll see in Chapter 9 how to
make use of words in the future for tasks that need it).
q1•k1 −∞ −∞ −∞
q2•k1 q2•k2 −∞ −∞
N
q3•k1 q3•k2 q3•k3 −∞
Figure 8.10 The N × N QK| matrix showing the qi · k j values, with the upper-triangle
portion of the comparisons matrix zeroed out (set to −∞, which the softmax will turn to
zero).
Fig. 8.11 shows a schematic of all the computations for a single attention head
parallelized in matrix form.
X Q X K X V
Input
WQ Query Input WK Key Input WV Value
Token 1 Token 1 Token 1 Token 1 Token 1
Token 1
Input Input Key Input Value
Query
Token 2 Token 2 Token 2 Token 2
Input x =
Token 2
x = Key
x =
Token 2
Query Input Input Value
Token 3 Token 3 Token 3 Token 3 Token 3
Token 3
Input Input Key Input Value
Query
Token 4 Token 4 Token 4 Token 4
Token 4 d x dk d x dv Token 4
d x dk
Nxd N x dk Nxd N x dk N x dv
Nxd
q1
x = −∞ −∞ −∞ v1 a1
k1
k2
k3
k4
N x dk NxN NxN N x dv N x dv
Figure 8.11 Schematic of the attention computation for a single attention head in parallel. The first row shows
the computation of the Q, K, and V matrices. The second row shows the computation of QKT , the masking
(the softmax computation and the normalizing by dimensionality are not shown) and then the weighted sum of
the value vectors to get the final attention vectors.
14 C HAPTER 8 • T RANSFORMERS
Fig. 8.9 and Fig. 8.10 also make it clear that attention is quadratic in the length
of the input, since at each layer we need to compute dot products between each pair
of tokens in the input. This makes it expensive to compute attention over very long
documents (like entire novels). Nonetheless modern large language models manage
to use quite long contexts of thousands or tens of thousands of tokens.
Parallelizing multi-head attention In multi-head attention, as with self-attention,
the input and output have the model dimension d, the key and query embeddings
have dimensionality dk , and the value embeddings are of dimensionality dv (again,
in the original transformer paper dk = dv = 64, A = 8, and d = 512). Thus for
each head c, we have weight layers WQ c of shape [d × dk ], WK c of shape [d × dk ],
and WV c of shape [d × dv ], and these get multiplied by the inputs packed into X to
produce Q of shape [N × dk ], K of shape [N × dk ], and V of shape [N × dv ]. The
output of each of the A heads is of shape [N × dv ], and so the output of the multi-
head layer with A heads consists of A matrices of shape [N × dv ]. To make use
of these matrices in further processing, they are concatenated to produce a single
output with dimensionality [N × Adv ]. Finally, we use a final linear projection WO
of shape [Adv × d], that reshapes it to the original output dimension for each token.
Multiplying the concatenated [N × Adv ] matrix output by WO of shape [Adv × d]
yields the self-attention output A of shape [N × d].
Qi = XWQi ; Ki = XWKi ; Vi = XWVi (8.35)
i i |
QK
headi = SelfAttention(Q , K , V ) = softmax mask √
i i i
Vi (8.36)
dk
MultiHeadAttention(X) = (head1 ⊕ head2 ... ⊕ headA )WO (8.37)
Putting it all together with the parallel input matrix X The function computed
in parallel by an entire layer of N transformer blocks—each block over one of the N
input tokens—can be expressed as:
O = X + MultiHeadAttention(LayerNorm(X)) (8.38)
H = O + FFN(LayerNorm(O)) (8.39)
Note that in Eq. 8.38 we are using X to mean the input to the layer, wherever it
comes from. For the first layer, as we will see in the next section, that input is the
initial word + positional embedding vectors that we have been describing by X. But
for subsequent layers k, the input is the output from the previous layer Hk−1 . We
can also break down the computation performed in a transformer layer, showing one
equation for each component computation. We’ll use T (of shape [N × d]) to stand
for transformer and superscripts to demarcate each computation inside the block,
and again use X to mean the input to the block from the previous layer or the initial
embedding:
T1 = LayerNorm(X) (8.40)
T 2
= MultiHeadAttention(T )1
(8.41)
3 2
T = T +X (8.42)
T 4
= LayerNorm(T )3
(8.43)
T5 = FFN(T4 ) (8.44)
5 3
H = T +T (8.45)
Here when we use a notation like FFN(T3 ) we mean that the same FFN is applied
in parallel to each of the N embedding vectors in the window. Similarly, each of the
8.4 • T HE INPUT: EMBEDDINGS FOR TOKEN AND POSITION 15
N tokens is normed in parallel in the LayerNorm. Crucially, the input and output
dimensions of transformer blocks are matched so they can be stacked. Since each
token xi at the input to the block is represented by an embedding of dimensionality
[1 × d], that means the input X and output H are both of shape [N × d].
5 |V| 5 d
1 0000100…0000 ✕ E = 1
|V|
Figure 8.12 Selecting the embedding vector for word V5 by multiplying the embedding
matrix E with a one-hot vector with a 1 in index 5.
We can extend this idea to represent the entire token sequence as a matrix of one-
hot vectors, one for each of the N positions in the transformer’s context window, as
shown in Fig. 8.13.
16 C HAPTER 8 • T RANSFORMERS
d
|V| d
0000100…0000
0000000…0010
1000000…0000 ✕ E =
…
N 0000100…0000
N
| V|
Figure 8.13 Selecting the embedding matrix for the input sequence of token ids W by mul-
tiplying a one-hot matrix corresponding to W by the embedding matrix E.
Transformer Block
X = Composite
Embeddings
(word + position)
+
+
Word
Janet
back
will
the
bill
Embeddings
Position
1
Embeddings
Janet will back the bill
Figure 8.14 A simple way to model position: add an embedding of the absolute position to
the token embedding to produce a new embedding of the same dimensionality.
positions, like the fact that position 4 in an input is more closely related to position
5 than it is to position 17.
A more complex style of positional embedding methods extend this idea of cap-
relative
position turing relationships even further to directly represent relative position instead of
absolute position, often implemented in the attention mechanism at each layer rather
than being added once at the initial input.
Figure 8.15 The language modeling head: the circuit at the top of a transformer that maps from the output
embedding for token N from the last transformer layer (hLN ) to a probability distribution over words in the
vocabulary V .
A softmax layer turns the logits u into the probabilities y over the vocabulary.
u = hLN ET (8.46)
y = softmax(u) (8.47)
hLi
extra layer norm
feedforward
layer norm
Layer L
attention
layer norm
hL-1i = xLi
…
h2i = x3i
feedforward
layer norm
Layer 2
attention
layer norm
h1i = x2i
feedforward
layer norm
Layer 1
attention
layer norm
x1i
+ i
Input
E Encoding
Input token wi
Figure 8.16 A transformer language model (decoder-only), stacking transformer blocks
and mapping from an input token wi to a predicted next token wi+1 .
words tend to be more creative and more diverse, but less factual and more likely to
be incoherent or otherwise low-quality.
tion.
5. Randomly sample a word from within these remaining k most-probable words
according to its probability.
When k = 1, top-k sampling is identical to greedy decoding. Setting k to a larger
number than 1 leads us to sometimes select a word which is not necessarily the most
probable, but is still probable enough, and whose choice results in generating more
diverse but still high-enough-quality text.
8.7 Training
We described the training process for language models in the prior chapter. Re-
call that large language models are trained with cross-entropy loss, also called the
negative log likelihood loss. At time t the cross-entropy loss is the negative log prob-
ability the model assigns to the next word in the training sequence, − log p(wt+1 ).
Fig. 8.17 illustrates the general training approach. At each step, given all the
preceding words, the final transformer layer produces an output distribution over the
entire vocabulary. During training, the probability assigned to the correct word by
the model is used to calculate the cross-entropy loss for each item in the sequence.
The loss for a training sequence is the average cross-entropy loss over the entire
sequence. The weights in the network are adjusted to minimize the average CE loss
over the training sequence via gradient descent.
With transformers, each training item can be processed in parallel since the out-
put for each element in the sequence is computed separately.
Large models are generally trained by filling the full context window (for exam-
ple 4096 tokens for GPT4 or 8192 for Llama 3) with text. If documents are shorter
than this, multiple documents are packed into the window with a special end-of-text
token between them. The batch size for gradient descent is usually quite large (the
largest GPT-3 model uses a batch size of 3.2 million tokens).
8.8 • D EALING WITH S CALE 21
Loss
<latexit sha1_base64="AovqpaL476UmJ1EU1xZPgDZ70tQ=">AAAB9nicbVDLSsNAFL2pr1pfURcu3AwWwY0lEakui25cVrAPaEqYTCbt0EkmzEzEEvIrbkTcKPgZ/oJ/Y9Jm09YDA4dzznDvPV7MmdKW9WtU1tY3Nreq27Wd3b39A/PwqKtEIgntEMGF7HtYUc4i2tFMc9qPJcWhx2nPm9wXfu+ZSsVE9KSnMR2GeBSxgBGsc8k1Ty4dLkZo6qZOiPVYhimO/CyruWbdalgzoFVil6QOJdqu+eP4giQhjTThWKmBbcV6mGKpGeE0qzmJojEmEzyi6WztDJ3nko8CIfMXaTRTF3I4VGoaenmy2E0te4X4nzdIdHA7TFkUJ5pGZD4oSDjSAhUdIJ9JSjSf5gQTyfINERljiYnOmypOt5cPXSXdq4bdbDQfr+utu7KEKpzCGVyADTfQggdoQwcIZPAGn/BlvBivxrvxMY9WjPLPMSzA+P4DPEiSHA==</latexit>
log yand
<latexit sha1_base64="q3ZgXDyG7qtkT7t8hT47RdlwYG4=">AAAB+XicbVDLSsNAFJ3UV62vWHe6GVsEN5bERXUlBUVcVrAPaEqYTCft0MlMmJkIIQT8AT/CTRE3Cv6Ev+DfmLTdtPXAwOGcM9x7jxcyqrRl/RqFtfWNza3idmlnd2//wDwst5WIJCYtLJiQXQ8pwignLU01I91QEhR4jHS88W3ud56JVFTwJx2HpB+gIac+xUhnkmseXzhMDGHsJk6A9EgGiR4hPlZpWnLNqlWzpoCrxJ6TauP0tXw3qdw0XfPHGQgcBYRrzJBSPdsKdT9BUlPMSFpyIkVChMdoSJLp5ik8y6QB9IXMHtdwqi7kUKBUHHhZMl9PLXu5+J/Xi7R/3U8oDyNNOJ4N8iMGtYB5DXBAJcGaxRlBWNJsQ4hHSCKss7Ly0+3lQ1dJ+7Jm12v1x6yDezBDEZyACjgHNrgCDfAAmqAFMHgBE/AJvozEeDPejY9ZtGDM/xyBBRjff79pldo=</latexit>
log ythanks … =
Language
Modeling
logits logits logits logits logits …
Head U U U U U
Stacked
… … … … …
Transformer …
Blocks
x1 x2 x3 x4 x5 …
+ 1 + 2 + 3 + 4 + 5
Input
Encoding E E E E E
…
Large language models are large. For example the Llama 3.1 405B Instruct model
from Meta has 405 billion parameters (it has L=126 layers, model dimensionality
d=16,384, and A=128 attention heads) and was trained on 15.6 terabytes of text
tokens using a vocabulary of 128K tokens (Llama Team, 2024). So there is a lot of
research on understanding how LLMs scale, and especially how to implement them
given limited resources. In the next few sections we discuss how to think about
scale (the concept of scaling laws), and important techniques for getting language
models to work efficiently, such as the KV cache and parameter-efficient fine tuning
(PEFT).
or compute budget, if in each case the other two properties are held constant:
Nc
αN
L(N) = (8.49)
N
Dc
αD
L(D) = (8.50)
D
Cc
αC
L(C) = (8.51)
C
8.8.2 KV Cache
We saw in Fig. 8.11 and in Eq. 8.34 (repeated below) how the attention vector can
be very efficiently computed in parallel for training, via two matrix multiplications:
QK|
A = softmax √ V (8.53)
dk
Q QKT V A
KT v1
x = x v2
k1
k2
k3
k4
=
v3
dk x N v4
q4 q4•k1 q4•k2 q4•k3 q4•k4 a4
1 x dk 1xN N x dv 1 x dv
Figure 8.18 Parts of the attention computation (extracted from Fig. 8.11) showing, in black,
the vectors that can be stored in the cache rather than recomputed when computing the atten-
tion score for the 4th token.
d
h 1
d
× r B
Pretrained
Weights
k k A
W
d r
x 1
d
Figure 8.19 The intuition of LoRA. We freeze W to its pretrained values, and instead fine-
tune by training a pair of matrices A and B, updating those instead of W, and just sum W and
the updated AB.
Figure
Figure 1:8.20 An induction
In the sequence head
“...vintage looking
cars ... vintage”,atanvintage uses
induction head the prefix
identifies matching
the initial mechanism
occurrence of “vintage”,to
find a prior
attends to theinstance
subsequentofword “cars” forand
vintage, prefixthe copying
matching, and mechanism
predicts “cars” to predict
as the next wordthatthrough will
cars the occur
copying
mechanism.
again. Figure from Crosbie and Shutova (2022).
determines each head’s independent output for the 4.2 Identifying Induction Heads
current token.
Olsson et al. (2022) propose that a generalized To identifyfuzzy version
induction heads of thismodels,
within pattern we com-
mea-
Leveraging this decomposition, Elhage et al. sure the ability of all attention heads to perform
pletion rule, implementing a rule
(2021) discovered a distinct behaviour in certain
like A*B*...A→ B, where A* ≈ A and B* ≈B
prefix matching on random input sequences.4 We
(by ≈ weheads,
attention mean they
which theyare
namedsemantically
induction heads. similar in some way), might be responsible
follow the task-agnostic approach to computing pre-
for in-context
This learning.
behaviour emerges whenSuggestive evidencefixfor
these heads process theirscores
matching hypothesis comes
outlined by Bansalfrom Cros-
et al. (2023).
ablating sequences
bie of the form
and Shutova "[A] [B]
(2022), who [A] → that
... show ". Inablating
We argue induction heads
that focusing solelycauses
on prefixin-context
matching
these heads, the QK circuit directs attention to- scores is sufficient for our analysis, as high pre-
learning performance to decrease. Ablation is originally a medical term meaning
wards [B], which appears directly after the previous fix matching cores specifically indicate induction
the removal
occurrence of current
of the something.
token [A].We Thisuse it in NLP
behaviour interpretability studies as a tool for
heads, while less relevant heads tend to show high
testing
is termedcausal
prefix effects;
matching. ifTheweOVknockcircuit out a hypothesized
subse- cause,
copying capabilities we etwould
(Bansal expect
al., 2023). the
We gen-
quently
effect toincreases the output
disappear. logit of
Crosbie and theShutova
[B] token, (2022)
erate aablate
sequenceinduction heads
of 50 random by first
tokens, find-
excluding
termed copying. An overview of this mechanism is
ing attention heads that perform as inductiontheheads
shown in Figure 1.
4% most on common
randomand leastsequences,
input common tokens. and
This sequence is repeated four times to form the
then zeroing out the output of these heads by setting certain terms of the output ma-
input to the model. The prefix matching score is cal-
trix
4 W O
to zero. Indeed they find that ablated
Methods models
culated are much
by averaging worsevalues
the attention at in-context
from each
learning: they have much worse performance token to the tokens that directly followed the in
at learning from demonstrations the
same
4.1 Models
prompts. token in earlier repeats. The final prefix matching
We utilise two recently developed open-source scores are averaged over five random sequences.
models, namely Llama-3-8B2 and InternLM2-20B The prefix matching scores for Llama-3-8B are
(Cai et al., 2024), both of which are based on the shown in Figure 2. For IntermLM2-20B, we refer
8.9.2 Logit
original Llama Lenset al., 2023a) architec- to Figure 8 in Appendix A.1. Both models exhibit
(Touvron
ture. These models feature grouped-query atten- heads with notably high prefix matching scores,
tion mechanisms (Ainslie et al., 2023) to enhance distributed across various layers. In the Llama-3-
logit lens Another
[Link] interpretability
Llama-3-8B, the logit
tool, each
comprises 32 layers, lens (Nostalgebraist, 2020), offers a
8B model, ~3% of the heads have a prefix matching
way
withto32visualize whatandthe
attention heads internal
it uses layers
a query group of score
the transformer
of 0.3 or higher, might be representing.
indicating a degree of spe-
The
size of 4idea is that
attention weIttake
heads. any vector
has shown superiorfrom any layer
cialisation of the
in prefix transformer
matching, and,have
and some heads pre-
performance
tending that compared
it is the toprefinal
its predecessors, even simply
embedding, high scores of up to 0.98.
multiply it by the unembedding
the larger Llama-2 models.
layer to get logits, and compute a softmax to see the distribution over words that
InternLM2-20B, featuring 48 layers with 48 at- 4.3 Head Ablations
that vector
tention headsmight be arepresenting.
each, uses query group sizeThis of 6 can be a useful
To investigate window into
the significance the internal
of induction heads
representations
attention heads. We of selected
the model. Since thefornetwork
InternLM2-20B wasn’t
for a specific ICLtrained
task, weto makezero-ablations
conduct the internal
its exemplary performance
representations functiononinthe Needle-in-the-
this way, the logitoflens
1% and doesn’t
3% of thealways worktheperfectly,
heads with highest prefixbut
Haystack3 task, which assesses LLMs’ ability to matching scores. This ablation process involves
this can still be a useful trick to help us visualize the internal layers of a
retrieve a single critical piece of information em- masking the corresponding partition of the output
transformer.
bedded within a lengthy text. This mirrors the matrix, denoted as Woh in Eq. 1, by setting it to
functionality of induction heads, which scan the zero. This effectively renders the heads inactive
context for prior occurrences of a token to extract 4
In this work, the term "induction heads" refers to what
relevant subsequent information. we define as behavioural induction heads, not mechanistic
ones. A true induction head must be verified mechanistically;
2
[Link] however, our analysis employs prefix-matching scores as a
3
[Link] proxy. We will continue to use the term "induction heads" for
NeedleInAHaystack simplicity throughout the rest of the paper.
4
26 C HAPTER 8 • T RANSFORMERS
8.10 Summary
This chapter has introduced the transformer and its components for the language
modeling task introduced in the previous chapter. Here’s a summary of the main
points that we covered:
• Transformers are non-recurrent networks based on multi-head attention, a
kind of self-attention. A multi-head attention computation takes an input
vector xi and maps it to an output ai by adding in vectors from prior tokens,
weighted by how relevant they are for the processing of the current word.
• A transformer block consists of a residual stream in which the input from
the prior layer is passed up to the next layer, with the output of different com-
ponents added to it. These components include a multi-head attention layer
followed by a feedforward layer, each preceded by layer normalizations.
Transformer blocks are stacked to make deeper and more powerful networks.
• The input to a transformer is computed by adding an embedding (computed
with an embedding matrix) to a positional encoding that represents the se-
quential position of the token in the window.
• Language models can be built out of stacks of transformer blocks, with a
language model head at the top, which applies an unembedding matrix to
the output H of the top layer to generate the logits, which are then passed
through a softmax to generate word probabilities.
• Transformer-based language models have a wide context window (200K to-
kens or even more for very large models with special mechanisms) allowing
them to draw on enormous amounts of context to predict upcoming words.
• There are various computational tricks for making large language models
more efficient, such as the KV cache and parameter-efficient finetuning.
Historical Notes
The transformer (Vaswani et al., 2017) was developed drawing on two lines of prior
research: self-attention and memory networks.
Encoder-decoder attention, the idea of using a soft weighting over the encodings
of input words to inform a generative decoder (see Chapter 12) was developed by
Graves (2013) in the context of handwriting generation, and Bahdanau et al. (2015)
for MT. This idea was extended to self-attention by dropping the need for separate
encoding and decoding sequences and instead seeing attention as a way of weighting
the tokens in collecting information passed from lower layers to higher layers (Ling
et al., 2015; Cheng et al., 2016; Liu et al., 2016).
Other aspects of the transformer, including the terminology of key, query, and
value, came from memory networks, a mechanism for adding an external read-
write memory to networks, by using an embedding of a query to match keys rep-
resenting content in an associative memory (Sukhbaatar et al., 2015; Weston et al.,
2015; Graves et al., 2014).
MORE HISTORY TBD IN NEXT DRAFT.
Historical Notes 27