M4-025M4: Sequences & TransformersPositional Embeddings (Sinusoidal, RoPE, ALiBi)Easy
⚡Flashcard Drill➔Mastery:

Sinusoidal Positional Encoding: Mathematical Mechanics and Theoretical Length Extrapolation

📐 Mathematical Definition
PE(p,2i)=sin⁡(p/100002i/d),PE(p,2i+1)=cos⁡(p/100002i/d)PE_{(p,2i)}=\sin(p/10000^{2i/d}),\quad PE_{(p,2i+1)}=\cos(p/10000^{2i/d})
⚡ One-Sentence Core Answer
Sinusoidal encoding assigns geometric frequency waves across dimensions; because relative offsets can be expressed as linear transformations, it theoretically generalizes to unseen positions.

📌 Key Takeaways

  • •
    Formulation: PE(pos,2i)=sin⁡(pos/100002i/d)\text{PE}_{(pos, 2i)} = \sin(pos / 10000^{2i/d}), PE(pos,2i+1)=cos⁡(pos/100002i/d)\text{PE}_{(pos, 2i+1)} = \cos(pos / 10000^{2i/d})
  • •
    Linear shift property: there exists a linear rotation matrix MΔM_{\Delta} such that PEpos+Δ=MΔPEpos\text{PE}_{pos + \Delta} = M_{\Delta} \text{PE}_{pos}
  • •
    Extrapolation limitation: while mathematically defined for all pos∈[0,∞)pos \in [0, \infty), empirical attention fails on unseen out-of-distribution position coordinates

📐 Mathematical Derivations

Mathematical Formulations (Vaswani et al., 2017):
For position pospos and dimension index i∈[0,d/2−1]i \in [0, d/2 - 1], define frequency ωi=1100002i/d\omega_i = \frac{1}{10000^{2i/d}}.
PE(pos,2i)=sin⁡(pos⋅ωi)\text{PE}(pos, 2i) = \sin(pos \cdot \omega_i), PE(pos,2i+1)=cos⁡(pos⋅ωi)\quad \text{PE}(pos, 2i+1) = \cos(pos \cdot \omega_i).
Proof of Linear Relative Shift Property:
By standard trigonometric angle-addition identities:
sin⁡((pos+Δ)ωi)=sin⁡(pos⋅ωi)cos⁡(Δ⋅ωi)+cos⁡(pos⋅ωi)sin⁡(Δ⋅ωi)\sin((pos + \Delta) \omega_i) = \sin(pos \cdot \omega_i) \cos(\Delta \cdot \omega_i) + \cos(pos \cdot \omega_i) \sin(\Delta \cdot \omega_i),
cos⁡((pos+Δ)ωi)=cos⁡(pos⋅ωi)cos⁡(Δ⋅ωi)−sin⁡(pos⋅ωi)sin⁡(Δ⋅ωi)\cos((pos + \Delta) \omega_i) = \cos(pos \cdot \omega_i) \cos(\Delta \cdot \omega_i) - \sin(pos \cdot \omega_i) \sin(\Delta \cdot \omega_i).
In matrix form for each 2D frequency subspace:
[PE(pos+Δ,2i)PE(pos+Δ,2i+1)]=[cos⁡(Δωi)sin⁡(Δωi)−sin⁡(Δωi)cos⁡(Δωi)][PE(pos,2i)PE(pos,2i+1)]=R(Δωi)[PE(pos,2i)PE(pos,2i+1)]\begin{bmatrix} \text{PE}(pos + \Delta, 2i) \\ \text{PE}(pos + \Delta, 2i+1) \end{bmatrix} = \begin{bmatrix} \cos(\Delta \omega_i) & \sin(\Delta \omega_i) \\ -\sin(\Delta \omega_i) & \cos(\Delta \omega_i) \end{bmatrix} \begin{bmatrix} \text{PE}(pos, 2i) \\ \text{PE}(pos, 2i+1) \end{bmatrix} = R(\Delta \omega_i) \begin{bmatrix} \text{PE}(pos, 2i) \\ \text{PE}(pos, 2i+1) \end{bmatrix}.
Because R(Δωi)R(\Delta \omega_i) is independent of absolute position pospos, the self-attention mechanism can theoretically learn to attend based on relative distance Δ\Delta via linear projections.

🏭 Production Trade-offs

Theory vs Practice: Although mathematically defined for arbitrary sequence lengths, empirical extrapolation fails because high-dimensional attention weights overfit to the specific coordinate range seen during training. Modern LLMs have replaced sinusoidal encodings with RoPE.
⚠️ Common Interview Pitfalls
  • ✕
    Assuming sinusoidal position embeddings extrapolate seamlessly to 2×2\times training length in practice without fine-tuning
  • ✕
    Adding positional encodings after the first LayerNorm instead of directly to initial token embeddings
