Bare RoFormer encoder trunk with rotary position embeddings.
Architecturally a BERT-base encoder (12 layers, 768 hidden, 12 heads, intermediate width 3072) where the absolute learned position embedding is removed and rotary position embedding (RoPE) is applied to the query and key vectors inside every self-attention layer. RoPE injects relative-position information multiplicatively inside attention so the dot product depends only on the offset — granting better length extrapolation and composing naturally with linear attention.
Parameters
configRoFormerConfigrotary_base. The per-head dimension
hidden_size / num_attention_heads must be even (enforced by
RoFormerConfig).Attributes
embeddingsnn.ModulerotaryModule(cos, sin) tables of shape
(max_position_embeddings, head_dim), sliced to the actual
sequence length at each forward. Uses the original RoPE
pairing (x_2i, x_2i+1) (each frequency repeated twice) rather
than the shared half-split layout, matching the RoFormer reference
checkpoints.encodernn.Moduleconfig.num_hidden_layers rotary-attention transformer
layers.poolernn.ModuleNotes
Reference: Su, Lu, Pan, Murtadha, Wen, and Liu, "RoFormer: Enhanced Transformer with Rotary Position Embedding", Neurocomputing, vol. 568, 2024, article 127063 (arXiv:2104.09864).
Rotary rotation per pair, with :
Applied independently to every pair of and so becomes a function of only.
Examples
>>> import lucid
>>> from lucid.models.text.roformer import RoFormerConfig, RoFormerModel
>>> cfg = RoFormerConfig(num_hidden_layers=2, hidden_size=128,
... num_attention_heads=2, intermediate_size=512)
>>> model = RoFormerModel(cfg).eval()
>>> input_ids = lucid.tensor([[101, 7592, 2088, 102]])
>>> out = model(input_ids)
>>> out.last_hidden_state.shape, out.pooler_output.shape
((1, 4, 128), (1, 128))