Search

Search papers and lessons

Multimodal Rotary Position Embedding

Level 4 · 14 min read

Multimodal rotary position embedding splits each token's position into temporal, height and width parts, so images and video get grid positions while text keeps ordinary 1D positions.

On this page

  • Colour = topic mix
  • Calculus
  • Discrete math
  • Linear algebra
  • Neural network basics
  • Architectures
  • Probability
  • ReadReady to readNot yet

TL;DR

A multimodal model reads text, images and video in one sequence, and each kind of input has a different shape. Standard rotary position embedding gives every token one number, its place in the sequence, which is a poor fit for a grid of image patches or a stack of video frames. Multimodal rotary position embedding (M-RoPE) gives each token three position numbers, for time, height and width, and rotates different parts of the query and key vectors by each one. Text uses the same number three times, so it behaves exactly like ordinary 1D rotary embedding.

Builds on: Rotary Position Embedding, Vision Transformer (ViT) · Leads to: Qwen2-VL: Enhancing Vision-Language Model's Perception at Any Resolution

Where it's used

Qwen2-VL (Alibaba, 2024). The Qwen2-VL vision-language models, in sizes of 2B, 7B and 72B parameters, use M-RoPE in their language model. Images are cut into patches, and an MLP then merges each 2 by 2 block of patch tokens into one visual token before the language model sees it. In the paper's ablation, a 1.5B-parameter pretrained model with M-RoPE beat the same model with 1D rotary embedding on most video benchmarks (for example NextQA rose from 43.9 to 46.0) and on most image benchmarks, though RealWorldQA and InfoVQA dropped slightly.

Video understanding with long inputs. Video is a sequence of frames, so it has time as well as space. The paper trained with each video capped at 16384 tokens, then evaluated the 72B model on medium-length videos at inference lengths up to 80K tokens. The paper says M-RoPE "reduces the value of position IDs for images and videos", and that this is what lets the model extrapolate to longer sequences than it was trained on.

Mixing images, video and text in one prompt. A chat that contains a text question, a photo and a short clip needs one consistent position scheme for all three. The paper's rule is that each new modality starts its IDs at one more than the largest ID used so far, so the model keeps the order of the whole prompt and can refer back to an image mentioned earlier in the text.

How it works

Take a small input: a text sentence, then a picture cut into a 2 by 2 grid of patches, then another text sentence. Each token will get a triple of IDs (t,h,w)(t, h, w) for time, height and width. To keep the arithmetic visible, this walkthrough gives each patch its own token. In the real model, a 2 by 2 block of patches is merged into one token, which shrinks the grid but leaves the rule unchanged.

  1. Assign the first text tokens. For text, all three IDs are the same number. Use positions 0, 1, 2 for three text tokens. Check: (0,0,0)(0,0,0), (1,1,1)(1,1,1), (2,2,2)(2,2,2). This is exactly 1D rotary embedding.
  2. Start the image. Its first ID is one more than the largest ID used so far, so the image starts at 3. Every patch in a still image shares the same time ID, so t=3t = 3 for all four patches.
  3. Give the patches their grid position. The top-left patch gets (h,w)=(3,3)(h, w) = (3, 3). The top-right gets (3,4)(3, 4). The bottom-left gets (4,3)(4, 3). The bottom-right gets (4,4)(4, 4). Check: the four patches use the height values {3,4}\{3, 4\} and the width values {3,4}\{3, 4\}, so the image occupies two ID values in each direction, not four.
  4. Start the next text tokens after the image. The largest ID so far is 4, so the next text token gets 5, and the one after it gets 6. Check: a 1D scheme would have needed positions 3 to 6 for the four patches alone.
  5. For each query and key, split the rotary pairs into three groups, one per component. Each group rotates its pairs by the angle that its own ID gives. Check: a text token has the same ID in all groups, so every group rotates by the same angle, and the result is the 1D rotation.
  6. Compute attention as usual, taking the dot product of each query with each key. Because each group is a rotation, that dot product depends only on the difference between the IDs in each group, as explained below.

