Rotary Position Embeddings

Rotary Position Embeddings are a technique for injecting positional information into attention mechanisms without relying on explicit position encodings.

Verified

On this page

Definition

Rotary Position Embeddings are a technique used to inject positional information into attention mechanisms without relying on explicit position encodings. The Transformers documentation says that instead of adding position vectors to token embeddings, RoPE rotates query and key vectors in the complex plane according to their positions, enabling relative positional awareness and better extrapolation to unseen sequence lengths. The documentation page explains how the Rotary Embedding is computed and applied in Transformers and which types of RoPE are supported.

At a glance

The documentation covers the rope_parameters configuration field, RoPE parameters set per layer type, MRoPE, and the axial rope_type.

  • A rope_parameters field is added to the model configuration file config.json to enable and customize rotary embeddings.
  • Some models such as Gemma-3 use different layer types, and Transformers supports specifying distinct RoPE parameters per layer type for these models.
  • MRoPE is a type of rotation applied in multimodal models and defined by mrope_section.
  • Axial is a registered rope_type for vision models, but it is not listed in ROPE_INIT_FUNCTIONS.

The source does not state the order of these points, so they are listed without arrows.

How it works

Each RoPE variant defines its own set of expected keys, and a missing key raises an error. The default type is the standard rotary embedding as in LLaMA. The linear type is linear-scaled RoPE, which allows longer context windows.

The dynamic type is NTK-aware scaling computed by rescaling the frequency base θ for longer context. The documentation describes the yarn type as a YaRN scaling variant that provides smoother extrapolation and stability. The longrope type is LongRoPE scaling as in the Phi-2 model series.

The llama3 type is RoPE scaling as in Llama3.1. mrope_section sizes contiguous frequency bands for the temporal, height, and width axes, and those sizes sum to head_dim // 2. The frequencies are then repeated so the embedding spans the full head_dim.

For multimodal inputs, usually vision, RoPE is applied in one shot, as a matmul or an elementwise multiply of frequencies with positions. mrope_section only reorders those frequencies along (t, h, w) first, as in Qwen2-VL’s recomposition_frequencies. Prompt text and generated tokens keep normal 1D RoPE by using identical position ids on all three THW grids.

MRoPE is not a separate entry in the rope_type table. You can still apply rope scaling (linear, dynamic) with MRoPE. Some VLMs do not use MRoPE and stay on normal 2D text RoPE instead.

For axial, frequency setup stays on the model. Axial usually applies the same frequencies (head_dim // 4 per spatial axis) for height and width positions and does not allow scaling on top. Examples of axial include the Qwen2-VL vision model and other vision stacks such as Pixtral.

Example

The first example in the documentation creates a llama config with default RoPE parameters. Some models such as Gemma-3 use full attention in some blocks and sliding-window attention in others, so different layer types have different attention mechanisms. In this case, rope_parameters should be a nested dictionary, where top-level keys correspond to config.layer_types and values are per-type RoPE parameters.

During model initialization, each decoder layer automatically looks up the matching RoPE configuration based on its declared layer type. Qwen2-VL uses mrope_section of [16, 24, 24], which is 16 temporal, 24 height and 24 width when head_dim is 128. The documentation says multimodal models often use this layout in the text backbone (Qwen2-VL, GLM-4V).

From the official source
python
from transformers import LlamaConfig

config = LlamaConfig()
config.rope_parameters = {
    "rope_type": "default", # type of RoPE to use
    # rope_theta is optional — omitting it uses the model’s default_theta (typically 10000.0)
}

# If we want to apply a scaled RoPE type, we need to pass extra parameters
config.rope_parameters = {
    "rope_type": "linear",
    "rope_theta": 10000.0,  # can be omitted to fall back to default_theta
    "factor": 8.0  # scale factor for context extension
}
From the official source
python
from transformers import Gemma3Config

config = Gemma3Config()
config.rope_parameters = {
    "full_attention": {
        "rope_type": "dynamic",
        "rope_theta": 1000000.0,
        "factor": 8.0,
        "original_max_position_embeddings": 8096,
    },
    "sliding_attention": {
        "rope_type": "default",
        "rope_theta": 10000.0,
    }
}
From the official source
python
from transformers import Qwen2VLConfig

config = Qwen2VLConfig()
config.text_config.rope_parameters = {
    "rope_type": "default",
    "rope_theta": 1000000.0,
    "mrope_section": [16, 24, 24],  # temporal, height, width frequency band sizes
}

Sources

This page is written from the official sources below. Open them to check the original text.

Verified ·

This page is written by AI from the official sources above and checked automatically. This site has not tested it. Found a mistake? Tell us.