Concepts / Attention Mechanisms in LLMs

Attention Mechanisms in LLMs

A self-attention mechanism computes the context vector representation as a weighted sum over the inputs.

  • Programming

Why Sequences Need Attention

Large language models process text as sequences of inputs. Each position in a sequence is represented by a vector, and a model needs a way to let each position draw on information from the other positions. The mechanism that does this is called attention. This article focuses on one central form of it, self-attention, and on a variant named scaled-dot product attention, which is the form used in LLMs.

The goal here is not to memorize a formula but to understand what the mechanism computes and how its pieces fit together. Once you can trace a small example by hand, the later material on attention becomes much easier to follow. We will move in small steps: first the arithmetic of a dot product, then the weighted sum that forms a context vector, then the way attention weights are obtained, and finally the trainable matrices that the LLM form adds.

Tracing a Dot Product Step by Step

A dot product takes two vectors of the same length and multiplies them element-wise, then sums the products. The result is a single number, not a vector. That single number is what makes the dot product useful for measuring how two vectors relate.

Formula
Worked example

Dot product of two three-element vectors

Compute the dot product of a = [1, 2, 3] and b = [4, 5, 6].

  1. 1
    Pair the elements

    Element 1 of a pairs with element 1 of b, element 2 with element 2, and element 3 with element 3.

  2. 2
    Multiply each pair

    The pairwise products are $1 \times 4 = 4$, $2 \times 5 = 10$, and $3 \times 6 = 18$.

  3. 3
    Sum the products

    Adding the products gives $4 + 10 + 18 = 32$.

Result

The dot product is the single number 32.

What do you think happens?

Before reading the answer: if you pair the elements of [1, 2, 3] and [4, 5, 6] and sum the products, what single number do you get?

Reveal answer
Answer:

32

The products are 4, 10, and 18, and 4 + 10 + 18 = 32.

Blending Inputs Into a Context Vector

The key idea behind attention is the word context. Each input is represented by a vector. A context vector is a new representation built by combining the input vectors, where each input contributes in proportion to a weight. Inputs with larger weights influence the context vector more, and inputs with smaller weights influence it less. The result is a single vector that blends information from the whole set of inputs according to those weights.

input x1vector of valuesinput x2vector of valuesattention weightsone weight per inputcontext vectorweighted blend of inputsinput x3vector of valuesscoresscoresscoresweighted sumcontributescontributescontributes
How do the input vectors combine into one context vector, and how does each input's weight determine its contribution?

Read the diagram from left to right. Each input vector feeds into the computation of attention weights. Those weights then combine the inputs into the context vector. The context vector is therefore a summary of all the inputs, not just one of them, and the weights determine how that summary is shaped.

Formula

In this formula, $x_j$ is the j-th input vector, $\alpha_j$ is its weight, and $c$ is the context vector. Multiplying a vector by a weight scales each of its elements, and adding the scaled vectors together produces the blend.

Worked example

Building a context vector from three inputs

Inputs are x1 = [1, 0], x2 = [0, 1], and x3 = [1, 1]. Use the illustrative weights 0.5, 0.25, and 0.25. Compute the context vector.

  1. 1
    Scale each input by its weight

    0.5 times [1, 0] is [0.5, 0]. 0.25 times [0, 1] is [0, 0.25]. 0.25 times [1, 1] is [0.25, 0.25].

  2. 2
    Add the scaled vectors element by element

    The first element is 0.5 + 0 + 0.25 = 0.75. The second element is 0 + 0.25 + 0.25 = 0.5.

Result

The context vector is [0.75, 0.5]. It leans toward x1 because x1 has the largest weight.

Key point

A context vector is a single vector that summarizes the whole set of inputs. Its shape is determined by the weights: a larger weight pulls the result toward that input, and a smaller weight lets it influence the result less.

Where the Weights Come From

To form a context vector, you first need the weights. In the simplified attention mechanism, the weights are obtained by taking dot products between the input vectors. Each dot product produces one number, so the set of dot products gives one number per input. Those numbers are the attention weights used in the weighted sum.

query vectorthe input being compareddot with x1one numberdot with x2one numberattention weightsone weight per inputdot with x3one numberdot productdot productdot productbecomes weightbecomes weightbecomes weight
What steps turn a query's dot products with every input into the attention weights, and in what order do they happen?
Worked example

Scores from dot products, then a weighted sum

