Transformer 的结构是什么?
现在的 decoder-only LLM 改了哪些地方?

英文问法Walk me through the Transformer architecture. · Pre-LN vs post-LN? · What does cross-attention do? · How do modern LLMs differ from the original Transformer?

考过面经:微软 AS 实习电面(1p3a 1158834)、微软 Office Copilot AS PhD 实习、微软中国 Bing LLM 应用组实习、Glassdoor AS II、NVIDIA MLE 电面 2026 考 transformer 基础(1p3a 1184818)、NVIDIA Senior MLE 电面 2025-10 问 attention 变体的参数变化(1p3a 1152213)

先弄清楚:每个概念是什么

Encoder 与 decoder
像同声传译。encoder 先听完整句,每个词都能看到所有词。decoder 逐词说出译文,causal mask 让它只看自己和前面的词。例:原版 encoder、decoder 各 6 层,\(d_{\text{model}} = 512\),8 个 head。
Cross-attention
decoder 里的第二个 attention,相当于译员边说边查原文。
Residual + LayerNorm
每个 sub-layer 的输出加回输入 \(x + F(x)\),gradient 能沿这条直达通道流回浅层。LayerNorm 把每个 token 的向量拉回 mean 0、variance 1。
Post-LN 与 pre-LN
LayerNorm 放在 residual 相加之后是 post(原版),放进分支里、在 \(F\) 之前是 pre。pre-LN 的直达通道上没有 LN。
FFN(position-wise)
两层 MLP,对每个 token 单独做,所有位置共享参数,占每层约 2/3 的参数。attention 在 token 之间交换信息,FFN 给每个 token 加非线性变换。例:512 → 2048 → 512。

30 秒回答

The original Transformer is an encoder-decoder. Each encoder layer has multi-head self-attention and a position-wise feed-forward network with hidden size 4·d_model, each wrapped in a residual connection followed by LayerNorm. The decoder adds a causal mask on its self-attention and a cross-attention sub-layer that takes queries from the decoder and keys and values from the encoder output. Positional encodings are added to the input embeddings. Modern LLMs are decoder-only and use pre-LN, which keeps gradients stable without warm-up, plus RMSNorm, SwiGLU, RoPE and grouped-query attention.

关键公式

Post-LN vs pre-LN(\(F\) = attention 或 FFN)

\[ \text{post-LN:}\quad x_{l+1} = \mathrm{LN}\big(x_l + F(x_l)\big) \] \[ \text{pre-LN:}\quad x_{l+1} = x_l + F\big(\mathrm{LN}(x_l)\big) \]

pre-LN 在最后一层后再加一个 LN,GPT-2 已经在用。Xiong et al. 证明 post-LN 初始化时靠近输出层的 gradient 很大,要靠 learning-rate warm-up。pre-LN 的 gradient 被 \(1/\sqrt{L}\) 压住,可以去掉 warm-up。

FFN 与 cross-attention

\[ \mathrm{FFN}(x) = \max(0,\, xW_1 + b_1)\,W_2 + b_2 \] \[ \mathrm{Cross}(H_{\text{dec}}, H_{\text{enc}}) = \mathrm{softmax}\Big(\frac{QK^\top}{\sqrt{d_k}}\Big)V \]

\(W_1\) 是 \(d\times 4d\),\(W_2\) 是 \(4d\times d\)。\(Q = H_{\text{dec}}W_Q\),\(K, V\) 由 \(H_{\text{enc}}\) 投影得到。

Parameter count:每层 ≈ \(12d^2\)(attention \(4d^2\) + FFN \(8d^2\)),decoder 层多一个 cross-attention,是 \(16d^2\)。算法见 Transformer 复杂度。

原版 vs 现代 LLM

原版 Transformer(2017)现代 decoder-only LLM
结构encoder-decoder只有 decoder(causal mask)
Normpost-LN,LayerNormpre-LN,RMSNorm(不减 mean,只除以 RMS)
FFNReLU,hidden \(4d\)SwiGLU,3 个矩阵,hidden \(\tfrac23\cdot 4d\),参数量不变
位置sinusoidal,加在输入RoPE,每层旋转 q、k
AttentionMHAGQA:几个 query head 共享一组 K、V,KV cache 变小

必答点

追问

为什么用 LayerNorm 不用 BatchNorm?

文本长短不一,batch 统计量不稳,LayerNorm 只用每个 token 自己的 \(d\) 维。见 BatchNorm vs LayerNorm。

SwiGLU 为什么 hidden 取 \(\tfrac23\cdot 4d\)?

SwiGLU 有 3 个 \(d\times h\) 矩阵,\(h = \tfrac83 d\) 时 \(3dh = 8d^2\),和 ReLU FFN 参数量相同。

encoder-only、decoder-only、encoder-decoder 分别用在哪?

encoder-only(BERT)做理解、分类,decoder-only(GPT)做生成,encoder-decoder(原版、T5)做翻译、摘要这类 seq2seq。LLM 为什么都是 decoder-only 见 BERT vs GPT。

来源Vaswani et al. 2017 §3.1(p.3)、§3.2.3、§3.3(p.5)· Xiong et al. 2020 Abstract、§3.3 Theorem 1、Lemma 2–3(pp.5–6)· Radford et al. 2019(GPT-2) §2.3(p.4)· Touvron et al. 2023(LLaMA) §2.2(p.3)· Zhang & Sennrich 2019(RMSNorm) · Shazeer 2020(SwiGLU) §2 eq. 6(p.2) · Ainslie et al. 2023(GQA) · BigScience 2022(BLOOM) §3.2.2(p.15)· Dive into Deep Learning §11.7.1–11.7.3