Skip to main content

Mojo struct

KVCacheScalesMHAOperand

@register_passable(trivial) struct KVCacheScalesMHAOperand[cache_t: KVCacheT]

An MHAOperand that accesses the scales field of a KVCache.

This is useful for MLA attention where k_s (per-token scales) are stored in the scales field of the k cache with quantization_granularity = head_size. The scales have shape [num_blocks, page_size, num_heads, head_dim_granularity].

Fields

  • cache (cache_t):

Implemented traits

AnyType, Copyable, DevicePassable, ImplicitlyCopyable, ImplicitlyDestructible, MHAOperand, Movable, RegisterPassable, TrivialRegisterPassable

comptime members

__copy_ctor_is_trivial

comptime __copy_ctor_is_trivial = cache_t.__copy_ctor_is_trivial

__del__is_trivial

comptime __del__is_trivial = cache_t.__del__is_trivial

__move_ctor_is_trivial

comptime __move_ctor_is_trivial = cache_t.__move_ctor_is_trivial

device_type

comptime device_type = KVCacheScalesMHAOperand[cache_t]

dtype

comptime dtype = cache_t.scale_dtype

page_size

comptime page_size = cache_t.page_size_

quantization_enabled

comptime quantization_enabled = cache_t.quantization_enabled

quantization_granularity

comptime quantization_granularity = cache_t.quantization_granularity

scale_dtype

comptime scale_dtype = DType.invalid

Methods

__init__

__init__(cache: cache_t) -> Self

get_type_name

static get_type_name() -> String

Returns:

String

block_paged_ptr

block_paged_ptr[tile_size: Int](self, batch_idx: UInt32, start_tok_idx: UInt32, head_idx: UInt32, head_dim_idx: UInt32 = 0) -> UnsafePointer[Scalar[KVCacheScalesMHAOperand[cache_t].dtype], MutAnyOrigin]

Returns:

UnsafePointer

scales_block_paged_ptr

scales_block_paged_ptr(self, batch_idx: Int, start_tok_idx: Int, head_idx: Int, head_dim_idx: Int = 0) -> UnsafePointer[Scalar[DType.invalid], MutAnyOrigin]

Returns:

UnsafePointer

load_scale

load_scale[width: Int](self, batch_idx: Int, start_tok_idx: Int, head_idx: Int, head_dim_idx: Int) -> SIMD[DType.invalid, width]

Returns:

SIMD

cache_length

cache_length(self, batch_idx: Int) -> Int

Returns:

Int

max_context_length

max_context_length(self) -> UInt32

Returns:

UInt32

row_idx

row_idx(self, batch_idx: UInt32, start_tok_idx: UInt32) -> UInt32

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

Returns:

UInt32

create_tma_tile

create_tma_tile[swizzle_mode: TensorMapSwizzle, *, BN: Int, depth: Int, BK: Int = padded_depth[KVCacheScalesMHAOperand[cache_t].dtype, swizzle_mode, depth]()](self, ctx: DeviceContext, out tma: TMATensorTile[KVCacheScalesMHAOperand[cache_t].dtype, 3, _padded_shape[3, KVCacheScalesMHAOperand[cache_t].dtype, IndexList(BN, 1, BK, Tuple()), swizzle_mode](), _ragged_shape[3, KVCacheScalesMHAOperand[cache_t].dtype, IndexList(BN, 1, BK, Tuple()), swizzle_mode]()])

TMA not supported for KVCacheScalesMHAOperand.

Returns:

TMATensorTile

create_ragged_tma_tile

create_ragged_tma_tile[swizzle_mode: TensorMapSwizzle, *, BN: Int, depth: Int, BK: Int = padded_depth[KVCacheScalesMHAOperand[cache_t].dtype, swizzle_mode, depth]()](self, ctx: DeviceContext, out tma: RaggedTMA3DTile[KVCacheScalesMHAOperand[cache_t].dtype, swizzle_mode, BN, BK])

TMA not supported for KVCacheScalesMHAOperand.

Returns:

RaggedTMA3DTile

scales_raw_ptr

scales_raw_ptr(self) -> UnsafePointer[Float32, MutAnyOrigin]

Returns a null pointer. KVCacheScalesMHAOperand already points to the scales pointer.

Returns:

UnsafePointer

Was this page helpful?