4.1.7 · D1Transformer Architecture

Foundations — Rotary positional embeddings (RoPE)

2,832 words13 min readBack to topic

This page assumes you know nothing. We build every symbol the parent note (see the Hinglish version too) leans on, one brick at a time, each brick resting on the one before.


0. What a vector is (the very first brick)

The two numbers are called components. is how far right, is how far up. That is the whole picture — and Figure s01 draws exactly this arrow so you can attach the words to the geometry.

Figure — Rotary positional embeddings (RoPE)

In Figure s01 the blue arrow ends at ; the yellow dashed line is its (rightness) and the red dashed line is its (upness). Whenever this note says "vector," picture that arrow.

Why does RoPE need this? Because a token's meaning is stored as a longer such list (dimension , e.g. 64 numbers). Everything RoPE does is geometry on these arrows.


1. Position indices and

A token is one chunk of the input text (a word or word-piece) that the model processes as a unit.

Picture a row of boxes numbered — each box holds one token.

1b. What queries and keys are


2. Angles, and why we measure them in radians

An angle measures turning. We could use degrees (a full turn = ), but maths and code prefer radians.

Key fact we will reuse: angles wrap. Turning by radians lands you in the same spot as turning by radians, because every extra is one full circle back home.


3. Sine and cosine — the coordinates of a spun point

Put a point on a circle of radius 1 (the unit circle). Sweep an angle counter-clockwise from the positive -axis. Where did the point land?

Figure s02 draws this: the green arc is the swept angle , the yellow segment is (here pointing left, so negative) and the red segment is (pointing up, positive).

Figure — Rotary positional embeddings (RoPE)

Why does RoPE need and ? Because "spinning a vector" means computing where its tip goes on a circle — and / are exactly the machine that reports those new coordinates. Every quadrant is covered by the same two functions:

Quadrant angle range
I to
II to
III to
IV to

Notice (which we will use in the worked check at the end, and which Figure s02 draws) sits in Quadrant II: negative, positive. We will confirm the exact numbers below.


4. Rotating a vector — the matrix that spins arrows

We have an arrow and want a new arrow that is the old one turned by angle , same length, new direction.

Figure s03 shows this in action: the blue arrow is the original , the yellow arrow is , and the green arc is the turn between them — same length, new direction.

Figure — Rotary positional embeddings (RoPE)

Sanity check the four cases:

  • : , the identity — no turn (this is why a token at position 0 comes out unchanged).
  • : — right-arrow becomes up-arrow. Correct.
  • : every arrow flips to its opposite.
  • Negative : turns clockwise.

5. The dot product — how we measure "agreement"

Attention compares a query with a key (both defined in §1b). The comparison tool is the dot product.

Why RoPE needs it: the raw attention score between a query and key is a dot product. If we can make that number depend only on , we have won.


6. The magic property: spins subtract

Here is the fact the whole method rests on.

Now the identity we need:

WHAT we did: took . WHY: because undoes part of 's turn, leaving the net turn . WHAT IT LOOKS LIKE: two arrows each spun on their own dials, but the angle between them only knows the difference of the dial readings.

Set and and you get — the relative position drops out. That single line is RoPE.


7. The frequency schedule

Real vectors have numbers, not 2. RoPE chops them into pairs and spins each pair by its own angle-per-step .

Figure s04 plots against pair index on a log axis: early pairs (small ) sit high — fast dials; later pairs (large ) sink toward zero — slow dials.

Figure — Rotary positional embeddings (RoPE)

Prerequisite map

Vector as arrow

Rotation matrix R theta

Angle in radians

cosine and sine

Position index m n

Position angle m times theta

Dot product q dot k

Spins subtract R beta minus alpha

Relative position emerges

Frequency schedule theta i

Many dials many scales

RoPE

Each foundation on the left feeds the box to its right; all roads end at RoPE, which then powers Multi-Head Attention and rivals ALiBi (Attention with Linear Biases) and Relative Position Bias (T5).


Tiny worked check (spin one key, then dot)

Let us run the machinery on the smallest possible case so every symbol earns its keep. Take a query at position and a key at position , with per-step angle .

Step A — spin the query. At position the angle is , so leaves it alone: . (Recall from §1b that the prime just means "after rotation.")

Step B — spin the key. At position the angle is : Here and (Quadrant II, as the table in §3 predicts), so the first entry is and the second is .

Step C — dot them. The attention score is By §6, this equals — the score depends only on the gap , exactly as promised.

The wrap-around fact from §2 in numbers: radians, confirming that a huge position angle folds back into a familiar small one inside .


Equipment checklist

Test yourself — cover the right side.

  • A vector in 2D is... ::: an ordered pair , drawn as an arrow from the origin.
  • A token is... ::: one chunk of input text (word or word-piece) the model processes as a unit.
  • A query and a key are... ::: two vectors made from a token; ("what I seek") is compared against ("what I offer") to score attention.
  • A prime like means... ::: the vector after we rotated (spun) it, .
  • and mean... ::: the absolute positions (box numbers) of two tokens; is their relative distance.
  • One full turn in radians is... ::: .
  • and are... ::: the horizontal and vertical coordinates of a point spun by on the unit circle.
  • The rotation matrix is... ::: .
  • equals... ::: the identity matrix — no rotation.
  • The dot product equals... ::: , a single number measuring alignment.
  • "Orthogonal matrix" means... ::: ; it preserves lengths and angles, and for rotations .
  • The magic identity is... ::: , so absolute spins cancel and only the difference survives.
  • RoPE pairs coordinates as... ::: adjacent pairs :
  • The frequency schedule is... ::: ; small = fast dial, large = slow dial; must be even.
  • Why RoPE only rotates Q and K... ::: because decides where to attend (position matters); values carry content and stay untouched.