The story: the hidden knowledge of FFNs
Everyone talks about the Transformer's attention. It's the star mechanism, the one that makes headlines. But here's a secret that few people suspect: FFNs probably contain more knowledge than attention itself. FFN stands for Feed-Forward Network: an ordinary little neural network in which information only ever travels from input to output, with no loop and no feedback.
To understand, let's use a city analogy. Attention is like the roads between cities: they enable communication, transporting information from one point to another. FFNs are like the factories and warehouses in the cities: that's where resources are stored and transformed.
Without roads, cities don't communicate. Without factories, they have nothing to offer. Both are indispensable. And yet, in courses and articles, people talk almost exclusively about the roads (attention) while forgetting the factories (FFNs).
FFNs: the small networks that store the model's memory
Structure: simple but surprisingly powerful
After each attention block, each token passes independently through a small two-layer neural network. That's the FFN (Feed-Forward Network). Each token is processed in isolation, without interaction with the other tokens.
Step 1: project the token into a higher-dimensional space. If the token has size 512, we project it to 2048 (typically a 4x factor):
Symbol by symbol: is the first table of learned coefficients (512 rows to 2048 columns here), a small vector of learned offsets that gets added on (a bias, which keeps the layer from being forced through zero), and ReLU (Rectified Linear Unit) the function that replaces every negative value with zero and leaves positive ones untouched. In one sentence: spread the token out into a wider space, then switch off whatever comes out negative.
It's like opening a fan: the token is "unfolded" into a wider space, where more details become visible and manipulable.
Step 2: project back to the original size (2048 becomes 512 again):
Here is the second learned matrix, making the return trip (2048 back to 512), and its vector of offsets. No activation function this time: we simply recompress.
The fan closes: we recondense the information, keeping what's relevant.
This is applied to each token independently. Unlike attention, which puts tokens in relation with each other, the FFN only performs a local transformation on each token. Simple? Yes. But mysteriously powerful.
Researchers discovered something fascinating: FFNs function as a hidden associative memory. The rows of are "keys" and the columns of are "values". The mechanism works as follows: when a token strongly activates a row of (the key matches the observed pattern), the corresponding column of produces the associated response.
Concrete example: when the token represents "capital of France", a row of fires strongly (it recognizes this pattern), and the corresponding column of produces a vector close to "Paris". It's in the FFNs that the world's facts live, not in attention. Attention connects words to each other; FFNs inject knowledge.
The evolution of activation functions
The activation function between the two FFN layers has evolved considerably:
| Year | Activation | How it works |
|---|---|---|
| 2017 | ReLU | The simplest: max(0, x). If the input is positive, it passes. Otherwise, it's blocked at zero. |
| 2020, widely adopted from 2022 | SwiGLU | Two parallel branches: one that transforms, one that serves as a "gate" to filter. |
| Alternative | GeGLU | Same gating idea, but built on GELU (Gaussian Error Linear Unit) instead of Swish: a softer cutoff than ReLU, letting slightly negative values through a little rather than zeroing them outright. |
SwiGLU has become the de facto standard (LLaMA, Mistral, and essentially all recent models). Its operation is more sophisticated than ReLU:
- Branch 1 (): transforms the token into the expanded space
- Branch 2 (): computes a "relevance" score for each dimension, serving as a gate
The two branches are multiplied element-wise. Result: the model learns not only to transform information, but also to finely select which parts of the transformation are relevant. It's a gating mechanism (literally "gate"): one part of the network learns to filter and modulate the output of the other part, like a gate that controls information flow, some dimensions pass through, others are attenuated or blocked. The concept is similar to the gates in LSTM (Long Short-Term Memory).
The cost secret: FFNs dominate the model
FFNs look like modest components, but in terms of parameters they make up most of the model. Let's check that by counting it ourselves, on LLaMA 7B, whose dimensions are public: vector width 4096, 32 layers, FFN inner width 11,008, vocabulary of 32,000 tokens.
A matrix with rows and columns holds coefficients. Nothing else is needed to do the arithmetic.
Attention, per layer. Four square 4096 × 4096 matrices: the three projections , , plus the output projection . That is 4 × 4096 × 4096 = 67 million. Across 32 layers: 2.15 billion.
FFN, per layer. Three 4096 × 11,008 matrices, because SwiGLU needs three rather than two (the two branches on the way out, the projection on the way back). That is 3 × 4096 × 11,008 = 135 million. Across 32 layers: 4.33 billion.
Embeddings. The table turning tokens into vectors, 32,000 × 4096, and the one doing the reverse at the output, same size: 2 × 32,000 × 4096 = 0.26 billion.
| Component | Parameters | % of total |
|---|---|---|
| Attention (32 layers) | 2.15 billion | 32% |
| FFN (32 layers) | 4.33 billion | 64% |
| Embedding tables (input + output) | 0.26 billion | 4% |
| Total | 6.74 billion | 100% |
The total lands right on the 6.7 billion that the "7B" name advertises. And the verdict is clear: the FFNs weigh twice as much as attention. Two out of every three parameters in the model belong to the FFNs.
But this isn't waste: FFNs need this mass of parameters because they effectively store the world's knowledge. Each key-value pair in / represents a memorized fact or pattern. The larger the model, the more knowledge it can store. Attention gets by on four square matrices per layer, because its job is purely to connect and route information, not to retain it.
Skip connections: how to stack 100 layers without collapsing
The nightmare without skip connections
Imagine stacking 10 layers of neural networks on top of each other. During training, the error gradient must travel back from the last layer to the first via backpropagation.
The problem: at each layer, the gradient passes through a non-linear activation (ReLU, tanh, etc.) and a matrix multiplication. If the gradient is systematically reduced at each passage (multiplied by values < 1), after 10 layers it becomes infinitely small (near zero): that's the vanishing gradient. Conversely, if it's amplified, it explodes. In both cases, the first layers of the network learn nothing useful. Training collapses.
That's why for a long time, you couldn't train very deep networks. 5-10 layers was already difficult.
The solution: skip connections (gradient highways)
The idea, introduced by He et al. in 2015 with ResNet (Residual Network), is disarmingly simple: add the input directly to the transformation's output.
Read it this way: the block's output is what came in, plus whatever the block computed on it. The input was not replaced; a correction was added to it.
That's it. A simple "+". But the consequences are profound.
Now, the gradient has two paths to travel back:
- Through the transformation (the classic path, subject to vanishing/exploding problems)
- Directly via the "+": it's a highway where the gradient passes without any modification
The highway guarantees that the gradient can flow freely from the last layer to the first, without attenuation. The transformation, meanwhile, learns a "residual": the difference between what the network should produce and what it receives as input. Hence the name "residual connection".
The derivative of addition is simply 1. The gradient passes without amplification or dampening. It's this elementary mechanism that allows the Transformer to stack 80, 100, or even 200+ layers without the gradient vanishing or exploding. Without residual connections, modern LLMs (Large Language Models) would be simply impossible to train.
The complete flow in a Transformer layer
Let's see how all these components fit together in a single Transformer layer:
| Step | Operation | Result |
|---|---|---|
| 1 | passes through Self-Attention | |
| 2 | Skip connection + Normalization | |
| 3 | passes through FFN | |
| 4 | Skip connection + Normalization |
The pattern is always the same: transform, add the original input (skip), normalize. Transform, add, normalize. Each sub-block (attention, FFN) learns a "refinement" of the input, and the skip connection guarantees that the original information is never lost. The token "always knows who it was" before the transformation.
Normalization: regulating values to stabilize training
After each operation (attention or FFN), the vectors can have very large or very small values. Without normalization, these imbalances accumulate layer after layer and make training unstable (erratic gradients, divergence). Normalization brings values back into a reasonable range after each operation.
LayerNorm: the classic version
LayerNorm is short for layer normalization.
Each vector is individually recentered to mean 0 and rescaled to variance 1:
Symbol by symbol: is the vector being normalized, its mean, its variance (how widely it is spread), a scale factor and a shift, both learnable, so the network keeps the option of partly undoing the normalization where that helps. The tiny (epsilon) is a very small number added under the square root to avoid dividing by zero. In one sentence: recentre the vector on zero, bring its spread back to 1, then let the network restretch it as it sees fit.
RMSNorm: the modern version
LLaMA, Mistral, and nearly all recent models use RMSNorm, a simplification of LayerNorm:
The same symbols appear, minus and . The difference: no recentering (we don't subtract the mean), just a rescaling by the root mean square, hence the name, RMS. Intuitively, LayerNorm does two things, recentering values around zero then controlling their amplitude, while RMSNorm only controls amplitude. It's mathematically simpler, faster to compute (one fewer operation), and performance is identical in practice.
The recentering (mean subtraction) of LayerNorm doesn't provide a measurable benefit but costs computation time. RMSNorm eliminates this superfluous step. When you stack 80+ layers and each layer normalizes twice (after attention and after FFN), this simplification compounds into a significant speed gain.
Pre-norm vs Post-norm: a detail that changes everything
Where exactly to place the normalization? This "detail" has a major impact on training stability, so much so that it has a lesson of its own: pre-norm versus post-norm. In brief:
| Variant | Formula |
|---|---|
| Post-norm (original, 2017) | |
| Pre-norm (modern) |
The difference is subtle but crucial. In pre-norm, we normalize before the transformation, and the skip connection adds the result without additional normalization. This means the gradient highway stays perfectly clean: the gradient flows through the "+" without passing through any normalization. This is much more stable for training very deep models. All modern LLMs (GPT, LLaMA, Mistral) use pre-norm.
This idea of pre-activation was formalized by He et al. in their follow-up paper on ResNets. They showed that placing the normalization, in their case BatchNorm, batch normalization, a variant that normalizes over the batch of examples rather than over each vector, before the transformation, rather than after, significantly improves gradient propagation and final performance. These are called "pre-activation residual blocks", and it's exactly the principle that Transformers adopted with pre-norm.
Identity Mappings in Deep Residual Networks (He et al., 2016)Why this matters
FFNs and skip connections are the true workhorses of the Transformer. Without FFNs, the model has nowhere to store its world knowledge. Without residual connections, training deep networks (80 layers and up) is practically impossible. People always talk about attention because it's the most original and most publicized mechanism, but these unassuming components are equally indispensable to the system's operation.
Key takeaways
- FFNs are an associative key-value memory that stores factual knowledge
- SwiGLU = modern activation with gating (LLaMA, Mistral)
- FFNs occupy roughly 64% of a LLaMA 7B's parameters, against 32% for attention (counted above)
- Residual connections create gradient highways (the derivative of an addition is 1)
- RMSNorm = current standard normalization (faster than LayerNorm)
- Pre-norm = modern placement of normalization (stable, and it holds up at scale)
1. Where does a Transformer store most of its factual knowledge, such as the capital of France being Paris?
2. In LLaMA 7B, how are the parameters split between attention and FFN?
3. Why does the simple plus of the residual connection make it possible to stack 100 layers or more?
4. What does RMSNorm drop compared to LayerNorm, and why?