深入浅出 Transformer 位置编码:从 Sin/Cos PE 到 RoPE 旋转位置编码推导
前言
在 Transformer 架构中,Self-Attention 机制本身是置换不变的(Permutation Invariant),即它无法识别词语的先后顺序。为了引入序列的时序信息,位置编码(Positional Encoding)成为了不可或缺的核心模块。
本文将从 Google 传统的 Sin/Cos 绝对位置编码出发,通过泰勒展开推导欧拉公式,自底向上严格证明当前主流大模型(Llama、Qwen、DeepSeek)普遍采用的 RoPE(Rotary Position Embedding,旋转位置编码)。
一、 PE 与 RoPE 特性对比
| 特性 | Sin/Cos PE (Google) | RoPE (旋转位置编码) |
|---|---|---|
| 操作方式 | 词向量 + 位置向量 (加法) | 词向量 × \times × 旋转矩阵 (乘法) |
| 位置类型 | 绝对位置(隐式携带相对位置) | 相对位置(通过 Q , K Q, K Q,K 点积天然体现) |
| 代表模型 | Transformer (2017), BERT | Llama, GPT-4, Qwen, GLM, DeepSeek |
| 优势 | 实现简单、固定不需要训练 | 外推能力极强,长文本扩展性好 |
二、 PE - 正余弦绝对位置编码
P E ( p o s , 2 i ) = sin ( p o s 10000 2 i / d m o d e l ) P E ( p o s , 2 i + 1 ) = cos ( p o s 10000 2 i / d m o d e l ) \begin{align} PE_{(pos, 2i)} &= \sin\left(\frac{pos}{10000^{2i/d_{model}}}\right) \\ PE_{(pos, 2i+1)} &= \cos\left(\frac{pos}{10000^{2i/d_{model}}}\right) \end{align} PE(pos,2i)PE(pos,2i+1)=sin(100002i/dmodelpos)=cos(100002i/dmodelpos)
参数解析
- p o s pos pos:词在序列中的绝对位置(第 0 个词、第 1 个词…)。
- d m o d e l d_{model} dmodel:词向量的总维度(例如 512、4096)。
- i i i:维度索引,取值范围是 [ 0 , d m o d e l / 2 ) [0, d_{model}/2) [0,dmodel/2)。
- 2 i / 2 i + 1 2i / 2i+1 2i/2i+1:偶数下标使用 sin \sin sin,奇数下标使用 cos \cos cos。
- 10000 10000 10000:人为设定的大周期超参数,控制不同维度的波形疏密。
核心特性
- 唯一性:每个位置对应一个独一无二的位置向量。
- 相对位置可线性表示:利用三角函数展开式 sin ( A + B ) = sin A cos B + cos A sin B \sin(A+B) = \sin A \cos B + \cos A \sin B sin(A+B)=sinAcosB+cosAsinB,模型可以线性学到 p o s + k pos+k pos+k 相比于 p o s pos pos 的相对偏移。
三、 RoPE 数学前置:欧拉公式推导
RoPE 的核心在于利用复数平面的旋转来编码位置。首先我们通过泰勒级数推导欧拉公式。
1. 泰勒展开式
e x = 1 + x + x 2 2 ! + x 3 3 ! + x 4 4 ! + … cos θ = 1 − θ 2 2 ! + θ 4 4 ! − θ 6 6 ! + … sin θ = θ − θ 3 3 ! + θ 5 5 ! − θ 7 7 ! + … \begin{align} e^x &= 1 + x + \frac{x^2}{2!} + \frac{x^3}{3!} + \frac{x^4}{4!} + \dots \\ \cos \theta &= 1 - \frac{\theta^2}{2!} + \frac{\theta^4}{4!} - \frac{\theta^6}{6!} + \dots \\ \sin \theta &= \theta - \frac{\theta^3}{3!} + \frac{\theta^5}{5!} - \frac{\theta^7}{7!} + \dots \end{align} excosθsinθ=1+x+2!x2+3!x3+4!x4+…=1−2!θ2+4!θ4−6!θ6+…=θ−3!θ3+5!θ5−7!θ7+…
2. 将 x x x 替换为虚数 i θ i\theta iθ
利用虚数单位
i
i
i 的周期性(
i
2
=
−
1
,
i
3
=
−
i
,
i
4
=
1
,
i
5
=
i
i^2=-1, i^3=-i, i^4=1, i^5=i
i2=−1,i3=−i,i4=1,i5=i):
e
i
θ
=
1
+
(
i
θ
)
+
(
i
θ
)
2
2
!
+
(
i
θ
)
3
3
!
+
(
i
θ
)
4
4
!
+
(
i
θ
)
5
5
!
+
…
=
1
+
i
θ
−
θ
2
2
!
−
i
θ
3
3
!
+
θ
4
4
!
+
i
θ
5
5
!
−
…
\begin{align} e^{i\theta} &= 1 + (i\theta) + \frac{(i\theta)^2}{2!} + \frac{(i\theta)^3}{3!} + \frac{(i\theta)^4}{4!} + \frac{(i\theta)^5}{5!} + \dots \\ &= 1 + i\theta - \frac{\theta^2}{2!} - \frac{i\theta^3}{3!} + \frac{\theta^4}{4!} + \frac{i\theta^5}{5!} - \dots \end{align}
eiθ=1+(iθ)+2!(iθ)2+3!(iθ)3+4!(iθ)4+5!(iθ)5+…=1+iθ−2!θ2−3!iθ3+4!θ4+5!iθ5−…
3. 分离实部与虚部
e i θ = ( 1 − θ 2 2 ! + θ 4 4 ! − … ) ⏟ 实部: cos θ + i ( θ − θ 3 3 ! + θ 5 5 ! − … ) ⏟ 虚部: sin θ e i θ = cos θ + i sin θ \begin{align} e^{i\theta} &= \underbrace{\left( 1 - \frac{\theta^2}{2!} + \frac{\theta^4}{4!} - \dots \right)}_{\text{实部: } \cos\theta} + \underbrace{i \left( \theta - \frac{\theta^3}{3!} + \frac{\theta^5}{5!} - \dots \right)}_{\text{虚部: } \sin\theta} \\ \mathbf{e^{i\theta}} &= \mathbf{\cos \theta + i \sin \theta} \end{align} eiθeiθ=实部: cosθ (1−2!θ2+4!θ4−…)+虚部: sinθ i(θ−3!θ3+5!θ5−…)=cosθ+isinθ
四、 欧拉公式与二维旋转矩阵
在复平面上,给复数 z = x + i y z = x + iy z=x+iy(对应实数二维坐标 ( x , y ) (x, y) (x,y))乘以旋转算子 e i θ e^{i\theta} eiθ:
( x + i y ) ⋅ ( cos θ + i sin θ ) = x cos θ + i x sin θ + i y cos θ + i 2 y sin θ = ( x cos θ − y sin θ ) + i ( x sin θ + y cos θ ) \begin{align} (x + iy) \cdot (\cos\theta + i\sin\theta) &= x\cos\theta + ix\sin\theta + iy\cos\theta + i^2y\sin\theta \\ &= (x\cos\theta - y\sin\theta) + i(x\sin\theta + y\cos\theta) \end{align} (x+iy)⋅(cosθ+isinθ)=xcosθ+ixsinθ+iycosθ+i2ysinθ=(xcosθ−ysinθ)+i(xsinθ+ycosθ)
写成二维矩阵乘法形式:
[
x
′
y
′
]
=
[
cos
θ
−
sin
θ
sin
θ
cos
θ
]
[
x
y
]
\begin{bmatrix} x' \\ y' \end{bmatrix} = \begin{bmatrix} \cos\theta & -\sin\theta \\ \sin\theta & \cos\theta \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix}
[x′y′]=[cosθsinθ−sinθcosθ][xy]
本质:复平面上乘以
e
i
θ
e^{i\theta}
eiθ,完全等价于实数二维空间中的旋转矩阵变换。
五、 RoPE 相对位置注入的证明
对于位置 m m m 的 Query 向量 q \mathbf{q} q 和位置 n n n 的 Key 向量 k \mathbf{k} k,我们取其二维子空间 [ q 0 , q 1 ] [q_0, q_1] [q0,q1] 与 [ k 0 , k 1 ] [k_0, k_1] [k0,k1] 进行分析。
1. 向量复数化
- 位置 m m m 处的 Query: q m = q 0 + i q 1 \mathbf{q}_m = q_0 + iq_1 qm=q0+iq1
- 位置 n n n 处的 Key: k n = k 0 + i k 1 \mathbf{k}_n = k_0 + ik_1 kn=k0+ik1
2. 旋转变换注入绝对位置
分别乘以各自位置对应的旋转算子 e i m θ e^{im\theta} eimθ 与 e i n θ e^{in\theta} einθ:
- q ~ m = q m ⋅ e i m θ \tilde{\mathbf{q}}_m = \mathbf{q}_m \cdot e^{im\theta} q~m=qm⋅eimθ
- k ~ n = k n ⋅ e i n θ \tilde{\mathbf{k}}_n = \mathbf{k}_n \cdot e^{in\theta} k~n=kn⋅einθ
3. 计算 Self-Attention 内积(相似度)
在复数域中,两个向量的内积等于一个复数乘以另一个复数的共轭(Conjugate)。
(注:复数
z
=
a
+
b
i
z = a+bi
z=a+bi 的共轭
z
∗
=
a
−
b
i
z^* = a-bi
z∗=a−bi,对应
e
i
θ
e^{i\theta}
eiθ 的共轭为
e
−
i
θ
e^{-i\theta}
e−iθ)
Score = Re [ q ~ m ⋅ k ~ n ∗ ] = Re [ ( q m ⋅ e i m θ ) ⋅ ( k n ⋅ e i n θ ) ∗ ] = Re [ q m ⋅ k n ∗ ⋅ e i m θ ⋅ e − i n θ ] = Re [ ( q m ⋅ k n ∗ ) ⋅ e i ( m − n ) θ ] \begin{align} \text{Score} &= \text{Re}\left[ \tilde{\mathbf{q}}_m \cdot \tilde{\mathbf{k}}_n^* \right] \\ &= \text{Re}\left[ (\mathbf{q}_m \cdot e^{im\theta}) \cdot (\mathbf{k}_n \cdot e^{in\theta})^* \right] \\ &= \text{Re}\left[ \mathbf{q}_m \cdot \mathbf{k}_n^* \cdot e^{im\theta} \cdot e^{-in\theta} \right] \\ &= \mathbf{\text{Re}\left[ (\mathbf{q}_m \cdot \mathbf{k}_n^*) \cdot e^{i(m-n)\theta} \right]} \end{align} Score=Re[q~m⋅k~n∗]=Re[(qm⋅eimθ)⋅(kn⋅einθ)∗]=Re[qm⋅kn∗⋅eimθ⋅e−inθ]=Re[(qm⋅kn∗)⋅ei(m−n)θ]
4. 结论
推导结果表明,最终计算出的 Attention Score 仅与 q m , k n \mathbf{q}_m, \mathbf{k}_n qm,kn 的原始点积 以及 相对距离 ( m − n ) (m-n) (m−n) 有关!
无论绝对位置 m m m 和 n n n 多大,只要相对距离 ( m − n ) (m-n) (m−n) 保持一致,注意力得分就完全一致。这赋予了模型极强的相对位置感知能力与上下文长度外推能力。
openEuler 是由开放原子开源基金会孵化的全场景开源操作系统项目,面向数字基础设施四大核心场景(服务器、云计算、边缘计算、嵌入式),全面支持 ARM、x86、RISC-V、loongArch、PowerPC、SW-64 等多样性计算架构
更多推荐

所有评论(0)