RoFormer
13 memberslucid.models.text.roformerRoFormer family — Su et al., 2021 (BERT + rotary position embedding).
Su, Jianlin, et al. "RoFormer: Enhanced Transformer with Rotary Position Embedding." Neurocomputing, vol. 568, 2024, article 127063.
RoFormer replaces BERT's absolute (learned) position embedding with Rotary Position Embedding (RoPE) — a position-encoding scheme that injects relative-position information multiplicatively inside attention rather than additively at the input.
Consider a query/key vector at position . RoPE groups the features into complex coordinates and rotates the -th pair by angle , where
and base (default ) controls the spectrum of
rotational frequencies. In complex form
, the rotation is just
.
The crucial property is that the attention dot product becomes a function of the relative offset only:
so RoPE encodes relative positions without any extra parameters and
without an additive bias term. This grants three practical
benefits: (i) it generalises to longer sequences than seen during
training (with mild quality decay), (ii) it composes naturally
with linear attention variants because it acts on
before the softmax, and (iii) it requires the per-head dimension
to be even — enforced
by __post_init__ above. RoFormer otherwise mirrors BERT-base
(, , ) and is the de-facto
position-encoding choice for modern LLaMA-/Mistral-style LLMs.
Classes
RoFormerConfig1 methodsConfiguration for every RoFormer variant.
RoFormerForMaskedLM2 methodsRoFormer with a tied masked-language-modeling head.
RoFormerForMultipleChoice2 methodsRoFormer with a per-choice multiple-choice classification head.
RoFormerForQuestionAnswering2 methodsRoFormer with a 2-way span head for extractive question answering.
RoFormerForSequenceClassification2 methodsRoFormer with a pooled-CLS sequence-classification head.
RoFormerForTokenClassification2 methodsRoFormer with a per-token classification head.
RoFormerModel4 methodsBare RoFormer encoder trunk with rotary position embeddings.
RoFormerTokenizer1 methodsRoFormer tokenizer — pure-Python reference.
RoFormerTokenizerFast1 methodsRoFormer tokenizer — C++-backed.
Functions
roformer→ RoFormerModelConstruct a RoFormer encoder trunk.
roformer_cls→ RoFormerForSequenceClassificationConstruct a RoFormer model with a sequence-classification head.
roformer_mlm→ RoFormerForMaskedLMConstruct a RoFormer model with a tied masked-LM head.
roformer_token_cls→ RoFormerForTokenClassificationConstruct a RoFormer model with a per-token classification head.