M3-011M3: Deep Learning FoundationsWeight InitializationMedium
⚡Flashcard Drill➔Mastery:
Common Initialization Techniques in Transformers
📐 Mathematical Definition
⚡ One-Sentence Core Answer
Key techniques include residual branch scaling (), zero-initializing output projections, embedding rescaling, and small initial LayerNorm gains.
📌 Key Takeaways
- •Residual scaling: scale attention and FFN output projections by to keep variance across layers
- •Zero output projection: initialize and to 0, starting the network as a pure identity map (ReZero/Fixup)
- •Embedding scaling: scale token embeddings by (Vaswani et al.) or to match initial LayerNorm variances
📐 Mathematical Derivations
Motivations and Formulations:
① Residual Scaling ($1/\sqrt{2L}$) in GPT-2:
In a Transformer, the residual state accumulates as . Each layer adds two residual branches. If each branch contributes variance , after layers the total variance grows linearly: . To maintain , weights of output projections ( in attention, in MLP) are initialized with standard deviation scaled by .
② Zero-Initialization of Projections (ReZero / adaLN-Zero):
By initializing the final linear projection of each residual branch to zero, at step 0. The block behaves as an exact identity function . Gradients flow unimpeded through the skip connection highway, eliminating initial instability without requiring learning rate warm-up.
① Residual Scaling ($1/\sqrt{2L}$) in GPT-2:
In a Transformer, the residual state accumulates as . Each layer adds two residual branches. If each branch contributes variance , after layers the total variance grows linearly: . To maintain , weights of output projections ( in attention, in MLP) are initialized with standard deviation scaled by .
② Zero-Initialization of Projections (ReZero / adaLN-Zero):
By initializing the final linear projection of each residual branch to zero, at step 0. The block behaves as an exact identity function . Gradients flow unimpeded through the skip connection highway, eliminating initial instability without requiring learning rate warm-up.
🏭 Production Trade-offs
Practical adoption: Deep Transformers () without residual branch scaling or Pre-LN suffer from extreme gradient variance in early iterations, necessitating extensive learning rate warmup schedules (thousands of steps) to avoid immediate divergence.
⚠️ Common Interview Pitfalls
- ✕Using standard or unscaled Kaiming initialization on Transformer linear projections, causing immediate training explosion
- ✕Initializing input embeddings and output unembedding matrices with arbitrary random scales without checking scale compatibility with LayerNorm
🎯 Interviewer Follow-up Inquiries
- ?Why does initializing the output projection of a residual block to zero guarantee an exact identity transformation at step 0?
- ?How does adaLN-Zero in Diffusion Transformers (DiT) implement zero-initialization on adaptive conditioning layers?
⚡
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.
中文速览与面试要点
Transformer 中常见的初始化技巧有哪些?
缩放初始化(除以 √(2L))、残差分支小初始化、LayerNorm 前零初始化、输出投影零初始化。
核心要点
- 四项技巧及其动机:① 残差分支缩放(1/√(2L)) ——GPT-2 的做法:把每个残差分支(attention 与 FFN 的输出投影)的权重乘以 1/√(2L)(L 为层数)。 动机 :残差连接使 h⁽ˡ⁾=h⁽ˡ⁻¹⁾+F(h⁽ˡ⁻¹⁾),若 F 的方差为 σ²,则 L 层后累积方差 ≈ L·σ²(若独立);为使总方差保持在 O(1),需 σ²∝1/L,故每层分支缩放 1/√L(考虑两支则 1/√(2L))。这与后文的 μP 是同一思想。② LayerNorm 前的零初始化 ——某些实现把 LN 的 γ 初始化为 0(而非 1),使该 block 初始为恒等映射(F=0),训练更稳(类似 adaLN-Zero)。③ 输出投影零初始化 ——attention 的输出投影 W_o 与 FFN 的第二层 W_down 初始化为零(或很小),使残差分支初始输出为 0 → block 初始为恒等映射。这与 ReZero、Fixup 的思想一致('新模块初始不干扰')。④ 嵌入层的缩放 ——某些实现把词嵌入乘以 √d(如原始 Transformer)或 1/√d(视归一化位置),以匹配 LN 的尺度。
- 实践要点:① 为什么'初始为恒等'重要 ——深层网络的残差分支若初始输出较大,会逐层累积噪声,导致训练初期 loss 高、收敛慢;零初始化使初始状态等价于'更浅的网络',随训练逐步'启用'各层——这是 训练稳定性 的关键技巧(GPT-2/GPT-3/LLaMA 都采用某种形式)。② 与 μP 的关系 ——μP 系统化了这些技巧:它要求残差分支与输出层的初始化按 1/√width 缩放,使'最优学习率与宽度无关',从而实现超参迁移。③ 实践建议 ——若从零实现 Transformer,务必包含 (a) 残差分支缩放、(b) LN 位置(Pre-LN)、(c) 输出投影小初始化;否则深层模型可能无法收敛。④ 与微调的关系 ——预训练模型已包含合适的初始化;微调时新增的模块(如 LoRA、分类头)才需要初始化设计(LoRA 的 B 零初始化、分类头的随机初始化)。⑤ 诊断 ——若深层 Transformer 训练初期 loss 高且下降慢,或梯度范数随深度指数增长/衰减,应检查初始化缩放。⑥ 与 QK-Norm 的关系 ——现代 LLM(如 Gemma、Qwen)在 attention 的 Q、K 上加归一化(QK-Norm),进一步稳定 attention logits 的尺度,这是初始化之外的补充手段。
- GPT-2 用 1/√(2L) 缩放残差分支
- adaLN-Zero 门控零初始化
常见陷阱
- 深层 Transformer 不做残差分支缩放(训练不稳)
- 输出投影用标准随机初始化(初始破坏恒等路径)