M3-016M3: Deep Learning FoundationsActivation FunctionsMedium
⚡Flashcard Drill➔Mastery:

The Gated Linear Unit (GLU) Family and Why They Excel in Large Language Models

📐 Mathematical Definition
GLU(x)=(xW1)⊙σ(xW2)\mathrm{GLU}(x)=(xW_1)\odot\sigma(xW_2)
⚡ One-Sentence Core Answer
GLUs compute element-wise products between a linear projection and a non-linearly gated projection; variants like SwiGLU outperform standard activations by dynamically modulating feature flow.

📌 Key Takeaways

  • •
    GLU formulation: GLU(x)=(xW+b)⊗σ(xV+c)\text{GLU}(x) = (x W + b) \otimes \sigma(x V + c)
  • •
    SwiGLU variant: SwiGLU(x)=Swish1(xW)⊗(xV)\text{SwiGLU}(x) = \text{Swish}_1(x W) \otimes (x V); adopted by LLaMA, PaLM, and Mistral
  • •
    Parameter adjustment: because GLU requires 3 weight matrices instead of 2 in MLPs, hidden dimension is scaled by 2/32/3 to preserve parameter parity

📐 Mathematical Derivations

Mathematical Formulations (Dauphin et al., Shazeer 2020):
Standard MLP layer: FFN(x)=σ(xW1)W2\text{FFN}(x) = \sigma(x W_1) W_2.
Gated Linear Unit (GLU) replaces single non-linearity with a bilinear gating product:
- Bilinear: Bilinear(x)=(xW)⊗(xV)\text{Bilinear}(x) = (x W) \otimes (x V)
- GLU: GLU(x)=(xW)⊗σ(xV)\text{GLU}(x) = (x W) \otimes \sigma(x V)
- GeGLU: GeGLU(x)=GELU(xW)⊗(xV)\text{GeGLU}(x) = \text{GELU}(x W) \otimes (x V)
- SwiGLU: SwiGLU(x)=Swish1(xW)⊗(xV)=(xW⋅11+e−xW)⊗(xV)\text{SwiGLU}(x) = \text{Swish}_1(x W) \otimes (x V) = \left(x W \cdot \frac{1}{1 + e^{-x W}}\right) \otimes (x V).
Full SwiGLU MLP Block:
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}}.
Why it works: The linear path xVx V provides an unobstructed multiplicative highway where gradients can flow directly without being squashed by activation derivatives, while the gating path dynamically controls information pass-through.

🏭 Production Trade-offs

Parameter parity adjustment: Standard MLP has 8d28 d^2 parameters (two matrices of size d×4dd \times 4d). SwiGLU has three matrices (Wgate,Wup,WdownW_{\text{gate}}, W_{\text{up}}, W_{\text{down}}). To keep parameter count and FLOPs identical, the intermediate hidden dimension is adjusted from 4d4d to 83d≈2.67d\frac{8}{3}d \approx 2.67d (e.g., LLaMA sets hidden dimension to 23×4d=2.67d\frac{2}{3} \times 4d = 2.67d rounded to a multiple of 256).
⚠️ Common Interview Pitfalls
  • ✕
    Adopting SwiGLU with standard 4d4d intermediate dimension without realizing parameter count and FLOPs have increased by 50%
  • ✕
    Confusing the roles of WgateW_{\text{gate}} and WupW_{\text{up}} in SwiGLU implementations
🎯 Interviewer Follow-up Inquiries
  • ?
    Why is the intermediate hidden dimension of SwiGLU set to ≈83d\approx \frac{8}{3}d instead of 4d4d in LLaMA?
  • ?
    What gradient flow advantages does the bilinear branch in SwiGLU offer over standard feedforward layers?
⚡
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

Explore the comprehensive technical article, exam cards, and global architecture tree.

← PreviousM3-015: Why Modern Transformers Use GELU and SiLU Instead of ReLU📋Back to BankNext →M3-017: How Activation Function Choice Impacts Numerical Stability and Training Speed
中文速览与面试要点

什么是门控线性单元(GLU)家族?为什么它们有效

用一路输入作为'门'乘另一路,实现输入依赖的非线性调制;表达力强于单路激活。

核心要点

  • GLU 家族的形式:GLU(x)=(xW₁)⊙σ(xW₂),即用一路线性变换的输出作为 门 (经激活后),逐元素乘上另一路线性变换。 门控的直觉 :单路激活(如 ReLU(Wx))是'对每个特征独立地做非线性';而门控是 输入依赖的调制 ——门的值决定'让哪些特征通过、通过多少',这种 乘性交互 比加性/逐点非线性表达力更强(能表达特征间的条件依赖)。 家族成员 (按门控函数区分): SwiGLU (SiLU 门控,LLaMA 用)、 GeGLU (GELU 门控)、 ReGLU (ReLU 门控)、 Bilinear (线性门控)。 为什么有效 :① 乘性交互提供更高阶的非线性(ReLU(Wx) 是分段线性,GLU 是'分段线性 × 分段线性');② 门控实现了'条件计算'的雏形(类似注意力对信息的加权);③ 实验上在同等参数量下,GLU 变体一致优于标准 FFN。 代价 :GLU 需要 三个 投影矩阵(gate/up/down),而标准 FFN 只需两个(up/down);为保持参数量可比,通常把中间维度从 4d 降到 8/3·d (因为 3×(8/3)=8=2×4),使参数量与标准 FFN 相当。
  • 实践要点:① SwiGLU 的结构 ——SwiGLU(x)=(SiLU(xW_gate)⊙xW_up)W_down;注意是 门控与上投影逐元素相乘 ,再经下投影。LLaMA 的 FFN 即此结构,中间维度 d_ff=8/3·d(且向上取整到 256 的倍数以利硬件)。② 与注意力的关系 ——门控与注意力都是'输入依赖的加权':注意力在 序列维度 上加权(token 之间),门控在 特征维度 上加权(特征之间);两者共同构成 Transformer 的两类动态计算。③ 与 MoE 的关系 ——MoE 可视为'门控的极端形式'(在专家维度上硬/软选择),SwiGLU 的 gate 是连续的门控、MoE 的 router 是稀疏的门控。④ 实验证据 ——Shazeer (2020) 的消融显示:同等参数量/计算量下,SwiGLU/GeGLU 优于标准 ReLU/GELU FFN(在 T5 等模型上);这是 LLaMA 采用 SwiGLU 的直接依据。⑤ 实现注意 ——三个矩阵的初始化、d_ff 的取整、以及推理时的融合(gate 与 up 可合并为一次矩阵乘再拆分,提升效率)。⑥ 实践建议 ——从零设计 FFN 时优先用 SwiGLU;若需与已有模型兼容则沿用其结构;微调时不要改动 FFN 结构(否则无法加载预训练权重)。
  • SwiGLU 是 LLaMA FFN 标配
  • 代价是多一个投影矩阵(参数量 +50%)

常见陷阱

  • 保持 d_ff=4d 导致 SwiGLU 参数量超标准 FFN 50%
  • 混淆门控(特征维度)与注意力(序列维度)的作用维度