Skip to main content

Mojo trait

KVCacheT

Trait for different KVCache types and implementations.

Represents a single (key or value) cache.

Implemented traits

AnyType, Copyable, ExplicitlyCopyable, Movable, UnknownDestructibility

Aliases

dtype

alias dtype

kv_params

alias kv_params

Methods

__copyinit__

__copyinit__(out self: _Self, existing: _Self, /)

Create a new instance of the value by copying an existing one.

Args:

  • existing (_Self): The value to copy.

Returns:

_Self

__moveinit__

__moveinit__(out self: _Self, var existing: _Self, /)

Create a new instance of the value by moving the value of another.

Args:

  • existing (_Self): The value to move.

Returns:

_Self

cache_lengths_nd

cache_lengths_nd(self: _Self) -> NDBuffer[uint32, 1, MutableAnyOrigin]

Returns the cache lengths as a NDBuffer.

Returns:

NDBuffer

cache_length

cache_length(self: _Self, batch_idx: Int) -> Int

Returns the length of the cache for a given batch index.

Returns:

Int

load

load[width: Int](self: _Self, bs: Int, head_idx: Int, tok_idx: Int, head_dim_idx: Int) -> SIMD[get_witness(_Self, kv_cache::types::KVCacheT, dtype), width]

Loads an element from the given index.

Returns:

SIMD

store

store(self: _Self, bs: Int, head_idx: Int, tok_idx: Int, head_dim_idx: Int, val: SIMD[get_witness(_Self, kv_cache::types::KVCacheT, dtype), size])

Stores an element at the given index.

empty_cache

empty_cache(self: _Self) -> Bool

Returns true if the cache_lengths for all requests is 0, false otherwise.

Returns:

Bool

max_prompt_length

max_prompt_length(self: _Self) -> SIMD[uint32, 1]

Returns the maximum sequence length across all batches of the current request.

Returns:

SIMD

max_context_length

max_context_length(self: _Self) -> SIMD[uint32, 1]

Returns the maximum cache length used across all batches of the current request.

Returns:

SIMD

block_paged_ptr

block_paged_ptr[tile_size: Int](self: _Self, batch_idx: Int, start_tok_idx: Int, head_idx: Int, head_dim_idx: Int = 0) -> UnsafePointer[SIMD[get_witness(_Self, kv_cache::types::KVCacheT, dtype), 1]]

Returns a LayoutTensor pointing to the KVCache block at the given index.

Paged KVCache implementations must have a block_size which is a multiple of the and greater than the layout's first dimension.

Returns:

UnsafePointer

max_tile_size

static max_tile_size() -> Int

Returns the maximum tile size for the KVCache.

Returns:

Int

row_idx

row_idx(self: _Self, batch_idx: SIMD[uint32, 1], start_tok_idx: SIMD[uint32, 1]) -> SIMD[uint32, 1]

Returns the row idx when viewing the memory as a matrix.

Returns:

SIMD

col_idx

col_idx(self: _Self, head_idx: SIMD[uint32, 1]) -> SIMD[uint32, 1]

Returns the col idx when viewing the memory as a matrix.

Returns:

SIMD

create_tma_tile

create_tma_tile[tile_m: Int, tile_n: Int, swizzle_mode: TensorMapSwizzle, *, is_k_major: Bool](self: _Self, ctx: DeviceContext) -> TMATensorTile[get_witness(_Self, kv_cache::types::KVCacheT, dtype), tile_layout_k_major[::DType,::Int,::Int,::TensorMapSwizzle]() if is_k_major else tile_layout_mn_major[::DType,::Int,::Int,::TensorMapSwizzle](), _tma_desc_tile_layout[::DType,::Int,::IndexList[$1, ::DType(), is_k_major]

Creates a TMA tile for this KV cache.

Returns:

TMATensorTile

copy

copy(self: _Self) -> _Self

Explicitly construct a copy of self.

Returns:

_Self: A copy of this value.

Was this page helpful?