← all notes

Attention

concept

I vaguely understand the mechanism and what it’s trying to do around building a stronger contextual representation of the current input with respect to the other supplied inputs but I struggled with understanding how the underlying operations lead to this effect.

Let’s work through the idea.

Let’s say we have a sequence of length
𝑛
of vectors of dimension
𝑑
, each vector acting as a representation of some kind for our input.

Pack them into a matrix
𝑋
with dimension
𝑛
×
𝑑
. Each row containing a vector. When I first read about this I was confused about why we need to pack them into a matrix. The answer is that if we need to pack in context about other vectors in the sequence, we need to able to reason about them in the same place.

Let’s project them into 3 matrices,
𝑊
𝑄
,
𝑊
𝐾
and
𝑊
𝑉
,

We get,

𝑄
=
𝑋
𝑊
𝑄
𝐾
=
𝑋
𝑊
𝐾
𝑉
=
𝑋
𝑊
𝑉

The letter names are arbitrary for now, it should ideally make sense after we work through what the mechanism is doing.

We have,

𝐴
=
softmax
(
𝑄
𝐾
𝑇
)
𝑍
=
𝐴
𝑉

Let’s dive into what’s happening. Why
𝑄
𝐾
𝑇
?

We have,

[
𝑞
1
𝑞
2
…
𝑞
𝑛
]
[
𝑘
1
𝑘
2
…
𝑘
𝑛
]
=
[
𝑞
1
𝑘
1
𝑞
2
𝑘
1
⋮
𝑞
𝑛
𝑘
1
𝑞
1
𝑘
2
𝑞
2
𝑘
2
⋮
𝑞
𝑛
𝑘
2
…
…
⋱
…
𝑞
1
𝑘
𝑛
𝑞
2
𝑘
𝑛
⋮
𝑞
𝑛
𝑘
𝑛
]

This gives us a matrix that shows us how aligned each
𝑞
and
𝑘
vector is to each other. One way to look at a dot product is as a scalar measure of how directionally aligned each vector is.

The softmax operation is then done row-wise. Each row now consists of numbers that sum up to 1, which gives us a weight. The most ‘aligned’
𝑞
𝑘
pair will be the closest to 1.

We take these vectors and apply them to our third linear projection
𝑉
,

𝑍
=
𝐴
𝑉
=
[
𝑎
1
𝑎
2
…
𝑎
𝑛
]
[
𝑣
1
𝑣
2
…
𝑣
𝑛
]

The resultant
𝑍
matrix consists of vectors that are a weighted sum of the vectors in
𝑉
, each weight between 0 to 1.

Now, that’s the base operation. How does this become a contextual representation of the sequence?

If you trace how the gradients flow for an objective. Let’s say that this was part of a language model and the goal was to predict the next token autoregressively.

The
𝑊
𝑄
,
𝑊
𝐾
,
𝑊
𝑉
matrices don’t really do anything when initialized. But they start learning projections that suit their position in the computation.

𝑊
𝑄
and
𝑊
𝐾
are responsible for finding the right coefficients for the weighted sum. I struggled a lot on what the role of
𝑊
𝑉
seemed to be. Why not just use
𝑋
directly and you’d get a weighted sum of the tokens to pay attention to.
𝑊
𝑉
is simply a linear transformation of the tokens so that the model gets to make a linear transformation best suited for the current layer. Functionally, due to the way matrix multiplication works,
𝑍
=
𝐴
𝑉
is nothing but
𝑍
=
𝐴
(
𝑋
𝑊
𝑉
)
=
(
𝐴
𝑋
)
𝑊
𝑉
, in other words,
𝑊
𝑉
really is a learned linear projection that’s transforming the representation to best suit the task at hand.

𝑊
𝑄
and
𝑊
𝐾
get a positive signal from the loss function when they correctly assign the contributions of each token in the final weighted sum.
𝑊
𝑉
gets a positive signal from the loss function when it transforms the result into a representation more useful for subsequent computations.

Over the course of training, these matrices are jointly optimized.
𝑊
𝑞
,
𝑊
𝑘
learn contributions should be weighted.
𝑊
𝑣
learns to represent the information after it’s combined.