Attention lets a token draw information from other tokens. A feed-forward network (FFN) then transforms the resulting representation at that token’s position. Attention mixes information across positions; the FFN reshapes it separately at each position. section 5.1
Expand, activate, project
In the original Transformer, an FFN applies two linear transformations with a ReLU activation between them. A 512-dimensional token vector expands to 2048 dimensions, passes through ReLU, then returns to 512 dimensions. This gives the attended representation a nonlinear transformation without another round of token-to-token mixing. section 5.1
FFNs appear in both encoder and decoder layers, alongside attention. Residual connections and layer normalization help stabilize the repeated stack. The 2018 Universal Transformer kept the pairing of attention and FFNs but reused those layers across depth to refine representations over several steps. That design offered a conceptual alternative, though it was slower than the original Transformer without clear performance gains. section 5.1 section 5.1.2
Figure 5.1 A simplified schematic of the Transformer architecture. The model consists of an encoder (left) and a decoder (right), each comprising repeated stacks of sublayers, including multihead attention mechanisms, feed-forward neural networks, and postprocessing steps such as residual connections and layer normalization. Positional encodings (depicted as circled crosses) are added to the input embeddings to explicitly represent token order. The diagram emphasizes the cross-attention operation (dashed box labeled “Cross-attention”), in which the decoder’s queries (Q) interact with the encoder’s keys (K) and values (V). During cross-attention, each decoding step computes attention scores to determine the relevance of encoded input representations, producing a context vector that guides token generation. This selective alignment enables the decoder to integrate contextual information from the encoder, improving the accuracy and coherence of the output sequence.
The architecture diagram locates feed-forward networks within the repeated encoder and decoder layers, after their attention sublayers. section 5.1
For a code-level view, The Annotated Transformer includes FFNs in a commented, working PyTorch reproduction of the architecture. section 5.2












