Position-wise Feedforward Network (FFN) Architecture
Agent-Assisted Engineering Check
Prerequisites
Day 46 Transformer Block and Day 41 MLP fundamentals.
Learning Objective
Explain the role of the FFN as a key-value memory over tokens, with expansion dimension d_ff = 4 * d_model.
Instructions
1. Write the FFN with every shape: x is (T, d_model), W1 is (d_model, d_ff), b1 is (d_ff), W2 is (d_ff, d_model), b2 is (d_model), so FFN(x) = act(x W1 + b1) W2 + b2 is (T, d_model) again.
2. Explain "position-wise": the same W1, b1, W2, b2 act on every row x_t separately; no value from token s enters the FFN output at token t. Mixing across tokens is attention's job.
3. Compare the activations: ReLU max(0, z) has a hard zero; GELU z * Phi(z) is smooth and lets small negative values through; SwiGLU uses a gate, (Swish(x W) ⊙ (x V)) W2, and so needs three matrices instead of two.
4. Count the parameters for d_model = 64, d_ff = 256, first without biases, then with them.
5. Work out the hidden width that gives SwiGLU about the same parameter count, using d_ff' ≈ (2/3) d_ff rounded to an integer.
Time plan (120 min): reading 30 + exercise 60 + check 30. Reading: Vaswani et al. Section 3.3 (position-wise feed-forward networks) and the MLP class in nanoGPT model.py. Exercise: items 1-5. Check: the mastery item, with both parameter counts done unaided.
Practice & Prediction Log
1. Write the standard two-layer FFN formula with the shape of every tensor.
2. Why is the FFN called 'position-wise'?
3. Calculate the parameter count of W1 (64x256) and W2 (256x64) without biases.
4. Add the biases b1 and b2 and give the new total.
5. SwiGLU uses three matrices (W, V of size d_model x d_ff' and W2 of size d_ff' x d_model). Choose d_ff' so that its count stays close to item 3, and give that count.
The FFN is applied independently to each token position t=1..T without cross-token interaction.
1. FFN(x) = act(x W1 + b1) W2 + b2, with x: (T, 64), W1: (64, 256), b1: (256), x W1 + b1: (T, 256), W2: (256, 64), b2: (64), output: (T, 64). The original Transformer uses ReLU; GPT-2 and nanoGPT use GELU.
2. Position-wise means the FFN is applied to each token vector x_t separately with the same weights at every position. Row t of the output depends only on row t of the input, so the FFN can be computed for all T rows as one batched matrix product. It adds no communication between tokens; attention provides that.
3. W1: 64 * 256 = 16,384. W2: 256 * 64 = 16,384. Total without biases: 32,768. This is 2 * d_model * d_ff, with d_ff = 4 * d_model.
4. b1 adds 256 and b2 adds 64, so the total is 32,768 + 320 = 33,088 parameters.
5. Three matrices of d_model x d_ff' cost 3 * 64 * d_ff'. Setting this equal to 2 * 64 * 256 gives d_ff' = (2/3) * 256 ≈ 170.7; rounding to 171 gives 3 * 64 * 171 = 32,832 parameters (bias-free, as in LLaMA-style SwiGLU), within 0.2% of 32,768. In practice d_ff' is often rounded up to a hardware-friendly multiple, which shifts the count slightly.
Common errors: counting only W1; writing the bias of W1 with size 64; assuming SwiGLU keeps d_ff = 256, which would give 49,152 parameters.
Articulates the position-wise nature and correctly calculates the 4x expansion parameter ledger.
Authoritative Source Resources & Provenance
Attention Is All You Need
Relevant Section / Range: Section 3.3 Position-wise Feed-Forward Networks
Verified Version & Date: arXiv:1706.03762v7 (Verified: 2026-10-30)
Foundational primary literature defining the Transformer architecture, multi-head attention, and scaled dot-product attention.
Matrix multiplication and basic neural network concepts from Days 22-24 and 34-36.
All mathematical formulas and structural definitions are self-contained in the lesson text.
Compared Candidate Sources & Selection Analysis (2)
High-level visual intuition, but Vaswani et al. provides the primary mathematical specifications needed for implementation.
Co-primary: PyTorch line-by-line implementation alongside the paper equations.
nanoGPT: model.py architecture implementation
Relevant Section / Range: model.py: class MLP (c_fc expands to 4 * n_embd, GELU, c_proj back to n_embd)
Verified Version & Date: Git master (Verified: 2026-10-30)
Compact historical code reference for the named implementation functions; CS336 spring2025 provides the lead component exercises. Do not infer top-p, atomic persistence or cloud budget enforcement from nanoGPT.
Python, PyTorch, and Day 43-45 attention fundamentals.
Local models and classes in the curriculum mirror nanoGPT semantics directly.
Compared Candidate Sources & Selection Analysis (2)
Original legacy TensorFlow 1.x code; nanoGPT provides clean modern PyTorch equivalent.
Includes advanced production features (RoPE, GQA), but nanoGPT's simplicity is ideal for the core Days 46-48 assembly.