The diagram shows how a token's three IDs feed the three groups of rotary pairs.

flowchart LR
  T["token position (t, h, w)"] --> G1["temporal pairs rotate by t times theta"]
  T --> G2["height pairs rotate by h times theta"]
  T --> G3["width pairs rotate by w times theta"]
  G1 --> Q["rotated query and key"]
  G2 --> Q
  G3 --> Q

The plot shows how the attention contribution of one rotary pair depends on the gap between two tokens in one component. Each pair contributes cos⁡(Δθ)\cos(\Delta \theta), where Δ\Delta is the gap in that component's ID and θ\theta is the pair's frequency. The example uses θ=0.5\theta = 0.5.

024681012−1−0.500.51gap between the two IDs in one componentcontribution of one pair
  • cos(0.5 · gap)
The contribution depends only on the gap. Two tokens at the same height produce the same value whatever their absolute height.

The math

Definition

Definition: Multimodal rotary position embedding

Let a token have position IDs (t,h,w)(t, h, w). Partition the d/2d/2 rotary pairs of the head dimension into three groups GtG_t, GhG_h and GwG_w. Index the pairs by i=0,1,…,d/2−1i = 0, 1, \dots, d/2 - 1. For pair ii with base frequency θi=10000−2i/d\theta_i = 10000^{-2i/d}, let pip_i be the ID of the group that pair ii belongs to, so pi=tp_i = t, hh or ww. The token's rotation for pair ii is the angle pi θip_i \, \theta_i. This turns each query and key pair (x2i,x2i+1)(x_{2i}, x_{2i+1}) into a rotated pair using the rotation matrix R(piθi)R(p_i \theta_i).

In words: each rotary pair is rotated by an angle that is set by one of the three IDs, chosen by the group the pair belongs to. The base frequency θi\theta_i is the same as in the 1D case, so pairs that rotate fast handle fine position differences and pairs that rotate slowly handle coarse ones. The trigonometric form of the rotation is what makes the next step work.

Step 3 in symbols: the grid IDs

For an image starting at ID ss, a patch in row rr and column cc of the grid has IDs

t=s,h=s+r,w=s+c.t = s, \qquad h = s + r, \qquad w = s + c.

The three components can have different maxima. The next text token starts at one more than the largest value of tt, hh or ww taken over all visual tokens so far.

Step 5 in symbols: the rotation

Rotating a pair by angle aa is the 2 by 2 matrix

R(a)=(cos⁡a−sin⁡asin⁡acos⁡a).R(a) = \begin{pmatrix} \cos a & -\sin a \\ \sin a & \cos a \end{pmatrix}.

A query at position pp and a key at position p′p' are rotated by R(pθ)R(p\theta) and R(p′θ)R(p'\theta) respectively.

Step 6 in symbols: why only the gap matters

Rotation matrices satisfy R(a)⊤R(b)=R(b−a)R(a)^{\top} R(b) = R(b - a). The dot product of the rotated query and key pair is therefore

(R(pθ) q)⋅(R(p′θ) k)=q⊤R((p′−p)θ) k.\big(R(p\theta)\, q\big) \cdot \big(R(p'\theta)\, k\big) = q^{\top} R\big((p' - p)\theta\big)\, k.

The absolute positions cancel and only the gap p′−pp' - p remains. This is the property that standard RoPE gives, and M-RoPE keeps it separately for each of the three components. For a pair in the height group, the gap is the difference of the two tokens' hh IDs, and the same holds for width and time.

For text, all three IDs are equal, so the three gaps are equal and the formula reduces to ordinary 1D RoPE. This is why text-only behaviour does not change.

Worked examples

Example
1. One rotary pair, one component (tiny)

Take a single pair in the height group with frequency θ=1\theta = 1 radian. Compare a query at height 2 with a key at height 5.

  1. The gap is 5−2=35 - 2 = 3.
  2. The relative angle is 3×1=33 \times 1 = 3 radians.
  3. The pair's contribution is cos⁡3=−0.990\cos 3 = -0.990.
  4. Move both tokens by the same amount, to heights 10 and 13. The gap is still 3, so the contribution is still −0.990-0.990.

