Attention Mechanism in Transformers
The second is implementing the attention mechanism, the component that lets the model weigh different parts of its input against one another.
Predicting the Order of Building
Once you understand the basic ideas behind large language models, the natural next question is how one actually gets built. The approach taken here is to start from the core idea behind GPT and use it as a blueprint for a model you write yourself. That blueprint is carried out in three stages, and each stage hands its result to the next one.
What do you think happens?
If you were building a model that can hold a conversation, which would you expect to come first: training it to chat, or deciding how its internal structure should look?
Reveal answer
Deciding how the internal structure should look comes first, together with preparing the data. Chat behaviour is shaped later, during fine-tuning.
At the very start there is no trained model, only the intention to build one. Two things must exist before any learning can happen: a description of the model's architecture, and a body of data the model can learn from. Training only makes sense once those foundations are in place.
Three Stages on a Single Path
The three stages form a single path. Stage one covers data preparation and sampling, the attention mechanism, and the overall LLM architecture. Stage two pretrains the model on unlabeled data, producing what is called a foundation model. Stage three fine-tunes that foundation model so that it serves a specific purpose, such as acting as a personal assistant or classifying text.
A useful way to hold the whole path in mind is this: stage one builds the empty machine and gathers its raw material, stage two runs that machine over a large amount of general material, and stage three steers the already-capable machine toward one particular job. The diagram below shows the order and the handoff at each boundary.
Stage One: Getting the Machine and Data Ready
Stage one is about getting ready, and it has three parts. The first is preparing the data, which means choosing how text is sampled so the model can be fed with it in a structured way. The second is implementing the attention mechanism, the component that lets the model weigh different parts of its input against one another. The third is defining the LLM architecture, the full structure that the attention mechanism sits inside.
Stage Two: Building a Foundation Model
Stage two is pretraining. The model is trained on unlabeled data, which means the training text does not come with annotations telling the model what each example is supposed to mean. The purpose of this stage is to produce a foundation model, a general-purpose result that can be specialized later. Pretraining is not yet about any single job. It is about building a broad base.
This is why the output of pretraining is described as a base rather than a finished application. A model that has only been pretrained has learned from general material, not from examples of a specific task. It is reusable because it is general, and it is incomplete for any one purpose until stage three adjusts it.
Stage Three: Specializing the Foundation Model
Stage three takes the foundation model from stage two and adjusts it for a particular application. Fine-tuning can produce two kinds of result named in the source material: a personal assistant or a text classifier. The kind of data used determines which one you get. A dataset with class labels leads toward a classifier that sorts text into categories, while an instruction dataset leads toward a personal assistant or chat model.
| Dataset used in fine-tuning | Result | What the result is for |
|---|---|---|
| Dataset with class labels | Text classifier | Sorting text into categories |
| Instruction dataset | Personal assistant or chat model | Acting as a conversational assistant |
The same pretrained foundation model can lead to different results depending on the fine-tuning data.
Tracing Relative Importance Through a Sentence
Read a sentence and you constantly decide which other words matter for understanding a given word. A pronoun such as 'it' is meaningless until you connect it to something earlier in the text. A word near the end of a long passage may depend on a phrase introduced many words before it. Language is full of these links, and they do not always sit next to each other.
Self-attention is the component designed for this problem. Its job is to let the model weigh the importance of different words or tokens in a sequence relative to each other. Because the weighing happens between the words of the input itself, the mechanism is described as self-attention. When the model processes one token, not every other token is equally useful for interpreting it. Some neighbours carry most of the meaning, some carry little, and some far-away tokens carry a crucial clue.
The diagram below uses an original example sentence to make this unequal relevance visible. It is an illustration, not a computed result. The source does not show the arithmetic the model uses, so the strengths of the links shown here are a teaching device for the kind of judgement self-attention makes.
The important point is that the weights differ from one word to another. The same sentence contains many tokens, and each one has its own pattern of relevance. A different token, such as 'cross', would draw on the rest of the sentence in its own way. The representation of each token can therefore draw more heavily on the tokens that matter most to it.
Encoder and Decoder Splitting the Translation
The source uses the transformer architecture as the setting for self-attention. A transformer is a deep learning model architecture for language translation. In the simplified depiction the source gives, it is made of two parts: an encoder and a decoder. In the original transformer architecture, the encoder processes the complete input text and produces an embedding representation, and the decoder uses that representation to generate output one word at a time.
The diagram below follows one translation from the input sentence 'This is an example' toward the German output. The partially translated sentence is 'Das ist ein'. The decoder's remaining work at that point is to generate only the last word, 'Beispiel', to complete the translation. Read the diagram from the input on the left to the complete output on the right.
The two parts have distinct jobs, and keeping them separate makes it easier to see where the attention-based weighing of words does its work. The encoder reads and represents the whole input. The decoder builds the output, one word at a time, drawing on that representation. Self-attention sits within this larger structure, and the source places it there rather than treating it as a free-standing piece.
Long-Range Links Without Neighbouring Words
Self-attention is what allows a model to relate every position in an input to other positions, rather than reading each word in isolation. The source identifies two closely linked benefits. First, the mechanism enables the model to capture long-range dependencies, meaning relationships between words that sit far apart. Second, it captures contextual relationships within the input, so that a token's role is shaped by its surroundings.
Consider a clue introduced near the start of a long passage and needed again near the end. Because each position can weigh every other position in the input, the later word is not forced to depend only on the words immediately beside it. The link can be drawn directly, however far apart the two words are.
The source connects these capabilities to output quality. By capturing these relationships, the mechanism enhances the model's ability to generate output that is coherent and contextually relevant. The reason self-attention matters is not only that it sees many words at once, but that it uses those words to judge what each one means in its setting.
Worked Example: Which Stage Shapes the Result
Choosing the fine-tuning data for a support team
A team has a foundation model from stage two and wants it to sort incoming support messages into categories such as billing or technical. They ask which kind of fine-tuning data will lead to the result they want. This scenario is original to this article.
- 1Identify what the team needs
The goal is to sort text into categories, not to hold a conversation.
- 2Match the need to the data type
The source says a dataset with class labels leads toward a classifier that sorts text into categories. An instruction dataset leads toward a personal assistant or chat model.
- 3Confirm what stays the same
The starting point is still the same foundation model from pretraining. Only the stage three fine-tuning data changes the outcome.
The team should fine-tune on a labeled dataset of messages with their categories, which leads toward a text classifier.
Common Mistakes With Attention and Stages
Common mistake Thinking pretraining produces a finished chatbot
Why it failsPretraining on unlabeled data produces a foundation model, which is a base rather than a finished application.
Do this insteadTreat the output of stage two as a general base that stage three must specialize.
Common mistake Treating attention as a topic separate from the architecture
Why it failsAttention is one of the required pieces of the LLM architecture defined in stage one.
Do this insteadUnderstand and implement the attention component before training begins.
Common mistake Assuming self-attention only looks at neighbouring words
Why it failsThe source says self-attention helps capture long-range dependencies between words that sit far apart.
Do this insteadThink of attention weights as relating any position to any other position in the input, weighted by relevance.
Common mistake Believing the encoder and decoder do the same job
Why it failsThe encoder processes the complete input and produces an embedding representation, while the decoder generates output one word at a time.
Do this insteadKeep the reading role and the generating role separate when tracing a translation.
Common mistake Expecting the same fine-tuning data to produce both a classifier and an assistant
Practice Tracing Dependencies
Take the sentence 'The coach told the players that they would practise harder after the match, and they did.' Pick one pronoun or reference word and list the earlier words it would need to connect to. Then explain in your own words why a model needs to weigh far-apart words to interpret it correctly.
Hints
- Start by finding the word that has no meaning on its own.
- Ask whether the word it depends on is next to it or further back in the sentence.
- Remember that the source describes self-attention as weighing tokens relative to each other, not reading them in isolation.
Where This Introduction Stops
This article gives the conceptual foundation for self-attention. It explains what the mechanism is meant to do: weigh the importance of tokens relative to each other, support long-range dependencies, and help the encoder and decoder of the transformer divide translation work. It does not explain how the model computes those judgements.
Key Takeaways
- Building an LLM happens in three stages: implementing the architecture and preparing data, pretraining on unlabeled data to produce a foundation model, and fine-tuning that foundation model for a specific purpose.
- The foundation model from pretraining is a general base, and the fine-tuning dataset decides whether the result is a personal assistant or a text classifier.
- Self-attention weighs the importance of different words or tokens in a sequence relative to each other, and it is part of the architecture built in stage one.
- In the original transformer, the encoder processes the complete input into an embedding representation, and the decoder generates the output one word at a time.
- Because self-attention relates positions across the input, it captures long-range dependencies and context; the detailed mechanics are deferred to a later chapter.