Foundation Models: Tokens, Attention, And Generation · Lesson 5

How Attention Works In A Language Model

Attention uses query–key compatibility to weight value information from allowed source positions. A causal mask enforces the information boundary before normalization and mixing.

Study And Save Progress

Watch The Lesson

6 min 46 sec

Read the video transcript

A model has the prefix “The package arrived after it” and is preparing to continue. For our diagram, the displayed words are supplied sequence units. We are following a causal model, which can use the known prefix while generating onward.

How can information from package contribute to the representation at it, and how do we keep later words from entering that calculation? Each position already has a numerical representation. Learned transformations produce 3 further representations called queries, keys and values: Q, K and V.

The transformation’s parameter matrices can stay fixed while the resulting vectors change with the input. That is the parameter-versus-computed-state distinction from the previous lesson, now applied to a sequence operation. Focus on the query computed at it. Keys at candidate source positions

participate in matching that query. Values at those positions provide information to combine. The names describe numerical roles in the computation. A query is a vector at a position; a key is another vector used for compatibility;

a value is the vector carried into the combination. Keep the matching path and the information-carrying path separate on the diagram. Vaswani and colleagues’ 2017 Transformer paper gives a concrete method called scaled dot-product attention. A dot product multiplies matching query and key coordinates and adds the products.

Their method divides those scores by a dimension-dependent factor before normalization. We will start the next lesson with supplied scores that have already been scaled, so the calculation can focus on what happens afterward. The scores determine shares across allowed source positions.

Softmax exponentiates each allowed score and divides by the sum of those exponentials. For finite scores, the resulting weights are positive and add to 1. A larger score receives a larger share under this rule, while other allowed positions can still contribute. These weights concern positions supplying

information to the current calculation. Multiply each value vector by its position’s weight, then add the corresponding coordinates. The result is a combined vector. Several positions can contribute to it. That representation continues through the model’s later computation. Selecting an output token from a

vocabulary happens at another stage; an attention weight over package is not itself a command to print the word package. This example is self-attention because the queries, keys and values come from representations of the same sequence. Other attention arrangements can draw information from a separately represented input.

The original Transformer used different connections in its encoder and decoder. Identify the sources and allowed connections in the architecture rather than assuming every diagram has the same information boundary. Now suppose a teacher’s complete example continues with was delayed. Those later units can be visible

in the full teaching record while remaining unavailable to the earlier causal prediction. A causal mask prevents the representation at it from using those future positions. In a computation, this is enforced through which connections can receive weight. It is stronger than an instruction asking a model to overlook

information it was allowed to use. Apply the mask before forming the normalized mixture. Excluded positions contribute no weight; the remaining allowed scores determine shares among themselves. If a position is removed, renormalize over the allowed set instead of keeping shares that no longer sum to 1.

In the prefix example, known earlier positions and the current input position can contribute under the defined causal setup. The future continuation cannot supply evidence for predicting itself. This gives us a way to inspect the operation, while leaving a larger explanation still to do.

1 visible attention weight captures part of a computation involving other positions, heads, layers and transformations. To explain a final response, follow the rest of the model and test its behavior. The technical term attention names this numerical mechanism; it does not by itself establish

human attention, consciousness or a complete account of why an answer occurred. Pause for a checkpoint. At the final input unit it, our causal model has only the supplied package prefix. A teacher’s full record also contains the later words was delayed.

What information is combined by the attention operation? May the future words contribute merely because the teacher has them? Would a large weight on 1 position completely explain the final response? Finish your explanation before continuing. The operation combines value vectors using weights

determined from query-key compatibility over allowed positions. The causal mask excludes the future continuation, even when the full training or teaching record contains it. The combined vector proceeds into further computation. A large weight is 1 part of that process, so explaining and evaluating a final response

requires the wider model and task evidence. For a separate practice task, use the supplied prefix “The report was revised after.” A proposed causal-attention diagram gives a large weight to a later editor position from a teacher’s completed record. Explain how you would repair the allowed connections and weights,

and what would be passed forward. Pause and sketch your answer on the blank worksheet. The later editor position is outside the known prefix and must be excluded from this earlier causal calculation. Retain the defined known positions, including the current input position, and normalize their

allowed scores before combining their value vectors. The result is a representation for later model computation. It is separate from choosing a vocabulary token or proving that a particular continuation is factually correct. We can now follow the package example from computed Q, K and V through

compatibility, allowed-position weights and a value mixture. We can also identify an information leak when a diagram lets an earlier causal position use later words. Next, we will supply 3 already-scaled scores and small value vectors so you can reproduce normalization, mixing and the effect of removing a masked position.

Main Point

Attention uses query–key compatibility to weight value information from allowed source positions. A causal mask enforces the information boundary before normalization and mixing.

Key Points

1. Fix the available input

For this classroom diagram, the supplied units are `The`, `package`, `arrived`, `after`, `it`. These word-like units are not a measured production tokenizer result. At the final input unit `it`, a causal computation can use the known prefix, including that current input position, while preparing a next-token prediction.

