Skip to main content

BoundedState

Struct BoundedState 

Source
pub struct BoundedState {
    pub window: usize,
    pub num_kv_heads: usize,
    pub head_dim: usize,
    pub ring_k: Vec<f32>,
    pub ring_v: Vec<f32>,
    pub seen: usize,
    /* private fields */
}
Expand description

Per-layer bounded state: the ring of the last W raw keys/values per KV head plus the insert counter. len = min(seen, W), next write slot head = seen mod W, and the key of position j lives in slot j mod W — so no absolute position is ever stored.

Fields§

§window: usize§num_kv_heads: usize§head_dim: usize§ring_k: Vec<f32>

[kvh][W][hd], raw (unrotated) keys.

§ring_v: Vec<f32>

[kvh][W][hd].

§seen: usize

Tokens inserted since the last clear (= the next position).

Implementations§

Source§

impl BoundedState

Source

pub fn new(num_kv_heads: usize, head_dim: usize, window: usize) -> Self

Source

pub fn len(&self) -> usize

Filled slots, min(seen, W).

Source

pub fn is_empty(&self) -> bool

Source

pub fn head(&self) -> usize

Next write slot, seen mod W.

Source

pub fn state_bytes(&self) -> usize

Bytes of the wire-visible state (ring + counter). The undo rows are scratch of fixed size and are not state.

Source

pub fn clear(&mut self)

Zero the record (fresh sequence). Capacity never changes.

Source

pub fn insert(&mut self, k: &[f32], v: &[f32])

Write k, v ([kvh][hd], raw) into slot seen mod W, saving the overwritten rows for rollback.

Source

pub fn rollback(&mut self, n: usize) -> usize

Undo the last n inserts exactly (restores the overwritten slots). Returns how many were rolled back — fewer than n only when the rollback history (UNDO_DEPTH) or the inserted count is shorter.

Source

pub fn snapshot(&self) -> BoundedSnapshot

Source

pub fn restore(&mut self, s: &BoundedSnapshot)

Restore a snapshot taken on THIS state (same geometry). The undo history is discarded: it described inserts that no longer exist.

Source

pub fn same_state(&self, other: &BoundedState) -> bool

Bit-for-bit equality of the wire-visible state.

Source

pub fn attend( &self, q: &[f32], num_heads: usize, sink_k: &[f32], sink_v: &[f32], sink: usize, rope: &BoundedRope, scale: f32, out: &mut [f32], )

Attend every Q head of every KV group over sink ∪ window and write [nh][hd] into out. q is [nh][hd] RAW (unrotated); the current token must already be inserted (the window includes it, matching the trainer’s band col == S + row).

Trait Implementations§

Source§

impl Clone for BoundedState

Source§

fn clone(&self) -> Self

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 BoundedState

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 = !

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

fn try_from(value: U) -> Result<T, !>

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