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: usizeTokens inserted since the last clear (= the next position).
Implementations§
Source§impl BoundedState
impl BoundedState
pub fn new(num_kv_heads: usize, head_dim: usize, window: usize) -> Self
pub fn is_empty(&self) -> bool
Sourcepub fn state_bytes(&self) -> usize
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.
Sourcepub fn insert(&mut self, k: &[f32], v: &[f32])
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.
Sourcepub fn rollback(&mut self, n: usize) -> usize
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.
pub fn snapshot(&self) -> BoundedSnapshot
Sourcepub fn restore(&mut self, s: &BoundedSnapshot)
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.
Sourcepub fn same_state(&self, other: &BoundedState) -> bool
pub fn same_state(&self, other: &BoundedState) -> bool
Bit-for-bit equality of the wire-visible state.
Sourcepub fn attend(
&self,
q: &[f32],
num_heads: usize,
sink_k: &[f32],
sink_v: &[f32],
sink: usize,
rope: &BoundedRope,
scale: f32,
out: &mut [f32],
)
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).