14.2 2D RoPE and M-RoPE
The RoPE of Rotary Position Embeddings knows one coordinate: a token’s index in the sequence. Vision transformer encoder is about to cut an image into a grid of patches and feed them to attention as a flat list. Numbered , the patch directly below another is positions away, and that distance changes with every image width. A model trained at one resolution reads garbage offsets at another, which is exactly what any-resolution models such as Qwen2-VL cannot afford.
Concepts from other sections (11)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- RoPE: rotary position embedding, which rotates queries and keys by position to encode relative position. Read more: 5.3 Model landscape and lifecycle
- logits: unnormalized log-scores that softmax turns into probabilities; here the table stores . Read more: 6.1 Byte bigram: counts, logits, safetensors v0
- frequency ladder: the geometric sequence of angular speeds, from 1 down to about . Read more: 11.4 Positional encodings: sinusoidal, learned
- Complex number: a point of the plane with the multiplication defined by . Read more: 6.2 Trigonometry, rotations, complex numbers, and Euler’s formula in Mathematics
- Dot product: ; it measures how much two vectors point the same way, and it is zero exactly when they are perpendicular. Read more: Chapter 10: Calculus 3 in Mathematics
- Upstream gradient: , the gradient arriving at an op’s output. Read more: 14.3 Matrix differentials, the trace trick, and closed-form VJPs in Mathematics
- Gradient check: a comparison of an analytic gradient against the numerical one that reports pass or fail and the element furthest past its allowance. Read more: 10.1 Partial derivatives, gradients, and gradcheck in Mathematics
- Gradcheck: a comparison of a hand-written gradient against central differences of . Read more: 3.3 Oracles, golden and differential tests, gradcheck as a test in Software Engineering
- central differences: the numerical derivative , used as an oracle for analytic gradients. Read more: 6.3 The op library and gradcheck_all
- Golden test: a comparison against numbers an independent implementation produced and a maintainer recorded. Read more: 3.3 Oracles, golden and differential tests, gradcheck as a test in Software Engineering
- fp16: a 16-bit format with 5 exponent bits and 10 mantissa bits; finer precision but a maximum of 65504 and a smallest subnormal of . Read more: 17.1 Mixed precision, loss scaling, gradient accumulation, and activation checkpointing
A language model that reads images has a second problem: an image of a thousand tokens would consume a thousand 1D positions between the words before it and the words after it. This section fixes both. Axial 2D RoPE rotates by row and by column separately (the ViT’s pos='rope2d'), and Qwen2-VL’s M-RoPE gives every token of the language model three position ids, so an image occupies a small square of positions. The planned vision-language model section’s qwen2vl architecture needs both, bit-compatible with Hugging Face: a tiny Qwen2-VL model’s logits depend on these position ids and rotations.
Everything here rests on the one fact that rotations compose by adding angles, covered in Trigonometry, rotations, complex numbers, and Euler’s formula in Mathematics. You will reuse rope_cos_sin and apply_rope from the RoPE section and the Tensor from Tensor, broadcasting backward, and no_grad. The full solution is rope2d.py and its contract is course/contracts/py/tinyllm/vision/rope2d.pyi.
Learning objectives
- Show that RoPE’s relative-position property holds pair by pair, whatever coordinate drives each pair.
- Build axial 2D RoPE tables for a patch grid, in block-major order under a spatial merge.
- Compute Qwen2-VL’s M-RoPE position ids for a prompt mixing text and images, including where text resumes after an image.
- Map
mrope_sectiononto one frequency ladder and explain why M-RoPE on text is exactly 1D RoPE.
Notation
Section titled “Notation”The symbols below recur throughout.
| Symbol | Meaning | Type / shape |
|---|---|---|
rotary width (d_rot), pairs | int, multiple of 4 for 2D | |
frequency of pair (inv_freq) | float[r/2] (float32[r/4] for 2D) | |
| patch row and column | int64[H·W, 2] | |
| spatial merge: patches become one LM token | int | |
| M-RoPE position ids of each token | int64[3, T] | |
| position at which an image starts | int | |
| cos, sin | one value per pair (the RoPE section’s convention) | float32[·, r/2] |
One rotation, one coordinate
Section titled “One rotation, one coordinate”Write pair of a query as the complex number . Rotating by angle gives , which is . The dot product of two real pairs is . If the query at position rotates by and the key at by , the pair contributes because conjugation negates the key’s angle and angles add. The score is the sum over pairs, so it depends on positions only through .
Notice that this argument is per pair and never asks where a pair’s angle comes from. Everything that follows in this section is just a choice of which coordinate drives which pair.
Axial 2D RoPE for patches
Section titled “Axial 2D RoPE for patches”The first choice is for the vision tower. Give the first pairs the row and the last pairs the column. For , By the per-pair argument, the score of patch against is a function of only, whatever the image size.
The ladder is the 1D ladder for a head of width , used twice: each axis gets the full range of frequencies from 1 down toward . Spreading one ladder over the whole width would give neither axis the right range. must be a multiple of 4. The tables are float32[H·W, r/2], rows first then columns, each half computed by rope_cos_sin, and apply_rope(x, cos, sin, "half") applies them, where pair is . This is Qwen2-VL’s vision tower.
There is one more piece of contract hidden in the tables: row belongs to the -th patch, so the patch order matters. With merge = 1 it is row-major. With merge = m the grid is cut into blocks, blocks in row-major order and row-major inside each block. Qwen2-VL orders patches this way so one block’s patches are adjacent and its PatchMerger can concatenate them into one token.
M-RoPE position ids
Section titled “M-RoPE position ids”The second choice is for the language model, where each token gets three ids. A text token at 1D position gets . An image of patches arrives as image tokens, one per merge block. If it starts at position , token of that grid, in row-major order, gets and the next token starts at .
Two things follow. All ids are offset by , so by the per-pair argument the image looks the same wherever it sits in the prompt. And the image advances the position by its side length, not its token count: a token image costs 32 positions instead of 1024, while the text after it still has ids larger than every row and column id the image used. For a single frame () that is the image’s largest id plus one. The -th run of image tokens belongs to row of image_grid_thw; two images may be adjacent, so runs are split by the expected lengths, not by gaps.
Sections: three axes on one ladder
Section titled “Sections: three axes on one ladder”Having three ids per token, the language model must decide which pairs read which id. It keeps a single ladder over the whole head. mrope_section cuts the pairs into consecutive sections; section takes its position from axis (time, row, column, time, …). With the axis of the section holding pair ,
Qwen2-VL uses sections on a head of 128: time gets the 16 fastest pairs, rows the next 24, columns the slowest 24.
Unlike axial 2D RoPE, each pair keeps its own frequency and only its coordinate changes. So when every pair’s angle is : M-RoPE on text is 1D RoPE, bit for bit, and a qwen2vl model with no image is a plain Qwen2 language model. The sections must sum to .
apply_mrope rotates in the “half” layout through apply_rope. The map is linear in and orthogonal, so its backward is the transpose: the upstream gradient rotated by .
Working it by hand
Section titled “Working it by hand”Start with position ids. The sequence is two text tokens, one image of patches with , then two text tokens: [5, 6, IMG, IMG, IMG, IMG, 8, 9]. The image needs tokens. Text takes positions 0 and 1, so ; the grid runs ; the next token starts at . The resulting ids are:
| Token | 5 | 6 | IMG | IMG | IMG | IMG | 8 | 9 |
|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 2 | 2 | 2 | 2 | 4 | 5 | |
| 0 | 1 | 2 | 2 | 3 | 3 | 4 | 5 | |
| 0 | 1 | 2 | 3 | 2 | 3 | 4 | 5 |
Counting image tokens instead would put token 8 at position 6.
Next, patch order. patch_coords((2, 4), 2) has two blocks side by side: , then . Without a merge, patch_coords((2, 3)) is row-major: .
Finally, one rotation. Take on a grid. The ladder over the half-width 2 has one frequency, , so the patch at has . For patch : , . In the “half” layout holds the pairs and . The row pair rotates by 1 rad to and the column pair by 0, so the result is .
Designing the API
Section titled “Designing the API”Five functions cover both halves:
def patch_coords(grid_hw: tuple[int, int], merge: int = 1) -> NDArray: ... # int64 [H*W, 2]def rope2d_cos_sin(grid_hw, d_rot: int, base: float = 10000.0, merge: int = 1) -> tuple[NDArray, NDArray]: ... # float32 [H*W, d_rot/2] eachdef mrope_position_ids(input_ids, image_grid_thw, image_token_id: int, spatial_merge: int = 2) -> NDArray: ... # int64 [3, T]def mrope_cos_sin(pos3, inv_freq, mrope_section: Sequence[int]) -> tuple[NDArray, NDArray]: ... # float32 [T, r/2]def apply_mrope(x: Tensor, pos3, inv_freq, mrope_section: Sequence[int]) -> Tensor: ... # [..., T, r]image_grid_thw is [n_images, 3] in patches . The functions raise ValueError for or below 1, a merge that does not divide both, d_rot not a positive multiple of 4, image tokens that do not split into runs of exactly the expected lengths (too few, or a grid with no run), pos3 not [3, T], sections that do not sum to len(inv_freq), or an x whose last axis is not 2 * len(inv_freq).
Building it step by step
Section titled “Building it step by step”The design follows from six decisions.
- Reuse the 1D RoPE for every table. The per-pair argument shows that RoPE’s relative-position property holds whatever coordinate drives the pair. So the solution never writes a new rotation: it decides, for each pair, which coordinate and which frequency to use, and hands the resulting angles to
rope_cos_sinandapply_rope. - The patch order is data, so compute it explicitly. Row of every table belongs to patch .
patch_coordsreturns the of each patch in the order the vision tower sees them, and both 2D tables are built from it, so order and tables cannot disagree. - Block-major order is a reshape and a transpose. A row-major grid reshaped to has axes (block row, row in block, block column, column in block). Swapping the middle two gives (block row, block column, row in block, column in block), which flattened is block-major.
- For 2D, one ladder over the half-width, used twice. Each axis must get the full range of frequencies, so the ladder is the one for width , and the table is the row table next to the column table.
- For M-RoPE ids, one scan with a running position. Text increments the position by 1; an image run fills a grid offset by the current position and then advances it by . Runs are consumed by their expected length, so adjacent images split correctly.
- For M-RoPE tables, compute every axis at every frequency, then select.
rope_cos_sinon the[3, T]ids gives[3, T, r/2]; pair then reads axis . With the three slices are identical, so the selection returns exactly the 1D table.
patch_coords implements step 3:
def patch_coords(grid_hw, merge=1): h, w = (int(v) for v in grid_hw) if h < 1 or w < 1 or merge < 1 or h % merge or w % merge: raise ValueError(...) # a merge must tile the grid exactly rows, cols = np.meshgrid(np.arange(h), np.arange(w), indexing="ij") # row-major if merge > 1: # Blocks of m x m patches, block-major: [H/m, m, W/m, m] -> [H/m, W/m, m, m]. shape = (h // merge, merge, w // merge, merge) rows = rows.reshape(shape).transpose(0, 2, 1, 3) cols = cols.reshape(shape).transpose(0, 2, 1, 3) # the same permutation for both coordinates return np.stack([rows.ravel(), cols.ravel()], axis=-1).astype(np.int64)rope2d_cos_sin implements step 4. The ladder is built in float32 to match the Hugging Face vision tower bit for bit:
def rope2d_cos_sin(grid_hw, d_rot, base=10000.0, merge=1): if d_rot < 4 or d_rot % 4: raise ValueError(...) # r/2 pairs split evenly between two axes xy = patch_coords(grid_hw, merge) half = d_rot // 2 # pairs; each axis owns half of them # One ladder for both axes, over the half-width: base^(-2i / half), i.e. r/4 frequencies inv = 1.0 / (base ** (np.arange(0, half, 2, dtype=np.float32) / np.float32(half))) ch, sh = rope_cos_sin(xy[:, 0], inv) # rows drive the first r/4 pairs cw, sw = rope_cos_sin(xy[:, 1], inv) # columns the last r/4 return np.concatenate([ch, cw], axis=-1), np.concatenate([sh, sw], axis=-1)mrope_position_ids implements step 5, with every malformed split rejected:
pos = np.zeros((3, len(ids)), dtype=np.int64)cur, i, img = 0, 0, 0 # next position, token index, image indexwhile i < len(ids): if ids[i] != image_token_id: pos[:, i] = cur # text: (p, p, p) cur += 1 i += 1 continue # ... raise if there is no grid left for this run t, gh, gw = (int(v) for v in grids[img]) # ... raise if the merge does not divide the grid lh, lw = gh // m, gw // m # the token grid after merging n = t * lh * lw if i + n > len(ids) or np.any(ids[i : i + n] != image_token_id): raise ValueError(...) # the run must be exactly n image tokens tt, hh, ww = np.meshgrid(np.arange(t), np.arange(lh), np.arange(lw), indexing="ij") pos[0, i : i + n] = cur + tt.ravel() # every axis offset by the start position pos[1, i : i + n] = cur + hh.ravel() pos[2, i : i + n] = cur + ww.ravel() # Text resumes past the image: its widest spatial extent. cur += max(lh, lw) i += n # consume exactly this image's tokens img += 1if img != len(grids): raise ValueError(...) # a grid with no runmrope_cos_sin implements step 6:
cos, sin = rope_cos_sin(p, f) # [3, T, r/2]: every axis at every frequencyaxis = np.repeat(np.arange(len(sec)) % 3, sec) # which axis drives pair i: t, h, w, t, ...cols = np.arange(len(f))# fancy-index (axis[i], :, i) for each pair i -> [r/2, T], transposed to [T, r/2]return cos[axis, :, cols].T.copy(), sin[axis, :, cols].T.copy()apply_mrope checks the width and calls apply_rope(x, cos, sin, "half"), so the backward is the 1D RoPE backward: the gradient rotated by . The full solution is rope2d.py.
Why this design
Section titled “Why this design”Tables are , built once per image or prompt. mrope_cos_sin computes three times the needed table and discards two thirds, which is cheap next to attention and keeps the code a single gather; the id scan is . Because every rotation goes through the already-tested apply_rope, the only new correctness questions are which coordinate and frequency each pair gets, and those are exactly what the golden comparisons pin.
The two design choices that matter most are axial positions and resuming by side length. Flattened 1D positions make the vertical neighbour away, so offsets change with image width; axial RoPE makes the score a function of at every resolution, at the price of no special treatment for diagonal offsets (the RoPE-Mixed variant learns those). Resuming by token count would burn positions per image; resuming by side length burns and still keeps every later text id above every image id.
What can go wrong
Section titled “What can go wrong”The checks below pin the behaviour against Hugging Face and against the mathematical properties derived above.
| Case | Kind of check | Checks | Why it matters downstream |
|---|---|---|---|
| hand example | unit, smoke | the hand-worked id table and both patch orders, exactly | the resume rule is pinned |
| M-RoPE position ids match HF | golden | get_rope_index on a tall image, a wide one, and a two-frame one directly after it, integer-exact | the planned VLM feeds these ids to every attention layer |
apply_mrope matches HF | golden | queries and keys rotated with sections within of HF’s apply_rotary_pos_emb | the section-to-axis mapping |
| vision RoPE matches HF | golden | block-major positions exact; cos and sin within of the vision rotary tables (HF repeats them to the full head); rotated , within | the rope2d ViT |
| 2D scores depend only on offsets | property | shifting both patches by the same changes a score by less than ; a row step differs from a column step | resolution-independent attention |
| M-RoPE reduces to 1D RoPE on text | property | text-only ids are and the output equals 1D RoPE’s with tolerance 0 | a qwen2vl model without images is Qwen2 |
gradient check, apply_mrope | gradcheck | the backward to against central differences | M-RoPE sits inside trained attention |
| bad inputs rejected | boundary | every ValueError listed in the API | wrong splits fail loudly, not silently |
Warning: Resuming text after an image’s token count instead of its side length rotates every later token too far, and long prompts run out of positions.
mrope_position_idsadvancescurbymax(lh, lw), notn.
Several mistakes leave text-only prompts looking right while breaking images, which is why the golden tests use images. Sections that take their axes in reverse order swap columns and time; axis = np.repeat(np.arange(len(sec)) % 3, sec) keeps time, row, column in order. Image time ids not offset by the start position make every image look like it sits at time 0; all three axes get cur + ....
On the vision side, spreading the 2D ladder over the whole rotary width gives each axis the wrong frequencies and breaks pretrained towers, so rope2d_cos_sin builds inv over half = d_rot // 2. Leaving the coordinates row-major under a merge means the tables no longer line up with the merged patches; patch_coords transposes rows and columns together into block-major order. Putting columns before rows reads the image transposed; the tables concatenate [ch, cw], rows first.
The last group is validation. Accepting a rotary width that is only even produces tables whose width no longer matches the pairs they rotate, so rope2d_cos_sin raises unless d_rot % 4 == 0. Accepting sections that sum to less than leaves trailing pairs with no axis, so mrope_cos_sin raises unless sum(sec) == len(f). And a merge that does not divide the grid would silently floor to a fractional token grid, so patch_coords and mrope_position_ids raise on a remainder.
Where this leads
Section titled “Where this leads”The next section, Vision transformer encoder, uses rope2d_cos_sin for the ViT’s pos='rope2d' to rotate queries and keys. The planned vision-language model section’s qwen2vl architecture will use both halves: rope2d_cos_sin(..., merge=2) in the vision tower, and mrope_position_ids with apply_mrope in the language model.
Production code extends these pieces in a few ways. Hugging Face’s Qwen2VLModel.get_rope_index handles batches with padding and attention masks, and video runs; Qwen2VLRotaryEmbedding with apply_rotary_pos_emb caches cos and sin per batch in fp16; and Qwen2VLVisionRotaryEmbedding builds tables for a batch of images of different sizes in one pass, all in transformers/models/qwen2_vl/modeling_qwen2_vl.py. Qwen2.5-VL ties time ids to absolute seconds so video frame rate can vary. RoPE-Mixed and EVA-02 learn mixed-axis frequencies so diagonal offsets get their own rotations.
Further reading
Section titled “Further reading”- Su et al., “RoFormer: Enhanced transformer with rotary position embedding” (2021).
- Wang et al., “Qwen2-VL: Enhancing vision-language model’s perception of the world at any resolution” (2024), section 2.1.
- Heo et al., “Rotary position embedding for vision transformer” (2024).
- Bai et al., “Qwen2.5-VL technical report” (2025).
- Fang et al., “EVA-02” (2023).
Key terms
Section titled “Key terms”- axial 2D RoPE: rotary embedding where half the pairs rotate by the patch row and half by the column, so scores depend on .
- block-major order: patch ordering in which each merge block’s patches are consecutive, blocks in row-major order.
- M-RoPE: Qwen2-VL’s multimodal RoPE, giving each token three position ids .
mrope_section: the split of a head’s pairs into consecutive sections, each driven by the time, row, or column id.- spatial merge: combining adjacent patches into one language-model token.
Summary
Section titled “Summary”- Axial 2D RoPE gives half the rotary pairs to the row and half to the column, so a score depends only on .
- Both axes share one ladder of frequencies computed over the half-width , and patches are ordered block-major under a merge.
- In M-RoPE every token has three ids ; text sets all three to its position, and the token after an image starts later, not one per image token.
mrope_sectionassigns consecutive pairs of one frequency ladder to the axes ; when the result is exactly 1D RoPE.
Review questions
Section titled “Review questions”-
Why does a text token after a image with starting at get position 4 and not 6?
Answer
The image’s token grid is , so its row and column ids run from 2 to 3. Text resumes at , one past the largest id the image used. Counting its 4 tokens would give 6, wasting positions and rotating every later token further than the pretrained model expects.
-
Why does M-RoPE on text-only input equal 1D RoPE exactly, with tolerance 0?
Answer
Each pair keeps its own frequency and only chooses which axis supplies the position. For text all three axes hold , so every pair’s angle is , the same float32 computation 1D RoPE does, and the selection copies values without arithmetic.
-
Why is the 2D ladder built over instead of ?
Answer
Each axis owns pairs and should see frequencies from 1 down toward , like a head of width . A ladder over would give the row axis only the fast half and the column axis only the slow half of the range, and would not match pretrained vision towers.
-
What does the block-major order buy, and what would break without it?
Answer
It puts each block’s patches next to each other, so the
PatchMergercan concatenate consecutive rows into one token. If the coordinates stayed row-major while the patches were block-major, row of the table would rotate a different patch from the one at position .