GPT-3 › Understanding GPT-3's Attention Patterns

Michael BrenndoerferUpdated July 21, 20252 min read
Language AI HandbookAll chapters
Part 28 of 67GPT Architecture
Chapter 199GPT-3 › Understanding GPT-3's Attention Patterns
Reading section 19 of 25 · Understanding GPT-3's Attention Patterns · 2 minSection 19 of 25 · 2 min

Book contents

Section 19 of 25 · 2 min read

GPT-3 › Understanding GPT-3's Attention PatternsOne section · Sections in this chapter19 / 25 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

One way to build intuition about what GPT-3 is doing during in-context learning is to think carefully about how causal self-attention behaves when the context contains few-shot demonstrations followed by a test input.

Recall from our earlier chapters that causal attention allows each token to attend to all previous tokens but not to future ones. In a few-shot prompt, this means the test input token representations can attend to all demonstration tokens, but the demonstration tokens cannot attend to the test input. This asymmetry is important: the model has full access to the demonstrations when processing the test input, but the demonstrations are processed in isolation from each other and from the test input.

Think of the demonstrations as a kind of prefix that conditions the model's predictions. When GPT-3 generates the response to the test input, each generated token can attend to the entire preceding context, including all demonstrations. The attention mechanism thus acts as a soft lookup: the test input representation queries the demonstration representations, and tokens that are semantically similar to the test input receive high attention weights. Their associated value vectors, which encode information about the corresponding outputs, flow into the test input representation and influence the prediction.

The depth of GPT-3's transformer (96 layers) means this conditioning happens iteratively, with each layer refining the representation further. Early layers primarily process syntactic structure; later layers process semantic content and task-relevant patterns. By the final layers, the test input representation has been significantly shaped by the demonstrations, incorporating information about what kind of output is expected. This is the mechanism by which in-context learning integrates the signal from demonstrations without any parameter updates.

A particularly interesting consequence of causal attention is that the order of demonstrations matters in a specific way: later demonstrations have more influence because they can be directly attended to from the test input position with fewer intervening tokens. Earlier demonstrations must propagate their influence through the intermediate positions, which introduces some signal degradation. This is the mechanistic explanation for the recency effect in few-shot prompting.