Skip to main content

NystromState

Struct NystromState 

Source
pub struct NystromState { /* private fields */ }
Expand description

Streaming Nyström attention state for ONE GQA group.

State splits along the GQA grain, because the operator does:

  • SHARED per KV group (NystromGroup) — the exact window ring, the sink buffer and the key landmarks K̃. Under GQA every Q head of a group reads the SAME k/v rows, so all three are bit-identical across the group; storing them once per group instead of once per Q head is the point of this split (identical arithmetic, ×heads_per_kv less window memory). K̃ = seg_means(ks, t, d, m_eff) is a pure function of the group’s keys and of t (which fixes m_eff), so it is shareable for the same reason the keys are.
  • PRIVATE per Q head (NystromHead) — the far accumulators T̂/Ẑ and their per-landmark running maxima, the QUERY landmarks Q̃, and the mixing matrix M = pinv(exp(Q̃K̃ᵀ/√d)). Q̃ is built from that head’s own queries, so M and the far field it drives are per-Q-head and cannot be shared: the far mass a head accumulates is contracted against its own query landmarks.

Lifecycle: new(m, w, sink)prefill(prompt) once → step() per decode token (single-head façade), or new_group/prefill_group/ step_group for a whole GQA group at once. All buffers are flat Vec<f32>, row-major; the skeleton path performs no allocations inside step().

Implementations§

Source§

impl NystromState

Source

pub fn new(m: usize, w: usize, sink: usize) -> Self

Single-head state (heads_per_kv == 1, and the shape the kernel unit tests use).

m — landmark budget (≥ 4; see O1_DEFAULT_M), w — exact window width (validated setting is 128), sink — permanent exact sink keys (validated default is 4; 0 reproduces the sink-free kernel bit-for-bit). Rectifier defaults to O1_DEFAULT_RECT; override with with_rect (the golden-parity test pins it explicitly).

Source

pub fn new_group(m: usize, w: usize, sink: usize, q_heads: usize) -> Self

State for one GQA group of q_heads query heads sharing a KV head. The window/sink/K̃ are stored ONCE for the group; each Q head keeps its own far field, Q̃ and M.

Source

pub fn with_rect(self, rect: O1Rect) -> Self

Select the skeleton rectifier for every head of the group (builder; see O1Rect).

Source

pub fn num_q_heads(&self) -> usize

Query heads in this group.

Source

pub fn far_len(&self, head: usize) -> usize

Keys absorbed into head head’s far field. Exposed for the delayed-insertion invariant test: eviction is a GROUP event, but each head must absorb the evicted key EXACTLY once, so this must equal the number of evictions — never a multiple of it.

Source

pub fn prefill( &mut self, qs: &[f32], ks: &[f32], vs: &[f32], t: usize, d: usize, dv: usize, )

Absorb the whole prompt for a single-head state — see prefill_group.

Source

pub fn prefill_group( &mut self, qs: &[&[f32]], ks: &[f32], vs: &[f32], t: usize, d: usize, dv: usize, )

Absorb the whole prompt for a GQA group: freeze each head’s landmarks and M, then replay the prompt through the step() state semantics (window fill + delayed far insertion). qs[h] is that head’s [t][d] query block; ks is [t][d] and vs is [t][dv] — the group’s shared keys/values, row-major.

Source

pub fn step(&mut self, q: &[f32], k: &[f32], v: &[f32], out: &mut [f32])

One decode step for a single-head state — see step_group.

Source

pub fn step_group( &mut self, q_all: &[f32], k: &[f32], v: &[f32], out_all: &mut [f32], )

One decode step for the whole GQA group. Inserts the group’s (k, v) ONCE, evicting the oldest window key into every head’s far accumulators, then writes each head’s attention output. q_all is [q_heads][d], out_all is [q_heads][dv].

Source

pub fn memory_bytes(&self) -> usize

Heap bytes held by this group’s state (shared window + sinks + K̃, plus each head’s skeleton and scratch) — feeds the honest “KV+state” memory line, same discipline as counting linear_state for the linear core.

Trait Implementations§

Source§

impl Clone for NystromState

Source§

fn clone(&self) -> NystromState

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for NystromState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more