Transformer Attention Mechanism


Transformer Attention Mechanism

Anna
So we're finally getting into the attention mechanism—the core of transformers. And I know it can be intimidating, but let's break it down step by step. First, remind me: what's the overall goal of the model again?

Michael
Right: take a sequence of tokens—words or word pieces—and predict the next token. The first step is to convert each token into an embedding vector. But here's the catch: at this stage, the embedding for 'mole' is the same whether it's in 'American shrew mole' or 'one mole of carbon dioxide'. No context yet.

Anna
And that's exactly where attention comes in. It lets the model update each embedding based on the surrounding words. So for 'mole' in 'American shrew mole', the embedding should shift toward the animal meaning, while in 'one mole of carbon dioxide' it should shift toward the chemistry meaning. The attention mechanism computes what to add to the original embedding.

Michael
Exactly. And the way it does that is through queries, keys, and values. Think of each token as both asking a question and providing an answer. The query for a noun like 'creature' might be 'Are there adjectives near me?', and the keys for adjectives like 'fluffy' and 'blue' would be designed to match that query.

Anna
So we have a query matrix and a key matrix, both learned. They map the high-dimensional embedding down to a smaller space, say 128 dimensions. Then for each pair of tokens, we compute the dot product between their query and key—that gives a score of how relevant one token is to another.

Michael
Yes, and those scores form an attention pattern. But raw dot products can be any number, so we apply softmax to each column to turn them into a probability distribution—each column sums to 1. And there's a trick: we also divide by the square root of the key dimension for numerical stability.

Anna
Good point. And in a generative model like GPT, we also need masking. Since we're predicting the next token, later tokens shouldn't influence earlier ones. So we set the upper triangle of the attention scores to negative infinity before the softmax. That forces those weights to zero, keeping the model causal.

Michael
Masking is crucial for training too, because we can predict every next token in a sequence simultaneously without cheating. But the size of the attention pattern is the square of the context length, which is why scaling context windows is so hard—it's a quadratic bottleneck.

Anna
Okay, so now we have weights telling us which tokens are relevant to which. Next, we need to actually update the embeddings. That's where the value matrix comes in. For each token, we compute a value vector—also by multiplying its embedding by a learned matrix. Then, for the embedding we want to update, we sum the value vectors of all relevant tokens, weighted by the attention scores.

Michael
So if 'creature' is attending to 'fluffy' and 'blue', we add a large chunk of their value vectors to the 'creature' embedding. The result is a new, context-rich embedding. And that whole process—query, key, value, weighted sum—is one attention head.

Anna
But a single head isn't enough. There are many types of relationships—adjective-noun, subject-verb, coreference, etc. So transformers use multi-headed attention. GPT-3, for example, has 96 heads per layer. Each head learns its own queries, keys, and values, and their updates are summed together.

Michael
And that adds up in parameters. Each head has a query matrix (128x12288), a key matrix (same size), and a value matrix. But the value matrix is actually factored into two smaller matrices to keep the parameter count comparable. So per head we have about 6.3 million parameters. With 96 heads, that's around 600 million per layer.

Anna
And GPT-3 has 96 layers! So the total attention parameters alone are about 58 billion—but that's only a third of the total 175 billion. The rest come from the multilayer perceptron blocks that follow each attention layer. We'll cover those in the next chapter.

Michael
One more thing: attention is extremely parallelizable. Unlike recurrent networks that process tokens sequentially, attention computes all pairwise interactions at once. That's a huge win for GPUs and a big reason transformers scaled up so successfully.

Anna
And that's the essence of attention. It's all about allowing each token to gather information from the whole context, refining its meaning step by step. Even though it seems complex, the core idea is simple: use learned patterns to decide which words influence which, then blend their information.

Ask anything
We've read this whole study set, so questions about it get the best answers.