文章目录
论文:ROFORMER: ENHANCED TRANSFORMER WITH ROTARY POSITION EMBEDDING
作者:Jianlin Su, Yu Lu, Shengfeng Pan, Ahmed Murtadha, Bo Wen, Yunfeng Liu
时间:2021
地址:https://huggingface.co/docs/transformers/model_doc/roformer
一、完整代码
由于Transformer是老生常谈了,这里我们只简要实现RoPE
# 完整代码在这里
class RotaryEmbedding(tf.keras.layers.Layer):
def __init__( self, max_wavelength=10000, scaling_factor=1.0, **kwargs):
super().__init__(**kwargs)
self.max_wavelength = max_wavelength
self.scaling_factor = scaling_factor
self.built = True
def call(self, inputs, start_index=0, positions=None):
cos_emb, sin_emb = self._compute_cos_sin_embedding(inputs, start_index, positions)
output = self._apply_rotary_pos_emb(inputs, cos_emb, sin_emb)
return output
def _apply_rotary_pos_emb(self, tensor, cos_emb, sin_emb):
x1, x2 = tf.split(tensor, 2, axis=-1)
half_rot_tensor = tf.stack((-x2, x1), axis=-2)
half_rot_tensor = tf.reshape(half_rot_tensor, tf.shape(tensor))
return (tensor * cos_emb) + (half_rot_tensor * sin_emb)
def _compute_positions(self, inputs, start_index=0):
seq_len = tf.shape(inputs)[1]
positions = tf.range(seq_len, dtype="float32")
return positions + tf.cast(start_index, dtype="float32")
def _compute_cos_sin_embedding(self, inputs, start_index=0, positions=None):
feature_axis = len(inputs.shape) - 1
sequence_axis = 1
rotary_dim = tf.shape(inputs)[feature_axis]
inverse_freq = self._get_inverse_freq(rotary_dim)
if positions is None:
positions = self._compute_positions(inputs, start_index)
else:
positions = tf.cast(positions, "float32")
positions = positions / tf.cast(self.scaling_factor, "float32")
freq = tf.einsum("i,j->ij", positions, inverse_freq)
embedding = tf.stack((freq, freq), axis=-2)
# 这里 *tf.shape(freq)[:-1] 使用 model.fit 的话无法计算
# embedding = tf.reshape(embedding, (*tf.shape(freq)[:-1], tf.shape(freq)[-1] * 2))
embedding = tf.reshape(embedding, (tf.shape(freq)[0], tf.shape(freq)[-1] * 2))
if feature_axis < sequence_axis:
embedding = tf.transpose(embedding)
for axis in range(len(inputs.shape)):
if axis != sequence_axis and axis != feature_axis:
embedding = tf.expand_dims(embedding, axis)
cos_emb = tf.cast(tf.cos(embedding), self.compute_dtype)
sin_emb = tf.cast(tf.sin(embedding), self.compute_dtype)
return cos_emb, sin_emb
def _get_inverse_freq(self, rotary_dim):
freq_range = tf.divide(tf.range(0, rotary_dim, 2, dtype="float32"),tf.cast(rotary_dim, "float32"))
inverse_freq = 1.0 / (self.max_wavelength**freq_range)
return inverse_freq
二、论文解读
RoPE
通过其特性优先于现有的位置编码方法,包括序列长度的灵活性、随着相对距离的增加而减少的标记间依赖性,以及用相对位置编码装备线性自注意的能力。在各种长文本分类基准数据集上的实验结果表明,具有RoPE
嵌入的Transformer
,即RoFormer
,具有更好的性能;
RoPE
的关键思想是通过将上下文表示与一个旋转矩阵相乘来获取元素的相对位置;
2.1 注意力机制
下面是注意力机制的公式,老生常谈了,给个图就行;

2.2 绝对位置编码
这个是最普通的Transformer
采取的编码方式,非常的经典;

2.3 相对位置编码
下图是Transformer-XL
采取的编码方式,其目的是为了避免在循环机制中出现位置混淆;

下面两个是Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer采取的编码方式;

可以看到,这里直接把位置编码转化为一个要学习的参数 b i , j b_{i,j} bi,j进行嵌入,自由度非常大;

这里和上图的不同是这里另外添加了绝对位置编码的信息;
DeBERTa: Decoding-enhanced BERT with Disentangled Attention这篇论文中认为常规注意力机制中的
p
m
T
⋅
W
q
T
⋅
W
k
⋅
p
n
p_m^T·W_q^T·W_k·p_n
pmT⋅WqT⋅Wk⋅pn并没有表达相对信息,只是做一个bias
的作用,而bias
在
q
,
k
,
v
q,k,v
q,k,v时就已经体现,不需要bias
,采取删除的方法,然后把绝对位置信息转化为相对位置信息;

