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
MatmulTileWriter
struct MatmulTileWriter[dtype: DType, tensor_layout: TensorLayout, tensor_storage: TensorStorage, linear_idx_type: DType, smem_tile_layout: TensorLayout, //, *, BM: Int, BN: Int, swizzle: TensorMapSwizzle, wgmma_shape: IndexList[Int(3)], num_consumer: Int = Int(1), use_tma_store: Bool = False, elementwise_lambda_fn: Optional[def[dtype: DType, width: SIMDLength, *, alignment: Int = Int(1)](IndexList[Int(2)], SIMD[dtype, width]) capturing thin -> None] = None, elementwise_compute_lambda_fn: Optional[def[dtype: DType, width: SIMDLength, *, alignment: Int = Int(1)](IndexList[Int(2)], SIMD[dtype, width]) capturing thin -> SIMD[dtype, width]] = None, swapAB: Bool = False]
Writes WGMMA accumulator register tiles to global memory for SM90 matmul.
Provides two write paths: TMA store (shared memory β global via hardware async copies) and thread-wise store (register β global directly). Handles swizzled shared memory layouts, optional elementwise epilogues, and A/B swap when the problem uses the swapAB small-M strategy.
Parametersβ
- βdtype (
DType): Element type of the output tensor and shared memory tile (inferred). - βtensor_layout (
TensorLayout): Memory layout of the output tensor in global memory (inferred). - βtensor_storage (
TensorStorage): Storage backing the output tensor in global memory (inferred). - βlinear_idx_type (
DType): Integer type used for linear index arithmetic into the output tensor (inferred). - βsmem_tile_layout (
TensorLayout): Memory layout of the shared memory tile used to stage output before storing to global memory; also defines the workgroup tile dimensionsWG_BMandWG_BN(inferred). - βBM (
Int): Row (M) dimension of the matmul output tile per block. - βBN (
Int): Column (N) dimension of the matmul output tile per block. - βswizzle (
TensorMapSwizzle): TMA swizzle mode applied to shared memory for TMA async stores. - βwgmma_shape (
IndexList[Int(3)]): Shape of each WGMMA instruction as a 3-element index list(M, N, K). - βnum_consumer (
Int): Number of consumer warp groups sharing the output tile (defaults to 1). - βuse_tma_store (
Bool): Whether to use TMA async stores for shared memory to global memory copies (defaults toFalse). - βelementwise_lambda_fn (
Optional[def[dtype: DType, width: SIMDLength, *, alignment: Int = Int(1)](IndexList[Int(2)], SIMD[dtype, width]) capturing thin -> None]): Optional epilogue lambda invoked with each output element's global coordinates and value, writing directly to global memory (defaults toNone). - βelementwise_compute_lambda_fn (
Optional[def[dtype: DType, width: SIMDLength, *, alignment: Int = Int(1)](IndexList[Int(2)], SIMD[dtype, width]) capturing thin -> SIMD[dtype, width]]): Optional compute lambda that transforms each output element value before it is written back to shared memory and then to global memory (defaults toNone). - βswapAB (
Bool): Whether to swap the A and B operand roles for the small-M strategy, transposing the tile and block coordinate mapping (defaults toFalse).
Fieldsβ
- βtensor (
MatmulTileWriter[BM=BM, BN=BN, swizzle=swizzle, wgmma_shape=wgmma_shape, num_consumer=num_consumer, use_tma_store=use_tma_store, elementwise_lambda_fn=elementwise_lambda_fn, elementwise_compute_lambda_fn=elementwise_compute_lambda_fn, swapAB=swapAB].CTensorType): - βsmem_tile (
TileTensor[dtype, smem_tile_layout, MutAnyOrigin, address_space=AddressSpace.SHARED]): - βwarp_group_thread_idx (
Int): - βlocal_warp_group_idx (
Int): - βlocal_thread_idx (
Int): - βblock_y (
Int): - βblock_x (
Int):
Implemented traitsβ
AnyType,
Copyable,
ImplicitlyCopyable,
ImplicitlyDeletable,
Movable,
RegisterPassable,
TrivialRegisterPassable
comptime membersβ
CTensorTypeβ
comptime CTensorType = TileTensor[dtype, tensor_layout, MutAnyOrigin, Storage=tensor_storage, linear_idx_type=linear_idx_type]
frag_sizeβ
comptime frag_size = (Int((mul wgmma_shape[Int(0)], wgmma_shape[Int(1)])) // _resolve_warpgroup_size())
lambda_typeβ
comptime lambda_type = def[dtype: DType, width: SIMDLength, *, alignment: Int = Int(1)](IndexList[Int(2)], mut SIMD[dtype, width]) -> None
Nβ
comptime N = tensor_layout.static_shape[Int(1)]
num_consumer_threadsβ
comptime num_consumer_threads = (num_consumer * _resolve_warpgroup_size())
num_m_mmasβ
comptime num_m_mmas = ((BM // wgmma_shape[Int(0)]) // num_consumer)
num_n_mmasβ
comptime num_n_mmas = (BN // wgmma_shape[Int(1)])
simd_sizeβ
comptime simd_size = simd_width_of[dtype]()
WG_BMβ
comptime WG_BM = smem_tile_layout.static_shape[Int(0)]
WG_BNβ
comptime WG_BN = smem_tile_layout.static_shape[Int(1)]
Methodsβ
__init__β
def __init__(tensor: TileTensor[dtype, tensor_layout, MutAnyOrigin, Storage=tensor_storage, linear_idx_type=linear_idx_type], smem_tile: TileTensor[dtype, smem_tile_layout, MutAnyOrigin, address_space=AddressSpace.SHARED], warp_group_thread_idx: Int, local_warp_group_idx: Int, local_thread_idx: Int, block_y: Int, block_x: Int) -> Self
write_tileβ
def write_tile[tma_rank: Int, tma_tile_shape: IndexList[tma_rank], tma_desc_shape: IndexList[tma_rank], accum_type: DType, reg_tile_layout: Layout, //](self, tma_op: TMATensorTile[dtype, tma_rank, tma_tile_shape, tma_desc_shape], reg_tile: LayoutTensor[accum_type, reg_tile_layout, MutAnyOrigin, address_space=AddressSpace.LOCAL])
Write output from registers to global memory.
Selects optimized st.matrix path for bf16 when constraints are met, otherwise uses general register-to-global path.
Parameters:
- βtma_rank (
Int): Number of dimensions in the TMA tensor descriptor. - βtma_tile_shape (
IndexList[tma_rank]): Shape of each TMA store tile per async copy, as an index list of lengthtma_rank. - βtma_desc_shape (
IndexList[tma_rank]): Full shape of the TMA tensor descriptor as an index list of lengthtma_rank. - βaccum_type (
DType): Data type of the WGMMA accumulator register tile. - βreg_tile_layout (
Layout): Memory layout of the accumulator register tile.
Args:
- βtma_op (
TMATensorTile[dtype, tma_rank, tma_tile_shape, tma_desc_shape]): TMA tensor tile descriptor used for async stores from shared memory to global memory. - βreg_tile (
LayoutTensor[accum_type, reg_tile_layout, MutAnyOrigin, address_space=AddressSpace.LOCAL]): WGMMA accumulator register tile containing the matmul result to write.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!