Transformer sequence models modulate attention scores by a function of the relative position between a query and a key: the attention logit between token and token is scaled by a fixed profile . Realizing this modulation the naive way means evaluating once per pair and materializing an adjustment over the whole sequence — quadratic in the sequence length . Rotary Position Embedding (RoPE) Su et al. 2021 avoids this entirely: it rotates the query at position and the key at position independently, each by an angle depending only on its own position, so that the pairwise quantity falls out of the dot product of the two separately rotated vectors — is never evaluated pairwise, and no matrix is ever built. This is what lets RoPE stay a linear, per-token preprocessing step compatible with efficient (sub-quadratic) attention implementations, rather than an modulation. The catch is that RoPE's specific log-linear frequency schedule bakes in one particular profile: a monotone, decaying . That schedule is a poor fit whenever the correlation structure of the data is not monotonically decaying with distance — the leading example being periodicity: in sequential recommendation, interactions separated by exactly one day or one week are more correlated than interactions separated by, say, half a day, and a decaying profile cannot express that "attention comes back" at the period.
Chen, Ainslie, Choromanski et al., ClockRoPE: Random Fourier Rotations for Temporal Routine Modeling (arXiv:2607.26369), ask a more general question first: which attention-modulation profiles can be realized at all by this same per-token, pairwise-iteration-free rotation trick — rotate each token once, on its own, and let the pairwise profile emerge from the dot product — and by what rotation-frequency schedule? Their answer is a random-features construction — sample the rotation frequencies from the kernel's own Fourier transform, rather than fixing them log-linearly — that realizes any continuous, normalized, positive-definite profile in expectation, with a quantified concentration rate, all while keeping the exact same per-token rotate-then-dot-product computation RoPE already uses. ClockRoPE is the periodic instance of this general theory, later deployed in a production-scale generative-retrieval system.
Fix an embedding dimension and group a vector into consecutive feature pairs for . For an angle , let
be the (Givens) rotation matrix. A real kernel is positive definite if for every finite family of points and complex coefficients , has nonnegative real part; it is normalized if . When is also continuous and Lebesgue-integrable, its Fourier transform
is (by Bochner's theorem) a genuine probability density on : this is the distribution the mission's rotation frequencies are sampled from.
Given a query at position , a key at position , and i.i.d. frequencies , the Random Fourier Rotation (RFR) estimator is
is exactly the modulated attention logit computed by rotating query/key feature pairs with per-pair, sampled RoPE frequencies — the same operation standard RoPE performs, but with drawn from instead of fixed by a log-linear schedule.
for every . This is the mission's central target: it upgrades the mean identity below into a quantitative, non-asymptotic guarantee that the sampled estimator is close to the target profile with high probability, at a rate that is exponential in the embedding dimension .
The expectation identity that the concentration bound above sharpens; it is the feasibility half of the claim ("this construction is correct on average") that the convergence half needs as its starting point.
For a continuous, positive-definite, -periodic with and Fourier coefficients ,
The periodic specialization needed to apply the goal and first milestone with a discrete frequency distribution over harmonics — the regime ClockRoPE actually deploys, since daily/weekly routines are periodic rather than merely decaying.
The result gives a general recipe — sample, don't hand-design — for turning any admissible attention-modulation profile into a RoPE-compatible rotation schedule, with a concentration guarantee that says how many feature pairs are needed before the sampled schedule reliably approximates the target profile. Crucially, the recipe changes only which frequencies the per-token rotation uses — it never touches the computational shape of RoPE itself: each query and key is still rotated once, independently, by an angle depending only on its own position, and the target pairwise profile is still recovered purely from the dot product of the two rotated vectors. So realizing an arbitrary positive-definite this way costs exactly what realizing RoPE's own log-linear profile costs — linear in the sequence length, with no pairwise evaluation of and no matrix ever materialized — rather than the quadratic cost a direct, per-pair implementation of an arbitrary modulation function would require. This subsumes standard RoPE's log-linear schedule as one instance and explains, via the periodic corollary, why a schedule built from the kernel's own spectrum (rather than an arbitrary log-linear one) is the right way to encode periodicity: nothing about the construction, or its efficiency, is specific to decay. The paper reports this translated into measured gains in a production-scale generative-retrieval system, which is unusual weight of practical evidence behind a Bochner/Herglotz-style spectral argument.
At the time of writing, none of these three results have a machine-checked proof; this mission asks for the first formalization of all three, together with the shared scaffolding (feature-pair extraction, the rotation estimator, and the notion of a positive-definite kernel) they are stated over.
The natural first attempt at the concentration bound is a direct union bound or a naive variance argument, but the estimator is a sum of terms that are each bounded (each rotated pair lies on a fixed-radius circle) rather than governed by a variance bound that shrinks with under a fixed frequency; the source proof instead applies McDiarmid's bounded-differences inequality, treating each sampled frequency as one coordinate of the input and bounding the one-coordinate change in by via the maximal distance between two points on the unit circle — not by directly bounding a variance term. Establishing Proposition 3.1 itself already requires care: it requires justifying that , defined purely as an integral transform of , is in fact a legitimate probability density (Bochner's theorem), and then a real/complex bookkeeping argument identifying the real inner product of rotated pairs with the real part of a product of complex exponentials.
The mission works over the reals and represents feature pairs as functions
Fin (2 * n) → ℝ sliced into Fin n-indexed pairs, matching the " feature pairs,
dimension " convention used throughout; rotations are ordinary
Matrix (Fin 2) (Fin 2) ℝ values built with Matrix.mulVec/Matrix.dotProduct, and
expectation over i.i.d. -distributed frequencies is formalized as integration
against the product measure MeasureTheory.Measure.pi of n independent copies of
the measure with density (MeasureTheory.Measure.withDensity) — this is
mathematically equivalent to, and more directly usable in Lean than, introducing an
abstract probability space with named i.i.d. random variables.
Since a general continuous positive-definite kernel need not have an
integrable Fourier transform (the periodic case in Corollary 3.3 is exactly the
counterexample: its "Fourier transform" is a discrete measure, not a density) — the
goal and first milestone add Integrable f as an explicit hypothesis beyond what the
paper states in prose, so that is genuinely a density rather than a junk value.
This is not a strengthening of the target profiles the paper cares about in practice
(Gaussian, Laplace, and the cosine/Gaussian priors used by ClockRoPE itself are all
integrable) and mirrors the paper's own split between the density case (Propositions
3.1–3.2) and the discrete, purely-periodic case (Corollary 3.3). A formalization that
dropped this hypothesis and instead let the Fourier integral silently evaluate to
Lean's junk value (0 for non-integrable integrands) would make the goal statement
possible to "prove" vacuously and must be avoided.
Definitions needed: a positive-definite-kernel predicate, the real-valued Fourier transform of a kernel, feature-pair extraction, the rotation matrix, and the RFR estimator itself — all reusable by any future mission formalizing RoPE-family positional encodings (e.g. STRING, nD-RoPE) or other random-Fourier-feature results. McDiarmid's inequality, if not already in Mathlib in the needed form, is itself a independently reusable contribution.
namespace ClockRoPE
open MeasureTheory
/-- **Proposition 3.2 (Convergence of Random Fourier Rotation Estimator)** — corrected against
`IsPositiveDefiniteKernel`, superseding `ClockRoPE.rfr_estimator_concentration`, which used the
under-hypothesized `IsPosDefKernel`.
Under the same setting as `rfr_estimator_unbiased_v2` (a continuous, integrable, positive-definite
kernel `f` with `f 0 = 1`, its Fourier transform `τ`, `n` i.i.d. frequencies `ξ_0, …, ξ_{n-1} ∼
τ`, and query/key vectors `q_m, k_n ∈ ℝ^{2n}` at positions `p_m, p_n`), the averaged estimator
`(1/n) · ĝ(q_m, k_n, p_m, p_n)` concentrates around `(1/n) · q_m^⊤ k_n · f(p_m - p_n)` at a rate
governed by a McDiarmid-type exponential tail bound. -/
theorem rfr_estimator_concentration_v2
(f : ℝ → ℝ) (hf_cont : Continuous f) (hf_int : Integrable f (volume : Measure ℝ))
(hf_pd : IsPositiveDefiniteKernel f) (hf0 : f 0 = 1)
(n : ℕ) (q k : Fin (2 * n) → ℝ) (pm pn : ℝ) (ε : ℝ) (hε : 0 < ε) :
(Measure.pi fun _ : Fin n =>
(volume : Measure ℝ).withDensity fun x => ENNReal.ofReal (fourierTransform f x))
{ξ : Fin n → ℝ |
ε ≤ |(1 / (n : ℝ)) * rfrEstimator n q k pm pn ξ
- (1 / (n : ℝ)) * (dotProduct q k * f (pm - pn))|}
≤ ENNReal.ofReal
(2 * Real.exp (-(ε ^ 2 * (2 * (n : ℝ)) ^ 2 /
(8 * ∑ j : Fin n,
(pairNorm (featurePair n q j) * pairNorm (featurePair n k j)) ^ 2)))) := by
sorry
end ClockRoPE
This is Proposition 3.2 (Convergence of the Random Fourier Rotation Estimator) of Chen et al., ClockRoPE: Random Fourier Rotations for Temporal Routine Modeling (arXiv:2607.26369), §3.2, corrected against IsPositiveDefiniteKernel (Definitions.Def_PositiveDefiniteKernel) and superseding ClockRoPE.rfr_estimator_concentration (theorem_id 64647d10-141e-40a5-a8c5-b9acdc7ad56c, now deprecated), which used the under-hypothesized IsPosDefKernel (see ClockRoPE.rfr_estimator_unbiased_v2 for why that predicate is insufficient).
Under the same setting as Proposition 3.1 (ClockRoPE.rfr_estimator_unbiased_v2) — a continuous, Lebesgue-integrable, positive-definite kernel with , its Fourier transform , i.i.d. frequencies , and a query , key at positions — the theorem gives a quantitative concentration bound for the averaged estimator around its mean. For every :
where are the -th feature pairs of and is the Euclidean norm on . The probability is taken over the same product measure as in Proposition 3.1.
This is the quantitative counterpart to the unbiasedness of Proposition 3.1: it shows the deviation of the sampled estimator from its target value decays exponentially as the embedding dimension grows, at a rate controlled by the magnitudes of the feature pairs.
Formalization Note The probability is represented as the measure, under the -fold product measure of MeasureTheory.Measure.withDensity copies of , of the set of frequency vectors for which the deviation exceeds ; the right-hand side is compared as an extended nonnegative real via ENNReal.ofReal.
No open leaves. Every sub-goal is proved or awaiting decomposition.