论文说Radford and Narasimhan
(这两货是GPT
模型的提出者)在2018年的时候对这四种变体进行了比较,发现第四个相对位置编码即删除了bias
的相对位置编码最为合理;但让我纳闷的是这不是2020年的论文吗?
2.4 旋转位置编码
旋转位置编码RoPE
的关键思想是通过将上下文表示与一个旋转矩阵相乘来编码相对位置;
所以RoPE
本质上也是一种相对位置编码,那么其目标肯定
q
m
T
k
n
q_m^Tk_n
qmTkn 只与
x
m
x_m
xm ,
x
n
x_n
xn 以及其相对位置
m
−
n
m-n
m−n 有关;公式如下:

但凡提到旋转Rotary
,肯定是离不开三角函数的,这种方法是把一串序列绕成一个圆,如图所示:

这是我随便从网上下载的图片,简单了解方式即可;第一个位置从3点钟方向开始,把所有的序列逆时针打满一圈,这就是旋转位置编码,论文中有一张图很形象,如图所示:

下面便是上图的公式化表达;

论文中得出这一公式有一个推导,有意思但同时有点长,我把他贴在下面;

不得不感慨,还是咱们中国人把文章写得明白和透彻;
矩阵的快速计算方法:

这样做有什么优势呢?
Long-term decay

这里的推理其实很简单,最后一个公式是由图像说明的,

∑ i = 1 d / 2 ∣ S i ∣ \sum_{i=1}^{d/2}|S_i| ∑i=1d/2∣Si∣在 n − m n-m n−m上虽然不是单调递减,但是其总体趋势是递减的
Adaption for linear attention
其相对位置不需要学习,不需要训练参数,只需要乘以一个旋转矩阵,类似于绝对编码,但是其实质有相对性;
2.5 模型效果
从下图中可以看到RoPE
的效果要比Sinusoidal positional encoding
要好;

三、过程实现
class RotaryEmbedding(tf.keras.layers.Layer):
def __init__( self, max_wavelength=10000, scaling_factor=1.0, **kwargs):
super().__init__(**kwargs)
self.max_wavelength = max_wavelength
self.scaling_factor = scaling_factor
self.built = True
def call(self, inputs, start_index=0, positions=None):
cos_emb, sin_emb = self._compute_cos_sin_embedding(inputs, start_index, positions)
output = self._apply_rotary_pos_emb(inputs, cos_emb, sin_emb)
return output
def _apply_rotary_pos_emb(self, tensor, cos_emb, sin_emb):
x1, x2 = tf.split(tensor, 2, axis=-1)
half_rot_tensor = tf.stack((-x2, x1), axis=-2)
half_rot_tensor = tf.reshape(half_rot_tensor, tf.shape(tensor))
return (tensor * cos_emb) + (half_rot_tensor * sin_emb)
def _compute_positions(self, inputs, start_index=0):
seq_len = tf.shape(inputs)[1]
positions = tf.range(seq_len, dtype="float32")
return positions + tf.cast(start_index, dtype="float32")
def _compute_cos_sin_embedding(self, inputs, start_index=0, positions=None):
feature_axis = len(inputs.shape) - 1
sequence_axis = 1
rotary_dim = tf.shape(inputs)[feature_axis]
inverse_freq = self._get_inverse_freq(rotary_dim)
if positions is None:
positions = self._compute_positions(inputs, start_index)
else:
positions = tf.cast(positions, "float32")
positions = positions / tf.cast(self.scaling_factor, "float32")
freq = tf.einsum("i,j->ij", positions, inverse_freq)
embedding = tf.stack((freq, freq), axis=-2)
# 这里 *tf.shape(freq)[:-1] 使用 model.fit 的话无法计算
# embedding = tf.reshape(embedding, (*tf.shape(freq)[:-1], tf.shape(freq)[-1] * 2))
embedding = tf.reshape(embedding, (tf.shape(freq)[0], tf.shape(freq)[-1] * 2))
if feature_axis < sequence_axis:
embedding = tf.transpose(embedding)
for axis in range(len(inputs.shape)):
if axis != sequence_axis and axis != feature_axis:
embedding = tf.expand_dims(embedding, axis)
cos_emb = tf.cast(tf.cos(embedding), self.compute_dtype)
sin_emb = tf.cast(tf.sin(embedding), self.compute_dtype)
return cos_emb, sin_emb
def _get_inverse_freq(self, rotary_dim):
freq_range = tf.divide(tf.range(0, rotary_dim, 2, dtype="float32"),tf.cast(rotary_dim, "float32"))
inverse_freq = 1.0 / (self.max_wavelength**freq_range)
return inverse_freq
四、整体总结
中国人牛逼!