Skip to main content

cortiq_engine/
nystrom.rs

1//! Nyström (landmark) attention kernel — streaming per-GQA-group runtime
2//! for long-context `attn_type: nystrom` layers.
3//!
4//! Attention splits into an EXACT sliding window (last `w` keys) and a
5//! landmark-skeleton far field sharing ONE joint denominator:
6//!
7//! ```text
8//! out(q_t) = (Σ_{j>t-w} e_j·v_j + F·M·T_far) / (Σ_{j>t-w} e_j + F·M·Z_far)
9//! e_j   = exp(q_t·k_j/√d)                      exact near weights
10//! F_i   = exp(q_t·k̃_i/√d)                      scores vs landmark keys
11//! M     = pinv_reg(exp(Q̃·K̃ᵀ/√d))               fixed after prefill
12//! T_far = Σ_{j≤t-w} exp(Q̃·k_j/√d)·v_jᵀ         [m × dv]
13//! Z_far = Σ_{j≤t-w} exp(Q̃·k_j/√d)              [m]
14//! ```
15//!
16//! exp(q·k) is a PSD kernel, so the UNNORMALIZED skeleton (classic
17//! Nyström/CUR) is legal.  Do NOT row-softmax the factors and do NOT
18//! normalize the key scores over landmarks — both "simplifications"
19//! measurably collapse quality (validated in the torch matrix probes).
20//!
21//! Boundary discipline: key j enters T/Z at the exact step it LEAVES
22//! the window (t = j+w) — delayed insertion, no overlap, no hole; the
23//! near mass stays exact rather than Nyström-estimated.
24//!
25//! Sink tokens (spec §5b, StreamingLLM discipline): the first `sink`
26//! keys of the sequence are PERMANENT exact keys — the near mask is
27//! (t-j < w) OR (j < sink) — and must never enter the far accumulators.
28//! Here they never enter the ring window in the first place (they live
29//! in a dedicated buffer), so delayed insertion cannot see them: no
30//! double count, no gap.  Measured: sinks make the full 28/28-layer
31//! O(1) conversion viable — the default mode.
32//!
33//! Quality of THIS kernel, measured through it (`cortiq ppl --o1 all`,
34//! Qwen3-0.6B, all 28 layers, m=32 W=128 sink=4, wikitext-2 val, 12×512
35//! windows, landmarks frozen at a 256-token prefill): ×1.296 vs exact
36//! attention over the same scored tokens (28.04 vs 21.63).
37//!
38//! The older ×1.177 figure is NOT this operator: it comes from the torch
39//! matrix probe, which (a) rectifies every per-(t,j) weight — impossible
40//! to stream, the weights are never materialized — (b) builds landmarks
41//! from the FULL sequence rather than the prefill, and (c) averages in
42//! the first W positions, which are pure-exact and cost nothing.  Quote
43//! ×1.296 for the runtime; ×1.177 is an upper bound the runtime cannot
44//! reach by construction.
45//!
46//! fp32 numerics: raw exp overflows on real logits, so shifts are
47//! absorbed into diagonals.  T̂[i]/Ẑ[i] live at scale e^{-m_i} with a
48//! per-landmark running max m_i (flash-style rescale on growth); each
49//! token's landmark row uses its own shift f; near and far are brought
50//! to one common scale before the single joint division.
51
52/// Which rectifier keeps the skeleton's estimated far mass non-negative.
53///
54/// pinv(exp(Q̃K̃ᵀ/√d)) is violently ill-conditioned, so M is indefinite
55/// and the raw skeleton estimates negative weights for a large minority
56/// of keys (measured on Qwen3-0.6B: 23.5% of far weights negative,
57/// carrying 24.5% of the absolute far mass).  Unrectified, the joint
58/// denominator goes near-zero/negative and the model collapses (×510).
59///
60/// The matrix probe rectifies every estimated weight — `west =
61/// ((Fu@Mu)@E).clamp_min(0)`.  A STREAMING kernel cannot do that: the
62/// per-(t, j) weights are never materialized, they exist only already
63/// contracted against the accumulators.  Two streaming-legal stand-ins:
64///
65/// MEASURED (Qwen3-0.6B, all 28 layers, W=128, sink=4, wikitext-2 val,
66/// 12×512 windows, landmarks frozen at a 256-token prefill — i.e. the
67/// runtime's real discipline, `cortiq ppl --o1`):
68///
69/// ```text
70///        m=8              m=16             m=32
71/// agg    28.51 (×1.318)   28.82 (×1.332)   28.04 (×1.296)  ← default
72/// fm     28.97 (×1.340)   29.69 (×1.373)   30.58 (×1.414)
73/// ```
74///
75/// `Aggregate` wins at every m, so it stays the default.  `Fm` is kept
76/// selectable because its per-key guarantee is the intuitively "correct"
77/// fix and someone will re-derive it: this table is the evidence that it
78/// costs quality HERE, and the reason is that the guarantee is bought by
79/// destroying signal — clamping a landmark's coefficient zeroes its
80/// contribution to EVERY far key, including the majority where the
81/// weighted sum was already positive and accurate.
82#[derive(Clone, Copy, Debug, PartialEq, Eq)]
83pub enum O1Rect {
84    /// Clamp only the AGGREGATE far denominator: a row whose skeleton
85    /// denominator comes out negative drops its far field entirely.
86    /// Coarse — negative per-key mass survives untouched whenever the
87    /// row sum happens to stay positive — but measured BEST (see above):
88    /// the surviving negatives are apparently error-cancelling, not
89    /// error-causing.
90    Aggregate,
91    /// Clamp FM = F_u·M_u (an m-vector, per query row) at zero.
92    /// ŵ(t,j) = Σ_b FM[b]·E[b,j] and E = exp(·) ≥ 0 ELEMENTWISE, so
93    /// FM ≥ 0 is SUFFICIENT for every far weight to be non-negative —
94    /// a per-key guarantee bought with O(m) work on a vector the row
95    /// already materializes, state untouched.  It is strictly stronger
96    /// than the probe's clamp (a negative landmark is dropped for every
97    /// key, not only where the sum would go negative), so this is a
98    /// DIFFERENT operator, not an emulation of the matrix reference —
99    /// and, measured, a worse one.  Opt in with `--o1-rect fm`.
100    Fm,
101}
102
103/// Ridge factor for the regularized pseudo-inverse of the landmark
104/// kernel: λ = RIDGE_REL · mean(diag(AᵀA)).
105const RIDGE_REL: f64 = 1e-6;
106/// Floor for the joint denominator (mirrors the reference probe).
107const DEN_EPS: f32 = 1e-30;
108/// Prompts of length ≤ w + EXACT_SLACK skip the skeleton entirely:
109/// tiny prefills duplicate segment-mean landmarks (singular Au).
110const EXACT_SLACK: usize = 8;
111
112/// Streaming Nyström attention state for ONE GQA group.
113///
114/// State splits along the GQA grain, because the operator does:
115///
116/// * SHARED per KV group (`NystromGroup`) — the exact window ring, the
117///   sink buffer and the key landmarks K̃.  Under GQA every Q head of a
118///   group reads the SAME k/v rows, so all three are bit-identical
119///   across the group; storing them once per group instead of once per
120///   Q head is the point of this split (identical arithmetic,
121///   ×heads_per_kv less window memory).  K̃ = seg_means(ks, t, d, m_eff)
122///   is a pure function of the group's keys and of `t` (which fixes
123///   m_eff), so it is shareable for the same reason the keys are.
124/// * PRIVATE per Q head (`NystromHead`) — the far accumulators T̂/Ẑ and
125///   their per-landmark running maxima, the QUERY landmarks Q̃, and the
126///   mixing matrix M = pinv(exp(Q̃K̃ᵀ/√d)).  Q̃ is built from that head's
127///   own queries, so M and the far field it drives are per-Q-head and
128///   cannot be shared: the far mass a head accumulates is contracted
129///   against its own query landmarks.
130///
131/// Lifecycle: `new(m, w, sink)` → `prefill(prompt)` once → `step()` per
132/// decode token (single-head façade), or `new_group`/`prefill_group`/
133/// `step_group` for a whole GQA group at once.  All buffers are flat
134/// `Vec<f32>`, row-major; the skeleton path performs no allocations
135/// inside `step()`.
136#[derive(Clone, Debug)]
137pub struct NystromState {
138    group: NystromGroup,
139    heads: Vec<NystromHead>,
140}
141
142/// The part of the state a GQA group shares: everything derived from
143/// the group's KEYS and VALUES alone (see `NystromState`).
144#[derive(Clone, Debug)]
145struct NystromGroup {
146    /// Landmark budget (m) — effective count may be lower (`m_eff`).
147    m: usize,
148    /// Exact-window width in keys.
149    w: usize,
150    /// Permanent exact sink keys at positions 0..sink (spec §5b).
151    sink: usize,
152    d: usize,
153    dv: usize,
154    /// Effective landmark count: clamp(t/8, 4, m) at prefill.  Derived
155    /// from the prompt length, hence equal for every head of the group.
156    m_eff: usize,
157    /// Short-prompt mode: window holds ALL keys, no skeleton.  The
158    /// buffer grows on decode, so this mode may allocate in `step()` —
159    /// acceptable for the ≤ w+8-token degenerate case.
160    exact_only: bool,
161    scale: f32,
162    /// Window keys `[cap][d]` — ring buffer in skeleton mode (cap = w),
163    /// append-only in exact-only mode.
164    win_k: Vec<f32>,
165    /// Window values `[cap][dv]`.
166    win_v: Vec<f32>,
167    win_len: usize,
168    /// Ring slot of the OLDEST window entry (0 while not yet full).
169    win_head: usize,
170    /// Sink keys `[sink_len][d]` — filled once at prefill, immutable.
171    sink_k: Vec<f32>,
172    /// Sink values `[sink_len][dv]`.
173    sink_v: Vec<f32>,
174    /// Number of stored sink tokens (0 in exact-only mode, where every
175    /// key is permanent-exact anyway).
176    sink_len: usize,
177    /// Key landmarks `[m_eff][d]` — segment means of the group's keys.
178    k_tilde: Vec<f32>,
179}
180
181/// The part of the state that is private to one Q head: everything that
182/// touches that head's QUERIES (see `NystromState`).
183#[derive(Clone, Debug)]
184struct NystromHead {
185    /// How the indefinite skeleton is rectified (see `O1Rect`).
186    rect: O1Rect,
187    /// Far numerator `[m_eff][dv]`, stored at scale e^{-m_max[i]}.
188    t_hat: Vec<f32>,
189    /// Far denominator `[m_eff]`, same scale.
190    z_hat: Vec<f32>,
191    /// Per-landmark running max of far logits q̃_i·k_j/√d.
192    m_max: Vec<f32>,
193    /// Number of keys absorbed into the far field.
194    far_len: usize,
195    /// Query landmarks `[m_eff][d]` (segment means of the prefill).
196    q_tilde: Vec<f32>,
197    /// Regularized pseudo-inverse of Au = exp(Q̃·K̃ᵀ/√d), `[m_eff][m_eff]`.
198    mu: Vec<f32>,
199    // Scratch preallocated at prefill so skeleton-mode step() is
200    // allocation-free.  Per head rather than per group: the heads of a
201    // group write it independently, and it is ~0.5 KB.
202    scr_s: Vec<f32>,
203    scr_fh: Vec<f32>,
204    scr_u: Vec<f32>,
205    scr_l: Vec<f32>,
206}
207
208/// Borrowed view of a sealed group's state for the GPU upload — every
209/// slice the device mirror needs, in the layout the kernels index.
210/// `exact_only` groups (degenerate short prompts) are not portable and
211/// make the caller refuse the GPU path for the layer.
212pub struct O1DeviceView<'a> {
213    pub m_eff: usize,
214    pub w: usize,
215    pub sink_len: usize,
216    pub d: usize,
217    pub dv: usize,
218    pub exact_only: bool,
219    pub scale: f32,
220    pub win_len: usize,
221    pub win_head: usize,
222    pub far_len: usize,
223    pub win_k: &'a [f32],
224    pub win_v: &'a [f32],
225    pub sink_k: &'a [f32],
226    pub sink_v: &'a [f32],
227    pub k_tilde: &'a [f32],
228    pub heads: Vec<O1HeadView<'a>>,
229}
230
231pub struct O1HeadView<'a> {
232    pub rect_fm: bool,
233    pub t_hat: &'a [f32],
234    pub z_hat: &'a [f32],
235    pub m_max: &'a [f32],
236    pub q_tilde: &'a [f32],
237    pub mu: &'a [f32],
238}
239
240impl NystromState {
241    pub fn device_view(&self) -> O1DeviceView<'_> {
242        let g = &self.group;
243        O1DeviceView {
244            m_eff: g.m_eff,
245            w: g.w,
246            sink_len: g.sink_len,
247            d: g.d,
248            dv: g.dv,
249            exact_only: g.exact_only,
250            scale: g.scale,
251            win_len: g.win_len,
252            win_head: g.win_head,
253            far_len: self.heads.first().map_or(0, |h| h.far_len),
254            win_k: &g.win_k,
255            win_v: &g.win_v,
256            sink_k: &g.sink_k,
257            sink_v: &g.sink_v,
258            k_tilde: &g.k_tilde,
259            heads: self
260                .heads
261                .iter()
262                .map(|h| O1HeadView {
263                    rect_fm: h.rect == O1Rect::Fm,
264                    t_hat: &h.t_hat,
265                    z_hat: &h.z_hat,
266                    m_max: &h.m_max,
267                    q_tilde: &h.q_tilde,
268                    mu: &h.mu,
269                })
270                .collect(),
271        }
272    }
273}
274
275impl NystromState {
276    /// Single-head state (`heads_per_kv == 1`, and the shape the kernel
277    /// unit tests use).
278    ///
279    /// `m` — landmark budget (≥ 4; see `O1_DEFAULT_M`),
280    /// `w` — exact window width (validated setting is 128),
281    /// `sink` — permanent exact sink keys (validated default is 4;
282    /// 0 reproduces the sink-free kernel bit-for-bit).
283    /// Rectifier defaults to `O1_DEFAULT_RECT`; override with
284    /// `with_rect` (the golden-parity test pins it explicitly).
285    pub fn new(m: usize, w: usize, sink: usize) -> Self {
286        Self::new_group(m, w, sink, 1)
287    }
288
289    /// State for one GQA group of `q_heads` query heads sharing a KV
290    /// head.  The window/sink/K̃ are stored ONCE for the group; each Q
291    /// head keeps its own far field, Q̃ and M.
292    pub fn new_group(m: usize, w: usize, sink: usize, q_heads: usize) -> Self {
293        assert!(m >= 4, "landmark budget must be at least 4");
294        assert!(w >= 1, "window must hold at least one key");
295        assert!(q_heads >= 1, "a GQA group needs at least one query head");
296        NystromState {
297            group: NystromGroup {
298                m,
299                w,
300                sink,
301                d: 0,
302                dv: 0,
303                m_eff: 0,
304                exact_only: true,
305                scale: 0.0,
306                win_k: Vec::new(),
307                win_v: Vec::new(),
308                win_len: 0,
309                win_head: 0,
310                sink_k: Vec::new(),
311                sink_v: Vec::new(),
312                sink_len: 0,
313                k_tilde: Vec::new(),
314            },
315            heads: (0..q_heads).map(|_| NystromHead::new()).collect(),
316        }
317    }
318
319    /// Select the skeleton rectifier for every head of the group
320    /// (builder; see `O1Rect`).
321    pub fn with_rect(mut self, rect: O1Rect) -> Self {
322        for h in &mut self.heads {
323            h.rect = rect;
324        }
325        self
326    }
327
328    /// Query heads in this group.
329    pub fn num_q_heads(&self) -> usize {
330        self.heads.len()
331    }
332
333    /// Keys absorbed into head `head`'s far field.  Exposed for the
334    /// delayed-insertion invariant test: eviction is a GROUP event, but
335    /// each head must absorb the evicted key EXACTLY once, so this must
336    /// equal the number of evictions — never a multiple of it.
337    pub fn far_len(&self, head: usize) -> usize {
338        self.heads[head].far_len
339    }
340
341    /// Absorb the whole prompt for a single-head state — see
342    /// `prefill_group`.
343    pub fn prefill(&mut self, qs: &[f32], ks: &[f32], vs: &[f32], t: usize, d: usize, dv: usize) {
344        assert_eq!(self.heads.len(), 1, "use prefill_group for a GQA group");
345        self.prefill_group(&[qs], ks, vs, t, d, dv);
346    }
347
348    /// Absorb the whole prompt for a GQA group: freeze each head's
349    /// landmarks and M, then replay the prompt through the step() state
350    /// semantics (window fill + delayed far insertion).  `qs[h]` is that
351    /// head's `[t][d]` query block; `ks` is `[t][d]` and `vs` is
352    /// `[t][dv]` — the group's shared keys/values, row-major.
353    pub fn prefill_group(
354        &mut self,
355        qs: &[&[f32]],
356        ks: &[f32],
357        vs: &[f32],
358        t: usize,
359        d: usize,
360        dv: usize,
361    ) {
362        assert_eq!(qs.len(), self.heads.len(), "one query block per head");
363        for q in qs {
364            assert_eq!(q.len(), t * d);
365        }
366        assert_eq!(ks.len(), t * d);
367        assert_eq!(vs.len(), t * dv);
368
369        let Some(k_tilde64) = self.group.prefill_shared(ks, vs, t, d, dv) else {
370            // exact-only: no skeleton, no far field — nothing per head
371            // beyond the score scratch.
372            for h in &mut self.heads {
373                h.seal_exact(t);
374            }
375            return;
376        };
377        for (h, q) in self.heads.iter_mut().zip(qs) {
378            h.seal(&self.group, q, t, &k_tilde64);
379        }
380        // Replay the post-sink prompt ONCE for the group: each key
381        // enters the shared window, evicting the (j-w)-th into every
382        // head's far field.
383        for j in self.group.sink..t {
384            Self::advance(
385                &mut self.group,
386                &mut self.heads,
387                &ks[j * d..(j + 1) * d],
388                &vs[j * dv..(j + 1) * dv],
389            );
390        }
391    }
392
393    /// One decode step for a single-head state — see `step_group`.
394    pub fn step(&mut self, q: &[f32], k: &[f32], v: &[f32], out: &mut [f32]) {
395        assert_eq!(self.heads.len(), 1, "use step_group for a GQA group");
396        self.step_group(q, k, v, out);
397    }
398
399    /// One decode step for the whole GQA group.  Inserts the group's
400    /// (k, v) ONCE, evicting the oldest window key into every head's far
401    /// accumulators, then writes each head's attention output.
402    /// `q_all` is `[q_heads][d]`, `out_all` is `[q_heads][dv]`.
403    pub fn step_group(&mut self, q_all: &[f32], k: &[f32], v: &[f32], out_all: &mut [f32]) {
404        let (d, dv) = (self.group.d, self.group.dv);
405        assert!(d > 0, "prefill() must run before step()");
406        let nh = self.heads.len();
407        assert_eq!(q_all.len(), nh * d);
408        assert_eq!(k.len(), d);
409        assert_eq!(v.len(), dv);
410        assert_eq!(out_all.len(), nh * dv);
411        // The current token is part of its own near window (t-j = 0),
412        // so insertion happens BEFORE any output is computed.
413        Self::advance(&mut self.group, &mut self.heads, k, v);
414        for (h, head) in self.heads.iter_mut().enumerate() {
415            head.step(
416                &self.group,
417                &q_all[h * d..(h + 1) * d],
418                &mut out_all[h * dv..(h + 1) * dv],
419            );
420        }
421    }
422
423    /// Heap bytes held by this group's state (shared window + sinks +
424    /// K̃, plus each head's skeleton and scratch) — feeds the honest
425    /// "KV+state" memory line, same discipline as counting
426    /// `linear_state` for the linear core.
427    pub fn memory_bytes(&self) -> usize {
428        self.group.memory_bytes()
429            + self
430                .heads
431                .iter()
432                .map(NystromHead::memory_bytes)
433                .sum::<usize>()
434    }
435
436    /// Push the group's (k, v) into the shared window.  In skeleton mode
437    /// a full ring first evicts its oldest key (delayed insertion — the
438    /// key leaves the exact window at this very step).
439    ///
440    /// The eviction is a GROUP event: the window is shared, so there is
441    /// exactly ONE eviction per position, not one per Q head.  The far
442    /// accumulators are per head, though, so that single evicted key is
443    /// absorbed once into EACH head — one eviction, `q_heads`
444    /// insertions.  Getting this wrong in either direction breaks the
445    /// boundary invariant (a key enters the far field at exactly the
446    /// step it leaves the window: no double count, no hole).
447    fn advance(g: &mut NystromGroup, heads: &mut [NystromHead], k: &[f32], v: &[f32]) {
448        let (d, dv) = (g.d, g.dv);
449        if !g.exact_only && g.win_len == g.w {
450            let slot = g.win_head;
451            // Every head absorbs the outgoing key BEFORE the slot is
452            // overwritten by the incoming one.
453            for h in heads.iter_mut() {
454                h.far_insert(g, slot);
455            }
456            g.win_k[slot * d..(slot + 1) * d].copy_from_slice(k);
457            g.win_v[slot * dv..(slot + 1) * dv].copy_from_slice(v);
458            g.win_head = (g.win_head + 1) % g.w;
459        } else if g.exact_only {
460            g.win_k.extend_from_slice(k);
461            g.win_v.extend_from_slice(v);
462            g.win_len += 1;
463        } else {
464            g.win_k[g.win_len * d..(g.win_len + 1) * d].copy_from_slice(k);
465            g.win_v[g.win_len * dv..(g.win_len + 1) * dv].copy_from_slice(v);
466            g.win_len += 1;
467        }
468    }
469}
470
471impl NystromGroup {
472    /// Freeze the group-shared geometry from the prompt's keys/values.
473    /// Returns the f64 key landmarks (which the heads need at full
474    /// precision to build Au), or None in exact-only mode.
475    fn prefill_shared(
476        &mut self,
477        ks: &[f32],
478        vs: &[f32],
479        t: usize,
480        d: usize,
481        dv: usize,
482    ) -> Option<Vec<f64>> {
483        self.d = d;
484        self.dv = dv;
485        self.scale = 1.0 / (d as f32).sqrt();
486        self.win_len = 0;
487        self.win_head = 0;
488        self.sink_len = 0;
489        self.exact_only = t <= self.w + self.sink + EXACT_SLACK;
490
491        if self.exact_only {
492            // Everything fits in the exact window (plus slack for a few
493            // decode steps before Vec growth); no skeleton is built and
494            // no separate sink buffer is needed — every key is already
495            // a permanent exact key in this mode.
496            self.win_k = Vec::with_capacity((t + 64) * d);
497            self.win_v = Vec::with_capacity((t + 64) * dv);
498            self.win_k.extend_from_slice(ks);
499            self.win_v.extend_from_slice(vs);
500            self.win_len = t;
501            return None;
502        }
503
504        // Sink tokens: positions 0..sink become permanent exact keys.
505        // They bypass the ring window entirely, so the delayed-insertion
506        // path can never move them into the far accumulators.
507        self.sink_len = self.sink; // skeleton mode guarantees t > sink
508        self.sink_k = ks[..self.sink * d].to_vec();
509        self.sink_v = vs[..self.sink * dv].to_vec();
510
511        // Landmarks: contiguous segment means of the prompt.  The
512        // integer split (i·t)/m matches the reference probe; the clamp
513        // keeps tiny prompts from producing duplicate landmarks.
514        let m_eff = (t / 8).clamp(4, self.m);
515        // Say so when the budget asked for is not the budget used. A
516        // prefill of 256 caps m_eff at 32, so `--o1-m 64`, `128` and
517        // `256` all run as 32 and report perplexities identical to the
518        // last digit — which reads as a saturating method rather than a
519        // clamp, and cost a sweep before it was noticed. This file's own
520        // discipline is that a file is either valid or open() fails
521        // loudly; a flag that silently does nothing is the same defect
522        // one level up.
523        if m_eff < self.m {
524            use std::sync::atomic::{AtomicBool, Ordering};
525            static SAID: AtomicBool = AtomicBool::new(false);
526            if !SAID.swap(true, Ordering::Relaxed) {
527                tracing::warn!(
528                    "o1: landmark budget m={} clamped to m_eff={} — the prefill is {t} tokens \
529                     and the skeleton takes t/8. Prefill at least {} tokens to use the budget \
530                     you asked for.",
531                    self.m,
532                    m_eff,
533                    self.m * 8
534                );
535            }
536        }
537        self.m_eff = m_eff;
538        let k_tilde64 = seg_means(ks, t, d, m_eff);
539        self.k_tilde = k_tilde64.iter().map(|&x| x as f32).collect();
540
541        self.win_k = vec![0.0; self.w * d];
542        self.win_v = vec![0.0; self.w * dv];
543        Some(k_tilde64)
544    }
545
546    fn memory_bytes(&self) -> usize {
547        (self.win_k.len()
548            + self.win_v.len()
549            + self.sink_k.len()
550            + self.sink_v.len()
551            + self.k_tilde.len())
552            * std::mem::size_of::<f32>()
553    }
554}
555
556impl NystromHead {
557    fn new() -> Self {
558        NystromHead {
559            rect: O1_DEFAULT_RECT,
560            t_hat: Vec::new(),
561            z_hat: Vec::new(),
562            m_max: Vec::new(),
563            far_len: 0,
564            q_tilde: Vec::new(),
565            mu: Vec::new(),
566            scr_s: Vec::new(),
567            scr_fh: Vec::new(),
568            scr_u: Vec::new(),
569            scr_l: Vec::new(),
570        }
571    }
572
573    /// exact-only mode: no skeleton state at all, just room to score the
574    /// growing window.
575    fn seal_exact(&mut self, t: usize) {
576        self.far_len = 0;
577        self.scr_s = Vec::with_capacity(t + 64);
578    }
579
580    /// Freeze this head's query landmarks and mixing matrix against the
581    /// group's (already frozen) key landmarks.
582    fn seal(&mut self, g: &NystromGroup, qs: &[f32], t: usize, k_tilde64: &[f64]) {
583        let (d, dv, m_eff) = (g.d, g.dv, g.m_eff);
584        self.far_len = 0;
585        let q_tilde64 = seg_means(qs, t, d, m_eff);
586        self.q_tilde = q_tilde64.iter().map(|&x| x as f32).collect();
587
588        // Au and its regularized pseudo-inverse in f64 — one-off m×m
589        // work at prefill only; the hot path stays f32.
590        let mut au = vec![0.0f64; m_eff * m_eff];
591        for i in 0..m_eff {
592            for j in 0..m_eff {
593                let mut s = 0.0f64;
594                for c in 0..d {
595                    s += q_tilde64[i * d + c] * k_tilde64[j * d + c];
596                }
597                au[i * m_eff + j] = (s * g.scale as f64).exp();
598            }
599        }
600        let mu64 = ridge_pinv(&au, m_eff);
601        self.mu = mu64.iter().map(|&x| x as f32).collect();
602
603        self.t_hat = vec![0.0; m_eff * dv];
604        self.z_hat = vec![0.0; m_eff];
605        self.m_max = vec![f32::NEG_INFINITY; m_eff];
606        self.scr_s = vec![0.0; g.sink + g.w];
607        self.scr_fh = vec![0.0; m_eff];
608        self.scr_u = vec![0.0; m_eff];
609        self.scr_l = vec![0.0; m_eff];
610    }
611
612    /// This head's output for `q` against the group's current window and
613    /// sinks and its own far field.  The window insertion for this
614    /// position already happened at group level (`NystromState::advance`).
615    fn step(&mut self, g: &NystromGroup, q: &[f32], out: &mut [f32]) {
616        let (d, dv) = (g.d, g.dv);
617        assert_eq!(q.len(), d);
618        assert_eq!(out.len(), dv);
619
620        // Near field: exact logits over sinks + window, one shared
621        // shift.  Sinks are permanent exact keys (near mask §5b:
622        // t-j < w OR j < sink); sink_len = 0 in exact-only mode.
623        let ns = g.sink_len;
624        let n = ns + g.win_len;
625        self.scr_s.resize(n, 0.0);
626        let mut c = f32::NEG_INFINITY;
627        for s in 0..ns {
628            let lg = dot(q, &g.sink_k[s * d..(s + 1) * d]) * g.scale;
629            self.scr_s[s] = lg;
630            c = c.max(lg);
631        }
632        // Window scores are the decode hot loop — NEON dot (same
633        // products, regrouped sums; parity-gated by the golden tests).
634        for s in 0..g.win_len {
635            let lg = crate::attention::dot_f32(q, &g.win_k[s * d..(s + 1) * d]) * g.scale;
636            self.scr_s[ns + s] = lg;
637            c = c.max(lg);
638        }
639
640        // Far field: shifted skeleton (spec §3).  All exp arguments are
641        // ≤ 0 relative to the joint shift c_all, so nothing overflows.
642        let mut far_den = 0.0f32;
643        let mut c_all = c;
644        let mut have_far = false;
645        if self.far_len > 0 {
646            // Per-token row shift f over landmark scores.
647            let mut f = f32::NEG_INFINITY;
648            for a in 0..g.m_eff {
649                let s = crate::attention::dot_f32(q, &g.k_tilde[a * d..(a + 1) * d]) * g.scale;
650                self.scr_fh[a] = s;
651                f = f.max(s);
652            }
653            for a in 0..g.m_eff {
654                self.scr_fh[a] = (self.scr_fh[a] - f).exp();
655            }
656            // u = (F·e^{-f}) · M — the landmark mixing row (= FM, up to
657            // the positive factor e^{-f}).
658            for b in 0..g.m_eff {
659                let mut s = 0.0f32;
660                for a in 0..g.m_eff {
661                    s += self.scr_fh[a] * self.mu[a * g.m_eff + b];
662                }
663                // FM rectifier: every far weight is Σ_b FM[b]·E[b,j]
664                // with E ≥ 0 elementwise, so clamping this m-vector is
665                // enough to make all of them non-negative — the per-key
666                // guarantee the streaming form otherwise cannot state.
667                // The row shift e^{-f} and the flash factors below are
668                // strictly positive, so clamping here or after the
669                // rescale is the same predicate.
670                self.scr_u[b] = if self.rect == O1Rect::Fm {
671                    s.max(0.0)
672                } else {
673                    s
674                };
675            }
676            // Joint scale: the far term b carries e^{f + m_max[b]}, the
677            // near term e^{c}; take the max so every factor is ≤ 1.
678            for b in 0..g.m_eff {
679                c_all = c_all.max(f + self.m_max[b]);
680            }
681            for b in 0..g.m_eff {
682                let gain = self.scr_u[b] * (f + self.m_max[b] - c_all).exp();
683                self.scr_u[b] = gain;
684                far_den += gain * self.z_hat[b];
685            }
686            // Aggregate guard — the rectifier of `O1Rect::Aggregate`,
687            // and a second line of defence under `Fm` (where far_den is
688            // a sum of non-negative terms, so this can only fire on
689            // rounding): a negative denominator means the skeleton
690            // estimate is unusable for this row — drop the far field.
691            if far_den >= 0.0 {
692                have_far = true;
693            } else {
694                far_den = 0.0;
695            }
696        }
697
698        for o in out.iter_mut() {
699            *o = 0.0;
700        }
701        if have_far {
702            for b in 0..g.m_eff {
703                crate::attention::axpy_f32(out, &self.t_hat[b * dv..(b + 1) * dv], self.scr_u[b]);
704            }
705        }
706        let mut den = far_den;
707        for s in 0..n {
708            let p = (self.scr_s[s] - c_all).exp();
709            den += p;
710            // scr_s rows 0..ns are sinks, the rest are window entries.
711            let vv = if s < ns {
712                &g.sink_v[s * dv..(s + 1) * dv]
713            } else {
714                &g.win_v[(s - ns) * dv..(s - ns + 1) * dv]
715            };
716            crate::attention::axpy_f32(out, vv, p);
717        }
718        let den = den.max(DEN_EPS);
719        for o in out.iter_mut() {
720            *o /= den;
721        }
722    }
723
724    /// Absorb the group's window slot into THIS head's far accumulators
725    /// with the per-landmark flash shift: T̂[i]/Ẑ[i] live at scale
726    /// e^{-m_max[i]}; when a new logit raises the max, existing mass is
727    /// rescaled by e^{old-new} (exactly 0 on first insertion, since
728    /// m_max = -inf).
729    fn far_insert(&mut self, g: &NystromGroup, slot: usize) {
730        let (d, dv) = (g.d, g.dv);
731        // Runs once per evicted key per head — NEON dot/axpy like the
732        // decode loop (same products, regrouped sums).
733        for i in 0..g.m_eff {
734            self.scr_l[i] = crate::attention::dot_f32(
735                &self.q_tilde[i * d..(i + 1) * d],
736                &g.win_k[slot * d..(slot + 1) * d],
737            ) * g.scale;
738        }
739        for i in 0..g.m_eff {
740            let l = self.scr_l[i];
741            if l > self.m_max[i] {
742                let r = (self.m_max[i] - l).exp();
743                self.z_hat[i] *= r;
744                for e in self.t_hat[i * dv..(i + 1) * dv].iter_mut() {
745                    *e *= r;
746                }
747                self.m_max[i] = l;
748            }
749            let e = (l - self.m_max[i]).exp();
750            self.z_hat[i] += e;
751            crate::attention::axpy_f32(
752                &mut self.t_hat[i * dv..(i + 1) * dv],
753                &g.win_v[slot * dv..(slot + 1) * dv],
754                e,
755            );
756        }
757        self.far_len += 1;
758    }
759
760    fn memory_bytes(&self) -> usize {
761        (self.t_hat.len()
762            + self.z_hat.len()
763            + self.m_max.len()
764            + self.q_tilde.len()
765            + self.mu.len()
766            + self.scr_s.len()
767            + self.scr_fh.len()
768            + self.scr_u.len()
769            + self.scr_l.len())
770            * std::mem::size_of::<f32>()
771    }
772}
773
774/// Contiguous segment means (the Nyströmformer landmark recipe), f64
775/// accumulation.  The split (i·t)/m matches the Python reference.
776fn seg_means(xs: &[f32], t: usize, d: usize, m: usize) -> Vec<f64> {
777    let mut out = vec![0.0f64; m * d];
778    for i in 0..m {
779        let lo = i * t / m;
780        let hi = (i + 1) * t / m;
781        for j in lo..hi {
782            for c in 0..d {
783                out[i * d + c] += xs[j * d + c] as f64;
784            }
785        }
786        let inv = 1.0 / (hi - lo) as f64;
787        for c in 0..d {
788            out[i * d + c] *= inv;
789        }
790    }
791    out
792}
793
794fn dot(a: &[f32], b: &[f32]) -> f32 {
795    let mut s = 0.0f32;
796    for (x, y) in a.iter().zip(b) {
797        s += x * y;
798    }
799    s
800}
801
802/// Regularized pseudo-inverse M = (AᵀA + λI)⁻¹ Aᵀ of a square matrix,
803/// λ = RIDGE_REL·mean(diag(AᵀA)), solved via Cholesky.  f64 internal —
804/// this runs once per prefill on an m×m matrix (m ≤ 32).  If Cholesky
805/// fails (Au numerically singular despite the m_eff clamp), λ grows
806/// tenfold — the jitter fallback of the reference probe.
807/// pub(crate): the FCD polish trainer builds its (constant-in-backward)
808/// mixing matrix with the SAME solver the runtime seals with.
809pub(crate) fn ridge_pinv(a: &[f64], n: usize) -> Vec<f64> {
810    let mut ata = vec![0.0f64; n * n];
811    for i in 0..n {
812        for j in 0..n {
813            let mut s = 0.0;
814            for k in 0..n {
815                s += a[k * n + i] * a[k * n + j];
816            }
817            ata[i * n + j] = s;
818        }
819    }
820    let mean_diag: f64 = (0..n).map(|i| ata[i * n + i]).sum::<f64>() / n as f64;
821    let mut lambda = RIDGE_REL * mean_diag.max(f64::MIN_POSITIVE);
822    for _ in 0..12 {
823        let mut g = ata.clone();
824        for i in 0..n {
825            g[i * n + i] += lambda;
826        }
827        if let Some(l) = cholesky(&mut g, n) {
828            // Solve G·M = Aᵀ column by column; column j of Aᵀ is row j
829            // of A.
830            let mut m_out = vec![0.0f64; n * n];
831            let mut x = vec![0.0f64; n];
832            for j in 0..n {
833                let rhs = &a[j * n..(j + 1) * n];
834                // Forward: L·y = rhs.
835                for i in 0..n {
836                    let mut s = rhs[i];
837                    for k in 0..i {
838                        s -= l[i * n + k] * x[k];
839                    }
840                    x[i] = s / l[i * n + i];
841                }
842                // Backward: Lᵀ·x = y.
843                for i in (0..n).rev() {
844                    let mut s = x[i];
845                    for k in i + 1..n {
846                        s -= l[k * n + i] * x[k];
847                    }
848                    x[i] = s / l[i * n + i];
849                }
850                for i in 0..n {
851                    m_out[i * n + j] = x[i];
852                }
853            }
854            return m_out;
855        }
856        lambda *= 10.0;
857    }
858    // Unreachable in practice: λ eventually dominates the diagonal.
859    // Degrade to a scaled identity rather than poison the output.
860    let mut fallback = vec![0.0f64; n * n];
861    for i in 0..n {
862        fallback[i * n + i] = 1.0 / mean_diag.max(f64::MIN_POSITIVE);
863    }
864    fallback
865}
866
867// ── Runtime configuration (v1: runtime-level, NOT a format change) ──
868//
869// A layer set + {m, w, sink}, resolved in priority order:
870//   1. CLI flag (`--o1` on run/serve/bench) — explicit user intent;
871//   2. env `CMF_O1` (all | deepN | i,j,k | off) with CMF_O1_M /
872//      CMF_O1_WINDOW / CMF_O1_SINK parameter overrides;
873//   3. converter hint in the header JSON (`provenance.o1_attn`,
874//      written by `cortiq convert --o1`) — additive metadata, the
875//      binary envelope is untouched.
876
877/// Validated defaults (spec: m=32, W=128, sink=4; sink ablation ×2.39).
878pub const O1_DEFAULT_M: usize = 32;
879pub const O1_DEFAULT_W: usize = 128;
880pub const O1_DEFAULT_SINK: usize = 4;
881/// Rectifier default (see `O1Rect`).
882pub const O1_DEFAULT_RECT: O1Rect = O1Rect::Aggregate;
883
884/// Which layers run the O(1) kernel.
885#[derive(Clone, Debug, PartialEq, Eq)]
886pub enum O1Layers {
887    All,
888    /// The N deepest layers (deep-N ladder of the price map; the
889    /// early stack is the most sink-dependent, depth converts best).
890    Deep(usize),
891    /// Explicit layer indices.
892    List(Vec<usize>),
893}
894
895/// Per-model O(1)-attention setting.
896#[derive(Clone, Debug)]
897pub struct O1Cfg {
898    pub layers: O1Layers,
899    /// Landmark budget (≥ 4; m=64 measured WORSE — collinear segment
900    /// means poison the pinv, so don't "help" by raising it).
901    pub m: usize,
902    /// Exact-window width — the main quality lever.
903    pub w: usize,
904    /// Permanent exact sink keys (StreamingLLM discipline, spec §5b).
905    pub sink: usize,
906    /// Skeleton rectifier (see `O1Rect`).
907    pub rect: O1Rect,
908}
909
910/// Three-state env reading: unset falls through to the header hint,
911/// `off`/`0` force-disables even a header hint (the escape hatch).
912pub enum O1Env {
913    Unset,
914    Off,
915    On(O1Cfg),
916}
917
918impl O1Cfg {
919    /// Parse a layer spec: `all` | `deepN` | `i,j,k`. None = not a spec
920    /// (also used for `off`/`0`/empty).
921    pub fn parse_layers(spec: &str) -> Option<O1Layers> {
922        let s = spec.trim();
923        match s {
924            "" | "off" | "0" | "none" => None,
925            "all" => Some(O1Layers::All),
926            _ => {
927                if let Some(n) = s.strip_prefix("deep") {
928                    return n
929                        .parse::<usize>()
930                        .ok()
931                        .filter(|&n| n > 0)
932                        .map(O1Layers::Deep);
933                }
934                let idx: Result<Vec<usize>, _> =
935                    s.split(',').map(|p| p.trim().parse::<usize>()).collect();
936                idx.ok().filter(|v| !v.is_empty()).map(O1Layers::List)
937            }
938        }
939    }
940
941    /// Parse a rectifier spec: `agg`/`aggregate` | `fm`. None = not a
942    /// spec.
943    pub fn parse_rect(spec: &str) -> Option<O1Rect> {
944        match spec.trim() {
945            "agg" | "aggregate" => Some(O1Rect::Aggregate),
946            "fm" => Some(O1Rect::Fm),
947            _ => None,
948        }
949    }
950
951    /// Rectifier from an explicit value, else `CMF_O1_RECT`, else the
952    /// default.
953    fn rect_or_env(rect: Option<O1Rect>) -> O1Rect {
954        rect.or_else(|| {
955            std::env::var("CMF_O1_RECT")
956                .ok()
957                .as_deref()
958                .and_then(Self::parse_rect)
959        })
960        .unwrap_or(O1_DEFAULT_RECT)
961    }
962
963    /// Build from an explicit spec (CLI path). None = `off` or malformed.
964    /// Explicit m/w/sink/rect beat env overrides beat validated defaults.
965    pub fn from_spec(
966        spec: &str,
967        m: Option<usize>,
968        w: Option<usize>,
969        sink: Option<usize>,
970        rect: Option<O1Rect>,
971    ) -> Option<O1Cfg> {
972        let layers = Self::parse_layers(spec)?;
973        let env = |k: &str| std::env::var(k).ok().and_then(|v| v.parse::<usize>().ok());
974        Some(O1Cfg {
975            layers,
976            // NystromState asserts m ≥ 4 and w ≥ 1 — clamp rather than
977            // panic deep in the first prefill.
978            m: m.or_else(|| env("CMF_O1_M")).unwrap_or(O1_DEFAULT_M).max(4),
979            w: w.or_else(|| env("CMF_O1_WINDOW"))
980                .unwrap_or(O1_DEFAULT_W)
981                .max(1),
982            sink: sink
983                .or_else(|| env("CMF_O1_SINK"))
984                .unwrap_or(O1_DEFAULT_SINK),
985            rect: Self::rect_or_env(rect),
986        })
987    }
988
989    /// Converter hint from the header JSON: `{"layers": "all"|[i,…],
990    /// "m": …, "w": …, "sink": …}`. Env parameter overrides still apply
991    /// (the operator's knob wins over the file's suggestion).
992    pub fn from_json(v: &serde_json::Value) -> Option<O1Cfg> {
993        let layers = match v.get("layers") {
994            Some(serde_json::Value::String(s)) => Self::parse_layers(s)?,
995            Some(serde_json::Value::Array(a)) => O1Layers::List(
996                a.iter()
997                    .filter_map(|x| x.as_u64().map(|n| n as usize))
998                    .collect(),
999            ),
1000            _ => return None,
1001        };
1002        let f = |k: &str| v.get(k).and_then(|x| x.as_u64()).map(|n| n as usize);
1003        let env = |k: &str| std::env::var(k).ok().and_then(|s| s.parse::<usize>().ok());
1004        Some(O1Cfg {
1005            layers,
1006            m: env("CMF_O1_M")
1007                .or_else(|| f("m"))
1008                .unwrap_or(O1_DEFAULT_M)
1009                .max(4),
1010            w: env("CMF_O1_WINDOW")
1011                .or_else(|| f("w"))
1012                .unwrap_or(O1_DEFAULT_W)
1013                .max(1),
1014            sink: env("CMF_O1_SINK")
1015                .or_else(|| f("sink"))
1016                .unwrap_or(O1_DEFAULT_SINK),
1017            // The rectifier is a runtime property of the kernel, not a
1018            // property of the weights — a file hint cannot pin it.
1019            rect: Self::rect_or_env(None),
1020        })
1021    }
1022
1023    /// Per-layer flags over `num_layers` (indices past the end are
1024    /// silently dropped; the pipeline additionally filters non-Full
1025    /// layers — a linear layer keeps its own operator).
1026    pub fn layer_flags(&self, num_layers: usize) -> Vec<bool> {
1027        let mut flags = vec![false; num_layers];
1028        match &self.layers {
1029            O1Layers::All => flags.iter_mut().for_each(|f| *f = true),
1030            O1Layers::Deep(n) => {
1031                for f in flags.iter_mut().skip(num_layers.saturating_sub(*n)) {
1032                    *f = true;
1033                }
1034            }
1035            O1Layers::List(idx) => {
1036                for &i in idx {
1037                    if i < num_layers {
1038                        flags[i] = true;
1039                    }
1040                }
1041            }
1042        }
1043        flags
1044    }
1045}
1046
1047/// Read `CMF_O1` (+ parameter overrides) — the embedding-friendly path
1048/// for hosts that don't go through the CLI flags.
1049pub fn o1_from_env() -> O1Env {
1050    match std::env::var("CMF_O1") {
1051        Err(_) => O1Env::Unset,
1052        Ok(s) => match O1Cfg::from_spec(&s, None, None, None, None) {
1053            Some(cfg) => O1Env::On(cfg),
1054            None => O1Env::Off,
1055        },
1056    }
1057}
1058
1059/// In-place lower Cholesky of an SPD matrix; None if a pivot fails.
1060fn cholesky(g: &mut [f64], n: usize) -> Option<&[f64]> {
1061    for i in 0..n {
1062        for j in 0..=i {
1063            let mut s = g[i * n + j];
1064            for k in 0..j {
1065                s -= g[i * n + k] * g[j * n + k];
1066            }
1067            if i == j {
1068                if s <= 0.0 || !s.is_finite() {
1069                    return None;
1070                }
1071                g[i * n + i] = s.sqrt();
1072            } else {
1073                g[i * n + j] = s / g[j * n + j];
1074            }
1075        }
1076    }
1077    Some(g)
1078}