M4-015M4: Sequences & TransformersTransformer Architecture AnatomyMedium
⚡Flashcard Drill➔Mastery:

Role of the Feed-Forward Network (FFN) and the 4x / (8/3)x Hidden Dimension Rationale

🎯 Active Recall Self-Check (Draft First, Verify After)
Login-free practice tracker: recall core answer, audit pitfalls, and log review status.
Step 1 · Draft your concise answer & assumptions
Step 2 · Check Pitfalls & Follow-up Inquiries
Next: M4-016 →
📐 Mathematical Definition
FFN(x)=W2⋅act(W1x);dff=4d or 83d (SwiGLU)\mathrm{FFN}(x)=W_2\cdot\mathrm{act}(W_1x);\qquad d_{ff}=4d\ \text{or}\ \tfrac{8}{3}d\ (\text{SwiGLU})
⚡ One-Sentence Core Answer
FFN layers act as associative key-value memory storing factual knowledge; standard MLPs expand hidden dimension by 4×4\times, while SwiGLU uses 83×\frac{8}{3}\times to preserve parameter parity.

📌 Key Takeaways

  • •
    Knowledge storage: Geva et al. demonstrated FFN layers store factual associations, while attention routes information
  • •
    Channel mixing: provides position-wise non-linear transformations across feature channels (d→dffn→dd \to d_{\text{ffn}} \to d)
  • •
    Parameter parity: SwiGLU introduces a third weight matrix; scaling intermediate dimension to ≈83d\approx \frac{8}{3}d keeps FLOPs and parameter count identical to standard 4d4d MLPs

📐 Mathematical Derivations

Mathematical Formulations and Parameter Parity:
① Standard 2-Layer MLP (ReLU/GELU):
FFN(x)=σ(xW1+b1)W2+b2\text{FFN}(x) = \sigma(x W_1 + b_1) W_2 + b_2, where W1∈Rd×dffnW_1 \in \mathbb{R}^{d \times d_{\text{ffn}}} and W2∈Rdffn×dW_2 \in \mathbb{R}^{d_{\text{ffn}} \times d}.
Standard expansion sets dffn=4dd_{\text{ffn}} = 4d.
Parameter count: 2×(d×4d)=8d22 \times (d \times 4d) = 8 d^2 parameters per block.
② Gated SwiGLU MLP (LLaMA/Mistral):
FFNSwiGLU(x)=(SiLU(xWgate)⊙(xWup))Wdown\text{FFN}_{\text{SwiGLU}}(x) = \left( \text{SiLU}(x W_{\text{gate}}) \odot (x W_{\text{up}}) \right) W_{\text{down}}.
Notice there are three weight matrices instead of two: Wgate,Wup,WdownW_{\text{gate}}, W_{\text{up}}, W_{\text{down}}.
Total parameters: 3×(d×dffn)3 \times (d \times d_{\text{ffn}}).
To maintain exact parameter parity with the standard 8d28d^2 budget:
3d⋅dffn=8d2  ⟹  dffn=83d≈2.67d3 d \cdot d_{\text{ffn}} = 8 d^2 \implies d_{\text{ffn}} = \frac{8}{3} d \approx 2.67 d.
(In LLaMA, d=4096  ⟹  dffn=⌊83×4096⌉=11008d=4096 \implies d_{\text{ffn}} = \lfloor \frac{8}{3} \times 4096 \rceil = 11008, rounded to a multiple of 256 for optimal GPU Tensor Core tile alignment).

🏭 Production Trade-offs

Parameter distribution in Transformers: FFN layers contain approximately 23\frac{2}{3} of all non-embedding parameters in a Transformer, while multi-head attention contains 13\frac{1}{3}. FFNs dominate memory capacity, while attention dominates sequence context routing.
⚠️ Common Interview Pitfalls
  • ✕
    Adopting SwiGLU with dffn=4dd_{\text{ffn}} = 4d without adjusting for the 50% parameter and FLOP inflation
  • ✕
    Setting intermediate hidden dimensions to odd numbers not divisible by 64 or 128, severely degrading GPU Tensor Core memory coalescing
🎯 Interviewer Follow-up Inquiries
  • ?
    How do FFN layers function as key-value associative memories in Transformer interpretability research?
  • ?
    Why is the intermediate dimension dffnd_{\text{ffn}} in LLaMA rounded to the nearest multiple of 256?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔
📚

Associated Knowledge Base Guides & Mindmaps

← PreviousM4-014: Scaled Dot-Product Attention Formula and the Vital Role of the 1/√d_k Factor📋 Back to BankNext →M4-016: Why Multi-Head Attention (MHA) is Effective: Subspace Projections and Diversity