7.3 消融与架构修改#
理解 Transformer 的最好方式,是修改它并观察行为。本节进行几项简单消融。
消融 1:层归一化#
人们常说层归一化对 Transformer 训练稳定性很重要。先尝试移除 RMSNorm,看看会发生什么。
练习 layer_norm_ablation1 分 · 0.5 B200 小时
移除 RMSNorm 后训练
移除 Transformer 中全部 RMSNorm 后训练。在原先最佳学习率下会发生什么?降低学习率能否恢复稳定?
交付物: 移除 RMSNorm 后的学习曲线,以及最佳学习率对应的曲线。
交付物: 几句话评论 RMSNorm 的影响。
再研究归一化位置这个看似任意的选择。预归一化块定义为:
z=x+MultiHeadSelfAttention(RMSNorm(x))(25)y=z+FFN(RMSNorm(z))(26)它是对原始 Transformer 较有共识的修改之一。原始架构使用后归一化:
z=RMSNorm(x+MultiHeadSelfAttention(x))(27)y=RMSNorm(z+FFN(z))(28)现在退回后归一化并比较。
练习 pre_norm_ablation1 分 · 0.5 B200 小时
实现后归一化并训练
将预归一化 Transformer 改为后归一化,训练并观察。
交付物: 后归一化模型与预归一化模型的学习曲线比较。
层归一化显著影响模型行为,连其位置也很重要。
消融 2:位置嵌入#
将使用 RoPE 的基础模型与完全不加入位置嵌入的 NoPE 比较。研究 [28, 29] 表明,仅解码器、使用因果掩码的 Transformer,理论上即使没有显式位置嵌入,也能推断相对或绝对位置信息。接下来进行实证检验。
练习 no_pos_emb1 分 · 0.5 B200 小时
实现 NoPE
从使用 RoPE 的 Transformer 中彻底移除位置嵌入信息,观察结果。
交付物: 比较 RoPE 与 NoPE 表现的学习曲线。
消融 3:SwiGLU 与 SiLU#
跟随 Shazeer [20],比较 SwiGLU 与使用 SiLU、但没有 GLU 门控的前馈网络:
FFNSiLU(x)=W2SiLU(W1x)(29)SwiGLU 使用约 38dmodel 的内部维度,并保证是 64 的倍数。本消融中的 SiLU 网络应改为 dff=4dmodel,使参数量与具有三个权重矩阵的 SwiGLU 大致一致;SiLU 网络只有两个权重矩阵。
练习 swiglu_ablation1 分 · 0.5 B200 小时
SwiGLU 与 SiLU
交付物: 在参数量大致匹配的条件下,比较 SwiGLU 与 SiLU 前馈网络的学习曲线。
交付物: 几句话讨论发现。
低资源提示
GPU 资源有限的自学者可继续用 TinyStories
后续会转向更大、更嘈杂的 OpenWebText,试验架构修改,并可选择参加排行榜。OpenWebText 上训练到较流畅需要很久;GPU 有限的线上自学者,可以继续在 TinyStories 上测试修改,用验证损失评价效果。
English original 展开本章英文原文 PDF 文本提取
下列文本提取自本地原版 PDF。图表和公式的准确布局请对照 PDF。
7.3 Ablations and architecture modification
The best way to understand the Transformer is to actually modify it and see how it behaves. We will now
do a few simple ablations and modifications.
Ablation 1: layer normalization
It is often said that layer normalization is important for the stability of Transformer training. But
perhaps we want to live dangerously. Let’s remove RMSNorm from each of our Transformer blocks and
see what happens.
Problem (layer_norm_ablation): Remove RMSNorm and train (0.5 B200 hrs) (1 point)
Remove all of the RMSNorms from your Transformer and train. What happens at the previous
optimal learning rate? Can you get stability by using a lower learning rate?
Deliverable: A learning curve for when you remove RMSNorms and train, as well as a learning
curve for the best learning rate.
Deliverable: A few sentences of commentary on the impact of RMSNorm.
Let’s now investigate another layer normalization choice that seems arbitrary at first glance. Pre-norm
Transformer blocks are defined as
𝑧 = 𝑥 + MultiHeadSelfAttention(RMSNorm(𝑥)) (25)
𝑦 = 𝑧 + FFN(RMSNorm(𝑧)). (26)
This is one of the few ‘consensus’ modifications to the original Transformer architecture, which used a
post-norm approach as
𝑧 = RMSNorm(𝑥 + MultiHeadSelfAttention(𝑥)) (27)
𝑦 = RMSNorm(𝑧 + FFN(𝑧)). (28)
Let’s revert back to the post-norm approach and see what happens.
Problem (pre_norm_ablation): Implement post-norm and train (0.5 B200 hrs) (1 point)
Modify your pre-norm Transformer implementation into a post-norm one. Train with the post-
norm model and see what happens.
Deliverable: A learning curve for a post-norm Transformer, compared to the pre-norm one.
We see that layer normalization has a major impact on the behavior of the Transformer, and that even
the position of the layer normalization is important.
Ablation 2: position embeddings
We will next investigate the impact of the position embeddings on the performance of the model.
Specifically, we will compare our base model (with RoPE) with not including position embeddings at all
(NoPE). It turns out that decoder-only transformers, i.e., those with a causal mask as we have
implemented, can in theory infer relative or absolute position information without being provided with
position embeddings explicitly [Y.-H. H. Tsai et al., 2019; A. Kazemnejad et al., 2023]. We will now test
empirically how NoPE performs compared to RoPE.
Problem (no_pos_emb): Implement NoPE (0.5 B200 hrs) (1 point)
Modify your Transformer implementation with RoPE to remove the position embedding
information entirely, and see what happens.
Deliverable: A learning curve comparing the performance of RoPE and NoPE.
Ablation 3: SwiGLU vs. SiLU
Next, we will follow N. Shazeer [20] and test the importance of gating in the feed-forward network, by
comparing the performance of SwiGLU feed-forward networks versus feed-forward networks using SiLU
activations but no gated linear unit (GLU):
FFNSiLU (𝑥) = 𝑊2 SiLU(𝑊1 𝑥). (29)
Recall that in our SwiGLU implementation, we set the dimensionality of the inner feed-forward layer to
be roughly 𝑑ff = 83 𝑑model (while ensuring that 𝑑ff mod 64 = 0, to make use of GPU tensor cores). In this
ablation baseline, your FFNSiLU implementation should instead set 𝑑ff = 4 × 𝑑model , to approximately
match the parameter count of the default SwiGLU feed-forward network (which has three instead of two
weight matrices).
Problem (swiglu_ablation): SwiGLU vs. SiLU (0.5 B200 hrs) (1 point)
Deliverable: A learning curve comparing the performance of SwiGLU and SiLU feed-forward
networks, with approximately matched parameter counts.
Deliverable: A few sentences discussing your findings.
Low-Resource Tip: Online students with limited GPU resources should test modifications
on TinyStories
In the remainder of the assignment, we will move to a larger-scale, noisier web dataset
(OpenWebText), experimenting with architecture modifications and (optionally) making a
submission to the course leaderboard.
It takes a long time to train an LM to fluency on OpenWebText, so we suggest that online
students with limited GPU access continue testing modifications on TinyStories (using validation
loss as a metric to evaluate performance).
本章讨论
在 GitHub 查看记录 ↗