Appearance
Prefix LM vs Causal LM
Transformer 语言模型的两种核心注意力架构范式——双向 Prefix 理解与因果自回归生成的设计差异,决定了 T5 与 GPT 系列的根本分野。
概述
在 Transformer 架构中,注意力掩码 (attention mask) 的设计决定了模型如何处理输入序列中的 token 关系。Prefix LM(前缀语言模型)和 Causal LM(因果语言模型)是两种最基本的掩码策略,分别对应了 双向理解 与 单向生成 两种不同的能力取向。理解它们的差异,是理解 T5 与 GPT 系列设计哲学的关键。
Causal LM: 因果语言模型
核心机制
Causal LM 使用因果掩码 (causal/autoregressive mask),也称为下三角掩码。每个位置只能 attend 到当前位置及之前的所有位置,不能"看到"未来的 token。
位置: 1 2 3 4 5
Token: [The] [cat] [sat] [on] [the]
注意力矩阵 (Causal Mask):
1 2 3 4 5
1 ✓ ✗ ✗ ✗ ✗
2 ✓ ✓ ✗ ✗ ✗
3 ✓ ✓ ✓ ✗ ✗
4 ✓ ✓ ✓ ✓ ✗
5 ✓ ✓ ✓ ✓ ✓预训练目标
Next Token Prediction (NTP): 给定前缀,预测下一个 token。
输入: [The] [cat] [sat] [on] [the] [___]
目标: [mat]代表模型
- GPT 系列 (OpenAI): GPT-1/2/3/4, 纯 Decoder-only 架构
- LLaMA (Meta): 开源大模型的标杆
- Claude (Anthropic): 基于因果 LM 的对话模型
- PaLM (Google): 大规模因果语言模型
优势
- 生成自然: 自回归生成与预训练目标完全一致
- 参数效率: 全部参数用于生成,无需 encoder
- 扩展性强: 简单架构易于规模化
- 统一框架: 所有任务统一为文本续写
局限
- 双向理解弱: 无法同时利用左右上下文理解当前 token
- 填充任务差: 不擅长需要双向理解的填空、去噪任务
Prefix LM: 前缀语言模型
核心机制
Prefix LM 对输入的前缀部分使用双向注意力(fully visible attention),对生成部分使用因果注意力。这通常通过 Encoder-Decoder 架构实现:
- Encoder: 处理输入前缀,使用双向注意力,可以 attend 到所有输入 token
- Decoder: 自回归生成输出,使用因果注意力,只能 attend 到已生成的 token 和 encoder 输出
输入 (Prefix): [Translate] [English] [to] [French] [:] [The] [cat]
输出 (Target): [Le] [chat]
Encoder 注意力 (双向):
Trans Eng to Fr : The cat
Trans ✓ ✓ ✓ ✓ ✓ ✓ ✓
Eng ✓ ✓ ✓ ✓ ✓ ✓ ✓
... ✓ ✓ ✓ ✓ ✓ ✓ ✓
Decoder 自注意力 (因果):
Le chat
Le ✓ ✗
chat ✓ ✓
Decoder 交叉注意力 (可看到全部 Encoder 输出):
Trans Eng to Fr : The cat
Le ✓ ✓ ✓ ✓ ✓ ✓ ✓
chat ✓ ✓ ✓ ✓ ✓ ✓ ✓预训练目标
Span Corruption (跨度损坏): 在输入中掩码连续的 token span,模型需要预测被掩码的内容。
输入: [The] [cat] [<mask>] [on] [the] [<mask>]
目标: [sat] [mat]代表模型
- T5 (Google): "Text-to-Text Transfer Transformer",将所有任务统一为文本到文本
- BART (Meta): 基于去噪自编码器的 seq2seq 模型
- UL2 (Google): 统一多种 denoiser 的框架
优势
- 双向理解强: Encoder 可以充分利用上下文信息
- 任务灵活: 天然适合翻译、摘要、问答等条件生成任务
- 填充能力: 擅长去噪、填空等需要双向推理的任务
局限
- 参数量效率: 相同总参数量需分给 encoder 和 decoder
- 生成一致性: decoder 的生成能力与纯 causal LM 相比可能稍弱
- 架构复杂: 需要处理 encoder-decoder 之间的交叉注意力
架构对比总结
| 维度 | Prefix LM (T5 风格) | Causal LM (GPT 风格) |
|---|---|---|
| 架构 | Encoder-Decoder | Decoder-only |
| 注意力模式 | Encoder 双向 + Decoder 因果 | 全部因果 |
| 预训练目标 | Span Corruption | Next Token Prediction |
| 双向理解 | 强 (encoder) | 弱 (无) |
| 生成能力 | 强 (自回归 decoder) | 强 (纯自回归) |
| 参数效率 | 需分 encoder/decoder | 全部用于生成 |
| 典型任务 | 翻译、摘要、结构化预测 | 文本生成、对话、代码 |
| 代表模型 | T5, BART, UL2 | GPT, LLaMA, Claude |
融合趋势
Prefix Decoder (非因果 Decoder)
一些研究尝试折中方案——使用 Decoder-only 架构,但对前缀部分使用双向注意力:
- GLM (清华): 自回归空白填充,2D 位置编码
- U-PaLM (Google): 在 PaLM 基础上引入前缀双向注意力
UL2: 统一范式
Google 的 UL2 (2022, ICML) 提出 Mixture of Denoisers,统一了 Prefix LM 和 Causal LM 的预训练目标:
- R-Denoiser: 类似 T5 的 span corruption (Prefix LM 风格)
- S-Denoiser: 类似 GPT 的因果语言建模 (Causal LM 风格)
- X-Denoiser: 长跨度损坏,结合两种风格
实验证明,统一训练后的模型可以在不同架构间迁移能力。
关键论文
| 论文 | 作者 | 年份 | 链接 |
|---|---|---|---|
| Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer (T5) | Raffel et al. (Google) | 2020 (JMLR) | arXiv |
| Language Models are Few-Shot Learners (GPT-3) | Brown et al. (OpenAI) | 2020 (NeurIPS) | arXiv |
| UL2: Unifying Language Learning Paradigms | Tay et al. (Google) | 2022 (ICML) | arXiv |
| GLM: General Language Model Pretraining with Autoregressive Blank Infilling | Du et al. (清华) | 2022 (ACL) | arXiv |
相关概念
- Transformer Architecture —— 现代大模型的基础架构范式
- Attention Mechanism —— Transformer 的核心计算单元
- Position Encoding —— 为模型提供序列位置信息的技术
- Large Language Model (LLM) —— 大语言模型的核心定义与技术原理
- Mixture of Experts —— 稀疏专家激活架构,与注意力设计正交