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.