Skip to main content

StateMoments

Struct StateMoments 

Source
pub struct StateMoments {
    pub m2_bhrr: Tensor<4>,
    pub m1_bhr: Tensor<3>,
    pub count: usize,
}
Expand description

Raw (un-normalised) first/second moments of the per-token SSM states of one block’s forward pass, pooled over tokens and per_head_dim rows.

Produced by forward_with_state_moments — closed-form for Mamba-2 (Mamba2SsdInput::state_moments, no state materialisation) and serial chunkwise for Mamba-3 (Mamba3MomentsInput::state_moments_phys, the per-token physical-frame states of the complex SSM).

Fields§

§m2_bhrr: Tensor<4>

Second-moment (Gram) sum Σₜ hₜᵀ hₜ — the contraction pools the per_head_dim rows, the Σₜ the (unpadded) tokens.

§Shape

  • [batch, nheads, state_rank, state_rank]
§m1_bhr: Tensor<3>

First-moment sum Σₜ Σₚ hₜ[p, :].

§Shape

  • [batch, nheads, state_rank]
§count: usize

Samples pooled into each (batch, head) slice: valid_tokens · per_head_dim (grows additively under Self::merge).

Implementations§

Source§

impl StateMoments

Source

pub fn merge(self, other: Self) -> Self

Pool two moment sets (e.g. consecutive streamed forward calls, or separate eval batches). PR of the merged moments is the exact PR of the union of samples.

Source

pub fn pool_batch(self) -> Self

Fold the batch dimension into the samples (batch-pooled moments with batch = 1), matching diagnostics that treat every (token, batch, per_head_dim) triple as one sample.

Source

pub fn pr(&self, center: bool) -> Tensor<2>

Participation ratio (tr Σ)² / tr(Σ²) of the sample covariance, per (batch, head) slice; center subtracts the sample mean (Σ becomes the centered covariance instead of the raw second moment).

Differentiable (two traces, no eigendecomposition).

§Shape
  • output: [batch, nheads]
Source

pub fn pr_complex(&self, pairing: &StatePairing, center: bool) -> Tensor<2>

Participation ratio of the Hermitian sample covariance, treating the state_rank axis as realified complex (or quaternionic) coordinates per pairing — the Mamba-3 counterpart of Self::pr.

With the pairing’s complex view c = x + iy, the Hermitian moment is M = A + iS with A = Σ(xxᵀ + yyᵀ) and S = Σ(xyᵀ − yxᵀ) — both linear recombinations of m2_bhrr sub-blocks, so centering Σ centers M identically. PR_ℂ = (tr M)² / tr(M²); the trace is real and equals the full real trace (frame-invariant), while tr(M²) = Σ|M_ab|² (= ‖A‖²_F + ‖S‖²_F for the fully-rotated complex case). One complex (or quaternionic) direction counts as one — the ×2 (×4) realified count is a representation artifact — so a rank-1 rotating conveyor reads PR_ℂ ≡ 1 where Self::pr reads up to the block size.

Un-rotated coordinates (partial rope_fraction) stay a real block U of the mixed Hermitian [[M, X], [Xᴴ, U]]; its trace and Σ|·|² join the sums (tr(·²) gains 2‖X‖²_F + ‖U‖²_F). Quaternionic pairing uses the same formulas with M_jk = Σ q̄ⱼqₖ (diagonal real, 4-component norms).

Differentiable; numerics mirror Self::pr (detached trace normalisation, min_positive / div_eps floors — see the comments there for why).

§Shape
  • output: [batch, nheads]
Source

pub fn trace(&self) -> Tensor<2>

Raw uncentered state magnitude tr Σ = trace(m2)/count per (batch, head) — the mean squared state magnitude ⟨‖h‖²⟩, which is Self::pr’s numerator scale. Reported alongside PR to tell a genuine rank-1 state (PR → 1, magnitude healthy) apart from a state collapsing toward zero (where pr’s 1e-12 denominator clamp drags the ratio below its true floor of 1).

§Shape
  • output: [batch, nheads]

Trait Implementations§

Source§

impl Clone for StateMoments

Source§

fn clone(&self) -> StateMoments

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 StateMoments

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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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, 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.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.