Skip to content
E
Educatum

Transformer

By Educatum AI teamLast updated: Original walkthrough ↗
Key Intuition

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.

Reference Diagram & Materials
Transformer matrix calculation diagram
Transformer worked exercise table

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 VV (derived from the input sequence features XX) by the attention weight matrix A=softmax ⁣(QK⊤dk)A = \text{softmax}\!\left(\frac{QK^\top}{\sqrt{d_k}}\right): Z=AVZ = A V
  • Shape preservation: For a sequence of 55 positions, ZZ still has 55 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 X1←X1+X2X_1 \leftarrow X_1 + X_2, X2←X2+X3X_2 \leftarrow X_2 + X_3, 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 ii computes a learned softmax-weighted sum over all attended Value vectors: zi=∑jαijvj,∑jαij=1z_i = \sum_{j} \alpha_{ij} v_j, \qquad \sum_{j} \alpha_{ij} = 1 (all j∈{1,…,n}j \in \{1,\dots,n\} for bidirectional encoders, or j≤ij \le i for causal autoregressive decoders), followed by a residual connection (X+Attention(X)X + \text{Attention}(X)) and Layer Normalization (or RMSNorm).

2. FFN Layer: Vertical Integration (Across Feature Dimensions)

Process

  • Takes each of the 55 attention-weighted token representations (plus residual/norm) and passes them through a two-layer position-wise Feed-Forward Network (MLP): FFN(x)=W2 σ(W1x+b1)+b2\text{FFN}(x) = W_2 \, \sigma(W_1 x + b_1) + b_2
  • In the toy spreadsheet diagram, a single linear expansion step increases feature width from 3→43 \to 4; in full Transformers, the FFN expands from dmodeld_{\text{model}} to a wider hidden dimension dffd_{\text{ff}} (typically 4 dmodel4\,d_{\text{model}}, or ∼83dmodel\sim \frac{8}{3}d_{\text{model}} with SwiGLU), applies a non-linearity (ReLU, GELU, or SwiGLU), and projects back to dmodeld_{\text{model}} 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 55 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 (n=2n = 2) with model and projection width dmodel=dk=2d_{\text{model}} = d_k = 2:

  1. Input embeddings (X∈R2×2X \in \mathbb{R}^{2 \times 2}) and identity/simple linear projections Q=XWQQ = X W_Q, K=XWKK = X W_K, V=XWVV = X W_V: Let X=[1001],Q=K=[2012],V=[1230]X = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}, \quad Q = K = \begin{bmatrix} 2 & 0 \\ 1 & 2 \end{bmatrix}, \quad V = \begin{bmatrix} 1 & 2 \\ 3 & 0 \end{bmatrix}
  2. Scaled dot-product scores (S=QK⊤dkS = \frac{Q K^\top}{\sqrt{d_k}} with 2≈1.414\sqrt{2} \approx 1.414): QK⊤=[4225],S=12[4225]≈[2.8281.4141.4143.536]Q K^\top = \begin{bmatrix} 4 & 2 \\ 2 & 5 \end{bmatrix}, \qquad S = \frac{1}{\sqrt{2}} \begin{bmatrix} 4 & 2 \\ 2 & 5 \end{bmatrix} \approx \begin{bmatrix} 2.828 & 1.414 \\ 1.414 & 3.536 \end{bmatrix} Dividing by dk\sqrt{d_k} keeps score variance at 11 as dkd_k grows so softmax\text{softmax} does not saturate.
  3. Row-wise attention weights (A=softmax(S)A = \text{softmax}(S)): A≈[0.8040.1960.1070.893]A \approx \begin{bmatrix} 0.804 & 0.196 \\ 0.107 & 0.893 \end{bmatrix} Row 1 puts 80.4%80.4\% weight on token 1 and 19.6%19.6\% on token 2; row 2 puts 10.7%10.7\% on token 1 and 89.3%89.3\% on token 2.
  4. Horizontal mixing + Residual (H=X+AVH = X + A V): AV≈[0.804(1)+0.196(3)0.804(2)+0.196(0)0.107(1)+0.893(3)0.107(2)+0.893(0)]=[1.3921.6082.7860.214]A V \approx \begin{bmatrix} 0.804(1) + 0.196(3) & 0.804(2) + 0.196(0) \\ 0.107(1) + 0.893(3) & 0.107(2) + 0.893(0) \end{bmatrix} = \begin{bmatrix} 1.392 & 1.608 \\ 2.786 & 0.214 \end{bmatrix} H=X+AV≈[2.3921.6082.7861.214]H = X + A V \approx \begin{bmatrix} 2.392 & 1.608 \\ 2.786 & 1.214 \end{bmatrix}
  5. Vertical FFN transformation + Residual (Y=H+ReLU(HW1)W2Y = H + \text{ReLU}(H W_1) W_2): Notice each row Hi,:H_{i,:} now passes through the same MLP weights independently across rows—so token 1’s output depends only on H1,:H_{1,:} while token 2’s output depends only on H2,:H_{2,:}.

More in GenAI by Hand

Try keywords like RoPE, FlashAttention, DPO, or Blind 75.