Rúben GalvãoThe Rabbit Hole

The Rabbit Hole / AI and machine learning / Language models / Inside a language model

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.

Attention brings the clues to rabbitburrowEarlier context is dimmed; outlined columns are current.oldEarlier context is dimmed; outlined columns are current.rabbitEarlier context is dimmed; outlined columns are current.Current token computation.Attention shares the cluesEarlier context is dimmed; outlined columns are current.Current token computation.“rabbit”, with context
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 square weight matrix produces a token updateToken vectorEarlier context is dimmed; outlined columns are current.Current token computation.Learned weightsUpdateEarlier context is dimmed; outlined columns are current.Current token computation.7,1687,1687,1687,168
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.

A learned network transforms the token vectorToken vectorEarlier context is dimmed; outlined columns are current.Current token computation.Learned weightsActivationMore roomLearned weightsUpdateEarlier context is dimmed; outlined columns are current.Current token computation.Back to the token's width7,1687,1687,16818,43218,43218,4327,168
Blue/orange: calculated data. Violet: learned weights.

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

A learned gate controls the feed-forward signalsThe same tokenEarlier context is dimmed; outlined columns are current.Current token computation.Signal weightsGate weightsEarlier context is dimmed; outlined columns are current.Current token computation.Earlier context is dimmed; outlined columns are current.Current token computation.−44Activation function×Coordinate by coordinateEarlier context is dimmed; outlined columns are current.Coordinate 1: 0.035Coordinate 2: -0.001Coordinate 3: 0.013Coordinate 4: 0.008Coordinate 5: 0.052Coordinate 6: -0.000Coordinate 7: -0.033Coordinate 8: -0.017Coordinate 9: 0.002Coordinate 10: 0.001Coordinate 11: 0.032Coordinate 12: 0.009Coordinate 13: 0.044Coordinate 14: 0.008Coordinate 15: -0.000Coordinate 16: -0.021Coordinate 17: -0.017Coordinate 18: 0.010Coordinate 19: 0.065Coordinate 20: -0.005Coordinate 21: 0.002Coordinate 22: 0.089Coordinate 23: 0.054Coordinate 24: -0.050Coordinate 25: 0.067Coordinate 26: -0.002Coordinate 27: -0.222Coordinate 28: -0.008Coordinate 29: -0.007Coordinate 30: -0.071Current token computation.Output weightsEarlier context is dimmed; outlined columns are current.Coordinate 1: 0.098Coordinate 2: -0.015Coordinate 3: 0.090Coordinate 4: -0.027Coordinate 5: -0.010Coordinate 6: 0.027Coordinate 7: 0.021Coordinate 8: 0.029Coordinate 9: -0.052Coordinate 10: 0.021Coordinate 11: -0.004Coordinate 12: 0.048Coordinate 13: 0.015Coordinate 14: -0.024Coordinate 15: -0.112Coordinate 16: 0.010Coordinate 17: 0.062Coordinate 18: -0.036Current token computation.Update7,1687,16818,4327,16818,43218,43218,43218,43218,4327,1687,168
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

