RoFormer: Enhanced Transformer with Rotary Position Embedding

20 Apr 2021  ·  Jianlin Su, Yu Lu, Shengfeng Pan, Ahmed Murtadha, Bo Wen, Yunfeng Liu ·

Position encoding recently has shown effective in the transformer architecture. It enables valuable supervision for dependency modeling between elements at different positions of the sequence. In this paper, we first investigate various methods to integrate positional information into the learning process of transformer-based language models. Then, we propose a novel method named Rotary Position Embedding(RoPE) to effectively leverage the positional information. Specifically, the proposed RoPE encodes the absolute position with a rotation matrix and meanwhile incorporates the explicit relative position dependency in self-attention formulation. Notably, RoPE enables valuable properties, including the flexibility of sequence length, decaying inter-token dependency with increasing relative distances, and the capability of equipping the linear self-attention with relative position encoding. Finally, we evaluate the enhanced transformer with rotary position embedding, also called RoFormer, on various long text classification benchmark datasets. Our experiments show that it consistently overcomes its alternatives. Furthermore, we provide a theoretical analysis to explain some experimental results. RoFormer is already integrated into Huggingface: \url{https://huggingface.co/docs/transformers/model_doc/roformer}.

PDF Abstract
Task Dataset Model Metric Name Metric Value Global Rank Result Benchmark
Semantic Text Matching CAIL2019-SCM - test RoFormer-1024 Accuracy 69.79% # 1
Semantic Text Matching CAIL2019-SCM - test RoFormer-512 Accuracy 68.29% # 2
Semantic Text Matching CAIL2019-SCM - test WoBERT-512 Accuracy 68.1% # 3
Semantic Text Matching CAIL2019-SCM - test BERT-512 Accuracy 67.77% # 4
Semantic Text Matching CAIL2019-SCM - val BERT-512 Accuracy 64.13% # 2
Semantic Text Matching CAIL2019-SCM - val WoBERT-512 Accuracy 64.07% # 4
Semantic Text Matching CAIL2019-SCM - val RoFormer-1024 Accuracy 66.07% # 1
Semantic Text Matching CAIL2019-SCM - val RoFormer-512 Accuracy 64.13% # 2

Methods