Skip to main content

LayerKvCache

Struct LayerKvCache 

Source
pub struct LayerKvCache {
    pub past_len: usize,
    pub layers_k: Vec<Vec<f32>>,
    pub layers_v: Vec<Vec<f32>>,
}
Expand description

Layer-wise past K/V tensors in row-major [past_len * kv_dim] layout per layer.

Fields§

§past_len: usize§layers_k: Vec<Vec<f32>>§layers_v: Vec<Vec<f32>>

Implementations§

Source§

impl LayerKvCache

Source

pub fn from_layer_outputs( num_layers: usize, batch: usize, past_seq: usize, kv_dim: usize, outputs: &[Vec<f32>], ) -> Result<Self, String>

Source

pub fn from_layer_outputs_per_layer( num_layers: usize, batch: usize, past_seq: usize, kv_dims: &[usize], outputs: &[Vec<f32>], ) -> Result<Self, String>

Like Self::from_layer_outputs but accepts a per-layer kv_dim vector. Gemma 4 12B’s full-attention layers have kv_dim = 1 * 512 = 512 while sliding layers have 8 * 256 = 2048; this constructor handles that heterogeneity.

Source

pub fn pad_layers_to_upper( &self, upper: u64, kv_dim: usize, ) -> (Vec<Vec<f32>>, Vec<Vec<f32>>)

Pad each layer’s K/V to upper rows along the sequence axis (kv_dim inner).

Source

pub fn pad_layers_to_upper_per_layer( &self, upper: u64, kv_dims: &[usize], ) -> (Vec<Vec<f32>>, Vec<Vec<f32>>)

Like Self::pad_layers_to_upper but pads each layer to its own kv_dim. The number of dims must equal the number of cached layers.

Source

pub fn advance_from_decode_outputs( &mut self, outputs: Vec<Vec<f32>>, batch: usize, kv_dim: usize, ) -> Result<(), String>

Update cache from decode outputs: [logits, k0, v0, k1, v1, …] (bucket-padded).

Source

pub fn trim_sliding_window_per_layer( &mut self, kv_dims_keep: &[Option<(usize, usize)>], ) -> Result<(), String>

Trim each layer’s K/V history to at most window rows on the sequence axis, keeping the most recent rows. Used by Gemma 3/4 sliding-attention layers — long contexts can keep only the last window (e.g. 1024) tokens per sliding layer without affecting attention semantics (those layers mask out older positions anyway).

kv_dims_keep selects which layers to trim and at what dim: kv_dims_keep[i] = Some((dim, window)) trims layer i, None leaves the layer untouched. Pass-through for layers whose attention is full-causal.

Note: past_len is unchanged — the per-layer K/V buffers just hold fewer real rows now; the decode flow’s per-layer past_k_{i} input shape will see the trimmed length. Caller is responsible for ensuring the graph’s declared past_seq matches the trimmed length OR the trimmed layer is bound dynamically.

Source

pub fn advance_from_decode_outputs_per_layer( &mut self, outputs: Vec<Vec<f32>>, _batch: usize, kv_dims: &[usize], ) -> Result<(), String>

Per-layer variant of Self::advance_from_decode_outputs.

Trait Implementations§

Source§

impl Clone for LayerKvCache

Source§

fn clone(&self) -> LayerKvCache

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 LayerKvCache

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, 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,