M4-013M4: Sequences & TransformersTransformer Architecture AnatomyEasy
⚡Flashcard Drill➔Mastery:
Complete Structural Anatomy of a Modern Transformer Block
📐 Mathematical Definition
⚡ One-Sentence Core Answer
A modern Pre-LN Transformer block consists of RMSNorm, Multi-Head / Grouped-Query Attention, Residual Skip, RMSNorm, and a SwiGLU Feed-Forward Network.
📌 Key Takeaways
- •Sub-layer 1: Pre-RMSNorm Multi-Head Attention (MHA/GQA) with RoPE Residual Addition
- •Sub-layer 2: Pre-RMSNorm SwiGLU MLP Block Residual Addition
- •Modern standard: Pre-LN configuration guarantees an unobstructed gradient highway throughout hundreds of layers
📐 Mathematical Derivations
Mathematical Formulations of a Modern LLaMA-style Block:
Let input hidden representation be .
① Attention Sub-Layer:
.
Compute projections with Rotary Position Embeddings (RoPE):
.
Compute FlashAttention with causal mask:
.
Project back and add residual skip:
.
② Feed-Forward Sub-Layer (SwiGLU):
.
Compute gated activation with intermediate dimension :
.
Add second residual skip:
.
Let input hidden representation be .
① Attention Sub-Layer:
.
Compute projections with Rotary Position Embeddings (RoPE):
.
Compute FlashAttention with causal mask:
.
Project back and add residual skip:
.
② Feed-Forward Sub-Layer (SwiGLU):
.
Compute gated activation with intermediate dimension :
.
Add second residual skip:
.
🏭 Production Trade-offs
Post-LN vs Pre-LN Historical Note: Original Vaswani Transformer placed LayerNorm after residual addition (). Modern models universally place normalization before sub-layers (Pre-LN / Pre-RMSNorm) to guarantee gradient stability.
⚠️ Common Interview Pitfalls
- ✕Omitting the final LayerNorm/RMSNorm after the last Transformer block before the output unembedding head
- ✕Using standard ReLU in modern Transformer MLPs instead of SwiGLU or GELU
🎯 Interviewer Follow-up Inquiries
- ?Why does the residual connection must bypass the normalization layer in Pre-LN architectures?
- ?What architectural changes distinguish a Decoder block from an Encoder-Decoder cross-attention block?
⚡
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 Block 的组成
多头自注意力 + 残差 + 归一化,接 FFN + 残差 + 归一化;解码器额外插入交叉注意力;Pre-LN 是现代默认。
核心要点
- 数学机理:一个 Pre-LN Transformer Block 的计算为:x₁=x+MHA(LN(x)),x₂=x₁+FFN(LN(x₁)),即两个子层各自'先归一化、再计算、再残差相加'。 子层一:多头自注意力(MHA) ——把输入投影为 Q、K、V(各 h 个头),计算 Attention(Q,K,V)=softmax(QKᵀ/√d_k)V,多头结果拼接后再做输出投影。 子层二:前馈网络(FFN) ——两层线性 + 激活:FFN(x)=W₂·act(W₁x),中间维度通常为 4d(或 SwiGLU 的 8/3·d)。 残差连接 ——每个子层外都有 h←h+SubLayer(·),为梯度提供恒等路径(Jacobian 含 I),是深层可训练的关键。 归一化 ——现代默认 Pre-LN (LN 放在子层 之前 ),使残差路径保持纯净恒等;Post-LN(原论文)把 LN 放在残差相加 之后 ,会打断恒等路径、需 warmup 才能稳定训练。 解码器块 额外插入一个 交叉注意力 子层(query 来自解码器、key/value 来自编码器),用于 Enc-Dec 架构;仅解码器模型则只有'因果自注意力 + FFN'两个子层。 顺序 (现代默认):LN → MHA → 残差 → LN → FFN → 残差,且模型末端通常加一个 final LN 以抑制 Pre-LN 的方差累积。
- 深度剖析与工程权衡:① 归一化的位置谱系 ——Post-LN(原论文,需 warmup、深层不稳)→ Pre-LN(现代默认,稳定但方差随深度累积,需末端 LN)→ RMSNorm (省去均值计算与 β,LLaMA 用)→ QK-Norm (对 Q/K 额外归一化,稳定注意力 logits,防熵崩塌)→ DeepNorm (Post-LN 的复兴,用缩放残差控制方差)。理解这条谱系即掌握 Transformer 稳定性的演进史。② 参数分布 ——每个 block 的参数量约 4d²(MHA 的 QKVO)+ 8d²(FFN 的 4d 中间层,双向)≈ 12d²;故 FFN 占约 2/3 参数,注意力占约 1/3。这解释了'为什么 FFN 是容量主体'与'MoE 把 FFN 换成专家'的选择。③ 计算分布 ——prefill 阶段注意力 FLOPs ∝ L²d、FFN ∝ Ld²;短序列时 FFN 主导、长序列时注意力主导(交叉点在 L≈d 附近)。这决定长上下文优化的重点。④ 激活与归一化的数量 ——现代架构倾向于'减少激活与归一化'(如只在 FFN 中用一次激活、用 RMSNorm 而非 LN),因为它们在低精度与融合算子下是开销点。⑤ 与残差流的统一视角 ——把 x 看作一条'残差流'(residual stream),每个子层从中读取、再把结果写回,是理解 Transformer 信息流的有力框架(见后续题)。⑥ 面试要点 ——被问'画一个 Transformer block',应写出 Pre-LN 公式并说明'残差 + LN 的顺序为何重要';能说出'FFN 占 2/3 参数'与'QK-Norm/DeepNorm'这些细节会显著加分。
- 两个子层:MHA 与 FFN,各带残差与归一化
- Pre-LN 把 LN 放在子层前(残差路径纯净)
- 解码器块多一个交叉注意力子层(encoder-decoder 模型)
常见陷阱
- 把 Post-LN 当作现代默认(现代用 Pre-LN)
- 忽略解码器块的交叉注意力子层