RoPE 怎么做?为什么表示相对位置?
怎么扩展到更长的 context?
先弄清楚:每个概念是什么
- RoPE(rotary position embedding)
- 不把位置向量加到 embedding 上,而是在每层 attention 里把 q、k 按位置转一个角度。d 维两两一组,转得快的组分辨相邻 token,转得慢的管远距离。例:d_head = 128、base = 10000,最快一组约 6.3 个 token 转一圈,最慢一组约 54k 个。
- Extrapolation
- 推理长度超过训练长度 \(L\)。慢的组在训练里一圈都没转完,新位置的角度从没见过,score 可能从 \([-1, 1]\) 冲到几千,perplexity 超过 1000。例:\(L = 4096\) 时,64 组里有 18 组转一圈要超过 4096 个 token。
- Interpolation
- 把位置或转速压缩,让更长的序列映射回训练见过的角度范围,再用少量长文本 fine-tune。插值后的位置落在训练过的整数位置之间,score 的误差界比 extrapolation 小约 600 倍。
30 秒回答
RoPE encodes position by rotating queries and keys instead of adding a vector to the input. The head dimension is split into d/2 pairs, and pair i at position m is rotated by mθᵢ, with θᵢ = base^(−2i/d). Rotations compose, so (R_m q)ᵀ(R_n k) = qᵀR_(n−m)k: the score depends only on the offset. It has no parameters and is applied to q and k in every layer, not to v. Beyond the training length, the slow pairs see unseen angles, so we interpolate: position interpolation scales positions by L/L′, NTK-aware scaling raises the base, and YaRN interpolates per frequency with an attention temperature, plus a short fine-tune.
推导 / 关键公式
转置就是反着转,两次旋转角度相减,所以只剩 \(n-m\)。d 维时 \(R\) 是 block-diagonal,第 i 组用自己的转速:
\[ \theta_i = \text{base}^{-2i/d},\qquad i = 0,\dots,d/2-1 \]实现不用真乘矩阵:预计算 cos、sin 表,\(q\cdot\cos + \text{rotate\_half}(q)\cdot\sin\)。
手算例子
为了好算取 \(\theta = 90^\circ\):转 90° 就是 \((x, y)\to(-y, x)\)。\(q = (1, 2)\),\(k = (2, 1)\):
- \(m = 1, n = 2\):\(R(90^\circ)q = (-2, 1)\),\(R(180^\circ)k = (-2, -1)\),点积 \(= 4 - 1 = 3\)
- \(m = 3, n = 4\):\(R(270^\circ)q = (2, -1)\),\(R(360^\circ)k = (2, 1)\),点积 \(= 3\)
- 直接用公式:\(q^\top R(90^\circ)k = (1, 2)\cdot(-1, 2) = 3\);同一位置 \(m = n\) 时是 \(q\cdot k = 4\)
怎么扩展 context(训练长度 \(L\) → \(L'\),\(s = L'/L\))
| 方法 | 改什么 | 要点 |
|---|---|---|
| Position interpolation | \(m \to m\cdot L/L'\) | 所有组一起压 \(s\) 倍,相邻 token 更难区分,约 \(s = 8\) 就变差;LLaMA 2048 → 32768 只 fine-tune 约 1000 步 |
| NTK-aware | \(\text{base}\to\text{base}\cdot s^{d/(d-2)}\) | 快的组几乎不动,最慢的组正好压 \(s\) 倍;不 fine-tune 也比 PI 好。例:\(s = 4\),10000 → 40890 |
| YaRN | 按波长分段 + temperature | 转得快的组不动,一圈超过 \(L\) 的组按 PI 压,中间过渡;比之前的方法少用 10 倍 token、2.5 倍步数 |
LLaMA 3 预训练时就把 base 设成 500,000。405B 最后分 6 个阶段把 context 从 8K 训到 128K。
必答点
- q、k 两两一组,位置 m 转 \(m\theta_i\),\(\theta_i = \text{base}^{-2i/d}\)
- \(R(m\theta)^\top R(n\theta) = R((n-m)\theta)\) → score 只依赖 \(n-m\)
- 没有参数;每层只作用在 q、k 上,不动 v,因为位置只需要影响谁和谁配对
- 超长失效:慢的组角度没见过 → PI 压位置、NTK-aware 调 base、YaRN 分组 + temperature,再少量 fine-tune
追问
和 sinusoidal positional encoding 有什么区别?
sinusoidal 是加在输入 embedding 上的 absolute 编码,相对位置要模型自己学(见 positional encoding)。RoPE 每层乘在 q、k 上,两者用同一组频率 \(10000^{-2i/d}\)。
YaRN 的 temperature 是什么?
softmax 前 logits 除以 \(t\),\(\sqrt{1/t} = 0.1\ln s + 1\)。
来源Su et al., RoFormer §3.2(pp.4–5,eq. 12–16)、§3.3、§3.4.2–3.4.3(pp.5–8,eq. 34–36)· Chen et al. 2023, Position Interpolation Abstract、§2.2–2.3(pp.3–5,eq. 4,Fig. 2,Theorem 2.1)· Peng et al., YaRN Abstract、§3.1–3.3(pp.4–6,eq. 10–15)、Appendix A.2(p.13,eq. 19)· Vaswani et al. 2017 §3.5(p.6)· Llama 3 Herd §3.2 Table 3(p.7)、§3.4.2(p.14)