A learned gate controls the feed-forward signalsEvery token in the contextEarlier context is dimmed; outlined columns are current.Current token computation.Signal weightsGate weightsEarlier context is dimmed; outlined columns are current.Current token computation.Earlier context is dimmed; outlined columns are current.Current token computation.−44Activation function×Coordinate by coordinateEarlier context is dimmed; outlined columns are current.Coordinate 1: 0.095Coordinate 2: 0.090Coordinate 3: 0.005Coordinate 4: -0.078Coordinate 5: -0.006Coordinate 6: -0.012Coordinate 7: -0.001Coordinate 8: -0.021Coordinate 9: 0.044Coordinate 10: 0.116Coordinate 11: -0.045Coordinate 12: -0.031Coordinate 13: 0.001Coordinate 14: 0.003Coordinate 15: -0.080Coordinate 16: 0.034Coordinate 17: -0.049Coordinate 18: -0.197Coordinate 19: 0.001Coordinate 20: 0.046Coordinate 21: -0.043Coordinate 22: 0.010Coordinate 23: 0.009Coordinate 24: 0.013Coordinate 25: 0.049Coordinate 26: -0.061Coordinate 27: -0.007Coordinate 28: -0.150Coordinate 29: 0.124Coordinate 30: 0.162Coordinate 1: 0.037Coordinate 2: -0.027Coordinate 3: 0.001Coordinate 4: -0.000Coordinate 5: -0.010Coordinate 6: 0.008Coordinate 7: 0.047Coordinate 8: -0.007Coordinate 9: 0.012Coordinate 10: 0.055Coordinate 11: 0.016Coordinate 12: -0.076Coordinate 13: -0.001Coordinate 14: 0.075Coordinate 15: 0.064Coordinate 16: 0.002Coordinate 17: 0.074Coordinate 18: 0.001Coordinate 19: 0.125Coordinate 20: 0.040Coordinate 21: 0.016Coordinate 22: 0.015Coordinate 23: 0.021Coordinate 24: 0.011Coordinate 25: 0.112Coordinate 26: -0.057Coordinate 27: -0.018Coordinate 28: -0.084Coordinate 29: 0.027Coordinate 30: -0.082Coordinate 1: 0.035Coordinate 2: -0.001Coordinate 3: 0.013Coordinate 4: 0.008Coordinate 5: 0.052Coordinate 6: -0.000Coordinate 7: -0.033Coordinate 8: -0.017Coordinate 9: 0.002Coordinate 10: 0.001Coordinate 11: 0.032Coordinate 12: 0.009Coordinate 13: 0.044Coordinate 14: 0.008Coordinate 15: -0.000Coordinate 16: -0.021Coordinate 17: -0.017Coordinate 18: 0.010Coordinate 19: 0.065Coordinate 20: -0.005Coordinate 21: 0.002Coordinate 22: 0.089Coordinate 23: 0.054Coordinate 24: -0.050Coordinate 25: 0.067Coordinate 26: -0.002Coordinate 27: -0.222Coordinate 28: -0.008Coordinate 29: -0.007Coordinate 30: -0.071Coordinate 1: 0.067Coordinate 2: 0.010Coordinate 3: -0.023Coordinate 4: 0.070Coordinate 5: -0.028Coordinate 6: -0.089Coordinate 7: -0.015Coordinate 8: -0.192Coordinate 9: 0.005Coordinate 10: 0.088Coordinate 11: -0.013Coordinate 12: -0.065Coordinate 13: -0.146Coordinate 14: -0.022Coordinate 15: 0.066Coordinate 16: 0.062Coordinate 17: -0.101Coordinate 18: -0.022Coordinate 19: 0.046Coordinate 20: -0.046Coordinate 21: 0.103Coordinate 22: -0.008Coordinate 23: 0.044Coordinate 24: 0.120Coordinate 25: -0.002Coordinate 26: -0.009Coordinate 27: -0.030Coordinate 28: -0.025Coordinate 29: -0.001Coordinate 30: -0.040Current token computation.Output weightsEarlier context is dimmed; outlined columns are current.Coordinate 1: -0.087Coordinate 2: 0.162Coordinate 3: 0.093Coordinate 4: 0.135Coordinate 5: -0.098Coordinate 6: 0.016Coordinate 7: 0.075Coordinate 8: -0.071Coordinate 9: 0.114Coordinate 10: -0.031Coordinate 11: -0.001Coordinate 12: 0.065Coordinate 13: 0.063Coordinate 14: -0.105Coordinate 15: -0.031Coordinate 16: -0.055Coordinate 17: 0.027Coordinate 18: -0.042Coordinate 1: -0.057Coordinate 2: 0.082Coordinate 3: -0.050Coordinate 4: -0.008Coordinate 5: -0.002Coordinate 6: 0.001Coordinate 7: -0.108Coordinate 8: -0.018Coordinate 9: 0.021Coordinate 10: 0.040Coordinate 11: 0.052Coordinate 12: 0.012Coordinate 13: -0.004Coordinate 14: -0.021Coordinate 15: 0.040Coordinate 16: -0.008Coordinate 17: 0.071Coordinate 18: -0.057Coordinate 1: 0.098Coordinate 2: -0.015Coordinate 3: 0.090Coordinate 4: -0.027Coordinate 5: -0.010Coordinate 6: 0.027Coordinate 7: 0.021Coordinate 8: 0.029Coordinate 9: -0.052Coordinate 10: 0.021Coordinate 11: -0.004Coordinate 12: 0.048Coordinate 13: 0.015Coordinate 14: -0.024Coordinate 15: -0.112Coordinate 16: 0.010Coordinate 17: 0.062Coordinate 18: -0.036Coordinate 1: 0.122Coordinate 2: 0.042Coordinate 3: 0.010Coordinate 4: -0.035Coordinate 5: -0.041Coordinate 6: 0.035Coordinate 7: -0.034Coordinate 8: 0.051Coordinate 9: 0.058Coordinate 10: -0.010Coordinate 11: 0.104Coordinate 12: 0.010Coordinate 13: -0.017Coordinate 14: 0.031Coordinate 15: 0.001Coordinate 16: 0.013Coordinate 17: 0.018Coordinate 18: 0.088Current token computation.An update for every tokenCLCLCLCLCL7,1687,16818,4327,16818,43218,43218,43218,43218,4327,1687,168
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.

Attention and a feed-forward network update the residual streamResidual streamToken representationsNormaliseAttentionShare between tokens+Add the updateNormaliseFeed-forward networkProcess each token+Add the updateInto the next layer7,168
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.

Six transformer layers along one residual streamA continuous residual stream carries the token vectors through six layers. In every layer, a normalised copy goes through attention and is added back; a second normalised copy goes through a feed-forward network and is added back. The resulting vectors continue into the next layer.Token vectorsEarlier context is dimmed; outlined columns are current.Current token computation.Layer 1 · its own learned weightsLayer 2 · its own learned weightsLayer 3 · its own learned weightsLayer 4 · its own learned weightsLayer 5 · its own learned weightsLayer 6 · its own learned weightsResidual streamNormaliseAttention+NormaliseFeed-forward+NormaliseAttention+NormaliseFeed-forward+NormaliseAttention+NormaliseFeed-forward+NormaliseAttention+NormaliseFeed-forward+NormaliseAttention+NormaliseFeed-forward+NormaliseAttention+NormaliseFeed-forward+Earlier context is dimmed; outlined columns are current.Current token computation.Updated vectors · same width
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.

That’s the repeating unit we’ll follow through a whole LLM, layer by layer.

Sources and assumptions

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.

← Browse all subjectsBack to the beginning ↑