M4-020M4: Sequences & TransformersTransformer Architecture AnatomyHard
⚡Flashcard Drill➔Mastery:
Transformer Weight Initialization Strategies and Maximal Update Parametrization (μP)
📐 Mathematical Definition
⚡ One-Sentence Core Answer
Deep Transformers require scaled residual initializations () or zero-init output heads; μP coordinates layer width with learning rates to enable zero-shot hyperparameter transfer.
📌 Key Takeaways
- •GPT-2 Residual Scaling: scale output projection weights () by to bound accumulated residual variance
- •Zero-init heads (ReZero / adaLN-Zero): initialize final sub-layer projections to zero, starting training as pure identity maps
- •Maximal Update Parametrization (μP): scales hidden weights and output LR , allowing hyperparameter tuning on small models
📐 Mathematical Derivations
Mathematical Formulations:
① GPT-2 Residual Variance Scaling:
Each Transformer layer has two residual additions: .
If each residual branch has output variance , after layers the total variance grows as . To ensure , weights of projection layers are initialized with: .
② μP Rules for Transformers (Yang et al., 2022):
Under Standard Parametrization (SP), scaling width alters gradient dynamics, causing optimal learning rate to shift.
- Query projections (): Variance ; Learning rate .
- Key projections (): Variance ; Attention logits scaled by (instead of ).
- Value projections () and FFN: Variance ; Learning rate .
- Output Unembedding (): Variance ; Learning rate .
Under μP, feature representations adapt at identical rates regardless of width, allowing optimal hyperparameters tuned on a 1B model to transfer directly to 70B+.
① GPT-2 Residual Variance Scaling:
Each Transformer layer has two residual additions: .
If each residual branch has output variance , after layers the total variance grows as . To ensure , weights of projection layers are initialized with: .
② μP Rules for Transformers (Yang et al., 2022):
Under Standard Parametrization (SP), scaling width alters gradient dynamics, causing optimal learning rate to shift.
- Query projections (): Variance ; Learning rate .
- Key projections (): Variance ; Attention logits scaled by (instead of ).
- Value projections () and FFN: Variance ; Learning rate .
- Output Unembedding (): Variance ; Learning rate .
Under μP, feature representations adapt at identical rates regardless of width, allowing optimal hyperparameters tuned on a 1B model to transfer directly to 70B+.
🏭 Production Trade-offs
Cost economics: Tuning learning rate on 70B models costs hundreds of thousands of dollars in compute. μP allows teams to execute 100 trials on a 100M parameter model and apply the optimal settings to the full-scale pretraining run with zero re-tuning.
⚠️ Common Interview Pitfalls
- ✕Using standard PyTorch default initialization without residual scaling on deep Transformers (), causing early loss spikes
- ✕Applying μP rules to depth scaling; μP strictly governs width scaling (depth scaling requires distinct depth-μP formulations)
🎯 Interviewer Follow-up Inquiries
- ?Why must attention logits be scaled by rather than in the infinite-width μP limit?
- ?How does zero-initialization of output projections (ReZero) eliminate the need for learning rate warmup?
⚡
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 的初始化策略与 μP
标准初始化随宽度增大导致激活/更新尺度漂移;μP 给出'随宽度一致的初始化与 lr 缩放',使超参可跨规模迁移。
核心要点
- 数学机理: 标准参数化(SP) 的问题——用 1/√d 初始化时,前向传播中各层激活的方差可保持稳定(这是 He/Xavier 的初衷),但 输出层的 logits 方差 ∝ d (因为输出层是 d 项求和);更重要的是,在 SGD/Adam 下每层的'更新尺度'(update scale,即 ‖Δθ‖/‖θ‖)会随宽度漂移 ——宽度越大,某些层的相对更新越小,导致'同一组超参在不同宽度下表现不同',必须为每个规模重新调参。 μP(Maximal Update Parameterization,Yang 等 2022) 的目标是让'每层的激活尺度与更新尺度都与宽度无关',从而超参(lr、初始化、warmup、甚至 weight decay)可 从窄模型零样本迁移到宽模型 。μP 的规则包括:(a) 隐藏层 权重初始化方差 ∝1/d(与标准相同);(b) 输出层 权重初始化 ∝ 1/d (而非 1/√d)——因为输出是 d 项求和,需额外 1/√d 补偿;(c) lr 随宽度反向缩放 (η∝1/d,对 Adam 是 ∝1/d 的'乘性'缩放);(d) 输入层(embedding)不缩放;(e) 某些实现的 attention logits 需额外缩放。 效果 ——在 μP 下,用窄模型(如 d=256)调出的最优 lr 可直接用于宽模型(d=4096)而无需重调,这极大降低了大规模训练的调参成本(称为 μTransfer )。
- 深度剖析与工程权衡:① μTransfer 的实用价值 ——超参迁移让'小模型调参、大模型训练'成为可行流程;对算力有限的团队尤其重要(可用小模型搜索 100 组超参,只把最优的用于大模型)。② 理论直觉 ——μP 的目标是让'每层的特征学习速度相同'(否则某些层学得太快、某些太慢);这通过让'每层更新量与宽度无关'实现。③ 与输出层 logit 缩放的对应 ——μP 要求输出层 1/d 初始化,等价于'把 logit 方差与宽度解耦';这与 M3 中'logit 尺度问题'是同一件事。④ 实践中的近似 ——多数开源模型的实现并非严格 μP(如 LLaMA 用标准参数化 + 调好的 lr),但 μP 的思想(尺度一致性)指导了 (a) QK-Norm、(b) 输出层缩放、(c) lr 与宽度的关系。⑤ 与 QK-Norm 的关系 ——QK-Norm 用显式归一化保证注意力 logits 尺度与宽度无关,是 μP 思想在注意力上的体现(比调初始化更鲁棒)。⑥ 面试要点 ——被问'超参如何跨规模迁移',应介绍 μP 与 μTransfer,并给出' 输出层 1/d + lr ∝1/d '这两条关键规则;能联系到'logit 尺度问题'与'QK-Norm'体现系统性理解。
- 标准参数化下不同宽度的最优 lr 不同(需重调)
- μP 让每层激活与更新的尺度与宽度无关
- 输出层用 1/d(而非 1/√d)是 μP 的关键改动
常见陷阱
- 以为 μP 只是'换个初始化'(核心是更新尺度的宽度不变性)
- 忽略输出层初始化与 logit 尺度的耦合