Keep the full teacher record separate. A later continuation such as `was delayed` cannot supply information to this earlier causal prediction. Architecture determines the allowed connections; another type of model can use a different mask.

2. Separate the numerical roles

A position's representation is transformed with learned parameters to produce Q, K and V. The matrix parameters can stay fixed while these computed vectors change with the input.

ObjectRole in the operation
Query at the current positionParticipates in compatibility with candidate source keys
Key at a source positionParticipates in matching the query
Value at that source positionSupplies vector information to the weighted combination

These are numerical roles. Q is a computed vector, and K is a computed vector rather than a manually assigned database keyword. The input representations and the learned transformations determine their values.

3. Follow scores into a mixture

Vaswani and colleagues' 2017 Transformer paper describes scaled dot-product attention. Its dot products compare queries and keys; division by a dimension-dependent factor controls the score scale before softmax. The next lesson supplies already-scaled scores for a small calculation.

For the defined allowed set, softmax exponentiates each score and divides by the sum of those exponentials. Finite allowed scores yield positive shares summing to 1. Multiply each corresponding value vector by its share and add matching coordinates. Several source positions can contribute to the output vector.

The combined vector continues through the model. It is distinct from the later projection and selection over vocabulary tokens. A high attention weight on a position labelled `package` does not itself instruct the system to print that word.

4. Enforce the mask before normalization

Self-attention draws Q, K and V from representations of the same sequence. Other arrangements can draw information from a separately represented input. The original Transformer uses different encoder and decoder connections; our classroom example isolates a causal prefix.

If the teacher's record contains `was delayed` after `it`, the mask excludes those later positions from this earlier calculation. Recompute the shares over the remaining allowed scores. Removing a contribution while leaving a total below 1 would no longer implement the defined normalized mixture.

The mask is part of the operation. Asking a model to ignore text while permitting the relevant information path is a different control arrangement.

5. Inspect the result at the right scope

One attention weight captures part of a computation. Other positions, heads, layers and transformations can affect the final output. Evaluate the actual response and task alongside the mechanism. The technical name alone establishes neither human consciousness nor a complete faithful explanation of an answer.

6. Closing

You can now trace a current-position query through compatibility, allowed-position shares and value mixing. You can also find a causal information leak in a proposed diagram. The numerical lesson will make the normalization and weighted-vector arithmetic inspectable, including what changes when a position is masked.

7. Learning Checks

Attempt each question before opening the explanation. Keep the separate worksheet blank during your attempt.

M14-L05-Q01

A causal model has only the supplied prefix “The package arrived after it.” A teacher’s complete record also contains the later words “was delayed.” What information does attention combine? May those future words contribute to the earlier calculation? Would a large weight on 1 position completely explain the final response?

Your explanation: ____________________

Show explanation after your attempt

Attention combines value vectors using weights derived from query–key compatibility over allowed source positions. Apply the causal mask before normalization so the later continuation cannot contribute to this earlier calculation. Normalize the allowed scores and form their weighted value mixture. A large attention weight is part of a wider computation, so a final-response explanation also needs the remaining model and task evidence.

M14-L05-P01

Use the supplied prefix “The report was revised after.” A proposed causal-attention diagram gives a large weight to a later editor position from the teacher’s completed record. Explain how to repair the allowed connections and weights, and what would be passed forward.

Your repaired diagram and reasoning: ____________________

Show explanation after your attempt

Exclude the later editor position from this earlier causal calculation. Retain the defined known prefix positions, including the current input position. Recompute normalization over allowed scores so their shares sum to 1, then combine the corresponding value vectors. The result continues through model computation; it is not itself a vocabulary-token selection or proof of factual truth.

Sources

Learner Materials

Download Lesson 5 Learner Materials

Use the supplied fictional cases and templates for practice.

Practice

Write your answer, compare it with the model answer and decide what to revisit. These written checkpoints are self-assessed.

A causal model has only the supplied prefix “The package arrived after it.” A teacher’s complete record also contains the later words “was delayed.” What information does attention combine? May those future words contribute to the earlier calculation? Would a large weight on 1 position completely explain the final response? Your explanation: ____________________

Show The Model Answer

Attention combines value vectors using weights derived from query–key compatibility over allowed source positions. Apply the causal mask before normalization so the later continuation cannot contribute to this earlier calculation. Normalize the allowed scores and form their weighted value mixture. A large attention weight is part of a wider computation, so a final-response explanation also needs the remaining model and task evidence.

Compare your reasoning with the model answer. These written checks are self-assessed.

Use the supplied prefix “The report was revised after.” A proposed causal-attention diagram gives a large weight to a later editor position from the teacher’s completed record. Explain how to repair the allowed connections and weights, and what would be passed forward. Your repaired diagram and reasoning: ____________________

Show The Model Answer

Exclude the later editor position from this earlier causal calculation. Retain the defined known prefix positions, including the current input position. Recompute normalization over allowed scores so their shares sum to 1, then combine the corresponding value vectors. The result continues through model computation; it is not itself a vocabulary-token selection or proof of factual truth.

Compare your reasoning with the model answer. These written checks are self-assessed.

Open Practice And Save Your Work