Transformers from the ground up
The feed-forward block
Between attention layers sits a two-layer network applied to each token separately. It holds most of the parameters and does much of the per-token computation.
Position-wise means independent
The feed-forward block uses the same weights at every position. It never looks at another position. Token one and token ten go through the same function separately. That is what “position-wise” means. It is the complement of attention. Attention mixes tokens with each other. The feed-forward block transforms what each token now holds.
The positions are independent. The block is a pair of matrix products over a batch of vectors. Its cost has no term for sequence length. It is therefore cheap next to attention on long inputs. It is fairly expensive on short ones. The two layers are usually the largest source of weights in the model.
The block is applied after attention has mixed the sequence. Each token therefore arrives carrying context from the positions it attended to. The feed-forward network transforms that context without looking sideways. Stacking the two operations lets a model alternate between routing information and working on it. A layer with attention alone can move information around. It can do far less to change what it moved.
Expand, activate, contract
The first matrix projects the model width up to a wider hidden layer. The hidden layer is typically four times as wide. An activation, a simple non-linear rule, is applied element by element. The second matrix projects back to the model width. The output has the same shape as the input. It can therefore be added to the residual stream without any change.
The wider layer is where the model gets room to work. It can represent many more mixes of features than the input width allows. The activation decides which of them fire for this token. The second matrix then sums those values into a single update vector.
The block as two linears
from torch import nn
block = nn.Sequential(
nn.Linear(512, 2048),
nn.GELU(),
nn.Linear(2048, 512),
)
x = block(x) # (batch, tokens, 512) -> (batch, tokens, 512)A feed-forward pass
Run the block on a four-dimensional token, then set the hidden width to four and compare the parameter count and the output.
What the block computes
A useful picture is a set of key-value memories. The first matrix compares the token’s vector with many learned directions. The activation decides which directions fire. The second matrix writes the matching output patterns back into the residual stream. The model is not storing facts in a clean database. The analogy still explains why the hidden layer is wide.
Single neurons often respond to features a person can name: a syntax pattern, a language, a topic. They also respond to mixtures, because the model packs far more features than it has neurons. That overlap is called superposition. It explains why removing one neuron can have a small and confusing effect. It does not remove one clean concept.
The width of the hidden layer is a choice about capacity. A narrow layer forces many features through few units. That increases overlap and interference. A wide layer gives each feature more room, at the cost of memory and work. The four-times rule is a starting point. It balances those costs for the model sizes where it was popular. It is not a theoretical optimum.
The four-times expansion is a convention, not a law. A wider hidden layer costs memory in the weights and in the activations; a narrower one saves both and changes what the model can represent. Count the parameters before you copy the default.
GELU and the gated variants
ReLU is the simplest activation. It keeps the positive part and sets the rest to zero. GELU multiplies the input by the chance that a standard normal is below it. That gives a smooth curve and a small negative dip near zero. The smoothness helps training. The negative region lets a unit suppress a signal, not only ignore it.
Modern models often use a gated form. SwiGLU computes two projections. It applies SiLU to one and multiplies it by the other before the output projection. The gate lets the network modulate a value, not only scale it. The cost is a third matrix. The hidden width is usually reduced to keep the weight count similar.
The activation is also a place where serving speed-ups land. ReLU is cheap and sparse. GELU is smoother but more expensive. Gated variants add a matrix but often improve quality per weight. A kernel can fuse the activation into the matrix product. The cost in a real model therefore depends on the code as well as the maths.
Three activations compared
Run the table, then count how many GELU values are negative and explain why a negative activation can still be useful.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
What does position-wise mean for the feed-forward block?
