Layer Normalization 与 RMSNorm:数学原理与数值稳定性
Agent 辅助工程分析
前置要求
第 28 日方差与标准差;第 22 日向量代数基础。
学习目标
针对输入向量 x = [2.0, -1.0, 0.0, 3.0] 逐步计算 LayerNorm 与 RMSNorm,阐释现代架构偏好 RMSNorm 的根本原因。
操作指引
对于 d=4 的向量 x = [2.0, -1.0, 0.0, 3.0]:(1) 计算均值 mu = 1.0、方差 sigma^2 = 2.5,以及 eps=1e-5 下的 LayerNorm 输出。(2) 计算均方根 RMS(x) = sqrt(mean(x^2)) = sqrt(3.5) ≈ 1.8708,及 RMSNorm 输出 x / RMS(x)。(3) 比较计算成本:RMSNorm 省去了在显存上对均值的遍历归约。
自主演练与记录
1. 在 gamma=1, beta=0 下计算 LayerNorm(x),保留 4 位小数。
2. 在 gamma=1 下计算 RMSNorm(x),保留 4 位小数。
3. 为何 RMSNorm 在 GPU 核函数执行时具有更显著的显存带宽优势?
x 的均值为 1.0,平方偏差和为 10,因此方差为 2.5。x 的平方和为 14,平方均值为 3.5。
LayerNorm: (x - 1.0)/sqrt(2.5) = [0.6325, -1.2649, -0.6325, 1.2649]。RMSNorm: x / sqrt(3.5) = [1.0690, -0.5345, 0.0, 1.6036]。RMSNorm 无需在激活维度上做零中心化,少了一次全局归约与显存访存遍历。
学习者能够手算完成两类归一化,并能准确论述 RMSNorm 的访存带宽优势。
权威源资源与权利证明
Root Mean Square Layer Normalization
对应章节 / 时间戳: Section 4 RMSNorm (Equation 4) and Section 4.1 Invariance Analysis
验证版本与日期: NeurIPS 2019 / arXiv:1910.07467v1 (Verified: 2026-10-30)
Primary paper showing that mean-centering in LayerNorm can be discarded without performance loss, speeding up modern LLMs (LLaMA, Gemma).
Variance, standard deviation, and LayerNorm basics.
RMSNorm formulas and numerical comparisons are derived completely in the lesson.
对比候选资源与选型分析 (2)
Original LayerNorm formulation with mean-centering; RMSNorm is compared directly against it as an optimization.
Batch-dependent statistics; unsuitable for autoregressive sequence modeling.
The Annotated Transformer
对应章节 / 时间戳: Encoder and Decoder Stacks (class LayerNorm, class SublayerConnection)
验证版本与日期: Updated PyTorch 2.0 edition (Verified: 2026-10-30)
Clear reference implementation of Transformer modules, residual connections, and layer normalization.
PyTorch tensor operations and attention theory.
All block assembly and normalization logic are provided in runnable local Python snippets.
对比候选资源与选型分析 (2)
Industrial scale with hundreds of model variations; Annotated Transformer is pedagogically focused on the core architecture.
Standard library implementation, but internal C++ bindings obscure the first-principles data flow.