第 10 章 构建了多头注意力层、位置编码以及可扩展至长上下文的注意力变体。然而,仅靠注意力本身还只是一个词元混合算子:缺少逐词元非线性变换、残差连接与规范化,单纯堆叠注意力层无法得到可训练的深度网络。本章将注意力组装为完整的 Transformer 架构 (Vaswani 等 2017年),并研究其训练、推理服务与缩放规律。
我们从零开始构建并训练因果堆叠。11.1 节 在前置规范化残差流(RMSNorm 与 SwiGLU)内将多头注意力与逐位置前馈网络配对,阐明为何残差投影缩放(\(1/\sqrt{2L}\))与查询-键规范化能跨深度保持信号范数与注意力对数几率有界。11.2 节 将这些因果块堆叠在词元嵌入与权重绑定语言建模头之间,在 WikiText-103 上训练字节级 GPT 并与参数匹配的循环基线对比,加载预训练 GPT-2 权重(124M)检验温度与 top-\(k\) 采样,并提炼现代开源权重 LLM 共享的架构方案(11.2.6 节)。由于朴素自回归生成每一步都要重算整个前缀,11.3 节 引入键值缓存,在硬件屋顶线上对比算力受限的预填充与内存带宽受限的解码,并沿头数(分组查询与多查询注意力)、通道宽度(低秩键值投影)与序列长度(带注意力汇点的滑动窗口)压缩缓存。
并非所有任务都是因果文本生成。11.4 节 将因果解码器与经掩蔽语言模型训练的双向编码器 (Devlin 等 2018年) 以及通过交叉注意力耦合的序列到序列编码器-解码器(含固定大小潜在瓶颈 (Jaegle 等 2021年))进行对比。随后,11.5 节 将不重叠像素图像块投影为词元嵌入,把双向编码器直接应用于图像 (Dosovitskiy 等 2021年),并与参数匹配的 ResNet 对比学得的二维位置结构和数据效率。
最后,我们转向缩放与条件计算。11.6 节 统计参数量(\(N \approx 12 L d^2\))与训练浮点运算(\(C \approx 6 N D\)),训练 15 个模型组成的网格,利用已发表的 Chinchilla 幂律拟合 (Kaplan 等 2020年; Hoffmann 等 2022年) 绘制 IsoFLOP 曲线与算力最优切分,并解释为何生命周期推理成本推动现代预训练转向远远超出 Chinchilla 词元/参数比的小模型。11.7 节 用一组路由专家替代稠密前馈网络,将存储参数容量与每词元浮点运算解耦 (Shazeer 等 2017年; Fedus 等 2022年),对比辅助损失负载均衡与无损失路由器偏置控制 (Wang 等 2024年);11.8 节 则综述规范化位置、潜在注意力与推测解码等前沿扩展。
Devlin, Jacob, Ming-Wei Chang, Kenton Lee, 和 Kristina Toutanova. 2018年.
《BERT: Pre-training of deep bidirectional transformers for language understanding》.
ArXiv:1810.04805.
https://arxiv.org/abs/1810.04805.
Dosovitskiy, Alexey, Lucas Beyer, Alexander Kolesnikov, 等. 2021年.
《An image is worth 16 x 16 words: Transformers for image recognition at scale》.
International Conference on Learning Representations.
https://openreview.net/forum?id=YicbFdNTTy.
Fedus, William, Barret Zoph, 和 Noam Shazeer. 2022年.
《Switch transformers: scaling to trillion parameter models with simple and efficient sparsity》.
Journal of Machine Learning Research 23 (120): 1~39.
https://arxiv.org/abs/2101.03961.
Hoffmann, Jordan, Sebastian Borgeaud, Arthur Mensch, 等. 2022年.
《Training compute-optimal large language models》.
Advances in Neural Information Processing Systems 35.
https://arxiv.org/abs/2203.15556.
Jaegle, Andrew, Felix Gimeno, Andy Brock, Oriol Vinyals, Andrew Zisserman, 和 Joao Carreira. 2021年. 《Perceiver: general perception with iterative attention》. International Conference on Machine Learning, 4651~64.
Kaplan, Jared, Sam McCandlish, Tom Henighan, 等. 2020年.
《Scaling laws for neural language models》.
ArXiv:2001.08361.
https://arxiv.org/abs/2001.08361.
Shazeer, Noam, Azalia Mirhoseini, Krzysztof Maziarz, 等. 2017年. 《Outrageously large neural networks: the sparsely-gated mixture-of-experts layer》. International Conference on Learning Representations.
Vaswani, Ashish, Noam Shazeer, Niki Parmar, 等. 2017年.
《Attention is all you need》.
Advances in Neural Information Processing Systems, 5998~6008.
https://doi.org/10.5555/3295222.3295349.
Wang, Lean, Huazuo Gao, Chenggang Zhao, Xu Sun, 和 Damai Dai. 2024年. 《Auxiliary-loss-free load balancing strategy for mixture-of-experts》. arXiv preprint arXiv:2408.15664.