In the paper that introduced the Transformer, attention gets most of the architecture section, a figure of its own and a whole further section justifying its cost. The perceptron that follows it in every block gets a single paragraph: five sentences and an equation. Almost everything written since, courses included, has inherited that proportion, and it is the opposite of what the parameters say. In GPT-3, two out of every three parameters in each block belong to that perceptron; in Mixtral, a 2024 model that has eight copies of it per layer, the figure is %.
This article asks what the perceptron is for, and the question is not rhetorical. Attention already combines the positions of the text using weights that depend on the content; a box that processes each position separately, with the same weights for all of them, looks like the least Transformer-like part of the Transformer. The answer has four parts: what attention cannot compute, what the perceptron stores that attention has nowhere to keep, why it has this shape and not another, and what has been tried in its place. First, though, a red herring needs clearing up. The paper with the most famous title on the subject, Attention is not all you need, proves something true and important, but the perceptron is not the hero of its proof.
The block, in the usual notation
A text of tokens enters each block as a matrix , with one row per position. The block has two sublayers, each wrapped in a sum and a normalisation, . Whether the normalisation comes before or after the sublayer (current models normalise the input) makes no difference to what follows. The first sublayer is multi-head attention,
and the second is what the original paper calls the FFN (feed-forward network, a network with no recurrence), which I will call the position-wise perceptron: a two-layer perceptron with a ReLU (rectified linear unit, applied coordinate by coordinate) in between,
with , and a column of ones that repeats each bias once per row. In the original paper, and : the hidden layer is four times as wide as the model.
Two properties follow from the formula, and they define the piece. First, and are the same for every row, so row of the output depends only on row of the input. The paper puts it in two sentences: the perceptron is applied to each position "separately and identically", and it is equivalent to two convolutions with a kernel of size . Second, each block has its own matrices: six blocks, six different perceptrons.
So the block splits the work into two halves that don't overlap. Attention is the only piece that moves information from one row to another; the perceptron transforms each row without being able to look at the others.
The two sublayers of a block. Attention mixes rows; the perceptron mixes the coordinates of each row. Neither does the other's job.
The red herring in the famous title
Attention is not all you need, by Dong, Cordonnier and Loukas (2021), studies what happens when you stack attention layers and nothing else: no sum around them, no perceptron and no normalisation. The result is easiest to grasp by looking at what a head does to a row. The weights that come out of the softmax are positive and sum to in each row, so row of the output is a weighted average of the rows of . Stacking layers means taking averages of averages, and averages of averages tend to look alike.
To measure how much, define the residual of as how far it is from having all its rows equal: , where is the vector that makes that difference as small as possible. A matrix whose rows are all equal has rank , and a small residual means that every position is saying almost the same thing. The central theorem bounds the residual after layers of pure attention. Gathering its constants (they depend on the norms of the weight matrices, the number of heads and ) into a single , it reads
The exponent is : in each layer the residual is, at worst, cubed. With and an initial residual of , the bound is after one layer, after two and after three. That is why the authors call the convergence doubly exponential. The cube has an intuitive reading: similar rows produce similar scores, similar scores give nearly uniform weights, and nearly uniform weights average things out even more. The similarity feeds on itself.
What matters for this article is the next section, where the paper puts back the pieces it had removed. Its three subheadings sum it up: the residual connection is crucial, the perceptron helps, and normalisation does nothing.
- The residual connection (skip connection, the sum ) breaks the bound completely. With it, among the paths from input to output there is always one that goes through no attention at all, and the authors show that there are infinitely many choices of weights for which the residual never shrinks, however many layers are stacked.
- The perceptron enters the bound as one more factor in the base: its Lipschitz constant, which is the most the function can stretch a distance. The more it stretches, the more slowly everything converges, but the convergence is still doubly exponential. The authors describe it as a tug of war and put a price on it: stretching a lot makes the model more sensitive to small perturbations of the input. In their experiments on BERT, ALBERT and XLNet with the residual connections removed, the perceptron does not seem to slow the collapse, although they warn that these models were not retrained without them.
- Normalisation plays no part: centring and scaling the rows can be written as multiplying the matrix and adding the same vector to every row. Multiplying cannot raise the rank, and adding that vector does not change the residual.
The title is about attention, and the conclusion is about the sum. The perceptron is a supporting actor in the proof, and rank collapse is not why it is there: the residual connection prevents it on its own. There is further evidence that, with the sum and without a perceptron, a Transformer can already do real work. A Mathematical Framework for Transformer Circuits, from Anthropic (2021), deliberately studies attention-only models, and with two layers induction heads appear: pairs of heads that find an earlier occurrence of the current token in the context and copy the token that came after it, a general way of learning from the text itself. The authors leave the perceptron out because, as they write, they have had much less success understanding it. That is an honest way of saying it is the hard part.
So the question still stands. If the sum prevents the collapse and attention alone can already copy from the context, what is missing?
What attention cannot compute
One specific operation is missing, and you can see which one by opening up the . Call the weight head gives to position when computing row , and the rows of that multiply what that head returns. Row of is
where is row of . All of the softmax lives inside the , and what the decide is where to read from. What happens to what has been read is another matter: a weighted average of rows, each multiplied by two matrices. Once the weights are fixed, row is a linear function of the rows of . The residual sum is linear too. Normalisation isn't, but its parameters, and , work coordinate by coordinate: none of them looks at two coordinates at once.
A small example shows why that is a problem. Take four phrases, "it's good", "it's bad", "it's not good" and "it's not bad", and assume attention has done its job well: it has carried to the row of "good" or "bad" the news of whether there is a "not" in front of it. That row now holds two coordinates that matter: if there is a negation and if not, and for "good" and for "bad". The sentiment of the phrase is their product, : "not" has no sign of its own, it flips the sign of the word that follows.
No affine function of gives that product at all four corners. If were equal to , subtracting the two phrases without a negation would give , and subtracting the two with one would give . This is XOR (exclusive or), the classic example for showing that a single neuron is not enough, and it sits entirely inside one row. Composing linear functions gives another linear function, so stacking more attention does not fix it: the two pieces of data are sitting together, and attention has no component built to combine them.
The perceptron does. With two hidden units it solves the problem exactly, because for
In terms of the block's formula, that is two columns of reading and , two rows of subtracting what comes out of the ReLU, and a bias that supplies the . The ReLU lets the output depend on whether a combination of coordinates is positive, which is exactly what no linear function can do. This site's deep learning course devotes a section of the lesson on the Transformer block to this, using the XOR square from the lesson on hidden layers.
There is a concession to make. I have reasoned with the weights already fixed, and they don't come fixed: the are computed from , so attention as a whole is non-linear in its input, and a trained network can exploit that. But that non-linearity exists to choose where to read from, and using it to combine the coordinates of a single row means asking one component to do another's job. Theory divides the roles the same way. The proof that Transformers are universal approximators of sequence-to-sequence functions (Yun et al., 2020) uses attention for one thing only, letting each position know what context it is in, and leaves everything else to the perceptron: discretising the input at the start and, at the end, turning each combination of row and context into the right output value. The proof's sections are titled accordingly: quantisation by feed-forward layers, contextual mapping by self-attention, function value mapping by feed-forward layers.
Attention gathers; the perceptron computes.
Where the parameters and the operations are
That the perceptron is the piece doing the computing shows up in the budget. Per block, attention has four matrices (the query, key and value matrices of all the heads together, plus ) and the perceptron has two of size :
That is two thirds of the block, not counting biases or normalisations, which are just vectors. GPT-3 keeps at every size, and in the largest ( blocks with ) that comes to million perceptron parameters per block against million for attention. Current models change the matrices but not the proportion: LLaMA uses three matrices instead of two and shrinks so that they add up to the same total (the section on alternatives comes back to this), and its later versions share keys and values across groups of heads (GQA, grouped-query attention), which slims down the attention and fattens the perceptron's share.
Then there is Mixtral, which has eight perceptrons per block. With , , three matrices per perceptron and eight key and value heads of coordinates each, the sums for one block are
That is % of the block. Multiply by the blocks, add the two embedding matrices ( words by coordinates, one at the input and one at the output), and you get about million parameters, the billion that Mistral AI publishes. Each token uses only two of the eight perceptrons, and even so the perceptron accounts for % of the parameters each token uses in each block.
The operations tell a similar story, with a twist that depends on the length of the text. For the token at position , counting each multiply followed by an add as two operations, a layer with does
The middle term is the only one that grows with : it comes from comparing the query with keys and adding up values. Dividing, the perceptron's share is : two thirds for a short text, and exactly half when .
The share of a layer's operations done by the perceptron, for the token at position , according to the sum above. The quadratic cost of attention, the thing people mention most about it, only overtakes the perceptron beyond twice the width of the model.
For GPT-3 that crossover is at tokens, twelve times the -token context it was trained with. Throughout its training, the perceptron's share of each layer never fell below %. The figure needs careful reading: these are operations, not time. A model generating text is usually limited by how long it takes to read the weights and the key-value cache from memory, not by how long it takes to multiply, and that is a different budget.
What matters about this budget is what it leads to. Whenever people have wanted to give a model more capacity without paying for it on every token, what they have multiplied is the perceptron, and that story is in the second-to-last section.
A memory with its keys written into the weights
To see what those parameters store, it helps to rewrite the perceptron column by column. Call column of and row of . For a row ,
Written like this, it has the same shape as an attention head: compare the input with some keys, turn the comparisons into weights, and add up some values using those weights. There are three differences, and all three matter. In attention the keys and values come from the text; in the perceptron they are stored in the parameters, the same for any text. Attention has keys, one per position, while the perceptron has . And the softmax shares one unit of weight among all the keys, whereas the ReLU normalises nothing and leaves many of them at exactly zero.
The same sum of keys, weights and values. Attention looks up what is in the text; the perceptron looks up what it learnt during training.
The equivalence has been taken literally. In Augmenting Self-attention with Persistent Memory (Sukhbaatar et al., 2019), swapping the ReLU for a softmax and removing the biases turns the perceptron into attention over fixed vectors. The authors then take the next step: they remove the perceptron and add those vectors, which they call persistent memory, to the keys and values of every head, alongside the ones from the text. With as many vectors as the ReLU had units, the parameter count is the same. On enwik8, a benchmark that measures, in bits per character, how well a model compresses the first hundred million bytes of English Wikipedia, their -million-parameter model matches, at , the best model of the time, which had million. And the most telling ablation is in the same table: with zero persistent vectors, that is, with attention and nothing acting as a perceptron, the model performs poorly. The piece can be disguised as attention; what cannot be done is to remove what it stores.
Researchers have probed what it stores in several ways. Transformer Feed-Forward Layers Are Key-Value Memories (Geva et al., 2021) takes a -layer language model trained on WikiText-103, a corpus of English Wikipedia articles, with and . For a sample of keys, it finds the pieces of text that switch each one on most strongly and asks people to describe what they have in common. Every key has at least one recognisable pattern, on average. In layers to shallow patterns dominate, such as ending in a particular word; in layers to , semantic ones, such as being about a particular topic. For the values, they project each onto the vocabulary and look at which word it pushes: in the lower layers it almost never matches the word that follows the text that switches on its key, and from layer on it matches in up to % of cases, against % by chance. No memory works alone: a typical example switches on between and % of the , and what the whole layer predicts differs from what any single one of them predicts in at least two out of three examples.
The follow-up (Geva et al., 2022) reads each term as a small update that raises the probability of a group of words, often a recognisable concept. And it puts that to use: switching on ten value vectors of GPT-2 by hand, % of the total, lowers the share of toxic continuations from % to %, at the cost of raising the perplexity (the exponential of the mean loss per token; lower is better) from to .
Two other papers go looking for specific facts. Knowledge Neurons (Dai et al., 2022) uses a gradient-based attribution method to locate, in BERT, the hidden units of the perceptron that most influence whether the model completes a fact correctly, and most of them are in the top layers. Switching them off lowers the probability of the correct answer by % on average, and doubling their activation raises it by %. ROME (Rank-One Model Editing, Meng et al., 2022) goes further, in GPT-2 XL and GPT-J. First it corrupts the input of sentences such as "the Eiffel Tower is in" and restores the internal states one at a time to see which ones bring back the right answer: the decisive ones are the perceptrons of the middle layers, at the last token of the subject, with an effect about four times as strong as that of attention at the same token. Then it edits one of those perceptrons, treating its second matrix as a linear associative memory, with a rank-one update: a single outer product, and the model comes to believe something else about that subject, consistently, even when the question is phrased differently.
All of this comes with a caveat that rules out reading it as a table of facts with one row per neuron. Networks represent more features than they have units by overlapping them, a phenomenon known as superposition, which is why a single unit fires for things that have nothing to do with each other. That each of Geva's keys responds to several patterns is the same observation seen from the inside. The memory is real, but it is spread out.
Why it has this shape
With both readings in hand, the perceptron as the piece that computes and as a memory, we can review the design decisions one by one. Some have a reason behind them; others are just habit.
One hidden layer. It is the minimum that breaks linearity, and in theory one is enough: a perceptron with a sufficiently wide hidden layer can approximate any continuous function on a bounded domain. The XOR above needed one, not two. None of the models cited in this article puts more than one hidden layer in the perceptron; the improvements that stuck came from changing its width, its activation or how many there are, not its depth.
Wider than the model. In the memory reading, is the number of slots; in the computing reading, it is the number of new features that can be built from each row, and in more dimensions it is easier for whatever needs separating to end up separable by a plane. Both readings call for widening. Neither calls for a factor of four: is simply the number in the original paper, which doesn't justify it, and GPT-3 kept it at every size. The scaling laws study by Kaplan et al. (2020) measured how much it matters, and the answer is not much: with the number of parameters fixed, the loss depends only very weakly on , on the number of layers and on the number of heads, and varies by only a few per cent across a wide range of shapes. A model with layers and comes within % of one with layers and . The four is a convention that works, not an optimum.
Back to . This one is not a choice. The sum is element by element, so what comes out must have the same width as what went in. The perceptron widens to to do its work and narrows again to hand the result back.
Position-wise. Using the same matrices for every row does two things. It makes the number of parameters independent of , so the same model can read a sentence or a book. And it keeps the perceptron from competing with attention: if it could look at the other rows it would also have to learn to mix them, and the division of labour from the first section would disappear. The next section shows that this independence also has a very large practical consequence, because it is what allows each token to be routed on its own.
What has been tried in its place
Another non-linearity, and a gate
The first change was the activation. GPT and BERT replaced the ReLU with the GELU (Gaussian error linear unit, 2016), which multiplies the input by the probability that a standard normal variable falls below it, : a smoothed ReLU, differentiable at zero and with a small negative dip.
The change that stuck came in 2020. In GLU Variants Improve Transformer, Noam Shazeer (one of the Transformer's authors) tries perceptrons with a gate: two projections of the input instead of one, with one of them passed through an activation, and the two multiplied element by element before the second layer,
with , the sigmoid and the element-wise product. The variants change the gate's activation: none (bilinear), a sigmoid (GLU, for gated linear unit), a ReLU (ReGLU), a GELU (GEGLU) or a Swish (SwiGLU). There are three matrices instead of two, so to avoid adding parameters Shazeer shrinks by a factor of , from to in his model. These are the mean losses (the logarithm of the perplexity) that he measures on T5-base at the end of pre-training:
| perceptron | ReLU | GELU | Swish | GLU | bilinear | ReGLU | SwiGLU | GEGLU |
|---|---|---|---|---|---|---|---|---|
| loss |
Changing the activation without a gate moves nothing, and every gate improves things. The paper's conclusion is famous because it explains nothing: it puts the variants' success down, "as all else, to divine benevolence".
I will venture a reading the paper does not make, and it comes from the example of the four phrases. The sentiment was a product, . The ReLU computed it with two units and a trick; a bilinear gate computes it with a single one, because it multiplies two projections of the row, and one can read while the other reads . What the gate adds is a direct way for one coordinate to modulate another, which is exactly the operation attention lacks. That the bilinear variant, with no activation of any kind, beats the ReLU fits that reading. It does not prove it.
Practice did not wait for the explanation. PaLM uses SwiGLU with , paying for the extra parameters, and LLaMA uses it with , Shazeer's adjustment. A good share of the open models that followed take the LLaMA route.
Many perceptrons, and one per token
A mixture of experts (MoE) puts several perceptrons in each layer, the experts, together with a small network, the router, that decides which ones each token uses. The parameters grow with the number of experts, but the computation per token grows only with the number actually used. Its history in language modelling is largely a list of where in the block it has been placed.
- Shazeer et al. (2017) place it between two layers of an LSTM (long short-term memory) recurrent network, with up to billion parameters.
- GShard (2020) replaces every other perceptron of a Transformer with experts, two experts per token, and reaches billion.
- Switch Transformer (2021) replaces the perceptron and keeps a single expert per token; in pre-training it is up to seven times faster than the dense model with the same computation.
- Mixtral (2024) swaps every perceptron for eight experts, two per token. DeepSeekMoE (2024) splits them into many small experts and reserves a few shared ones that every token goes through; its 16-billion-parameter model matches LLaMA 2 7B with % of its computation.
In all of them, what gets multiplied is the perceptron. There is a structural reason for this, beyond the parameter count: since the perceptron works on each row separately, each token can go to a different expert without anything else changing. Attention does not lend itself to that split as easily, because its job is to mix rows.
A real memory
If the perceptron is a memory, you can try building one on purpose. Large Memory Layers with Product Keys (Lample et al., 2019) replaces the perceptron of some layers with a table of , about key-value slots, from which each token reads only the ones most similar to it, and it organises the keys as the product of two small sets so that finding them is cheap. A -layer model with memory beats a -layer one without, and is twice as fast at inference. Memory Layers at Scale (Berges et al., 2024) takes the idea up to billion memory parameters and a trillion training tokens. It beats dense models that use more than twice the computation, as well as mixtures of experts matched on both computation and parameters, and the gains are largest where the memory reading says they should be: on factual questions. In their -billion-parameter model, accuracy on Natural Questions, a benchmark of real questions put to Google's search engine, goes from % to %.
Removing it, sharing it or moving it
Removing it with nothing in its place has already come up twice: the ablation without persistent memory performs poorly, and attention-only models are studied because they are easier to understand, not because they are better. The other two tests say more.
Sharing it: One Wide Feedforward is All You Need (Pires et al., 2023) tests what happens with fewer perceptrons in English-to-German translation. Sharing a single one across the six encoder layers and dropping it from the decoder leaves the model with % of the parameters and makes it % faster at translating, at a cost of BLEU points (bilingual evaluation understudy, the metric that counts the fragments in common with a reference translation), from to . If that shared perceptron is widened until the original parameter count is restored, the score rises to : better than the starting model. Perceptrons in different layers repeat each other a good deal, at least in translation; what doesn't work is going without any.
Moving it: the sandwich Transformer (Press et al., 2020) tries reordering the sublayers without changing how many there are, and the best orderings put more attention at the bottom and more perceptron at the top. On WikiText-103 the perplexity drops from to at no cost in parameters or time, although it makes no difference in translation. PaLM takes another route and computes the two sublayers in parallel on the same normalised input, , which trains % faster at large scale because the first multiplications of the two can be fused. It loses a little quality at billion parameters and none at .
The experiment in reverse
The most striking test does not touch the perceptron at all, but the other half. MetaFormer (Yu et al., 2022) takes a vision Transformer and replaces the attention with the simplest thing the authors could think of: averaging each patch of the image with its neighbours (pooling), with not a single parameter. It keeps the sum, the normalisation and the perceptron. The result, PoolFormer, reaches % on ImageNet, the classic image classification benchmark, points more than DeiT-B, a reference vision Transformer, with % fewer parameters. A few months earlier, MLP-Mixer had replaced the attention with another perceptron that mixes positions, and its authors concluded that neither convolutions nor attention are necessary.
This needs careful reading: it is vision, not language. In an image the neighbours say almost everything about a patch, whereas in a text the word that matters may be two pages away, which an average over neighbours cannot fetch and attention can. But the experiment shows what has been kept in every variant in this article, in vision and in language: the way positions are mixed has changed a great deal, and the perceptron that transforms each position has never been missing.
What remains
The Transformer has two halves because there are two jobs. One is deciding where to read from, which depends on the text and is what attention does. The other is doing something with what has been read: combining the coordinates of a single row in ways no weighted average can reach, as in the XOR of the four phrases, and bringing in what the model learnt that is not in the text. That is the perceptron, an attention whose keys and values are written into the weights. The residual connection, usually relegated to third place in explanations, is what keeps the stack from collapsing.
Almost everything about its shape is convention: the factor of four, the ReLU, having one per block. The variants that have survived have changed everything except one thing. They have given it gates, multiplied it into experts, turned it into a table of millions of slots, shared it and moved it around, and in all of them there is still, in every layer, a non-linear computation that works on each position separately.
So the next time a Transformer diagram draws the attention big and a little box labelled FFN on top, it is worth remembering the real budget. Attention gave the architecture its name. The perceptron holds two thirds of the parameters and most of the operations, and it is where the model keeps what it knows.