Skip to main content

Module rope

Module rope 

Source
Expand description

Rotary Position Embedding (RoPE) — the STEM definition: a 2-D rotation.

RoPE encodes token position by rotating each adjacent dimension pair (2i, 2i+1) of a head by a position- and frequency-dependent angle

θ_i = (pos / scale) · base^(−2i / head_dim)
(x0, x1) ↦ (x0·cosθ − x1·sinθ,  x0·sinθ + x1·cosθ)

That is exactly a rotation in the (2i, 2i+1) plane — the same rotation a geometric-algebra rotor (super::geometric_algebra) performs — applied per head. It is orthogonal, so it preserves the norm of every pair. Nothing proprietary: it is trigonometry. The LLM runtime’s rope_inplace is an f32 backend computing this same function.

In place on a caller-owned slice; zero allocation.

Functions§

rope_interleaved
Apply interleaved (“normal”/llama) RoPE to vec, treated as n_heads consecutive blocks of head_dim elements. Each block’s adjacent pairs (2i, 2i+1) are rotated by θ_i. pos is the token position, base the RoPE frequency base (e.g. 10000), scale the position scaling (≤ 0 or non-finite is treated as 1).