Skip to main content
Log in

Mojo struct

PagedKVCacheFA3Fallback

@register_passable(trivial) struct PagedKVCacheFA3Fallback[type_: DType, kv_params_: KVCacheStaticParams, page_size: Int]

The PagedKVCache is a wrapper around the KVCache blocks for a given layer. It is used to access the KVCache blocks for PagedAttention.

Aliases

  • type = type_:
  • kv_params = kv_params_:
  • KeyIdx = 0:
  • ValueIdx = 1: The entire region of memory for KVCache blocks with shape: [2, total_num_blocks, page_size, num_heads, head_size].

Fields

  • blocks (NDBuffer[type_, 5, MutableAnyOrigin]):
  • cache_lengths (NDBuffer[int32, 1, MutableAnyOrigin]): The lookup table with shape: [num_layers, batch_size, max_num_blocks_in_batch]. This is to conform to the expected layout in the DaoLabs FA3 kernel. We have a different lookup table for each layer.
  • lookup_table (NDBuffer[int32, 3, MutableAnyOrigin]):
  • max_seq_length (SIMD[uint32, 1]):
  • max_cache_length (SIMD[uint32, 1]):
  • layer_idx (Int):
  • kv_idx (Int):

Implemented traits

AnyType, Copyable, ExplicitlyCopyable, KVCacheT, Movable, UnknownDestructibility

Methods

__init__

__init__(blocks: NDBuffer[type_, 5, origin], cache_lengths: NDBuffer[int32, 1, origin], lookup_table: NDBuffer[int32, 3, origin], max_seq_length: SIMD[uint32, 1], max_cache_length: SIMD[uint32, 1], layer_idx: Int, kv_idx: Int) -> Self

max_tile_size

static max_tile_size() -> Int

Returns the maximum tile size for the KVCache.

cache_lengths_nd

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

cache_length

cache_length(self, batch_idx: Int) -> Int

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

load

load[width: Int](self, bs: Int, head_idx: Int, tok_idx: Int, head_dim_idx: Int) -> SIMD[type_, width]

Loads an element from the given index.

store

store(self, bs: Int, head_idx: Int, tok_idx: Int, head_dim_idx: Int, val: SIMD[type_, size])

Stores an element at the given index.

empty_cache

empty_cache(self) -> Bool

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

max_prompt_length

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

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

max_context_length

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

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

block_paged_ptr

block_paged_ptr[tile_size: Int](self, batch_idx: Int, start_tok_idx: Int, head_idx: Int, head_dim_idx: Int = 0) -> UnsafePointer[SIMD[type_, 1]]

Was this page helpful?