Interpretation: only the gap matters. Shifting both tokens together changes nothing, which is why a picture placed later in a prompt still has the same relative geometry inside itself.

Example
2. A 2 by 2 image between text (standard)

Use the first example from "How it works": three text tokens at 0, 1, 2, then a 2 by 2 image, then text.

  1. Image start: s=3s = 3, so the four patches have t=3t = 3.
  2. Patch (r,c)(r, c) has h=3+rh = 3 + r and w=3+cw = 3 + c. The patch IDs are (3,3,3)(3, 3, 3), (3,3,4)(3, 3, 4), (3,4,3)(3, 4, 3), (3,4,4)(3, 4, 4) in the order (t,h,w)(t, h, w).
  3. The largest ID among visual tokens is 4.
  4. The next text token gets 4+1=54 + 1 = 5, and the one after gets 6.
  5. Check the text after the image: the token at ID 5 has (5,5,5)(5, 5, 5), so it still rotates like a 1D token.
  6. Count the cost: the image spent four tokens but only raised the position counter from 2 to 4, a gain of two positions. A 1D scheme would raise it from 2 to 6.

Interpretation: patches that sit next to each other in the grid have IDs that differ by one in one component, so their attention pattern reflects the grid. The reduced counter is also what lets the model handle longer inputs.

Example
3. A three-frame video (realistic)

Take a video of three frames, each cut into a 2 by 2 grid, so 12 visual tokens. Let the video start at ID 0. (Real models usually merge frames and use a different patch size. This example uses the simplest case so the arithmetic stays visible.)

  1. Frame ff has temporal ID t=ft = f, so the frames get t=0,1,2t = 0, 1, 2.
  2. Within each frame, the height and width IDs follow the grid: h∈{0,1}h \in \{0, 1\} and w∈{0,1}w \in \{0, 1\}.
  3. The 12 tokens therefore have t∈{0,1,2}t \in \{0, 1, 2\}, h∈{0,1}h \in \{0, 1\} and w∈{0,1}w \in \{0, 1\}.
  4. The largest ID is 2, so the next text token gets 2+1=32 + 1 = 3.
  5. A 1D scheme would give the video 12 consecutive positions, so the next text token would start at 12.
  6. Consider a query in frame 2 at grid cell (1,0)(1, 0), so (t,h,w)=(2,1,0)(t, h, w) = (2, 1, 0), and a key in frame 0 at grid cell (1,0)(1, 0), so (0,1,0)(0, 1, 0). The temporal gap is 22, and the height and width gaps are 00. So the temporal pairs see a gap of 2 and the spatial pairs see no gap. This is the signal that the two tokens show the same place in different frames.

Interpretation: the video uses small ID values, so the text after it starts early and the model does not have to attend across a long run of position numbers. This is the effect the paper credits for the length extrapolation it reports.

Try it: a video of 8 frames, each 4 by 4 patches, starts at ID 0. What is the position ID of the first text token after it? Answer: the largest temporal ID is 77 and the largest height and width IDs are 33, so the maximum is 77 and the next text token gets 88. A 1D scheme would need 8×16=1288 \times 16 = 128 positions and would start the text at 128.

Common mistakes and tradeoffs

It is tempting to think M-RoPE gives images and video a different kind of attention. In fact the attention operation is unchanged. Only the rotation angles differ, and the dot product still depends only on gaps. The change is in which IDs feed which rotary pairs.

It is tempting to think text suffers from the extra dimensions. In fact a text token has equal IDs in all three components, so it is rotated exactly as a 1D token would be. A text-only model and the text parts of a multimodal model agree.

It is tempting to think the three IDs can be combined into one number. A single number would lose the grid. Two patches that are one row apart and two patches that are one column apart would look the same. Keeping three components keeps the geometry of each axis.

