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
📐 Mathematical Definition
⚡ One-Sentence Core Answer
FFN layers act as associative key-value memory storing factual knowledge; standard MLPs expand hidden dimension by , while SwiGLU uses 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 ()
- •Parameter parity: SwiGLU introduces a third weight matrix; scaling intermediate dimension to keeps FLOPs and parameter count identical to standard MLPs
📐 Mathematical Derivations
Mathematical Formulations and Parameter Parity:
① Standard 2-Layer MLP (ReLU/GELU):
, where and .
Standard expansion sets .
Parameter count: parameters per block.
② Gated SwiGLU MLP (LLaMA/Mistral):
.
Notice there are three weight matrices instead of two: .
Total parameters: .
To maintain exact parameter parity with the standard budget:
.
(In LLaMA, , rounded to a multiple of 256 for optimal GPU Tensor Core tile alignment).
① Standard 2-Layer MLP (ReLU/GELU):
, where and .
Standard expansion sets .
Parameter count: parameters per block.
② Gated SwiGLU MLP (LLaMA/Mistral):
.
Notice there are three weight matrices instead of two: .
Total parameters: .
To maintain exact parameter parity with the standard budget:
.
(In LLaMA, , rounded to a multiple of 256 for optimal GPU Tensor Core tile alignment).
🏭 Production Trade-offs
Parameter distribution in Transformers: FFN layers contain approximately of all non-embedding parameters in a Transformer, while multi-head attention contains . FFNs dominate memory capacity, while attention dominates sequence context routing.
⚠️ Common Interview Pitfalls
- ✕Adopting SwiGLU with 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 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.
📚
Associated Knowledge Base Guides & Mindmaps
Explore the comprehensive technical article, exam cards, and global architecture tree.
中文速览与面试要点
解释 FFN 的作用,以及为什么用 4× 或 8/3× 的中间维度
FFN 是逐位置的两层 MLP,提供非线性与'键值记忆'式的特征变换;4× 是容量经验值,SwiGLU 的 8/3× 用于保持参数量相当。
核心要点
- 数学机理: FFN 对每个位置 独立 施加同一个两层 MLP:FFN(x)=W₂·act(W₁x),其中 W₁∈ℝ^d_ff×d(升维)、W₂∈ℝ^d×d_ff(降维)。它的作用有三层理解: (1) 非线性变换 ——注意力是线性加权(softmax 权重 × V 的线性组合),若只有注意力则整个模型是'线性 + softmax 门控',表达力受限;FFN 提供逐位置的非线性,使模型能表达更复杂的函数。 (2) 键值记忆(key-value memory) ——Geva 等 (2021) 的分析显示:W₁ 的每一行可视为一个'模式检测器'(key),W₂ 的每一列是'输出模式'(value);当输入与某 key 匹配时(激活值大),对应的 value 被写入输出。故 FFN 相当于一个 稀疏激活的记忆库 ,存储事实知识与模式。这解释了'为什么 FFN 参数量占 2/3 且与知识存储相关'。 (3) 特征变换与升维 ——先升到高维(4d)再降回,类似核方法中的'升维后线性可分',提供更大的变换空间。 中间维度 4d 是原论文的经验选择(容量与算力的折中); SwiGLU 的 8/3·d 则是因为 GLU 家族需要 三个 矩阵(gate/up/down),为保持参数量与标准 FFN(2 个矩阵 × 4d)相当,把中间维度降为 8/3·d(因为 3×(8/3)=8=2×4),且通常向上取整到 256 的倍数以利硬件。
- 深度剖析与工程权衡:① 参数量账本 ——标准 FFN:2×d×4d=8d²;SwiGLU FFN:3×d×(8/3)d=8d²。两者相等,这是 8/3 的来源(不是巧合)。故 SwiGLU 在'同参数量'下用门控换取了更好的表达力。② FFN 与知识定位 ——大量研究(ROME、MEMIT 等知识编辑工作)把事实知识定位在 FFN 的特定神经元上,并可通过修改 W₁/W₂ 的特定行列来编辑知识;这印证了'FFN 即记忆'的解释。③ MoE 的切入点 ——既然 FFN 是参数量主体且'稀疏激活'(每个输入只激活部分 key),把它换成多个专家 + 路由器就是 MoE 的自然动机;MoE 用'参数量大但激活量小'实现容量-算力解耦。④ FFN 与注意力的分工 ——注意力做'位置间信息交换'(token mixing),FFN 做'逐位置特征变换'(channel mixing);这是 MetaFormer 框架的核心抽象(token mixer + channel mixer)。⑤ 激活的选择 ——ReLU → GELU(BERT/GPT)→ SwiGLU(LLaMA/PaLM);消融显示同参数量下 SwiGLU/GeGLU 优于标准 FFN(Shazeer 2020)。⑥ 面试要点 ——被问'FFN 有什么用',应给出' 非线性 + 键值记忆 + 升维变换 '三层解释,并能算出'4d 与 8/3·d 参数量相等'这一关键事实;只答'增加非线性'不足以体现深度。
- FFN 逐位置独立,是 block 中参数量主体(约 2/3)
- 4d 中间维是原论文经验值,容量-算力折中
- SwiGLU 用 3 个矩阵 → 8/3·d 使参数量与 4d 标准 FFN 相当
常见陷阱
- 以为 8/3·d 是随意选的(为匹配参数量)
- 忽略 FFN 在知识存储中的核心地位