No history yet

Modern Transformer Mechanics

The Attention Revolution

You understand how a single neuron works, firing based on weighted inputs. But how does a network of neurons read a sentence and grasp its meaning? Early models processed words one by one, like a person reading a ticker tape. This sequential approach struggled to connect words far apart, like a pronoun at the end of a paragraph referring to a name at the beginning. It was a memory bottleneck.

The Transformer architecture, introduced in 2017, shattered this limitation. It processes all the words in a sequence at once. To understand the relationships between them, it uses a mechanism called attention. At its heart, attention allows the model to weigh the importance of every other word in the input when considering a single word. It's like a sophisticated search function within the sentence itself.

For the word 'it' in the sentence 'The cat sat on the mat, but it was tired,' an attention mechanism would learn to assign a high score to 'cat', linking the pronoun to its subject.

This is achieved by creating three vectors for each word: a Query (Q), a Key (K), and a Value (V). The Query is like a question the current word is asking. The Keys are like labels on all the other words. The model compares the Query of one word to the Keys of all other words to find a match. The strength of this match (the attention score) determines how much of each word's Value is passed along.

This process is called Scaled Dot-Product Attention.

Attention(Q,K,V)=softmax(QKTdk)V\text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V
Lesson image

But looking at a sentence from just one perspective is limiting. To capture rich meaning, the model needs to consider different types of relationships simultaneously, like grammatical dependencies, semantic similarities, and co-occurrence patterns. This is where Multi-Head Attention comes in.

Instead of one set of Q, K, and V vectors, Multi-Head Attention creates multiple, smaller sets. It runs the attention mechanism independently for each "head" in parallel. Each head learns to focus on a different aspect of the relationships between words. Afterward, the outputs from all heads are combined and processed through another linear layer, creating a unified representation that is much richer than a single attention pass could provide.

As models grew, Multi-Head Attention became a computational bottleneck. Modern architectures use clever optimizations. Grouped-Query Attention (GQA) is a key refinement. Instead of each query head having its own dedicated key and value heads, GQA allows several query heads to share a single key/value head pair. This drastically reduces the number of calculations and memory needed, especially during inference, without a significant loss in performance.

Another challenge is context length. The original Transformer had a quadratic relationship between sequence length and computation—doubling the text meant quadrupling the work. To handle million-token contexts, methods like Sliding Window Attention were developed. Here, each word only pays attention to a fixed number of neighboring words, turning a global calculation into a much more manageable local one.

Architecture and Scaling

Since Transformers process tokens in parallel, they lose the inherent order of the sequence. To solve this, they inject information about each token's position. While early models used static, sinusoidal functions, modern architectures use (RoPE). RoPE modifies the key and query vectors directly, rotating them by an amount proportional to their position in the sequence. This is a more dynamic way to encode relative positional information, which has proven to scale better to very long sequences.

The original Transformer had two main parts: an Encoder that reads and understands the input text, and a Decoder that generates the output text. This Encoder-Decoder architecture is great for tasks like translation (e.g., T5, BART), where you map one sequence to another.

However, most of today's large language models (LLMs) like the GPT series are Decoder-Only. They are essentially powerful text predictors. Given a sequence of text, their only job is to generate the most likely next word. This simpler, unified architecture has proven incredibly effective for a wide range of generative tasks, from writing essays to coding.

The final piece of the puzzle is scale. Researchers discovered Scaling Laws, a predictable relationship between a model's performance, its size (number of parameters), and the amount of data it's trained on. Doubling the compute budget is best spent by increasing both model size and data, leading to better performance.

This relentless scaling led to a fascinating phenomenon: These are abilities that don't exist in smaller models but appear suddenly in larger ones, such as performing arithmetic, writing code, or understanding nuanced humor. These properties aren't explicitly programmed; they arise from the model learning complex patterns in the vast training data. This shift from simple pattern matching to apparent reasoning is what defines the current era of AI.

Quiz Questions 1/6

What was the primary limitation of pre-Transformer sequential models that the Transformer architecture's parallel processing was designed to overcome?

Quiz Questions 2/6

In the attention mechanism, each word is assigned three vectors: Query (Q), Key (K), and Value (V). What is their collective purpose?

These core mechanics, from attention and positional embeddings to architectural choices and scaling, form the foundation of modern language models.