🎯 Interviewer Follow-up Inquiries
  • ?
    Why does the inner product of two sinusoidal position encodings decay as the distance between them increases?
  • ?
    How did RoPE directly adapt the 2D rotation matrix from sinusoidal encodings into multiplicative query-key rotations?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔
📚

Associated Knowledge Base Guides & Mindmaps

Explore the comprehensive technical article, exam cards, and global architecture tree.

← PreviousM4-024: Transformer Training Stability Techniques: QK-Norm, Logit Soft-Capping, and Auxiliary Z-Loss📋Back to BankNext →M4-026: Rotary Position Embedding (RoPE): Mathematical Mechanics and Relative Distance Invariance
中文速览与面试要点

解释正弦位置编码为什么能外推到更长序列

用不同频率的正弦/余弦对编码位置,任意位置都有定义且相对位移可表示为线性变换,故理论上可外推。

核心要点

  • 数学机理: 正弦位置编码(Vaswani 等 2017) 用不同频率的正弦/余弦函数为每个位置 p 和维度对 (2i, 2i+1) 赋值:PE(p,2i)=sin(p/ω_i)、PE(p,2i+1)=cos(p/ω_i),其中频率 ω_i=10000^2i/d 从高频(小 i)到低频(大 i)呈几何分布。 三个性质 : (1) 无参数、全域定义 ——任意位置 p(包括训练时未见过的更远位置)都有确定的编码值,这与'可学习位置编码'(受限于最大训练长度、超出则无定义)不同。 (2) 相对位移的线性表示 ——利用三角恒等式,PE(p+Δ) 可表示为 PE(p) 的一个 线性变换 (旋转矩阵):存在与 p 无关的矩阵 M_Δ 使 PE(p+Δ)=M_Δ·PE(p)。这意味着'相对位置 Δ'在编码空间中对应于固定的线性操作,理论上模型可学会利用相对位置信息(这也是它被认为能外推的原因)。 (3) 多尺度频率 ——高频维度编码局部位置、低频维度编码全局位置,形成类似'二进制/傅里叶'的多尺度表示。 为什么实践中外推仍不佳 ——(a) 模型在训练中主要见到训练长度范围内的位置模式,超出范围后 注意力分布 会进入未训练过的区域(编码值本身有定义,但模型未学过如何解读);(b) 低频维度的变化在长距离上极缓慢,使远距离位置的编码近乎相同、难以区分;(c) 训练时的'位置使用习惯'(如局部偏置)未外推。故正弦编码的'可外推'是理论性质,实践中仍需位置插值/外推技巧。
  • 深度剖析与工程权衡:① 与可学习位置编码的对比 ——BERT/GPT-2 用可学习的位置嵌入(一个 max_len×d 的矩阵),优点是灵活、可拟合数据特定的位置模式;缺点是 硬上限 (超过 max_len 无定义,需插值或截断)。正弦编码无参数、无上限,但灵活性较低。② '线性变换'性质的实用含义 ——它意味着'相对位置'在正弦编码下是 可加性/平移不变 的结构,理论上有利于长度泛化;这也是后续 RoPE(用旋转实现相对位置)的思想源头。③ 外推失败的实证 ——Press 等 (2021) 的 ALiBi 论文与后续研究显示:正弦编码与可学习编码的外推能力都 不如 ALiBi/RoPE + 插值方案;说明'有定义'不等于'能用'。④ 与 RoPE 的演进关系 ——RoPE 可以看作'把正弦编码的旋转性质直接施加到 Q/K 上'(而不是加到输入上),从而在 每个注意力层 都保持相对位置结构,效果更好;理解正弦编码的旋转性质是理解 RoPE 的前提。⑤ 多模态中的扩展 ——视觉/视频需要 2D/3D 位置编码,正弦编码的形式可自然扩展到多维(每个维度分配一部分频率),如 ViT 的 2D 正弦编码。⑥ 面试要点 ——被问'正弦编码为什么能外推',应给出' 无参数全域定义 + 相对位移是线性变换 + 多尺度频率 '三点,并诚实指出'实践中外推有限,故现代多用 RoPE/ALiBi';能联系到 RoPE 的旋转性质是明显加分。
  • 无参数、任意位置都有定义(不像可学习编码受限于训练长度)
  • 不同频率覆盖多尺度,相对位移可写成旋转(线性)变换
  • 实践中外推能力有限(训练长度之外性能仍下降)

常见陷阱

  • 以为正弦编码实践外推能力很好(理论与实际有差距)
  • 忽略低频维度在长距离上难以区分的问题