Using x1 = [1, 0], x2 = [0, 1], and x3 = [1, 1], take x2 = [0, 1] as the vector being compared against each input. Compute the dot product with each input, use those numbers as the weights, and form the context vector.

  1. 1
    Dot product with x1

    [0, 1] paired with [1, 0] gives 0 times 1 plus 1 times 0, which is 0.

  2. 2
    Dot product with x2

    [0, 1] paired with [0, 1] gives 0 times 0 plus 1 times 1, which is 1.

  3. 3
    Dot product with x3

    [0, 1] paired with [1, 1] gives 0 times 1 plus 1 times 1, which is 1.

  4. 4
    Use the scores as weights in the weighted sum

    The weights are 0, 1, and 1. The context vector is 0 times [1, 0] plus 1 times [0, 1] plus 1 times [1, 1], which equals [0, 0] plus [0, 1] plus [1, 1] = [1, 2].

Result

The dot-product weights are 0, 1, and 1, and the resulting context vector is [1, 2]. This example is illustrative. The source pack describes using the dot products as the weights and does not cover any further transformation of those numbers.

Adding Trainable Query, Key, and Value Matrices

The simplified mechanism computes weights directly from the inputs. Self-attention in LLMs adds a further ingredient: trainable weight matrices. These matrices compute intermediate transformations of the inputs, and the source names three of them: queries, values, and keys. Because the matrices are trainable, the way the inputs are transformed is learned rather than fixed by hand.

query matrixtrainablequeryintermediate transformationinput embeddingone input vectorkey matrixtrainablekeyintermediate transformationvalue matrixtrainablevalueintermediate transformationtransformstransformstransformsproducesproducesproduces
How does the same input embedding flow through separate trainable matrices to produce the query, key, and value vectors?

The diagram shows the inputs entering three separate trainable matrices, each producing one of the intermediate transformations. This is the step that distinguishes scaled-dot product attention from the simplified mechanism described earlier, which computes weights directly from the inputs.

Mistakes Learners Make With Attention

  • Common mistake

    Treating the dot product result as a vector

    What it looks like

    Assuming that dotting [1, 2, 3] with [4, 5, 6] gives [4, 10, 18].

    Why it fails

    The dot product multiplies element-wise but then sums the products, so the output is one number.

    Do this instead

    Multiply the matching elements, then add all the products: 4 + 10 + 18 = 32.

  • Common mistake

    Confusing the weights with the context vector

    What it looks like

    Saying the attention weights are [0.5, 0.25, 0.25] and the context vector is also a list of three numbers.

    Why it fails

    The weights are one number per input. The context vector is a new vector built by weighting and adding the inputs.

    Do this instead

    Compute the weighted sum of the input vectors to get the context vector.

  • Common mistake

    Assuming the query, key, and value are separate inputs

    What it looks like

    Believing each token supplies three different raw vectors from the text.

    Why it fails

    The query, key, and value are produced from the same input by three trainable matrices.

    Do this instead

    Start from one input vector and pass it through the query, key, and value matrices.

  • Common mistake

    Assuming the simplified mechanism and scaled-dot product attention are identical, when the trainable matrices are the added ingredient

Practice Tracing a Context Vector

EASY

Inputs are x1 = [2, 0] and x2 = [0, 2]. Use the weights 0.75 for x1 and 0.25 for x2. Compute the context vector, then state whether it is closer to x1 or x2 and explain why.

Hints
  • Scale each input by its weight first: 0.75 times [2, 0] and 0.25 times [0, 2].
  • Add the two scaled vectors element by element.
  • Compare the weights: the larger weight belongs to x1, so the result should lean toward x1.
EASY

Compute the dot product of [3, 1, 2] and [1, 4, 0]. Then say whether the answer is a single number or a vector, and explain what the element-wise step contributes before the sum.

Hints
  • Multiply the first elements, then the second, then the third.
  • The three products are 3, 4, and 0.

Key Takeaways

  • A self-attention mechanism computes a context vector as a weighted sum over the inputs, so the context vector summarizes all inputs according to their weights.
  • A dot product multiplies two vectors element-wise and sums the products, producing a single number that measures how the vectors relate.
  • In a simplified attention mechanism, the attention weights are computed via dot products between input vectors, giving one weight per input.
  • Scaled-dot product attention, the form used in LLMs, adds trainable weight matrices that produce queries, values, and keys from the same inputs.
  • Keep the dot product (a number) and the weighted sum (a vector) distinct when tracing how attention combines its inputs.

More concepts starting with A