按位置前馈网络 (FFN) 架构
Agent 辅助工程分析
前置要求
第 46 日 Transformer Block 与第 41 日 MLP 基础知识。
学习目标
解释 FFN 作为词元键值记忆库的角色,及其典型的 4 倍隐层扩展维度 d_ff = 4 * d_model。
操作指引
1. 写出带全部形状的 FFN:x 为 (T, d_model),W1 为 (d_model, d_ff),b1 为 (d_ff),W2 为 (d_ff, d_model),b2 为 (d_model),因此 FFN(x) = act(x W1 + b1) W2 + b2 仍为 (T, d_model)。
2. 解释"按位置":同一组 W1, b1, W2, b2 分别作用于每一行 x_t;词元 s 的值不会进入词元 t 的 FFN 输出。跨词元的混合由注意力负责。
3. 比较激活函数:ReLU max(0, z) 有硬性的零;GELU z * Phi(z) 平滑,允许小的负值通过;SwiGLU 使用门控 (Swish(x W) ⊙ (x V)) W2,因此需要三个矩阵而不是两个。
4. 对 d_model = 64、d_ff = 256 计算参数量,先不含偏置,再含偏置。
5. 用 d_ff' ≈ (2/3) d_ff 并取整,求出使 SwiGLU 参数量大致相同的隐藏宽度。
时间安排(120 分钟):阅读 30 + 练习 60 + 检查 30。 阅读:Vaswani et al. 第 3.3 节(按位置前馈网络)和 nanoGPT model.py 中的 MLP 类。练习:第 1-5 项。检查:独立完成掌握题中的两种参数量计算。
自主演练与记录
1. 写出标准两层 FFN 数学公式,并标出每个张量的形状。
2. 为何 FFN 被称为 '按位置 (position-wise)'?
3. 计算无偏置下 W1 (64x256) 与 W2 (256x64) 的总参数量。
4. 加上偏置 b1 和 b2,给出新的总数。
5. SwiGLU 使用三个矩阵(d_model x d_ff' 的 W 和 V,以及 d_ff' x d_model 的 W2)。选择 d_ff' 使其参数量接近第 3 项,并给出该数量。
FFN 独立应用于序列中的每个词元位置 t=1..T,在不同位置之间完全没有信息交互,但共享同一套权重。
1. FFN(x) = act(x W1 + b1) W2 + b2,其中 x: (T, 64),W1: (64, 256),b1: (256),x W1 + b1: (T, 256),W2: (256, 64),b2: (64),输出: (T, 64)。原始 Transformer 使用 ReLU;GPT-2 和 nanoGPT 使用 GELU。
2. 按位置意味着 FFN 以相同的权重分别作用于每个位置的词元向量 x_t。输出的第 t 行只取决于输入的第 t 行,所以 T 行可以用一次批量矩阵乘法算完。它不在词元之间传递信息;那是注意力的工作。
3. W1: 64 * 256 = 16,384。W2: 256 * 64 = 16,384。无偏置总数: 32,768。即 d_ff = 4 * d_model 时的 2 * d_model * d_ff。
4. b1 增加 256,b2 增加 64,总数为 32,768 + 320 = 33,088 个参数。
5. 三个 d_model x d_ff' 矩阵共 3 * 64 * d_ff'。令其等于 2 * 64 * 256,得 d_ff' = (2/3) * 256 ≈ 170.7;取整为 171,得 3 * 64 * 171 = 32,832 个参数(与 LLaMA 式 SwiGLU 一样不含偏置),与 32,768 相差不到 0.2%。实际中 d_ff' 常向上取到便于硬件的倍数,参数量会略有变化。
常见错误:只计算 W1;把 W1 的偏置写成 64 个;以为 SwiGLU 仍保持 d_ff = 256,从而得到 49,152 个参数。
阐明按位置特性并准确推导出 4 倍扩展下的参数账目。
权威源资源与权利证明
Attention Is All You Need
对应章节 / 时间戳: Section 3.3 Position-wise Feed-Forward Networks
验证版本与日期: 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.
对比候选资源与选型分析 (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
对应章节 / 时间戳: model.py: class MLP (c_fc expands to 4 * n_embd, GELU, c_proj back to n_embd)
验证版本与日期: 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.
对比候选资源与选型分析 (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.