No history yet

Transformer Architecture

Inside the Transformer

The engine powering most modern large language models is an architecture called the Transformer. Before it came along in 2017, models processed text sequentially, word by word, like reading a book. This worked, but it was slow and often lost track of context in long sentences.

The Transformer changed the game. Its key innovation was parallel processing. Instead of reading one word at a time, it looks at the entire sentence at once. This allows it to grasp the relationships between all the words simultaneously, leading to a much deeper understanding of context.

We propose a new simple network architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence and convolutions entirely.

Self-Attention The Core Idea

The magic behind the Transformer's ability to understand context is a mechanism called self-attention. It allows the model to weigh the importance of different words in a sentence when it's processing a single word.

Consider the sentence: "The robot dropped the glass because it broke." What does "it" refer to? The robot or the glass? For a human, the answer is obvious. Self-attention gives the model this same intuition. As it processes the word "it," self-attention helps it determine that "glass" is the most relevant word to pay attention to, not "robot."

This is done by creating three special vectors for each word: a Query, a Key, and a Value.

  • Query (Q): Represents the current word's search for context. Think of it as the word asking, "Who in this sentence is important to me?"
  • Key (K): Acts like a label for all the other words. It responds to the Query, saying, "Here's what I'm about."
  • Value (V): Contains the actual meaning or substance of a word. Once a word is identified as important, its Value is used.

To find the relationships, the model compares the Query vector of the current word with the Key vector of every other word in the sentence. This comparison generates an "attention score." A high score means a word is highly relevant. These scores are then used to create a weighted sum of all the Value vectors, producing a new representation for the current word that is rich with context.

Lesson image

More Heads Are Better Than One

A single self-attention mechanism is good, but it can only focus on one type of relationship at a time. For example, in our sentence, it might focus on the "it broke" relationship. But what about other relationships, like what the robot was doing?

To capture diverse relationships, the Transformer uses multi-head attention. It's like having several people read the same sentence, with each person tasked to look for different things. One might focus on grammar, another on cause and effect, and a third on the sequence of actions.

The model runs multiple self-attention processes in parallel. Each of these "heads" produces its own contextualized representation of the input. These different perspectives are then combined and passed on, giving the model a much more nuanced and complete understanding of the text.

Lesson image

This parallel approach not only enriches the model's understanding but also makes it highly efficient to run on modern hardware like GPUs.

Putting It All Together

With all the words being processed at once, how does the model know their original order? The sentence "The dog chased the cat" means something very different from "The cat chased the dog." Word order is critical.

This is where positional encodings come in. Before the words enter the self-attention mechanism, a piece of information—a vector representing the word's position—is added to each word's embedding. This gives every word a unique signal that indicates its location in the sequence, ensuring the model doesn't lose this vital information.

After the attention layers have done their work, the output for each word is passed through a simple Feed-Forward Neural Network (FFN). This network processes each word's representation independently, applying a final transformation to it. You can think of this as a processing step that further refines the contextual information gathered by the attention heads.

Together, these components—self-attention, multi-head attention, positional encodings, and feed-forward networks—form a single Transformer block. Modern LLMs stack many of these blocks on top of each other, allowing them to build incredibly sophisticated representations of language.

Lesson image

Now, let's test your understanding of these core concepts.

Quiz Questions 1/6

What was the key innovation of the Transformer architecture that set it apart from previous sequential models?

Quiz Questions 2/6

In the sentence "The robot dropped the glass because it broke," what does the self-attention mechanism primarily help the model understand?

That's a high-level look at the Transformer. Its ability to process text in parallel and weigh word importance through self-attention is what makes today's powerful language models possible.