Transformer layers: attention is only half of what you need
Explore the feed-forward network (FFN or MLP), from activations to gated linear units (GLU) such as SwiGLU and GeGLU.
The tokens have shared their clues
In the attention article, “rabbit” gathered clues from the tokens around it. “Burrow” helped it mean an animal; “chocolate” helped it mean a sweet treat.
Attention is great at this: sharing information between tokens.
The tokens supply context. Training supplies the learned associations the model can apply to it.
But recognising an animal in a burrow is only a start. What can the model do with that information?
The sentence needn’t explain that rabbits are mammals, or that they eat plants. The model has encountered those associations during training. It needs a way to apply what it learned to the token in front of it.
Start with one learned table
We want to turn what “rabbit” means so far into a useful update. We’ve already met a way to let the model learn a conversion: multiply the vector by a matrix of weights.
One learned matrix turns the token vector into an update of the same width.
Each output coordinate can learn a different combination of the input. But there’s only so much this one table can do.
Make room for more
What if we gave it more weights to learn?
We can replace that single conversion with two matrices, with a wider vector between them. The first creates more combinations; the second turns them back into an update of the original width.
There is a catch. Two matrix multiplications alone could be folded into one. Remember why our first networks needed an activation function? We need an activation between the matrices so the extra stage can do something new.
Here, the activation keeps positive signals and sets negative ones to zero. Which combinations survive depends on the token coming in.
Now we have a small neural network inside the layer: a feed-forward network. Its learned weights can recognise patterns in the token’s information and turn them into an update.
Let the token control what passes through
So far, the activation follows the same fixed rule. Could the network also learn how strongly each signal should pass?
We can make a second path from the same token. It learns a gate for each coordinate of the wider vector.
SwiGLU
Two learned paths from the same token. Multiply their coordinates, then project back to the token width.
Multiply the two paths coordinate by coordinate. A gate near zero suppresses its signal; other gate values scale it before the final matrix produces the update.
The token supplies both paths, so the gates change with its meaning. This is a gated feed-forward network.
We can choose the activation on the gate path. With SiLU (also called Swish), the network is called SwiGLU. Choose GELU and it becomes GeGLU; ReLU gives ReGLU. The original GLU uses sigmoid.
Try a different activation in the diagram. The paths stay the same; the gate values and the resulting update change.
One network, every token
“Rabbit” isn’t getting a private set of weights. The same network works on every token, separately and in parallel.
SwiGLU
The same signal, gate and output weights act on every token column. CL is the context width; columns never mix in this network.
Different vectors produce different updates. The weights stay the same as the sentence passes through.
Attention lets positions exchange information. The feed-forward network gives each position more processing power to use it.
Add what we learned
We don’t need to throw away the token vector to make room for the update. We can add the update to it, just as we did with attention.
That gives us two stops along the residual stream: one addition after attention, then another after the feed-forward network.
Two branches, two updates. The residual stream carries both into the next layer.
The small normalise steps keep the scale of each branch’s input under control. They read a copy of the stream; the original continues to the addition.
Together, these two branches form a transformer layer.
Another layer, another chance
The next layer receives the updated token vectors. Its attention can share information the previous layer worked out; its feed-forward network can build on that information again.
Each layer has its own learned weights. The vectors keep their width as their meaning develops.
The residual stream runs through every layer. Each branch reads a normalised copy, then adds its update back into the stream at +. Attention and the feed-forward network have their own learned weights in each layer. Token width stays the same. Colours illustrate changing representations, not a trained model’s calculations.
The feed-forward size labels use the selected model’s dense intermediate width.
The network picture follows a dense feed-forward network with an expanding projection, ReLU, and a contracting projection. Its coloured cells follow the matrix calculations. The rabbit associations explain the role of learned knowledge, rather than identifying individual coordinates in a trained model.
The complete layer uses sequential attention and feed-forward branches with pre-normalisation. The gated diagram starts with SiLU; its selector also offers GELU, ReLU and sigmoid. Expert networks and different normalisation placements are explored in their own articles. Knowledge is distributed across the model’s learned parameters, including attention weights.