For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /max/get-started.md).
Mojo struct
DecodeKVBuffer
struct DecodeKVBuffer[dtype: DType, kv_tile_layout: TensorLayout, //, config: KVBufferConfig, tensor_core_mma: TiledTensorCore[tensor_core_mma.out_type, tensor_core_mma.in_type, tensor_core_mma.shape, tensor_core_mma.group_size, tensor_core_mma.transpose_b], swizzle: Optional[Swizzle], BN: Int, WN: Int, BK: Int, depth: Int, num_threads: Int, num_stages: Int = Int(1), token_gen: Bool = False]
Double-buffered register-staged KV buffer for the AMD decode mirror path.
Stages DRAM tiles through a register load_tile (optionally multi-stage)
before copying to SMEM and loading into the MFMA mma_tile. The
KVBufferConfig parameter selects the K (transpose) or V
(non-transpose) tiling, iterator axis, and warp-tile coordinate so a
single implementation serves both operands.
Parametersβ
- βdtype (
DType): Element type of the KV cache tiles (inferred). - βkv_tile_layout (
TensorLayout): Runtime layout of the DRAM KV tile used to buildgmem_tile(inferred). - βconfig (
KVBufferConfig): K or V role config selecting warp tile, block tile, iterator axis, and warp-tile coordinate. - βtensor_core_mma (
TiledTensorCore[tensor_core_mma.out_type, tensor_core_mma.in_type, tensor_core_mma.shape, tensor_core_mma.group_size, tensor_core_mma.transpose_b]): MFMA descriptor (shape, group size, transpose_b, out type) driving SMEM-to-register loads. - βswizzle (
Optional[Swizzle]): LDS bank-conflict swizzle for SMEM loads and stores, orNone. - βBN (
Int): Number of keys per DRAM tile (block tile extent along the key axis). - βWN (
Int): Number of keys per warp tile (warp tile extent along the key axis). - βBK (
Int): Strip width along the MMA K dimension of one DRAM-to-SMEM load. - βdepth (
Int): Per-head depth of the KV cache (head dimension). - βnum_threads (
Int): Number of threads in the block driving DMA and register distribution. - βnum_stages (
Int): Number of register buffer stages for double buffering (defaults to 1). - βtoken_gen (
Bool): Selects the token-generation thread layout for DMA distribution (defaults toFalse).
Fieldsβ
- βload_tile (
DecodeKVBuffer[config, tensor_core_mma, swizzle, BN, WN, BK, depth, num_threads, num_stages, token_gen].LoadTile): - βmma_tile (
DecodeKVBuffer[config, tensor_core_mma, swizzle, BN, WN, BK, depth, num_threads, num_stages, token_gen].MmaTile): - βsmem_tile (
DecodeKVBuffer[config, tensor_core_mma, swizzle, BN, WN, BK, depth, num_threads, num_stages, token_gen].SmemTile): - βgmem_tile (
DecodeKVBuffer[config, tensor_core_mma, swizzle, BN, WN, BK, depth, num_threads, num_stages, token_gen].GmemTileType): - βreg_loader (
DecodeKVBuffer[config, tensor_core_mma, swizzle, BN, WN, BK, depth, num_threads, num_stages, token_gen].RegLoaderType): - βtile_idx (
Int): - βload_tile_id (
Int):
Implemented traitsβ
comptime membersβ
GmemTileTypeβ
comptime GmemTileType = TileTensor[dtype, kv_tile_layout, ImmutAnyOrigin]
LoadTileβ
comptime LoadTile = TileTensor[dtype, Layout[*?, *?], MutUntrackedOrigin, address_space=AddressSpace.LOCAL]
MMA_Kβ
comptime MMA_K = shape[Int(2)]
MMA_Nβ
comptime MMA_N = shape[Int(1)]
MmaTileβ
comptime MmaTile = TileTensor[dtype, Layout[*?, *?], MutUntrackedOrigin, address_space=AddressSpace.LOCAL]
num_k_tilesβ
comptime num_k_tiles = ceildiv(BK, Int((mul shape[Int(2)], group_size)))
num_mmasβ
comptime num_mmas = ceildiv(config.wsize, shape[Int(1)])
num_warps_nβ
comptime num_warps_n = (BN // WN)
RegLoaderTypeβ
comptime RegLoaderType = RegTileLoader[dtype, row_major[(Int((mul min(num_threads, (Int((mul config.btile_dim0, config.btile_dim1)) // simd_width_of[dtype]())), simd_width_of[dtype]())) // config.btile_dim1) if token_gen else (num_threads // Int(4)), (config.btile_dim1 // simd_width_of[dtype]()) if token_gen else Int(4)](), num_threads]
simd_widthβ
comptime simd_width = simd_width_of[dtype]()
SmemTileβ
comptime SmemTile = TileTensor[dtype, Layout[*?, *?], MutAnyOrigin, address_space=AddressSpace.SHARED]
wtile_dim0β
comptime wtile_dim0 = config.wtile_dim0
wtile_dim1β
comptime wtile_dim1 = config.wtile_dim1
Methodsβ
__init__β
def __init__(out self, gmem_tile: TileTensor[dtype, kv_tile_layout, ImmutAnyOrigin], smem_tile: TileTensor[dtype, Layout[*?, *?], MutAnyOrigin, address_space=AddressSpace.SHARED])
load_from_dramβ
def load_from_dram(mut self)
get_mma_tileβ
def get_mma_tile(self) -> TileTensor[dtype, Layout[*?, *?], MutAnyOrigin, address_space=AddressSpace.LOCAL]
Returns:
TileTensor[dtype, Layout[*?, *?], MutAnyOrigin, address_space=AddressSpace.LOCAL]
copy_to_sharedβ
def copy_to_shared[tile_id: Int = Int(0)](self)
load_from_sharedβ
def load_from_shared[k_mma: Int](self)
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!