编码器结构
要解决的问题
Encoder 将 源序列 编码为上下文相关的表示,供 Decoder 在 cross-attention 中查询。在 Encoder-Decoder 模型(机器翻译、摘要)中承担「理解输入」角色。
单层 Encoder Block
- 多头 Self-Attention(无因果掩码,双向)
- 残差 + LayerNorm
- FFN(常 )
- 残差 + LayerNorm
与 Decoder 的区别
| Encoder | Decoder | |
|---|---|---|
| Self-Attention | 双向 | 因果(masked) |
| Cross-Attention | 无 | 对 Encoder 输出 |
| 典型用途 | 理解输入 | 自回归生成 |
Encoder-only 模型
BERT 等仅用 Encoder + MLM,擅长分类与理解,不原生做自回归生成。现代 LLM 主线为 Decoder-only(见 2.2.3 三大范式)。
工程要点
- 双向 attention 的 KV 在推理时通常 整段一次前向(非逐 token 生成)
- 长序列成本仍 ,可用稀疏或 Flash 加速