


Transformers power modern language models, but explanations often jump from a colorful attention diagram to dense matrix notation. This series takes the middle route: clear intuition, enough mathematics to verify the mechanism, and practical implications for model architecture and GPU inference. It assumes familiarity with programming, arrays and basic algebra, not previous experience building Transformers.
Reading path. Parts 1–4 build one attention calculation. Parts 5–8 assemble a Transformer block, including positional information and feed-forward networks (FFNs). Parts 9–13 connect the math to memory-efficient GPU execution and text generation. Parts 14–15 close with training, implementation and evidence-based performance analysis.
Editorial conventions. denotes token positions, the number of hidden features per token, Query/Key width, batch size and attention-head count. Dimensions are usually written as rows × columns. Approximate costs are analytic estimates, not measurements of a particular GPU.
Self-attention answers, roughly, “What information should this token gather from other positions?” The feed-forward network (FFN) tackles a different problem: “How should this token’s newly contextualised features be transformed?” It is tempting to describe the FFN as two linear layers. That description misses the most important ingredient: the nonlinear activation between them.
A conventional FFN uses two weight matrices and, optionally, two bias vectors:
If each token has width and the intermediate width is , then has shape and has shape . A common illustrative choice is . The output returns to width , allowing it to be added to the residual stream.
For sequences of tokens, the FFN input is and its intermediate activation is . Neither projection mixes token positions. The same are shared across positions, but they act on each token row separately.

Choose inputs and , model width 2, intermediate width 3, and the teaching matrices
After the first projection and bias, the two rows are and . ReLU, short for rectified linear unit, applies to each intermediate coordinate. The activated rows become and .
Multiplying by and adding gives final FFN outputs and . Notice that changing the second input row did not alter the first row’s FFN calculation. Attention and FFN have fundamentally different information-flow patterns.

Without an activation, two successive matrix multiplications collapse into one matrix product:
With biases, the composition still collapses into a single affine transformation, a matrix multiplication plus an added vector. An FFN without nonlinearity loses the ability to implement the same family of input-dependent changes in slope. For instance, ReLU has a sharp change at zero that a single affine map cannot reproduce for all inputs.
GELU, the Gaussian Error Linear Unit, offers a smoother alternative. Mathematically it can be written as , where is the standard normal cumulative distribution function. Unlike ReLU, GELU may preserve a small negative output. The exact shape of the activation therefore changes the result, even if the weight matrices stay fixed.
Some Transformers use a gated FFN such as SwiGLU instead of the conventional two-matrix design. One branch makes candidate features, another produces gate values through the SiLU activation, and their outputs are multiplied coordinate-wise before contraction:
SiLU is , where is the sigmoid function. The gate is a learned numerical modulation, not necessarily a binary switch. SwiGLU uses three matrices, so it must not inherit the conventional FFN parameter formula blindly.
The conventional FFN has weights before biases. When , that becomes . A three-matrix gated FFN of intermediate width has weights. Equating those counts gives as a rough parameter-matching choice; real models can round dimensions for implementation reasons.
On the compute side, two conventional dense projections require about floating-point operations, counting one multiply and one add as two operations. This is a model-work estimate, not elapsed time. During prefill, many token rows can reuse FFN weights in large matrix multiplications. During low-batch single-token decode, reading those weights can become relatively expensive because there is much less reuse.
A useful research interpretation views some FFN intermediate features as learned detectors whose activations scale corresponding output directions. This resembles associative key-value retrieval, but it does not prove that every feature stores one neat fact. Representations are distributed, and residual paths, attention and gating interact. The analogy is useful as intuition, not as a literal database inside the network.
Takeaway. The FFN is a shared position-wise nonlinear feature transformer. Its expansion provides a working feature space; its activation makes the computation more expressive; its contraction restores model width for the residual addition.
Next: Part 9 looks at what all these matrix operations cost when they run on a GPU.