Transformer
A Transformer block alternates between two complementary operations: self-attention mixes information horizontally across token positions using learned attention weights, while a position-wise Feed-Forward Network (FFN) transforms feature dimensions vertically at each token independently. Simplified examples like X_1 + X_2 illustrate token mixing conceptually; in real Transformers, each output is a softmax-weighted sum of Value vectors followed by residual connections and layer normalization.


A standard Transformer encoder/decoder block pairs two orthogonal operations: attention (mixing information across token positions) and a feed-forward network (transforming features within each token position).
1. Attention Weighting: Horizontal Integration (Across Positions)
Process
- Multiply the projected Value matrix (derived from the input sequence features ) by the attention weight matrix :
- Shape preservation: For a sequence of positions, still has positions—one updated contextual representation per token.
Effect & Clarification on Simplified Notation
- Self-attention combines features horizontally across token positions.
- Clarification on the hand-drawn exercise: Simplified diagrams sometimes write token updates schematically as , , etc., to illustrate a specific toy binary/sparse attention pattern where adjacent positions mix.
- In a real Transformer: Token updates are not restricted to unweighted adjacent addition. Instead, every position computes a learned softmax-weighted sum over all attended Value vectors: (all for bidirectional encoders, or for causal autoregressive decoders), followed by a residual connection () and Layer Normalization (or RMSNorm).
2. FFN Layer: Vertical Integration (Across Feature Dimensions)
Process
- Takes each of the attention-weighted token representations (plus residual/norm) and passes them through a two-layer position-wise Feed-Forward Network (MLP):
- In the toy spreadsheet diagram, a single linear expansion step increases feature width from ; in full Transformers, the FFN expands from to a wider hidden dimension (typically , or with SwiGLU), applies a non-linearity (ReLU, GELU, or SwiGLU), and projects back to before another residual connection and normalization.
Key Characteristics
- Vertical mixing: Combines features across feature channels within each token rather than mixing between different tokens.
- Position-wise independence: The exact same FFN weights are shared across all sequence positions, and every position is processed independently in parallel.
3. Step-by-Step Numerical Walkthrough (2-Token Example)
To connect the hand-drawn spreadsheet diagram directly to the exact equations, trace a 2-token sequence () with model and projection width :
- Input embeddings () and identity/simple linear projections , , : Let
- Scaled dot-product scores ( with ): Dividing by keeps score variance at as grows so does not saturate.
- Row-wise attention weights (): Row 1 puts weight on token 1 and on token 2; row 2 puts on token 1 and on token 2.
- Horizontal mixing + Residual ():
- Vertical FFN transformation + Residual (): Notice each row now passes through the same MLP weights independently across rows—so token 1’s output depends only on while token 2’s output depends only on .