M4-027M4: Sequences & TransformersPositional Embeddings (Sinusoidal, RoPE, ALiBi)Medium
⚡Flashcard Drill➔Mastery:
Comparing Positional Encodings: Absolute vs Relative vs RoPE vs ALiBi
📐 Mathematical Definition
⚡ One-Sentence Core Answer
Absolute PE adds coordinates to inputs (poor extrapolation); Relative PE learns offset biases; RoPE rotates queries and keys; ALiBi adds non-learned linear penalties with optimal extrapolation.
📌 Key Takeaways
- •Absolute (BERT/GPT-2): adds position vectors ; strictly bounded by maximum training sequence length
- •Relative (T5): adds learned bias matrix directly into attention logits; memory intensive ()
- •RoPE (LLaMA): multiplicative rotation of Q and K; elegant, hardware efficient, standard across foundation LLMs
- •ALiBi (Press et al.): adds non-learned static linear slope penalty ; strongest zero-shot length extrapolation
📐 Mathematical Derivations
Comparative Formulations:
① Absolute Learned PE (GPT-2):
. Strictly fails for .
② Relative Position Bias (T5 / Raffel et al.):
, where is a learned bucketed bias.
- Flaw: Materializing requires massive GPU VRAM bandwidth, slowing down inference.
③ Rotary Position Embedding (RoPE):
. Incurs zero memory overhead, compatible with KV caching.
④ ALiBi (Attention with Linear Biases / Press et al., ICLR 2022):
, where is a fixed geometric slope per head.
Zero learned parameters. Models trained on 2,048 tokens extrapolate zero-shot to 8,192 tokens with stable perplexity.
① Absolute Learned PE (GPT-2):
. Strictly fails for .
② Relative Position Bias (T5 / Raffel et al.):
, where is a learned bucketed bias.
- Flaw: Materializing requires massive GPU VRAM bandwidth, slowing down inference.
③ Rotary Position Embedding (RoPE):
. Incurs zero memory overhead, compatible with KV caching.
④ ALiBi (Attention with Linear Biases / Press et al., ICLR 2022):
, where is a fixed geometric slope per head.
Zero learned parameters. Models trained on 2,048 tokens extrapolate zero-shot to 8,192 tokens with stable perplexity.
🏭 Production Trade-offs
Why Industry Chose RoPE: While ALiBi achieves exceptional zero-shot extrapolation, RoPE combined with modern context extension techniques (NTK-aware, YaRN) delivers superior in-distribution perplexity and higher semantic retrieval precision.
⚠️ Common Interview Pitfalls
- ✕Using absolute learned positional embeddings for models intended to be fine-tuned on longer context windows
- ✕Assuming ALiBi requires trainable parameters; ALiBi slopes are static, non-learned constants
🎯 Interviewer Follow-up Inquiries
- ?Why does ALiBi's linear penalty slope vary across attention heads in a geometric progression?
- ?What factors led open-source LLM developers to universally prefer RoPE over ALiBi despite ALiBi's extrapolation benefits?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
📚
Associated Knowledge Base Guides & Mindmaps
Explore the comprehensive technical article, exam cards, and global architecture tree.
中文速览与面试要点
比较绝对位置编码、相对位置编码与 RoPE/ALiBi
绝对编码加到输入、位置固定;相对编码建模位置差;RoPE 用旋转实现相对位置;ALiBi 用线性衰减偏置,外推性最好。
核心要点
- 数学机理:四类方案的设计维度是'位置信息的表示形式'与'相对/绝对性质'。 (1) 绝对位置编码(正弦或可学习) ——把位置向量 加到输入 embedding 上。优点:实现最简单。缺点:(a) 只在第一层显式可见;(b) 可学习版本受训练长度硬约束;(c) 不显式建模相对位置。 (2) 相对位置编码 ——直接对位置差建模:在注意力 logits 上加一个偏置 bias_ij=f(i−j)(T5 用'相对位置桶'把距离离散化后查表;Transformer-XL 用正弦函数构造)。优点:显式相对结构、外推较好。缺点:需构造 L×L 的偏置矩阵(内存 O(L²))、桶的划分需设计。 (3) RoPE ——用 旋转 实现相对位置(内积只依赖 s−p),无额外参数、无 O(L²) 偏置矩阵、对 KV cache 友好。 (4) ALiBi(Attention with Linear Biases,Press 等 2021) —— 不加任何位置编码 ,而是在注意力 logits 上直接加一个 线性衰减偏置 :bias_ij=−m_h·(i−j),其中 m_h 是每个头固定的斜率(几何序列,如 1/2, 1/4, ...)。 为什么 ALiBi 外推最好 ——(a) 偏置是位置的 线性函数 ,对任意距离都有定义且行为一致(不像正弦编码的相位在远距离变得不可区分);(b) 不同头有不同的衰减斜率,使模型可同时表达'局部关注'与'全局关注';(c) 零参数、无外推依赖。实证上 ALiBi 在训练长度之外的表现显著优于正弦编码(Press 等报告在 1k 训练、可外推到 2k+)。 现状 ——RoPE 因'相对位置 + 无参数 + 与 KV cache 兼容 + 可外推扩展(NTK/YaRN)'的综合优势成为主流;ALiBi 在需要极致外推且不需位置区分的场景仍有使用。
- 深度剖析与工程权衡:① '加性 vs 乘性/旋转'的关键差异 ——绝对编码是 加 到输入(影响所有后续计算但只在入口),RoPE 是 旋转 Q/K(每层都影响注意力);后者的位置信息'更贴近注意力计算',这是效果差异的根源。② 外推性的三层对比 ——正弦绝对编码 与 KV cache 的工程兼容性 ——RoPE 最友好(K 已旋转,直接缓存);ALiBi 也友好(偏置在 logits 上动态加);相对位置桶需额外存储偏置。④ 多模态的适配 ——RoPE 的 2D/3D 扩展(M-RoPE)在 VLM/视频中成为主流;ALiBi 的 1D 线性衰减不易扩展到多维。⑤ 组合方案 ——实践中有'RoPE + 局部窗口 + attention sink'的组合(如某些长上下文模型),说明位置编码与注意力模式是联合设计的。⑥ 面试要点 ——被问'位置编码怎么选',应按' 表示形式(加性/旋转/偏置)+ 相对性 + 外推性 + 工程兼容性 '四维对比,并给出'现代主流是 RoPE(+ 外推技术)'的结论;能解释'ALiBi 为何外推强'(线性函数对任意距离行为一致)是加分。
- 绝对:实现简单但外推差、无相对结构
- 相对:直接建模位置差,外推较好但需偏置表
- ALiBi:固定线性衰减斜率,零参数、外推强
常见陷阱
- 认为绝对位置编码也有良好外推(实际最差)
- 忽略相对位置偏置矩阵的 O(L²) 内存开销