It is tempting to think that smaller IDs are only a convenience. They change what the model can handle. Because the counter grows slowly over images and video, the model sees smaller position values at inference than a 1D scheme would produce for the same content. The paper says this is what lets it extrapolate to longer inputs, and its Figure 5 shows robust results past the 16384-token training cap.

When to use it. Use M-RoPE when a language model reads grids of patches, video frames, or a mix of text and visual tokens in one sequence, and you want the geometry of each to be respected.

When not to use it. For text-only models, standard 1D rotary embedding is simpler and is all that is needed. If the visual tokens have no grid structure, for example a flat sequence of image features from a model that already mixes them, the three-component split adds nothing. Also note that the way the rotary pairs are divided among the three components is a design choice. The fetched paper text does not give the split, so check the model's configuration before copying one.

Alternatives. Learned absolute position embeddings add a vector per position and do not extend well to longer inputs. Other 2D schemes give each axis its own embedding and add them to the token. Positional Encoding covers the additive family, and M-RoPE stays within the rotary family.

History

Transformers in 2017 used sinusoidal position vectors added to the token embeddings. Learned position tables replaced them in many later models, but those tables have a fixed maximum length. Rotary position embedding, introduced in the RoFormer paper by Su and colleagues in 2021, moved position into the attention itself by rotating query and key vectors, so that scores depend on relative position.

Vision transformers, from 2020, showed that an image could be treated as a sequence of patches, with 2D position handled by an embedding table. Once vision and language were mixed in one sequence, a scheme for 2D and temporal position was needed. Qwen2-VL, released in 2024, extended rotary embedding to three components and reported gains on its ablation benchmarks, mostly on video, and better extrapolation to long video.

Recap

  • Text tokens carry one position value in all three components, so they are rotated as in 1D rotary embedding.
  • Image patches carry a time value that is constant within the image, and height and width values from the grid.
  • Video adds a time value that grows by frame.
  • Rotary pairs are grouped by component, and each group rotates by its own ID. The dot product then depends only on the gap in each component.
  • Visual tokens use a small range of IDs, so the text that follows them starts early and longer inputs are easier to reach.

Read next: Qwen2-VL: Enhancing Vision-Language Model's Perception at Any Resolution for the full model and its ablations, and Vision-Language Model for the systems this scheme serves.

Check yourself

QuestionRecall: what three position IDs does a text token get under M-RoPE, and why does that make it behave like ordinary 1D rotary embedding?Show answer

It gets the same number in all three components: time, height and width. Because all three IDs are equal, every group of rotary pairs rotates by the same angle, which is exactly the 1D rotation.

QuestionComputation: one rotary pair in the height group has frequency θ=0.5\theta = 0.5. A query is at height 4 and a key is at height 10. What is the angle of relative rotation for that pair? Give the number to two decimal places.Show answer

The gap is 10−4=610 - 4 = 6, so the relative angle is 6×0.5=3.006 \times 0.5 = 3.00 radians.

QuestionWhy: two patches are one row apart in an image, and two other patches are one column apart. Why would a single combined position number fail to tell these cases apart, and how does M-RoPE avoid the problem?Show answer

Counting patches in raster order turns a one-row step into a jump of one full row length and a one-column step into a jump of 1. A single gap value can therefore be read as either move, and it does not say which axis changed. M-RoPE keeps a separate height ID and width ID, so a row step changes only the height component and a column step changes only the width component. The rotary pairs assigned to each component then respond to the right axis.

QuestionApplied: a model reads a text question, then an image cut into 6 by 6 patches, then more text. Under M-RoPE with the image starting at ID 10, what ID does the first text token after the image get, and how many positions would a 1D scheme need for the image?Show answer

The image starts at 10, so its largest height and width IDs are 10+5=1510 + 5 = 15, and its time ID is 10. The largest ID is 15, so the next text token gets 16. A 1D scheme would need 36 positions for the image, running from 10 to 45, so the next text token would start at 46.

Sources