Random Fourier Rotation estimator
DefinitionRandomFourierRotationThis bundle defines the Random Fourier Rotation (RFR) estimator of query/key attention logits used throughout the mission, following ClockRoPE §3.2.
Fix a number of feature pairs and work with vectors in , i.e. embedding dimension .
- For an angle ,
rotation2 θis the (Givens) rotation matrix . - For and ,
featurePair n v jis the -th consecutive 2D feature pair . pairNormis the Euclidean norm on , , applied to a feature pair.- Given query and key at positions and frequencies ,
rfrEstimator n q k p_m p_n ξis
This is exactly the modulated attention logit computed by rotating each query/key feature pair by a position- and frequency-dependent angle before taking the dot product — the same operation standard RoPE performs, generalized to an arbitrary per-pair frequency vector rather than a fixed log-linear schedule.
Formalization Note featurePair is defined for j : Fin n against a vector indexed by Fin (2 * n), so the two index-bound side conditions are discharged internally; no sorry or external hypothesis is needed to form a feature pair.
import Mathlib
namespace ClockRoPE
/-- The 2D (Givens) rotation matrix by angle `θ`. -/
noncomputable def rotation2 (θ : ℝ) : Matrix (Fin 2) (Fin 2) ℝ :=
!![Real.cos θ, -Real.sin θ; Real.sin θ, Real.cos θ]
/-- The `j`-th consecutive 2D feature pair `(v (2j), v (2j+1))` of a `2n`-dimensional vector
`v`, grouping its features into `n` disjoint pairs as in RoPE-style rotations. -/
def featurePair (n : ℕ) (v : Fin (2 * n) → ℝ) (j : Fin n) : Fin 2 → ℝ :=
![v ⟨2 * j.1, by have := j.2; omega⟩, v ⟨2 * j.1 + 1, by have := j.2; omega⟩]
/-- The Euclidean norm of a 2D feature pair. -/
noncomputable def pairNorm (v : Fin 2 → ℝ) : ℝ :=
Real.sqrt (v 0 ^ 2 + v 1 ^ 2)
/-- The Random Fourier Rotation estimator `ĝ(q_m, k_n, p_m, p_n)`: for each of the `n` feature
pairs, rotate the query and key pair by the frequency-scaled angles `2πξ_j p_m` and `2πξ_j p_n`
respectively, then sum the pairwise dot products of the rotated pairs. -/
noncomputable def rfrEstimator (n : ℕ) (q k : Fin (2 * n) → ℝ) (pm pn : ℝ) (ξ : Fin n → ℝ) :
ℝ :=
∑ j : Fin n,
dotProduct
(Matrix.mulVec (rotation2 (2 * Real.pi * ξ j * pm)) (featurePair n q j))
(Matrix.mulVec (rotation2 (2 * Real.pi * ξ j * pn)) (featurePair n k j))
end ClockRoPE
Read-back
What the Lean code literally says, in plain math · claude-sonnet-5
rotation2
For a real number , rotation2 θ is the real matrix
with rows and columns indexed by Fin 2 (i.e. by ). No hypothesis is placed on : it ranges over all of , with no restriction to an interval such as .
featurePair
Fix a natural number and a vector , thought of as a function assigning a real value to each index . For ranging over Fin n (i.e. ), featurePair n v j is the 2-element vector
indexed by Fin 2. This is defined for every ; the Lean proof obligation attached to the definition merely certifies that and are valid indices into (both are ) and carries no further mathematical content. When , Fin n is empty, so there is no valid and the function has no inputs to apply to; correspondingly itself has type , the empty (zero-dimensional) domain, carrying no data.
pairNorm
For an arbitrary vector (not required to arise from featurePair, or from any other construction — any pair of reals), pairNorm v is the ordinary Euclidean norm
This definition is stated on its own and is not invoked anywhere inside rfrEstimator below; nothing in the file connects pairNorm to the estimator.
rfrEstimator
Fix ; two vectors (the "query" and "key," each with real coordinates, indexed , with no constraint such as boundedness or normalization); two real numbers (arbitrary reals, not restricted to be nonnegative, integer-valued, or distinct from one another); and a function assigning a real number to each . No hypothesis whatsoever is imposed on — the are arbitrary reals, with no assumption of positivity, distinctness, boundedness, or of being drawn from any probability distribution.
For each , write and for the -th feature pairs of and (as produced by featurePair), and set
The quantity rfrEstimator n q k pm pn ξ is defined as
where is the matrix from rotation2, matrix–vector multiplication is the standard one, and is the componentwise dot product on (Mathlib's generic dotProduct: sum of the products of corresponding entries), i.e. writing and ,
When , Fin n is empty, so the sum defining is a sum over the empty index set and hence equals identically — regardless of the values of , , , , and (all of which are then either the empty function , in the case of , or unconstrained reals, in the case of ). For , the value of is exactly the finite sum above, with no further normalization (e.g. no division by ) and no claim of any relationship to an expectation, an inner product of the un-rotated vectors, or any other target quantity — the statement asserts only the equality of with this explicit finite sum of rotated dot products.