Skip to main content

KvCache

Struct KvCache 

Source
pub struct KvCache<B: Backend, K: KvDtypeKind = KvFp16> {
    pub k: B::Buffer,
    pub v: B::Buffer,
    pub len: usize,
    pub capacity: usize,
    pub num_kv_heads: usize,
    pub head_dim: usize,
    pub block_size: usize,
    pub block_table: Option<B::Buffer>,
    pub context_lens: Option<B::Buffer>,
    pub paged_block_indices: Vec<u32>,
    pub _kv_dtype: PhantomData<K>,
}
Expand description

Per-layer KV cache. Each model owns its own Vec<KvCache<B, K>> per sequence. The K: KvDtypeKind parameter selects the cache element type — defaults to KvFp16 so existing call sites that wrote KvCache<B> keep compiling unchanged.

Two layouts are supported, selected at allocation time:

  1. Contiguous (default): k/v are [num_kv_heads, capacity, head_dim] f32 buffers. block_size == 0 and block_table / context_lens are None. Original ferrum layout — used when FERRUM_METAL_PAGED_KV is unset.
  2. Paged (vLLM-style): k/v are [num_blocks, num_kv_heads, block_size, head_dim] block pools. block_size > 0 and block_table (u32[max_num_blocks_per_seq]) + context_lens (u32[1] single-seq for now) are populated. Multi-seq sharing is a Phase 4 concern; today every paged cache_id has its own pool but the kernel-level indirection works.

The K parameter is currently a phantom-type marker — the buffer fields stay B::Buffer regardless. Future PRs will switch backends to BackendKvDtype<KvInt8> etc. and the kernel dispatch will read K::NAME / K::BYTES_PER_ELEM to pick the right append / attention kernel without any KvCache struct change.

Fields§

§k: B::Buffer§v: B::Buffer§len: usize§capacity: usize§num_kv_heads: usize§head_dim: usize§block_size: usize

Paged: KV positions per physical block. 0 => contiguous layout.

§block_table: Option<B::Buffer>

Paged: [max_num_blocks_per_seq] u32 — logical → physical block.

§context_lens: Option<B::Buffer>

Paged: [1] u32 — current context length for the kernel to read.

§paged_block_indices: Vec<u32>

Paged: host-side mirror of the physical block indices owned by this cache. Lets the model’s release path return blocks to the shared allocator without reading them back from device.

§_kv_dtype: PhantomData<K>

Marker — KV cache element type. Zero-sized.

Auto Trait Implementations§

§

impl<B, K> Freeze for KvCache<B, K>
where <B as Backend>::Buffer: Freeze,

§

impl<B, K> RefUnwindSafe for KvCache<B, K>

§

impl<B, K> Send for KvCache<B, K>

§

impl<B, K> Sync for KvCache<B, K>

§

impl<B, K> Unpin for KvCache<B, K>
where <B as Backend>::Buffer: Unpin, K: Unpin,

§

impl<B, K> UnsafeUnpin for KvCache<B, K>
where <B as Backend>::Buffer: UnsafeUnpin,

§

impl<B, K> UnwindSafe for KvCache<B, K>
where <B as Backend>::Buffer: UnwindSafe, K: UnwindSafe,

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> 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> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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