What did Andrej Karpathy say about attention and MLPs in Transformers?

Andrej Karpathy said that attention is the communication step in a Transformer, while the MLP is the per-token thinking step. Attention aggregates information across tokens with a weighted sum, whereas the MLP operates independently on each token. He summarized the Transformer as an alternating map-reduce style process.

“So the attention is the reduce, and the MLP is the map. And what you end up with is that the Transformer just ends up just being a repeated application of map produce if you want to think about it that way. So this is where they communicate, and this is where they think individually about the information that they've gathered. And every one of these blocks iteratively refines the representation at the residual stream.”
— Andrej Karpathy, OpenAI / online talk, 2022-11 · at 20:12
Andrej Karpathy: Andrej Karpathy on GPT-2 Reproduction: Rebuilding 124M From Scratch

Key takeaways

  • Use attention when tokens need to exchange information.
  • Use MLPs for per-token transformation after aggregation.
  • Think of the Transformer as repeated communication plus local processing.

Context

In the middle of his GPT-2 architecture walkthrough, Karpathy was explaining the role of each submodule in the block. The quote above is taken word for word from the recording's transcript.

In the transcript

…the residual stream again. And one more thing that is kind of interesting to note is that recall that attention is a communication operation. It is where all the tokens — and there's 1,24 tokens lined up in a sequence — and this is where the tokens communicate. This is where they exchange information. So attention is a aggregation function; it's a pooling function; it's a weighted sum function. It

So the attention is the reduce, and the MLP is the map. And what you end up with is that the Transformer just ends up just being a repeated application of map produce if you want to think about it that way. So this is where they communicate, and this is where they think individually about the information that they've gathered. And every one of these blocks iteratively refines the representation at the residual stream.

now move on to the MLP. So the MLP block I implemented as follows: it is relatively straightforward. We basically have two linear projections here that are sandwiched in between the G nonlinearity. So nn.G approximate is 10h. Now, when we swing over to the Pyro documentation, this is n.g, and it has this format, and it has two versions: the original version of G, which we'll step into in…

Read the full transcript of OpenAI / online talk, 2022-11

Topics

What others said about neural networks

More questions about Andrej Karpathy

All Andrej Karpathy answers · All Andrej Karpathy recordings

How this answer was made: the quote is checked word for word against the transcript of the recording and confirmed as the speaker's own words; the summary above it was drafted with AI from that transcript and quality-checked. Methodology and corrections.

Search every transcript
Open the full search page →