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
HopperMatmulSM90Kernel
struct HopperMatmulSM90Kernel[a_type: DType, b_type: DType, c_type: DType, a_layout: TensorLayout, b_layout: TensorLayout, c_layout: TensorLayout, c_smem_layout: Layout, block_tile_shape: IndexList[Int(3)], wgmma_shape: IndexList[Int(3)], cluster_shape: StaticTuple[Int32, Int(3)], num_pipeline_stages: Int, num_threads: Int = Int(128), transpose_b: Bool = True, a_swizzle: TensorMapSwizzle = TensorMapSwizzle.SWIZZLE_128B, b_swizzle: TensorMapSwizzle = TensorMapSwizzle.SWIZZLE_128B, c_swizzle: TensorMapSwizzle = TensorMapSwizzle.SWIZZLE_NONE, partitioned_multicast: Bool = False, use_tma_store: Bool = False, promotion_frequency: Int = Int(1), pdl_level: PDLLevel = PDLLevel(), 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, hilbert_swizzle: Bool = False, k_group_size: Int = Int(1), swapAB: Bool = False]
Hopper SM90 GEMM for NVIDIA H100 GPUs.
Uses TMA loads, WGMMA tensor-core MMA, multi-stage pipelining, and a producer-consumer warp-group layout.
Parametersβ
- βa_type (
DType): Data type of the A input matrix. - βb_type (
DType): Data type of the B input matrix. - βc_type (
DType): Data type of the C output matrix. - βa_layout (
TensorLayout): Memory layout of the A matrix. - βb_layout (
TensorLayout): Memory layout of the B matrix. - βc_layout (
TensorLayout): Memory layout of the C matrix. - βc_smem_layout (
Layout): Shared memory layout for the output tile. - βblock_tile_shape (
IndexList[Int(3)]): Tile dimensions[M, N, K]processed by each thread block. - βwgmma_shape (
IndexList[Int(3)]): Dimensions for each WGMMA instruction[M, N, K]. - βcluster_shape (
StaticTuple[Int32, Int(3)]): Thread block cluster dimensions for distributed shared memory. - βnum_pipeline_stages (
Int): Number of stages in the software pipeline (3-7 in most configs). - βnum_threads (
Int): Number of threads per block (must be a multiple of 128). - βtranspose_b (
Bool): Whether the B matrix is transposed (required to beTrue). - βa_swizzle (
TensorMapSwizzle): Memory swizzling for bank-conflict-free A tile access. - βb_swizzle (
TensorMapSwizzle): Memory swizzling for bank-conflict-free B tile access. - βc_swizzle (
TensorMapSwizzle): Swizzling mode for output writes. - βpartitioned_multicast (
Bool): Whether partitioned multicast is enabled for large tiles. - βuse_tma_store (
Bool): Whether TMA is used for storing output (versus regular stores). - βpromotion_frequency (
Int): How often FP8 accumulation is promoted to higher precision. - βpdl_level (
PDLLevel): Programmatic Dependency Launch (PDL) level. - βelementwise_lambda_fn (
Optional[def[dtype: DType, width: SIMDLength, *, alignment: Int = Int(1)](IndexList[Int(2)], SIMD[dtype, width]) capturing thin -> None]): Optional epilogue function. - β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 function. - βhilbert_swizzle (
Bool): Whether Hilbert-curve thread block scheduling is used. - βk_group_size (
Int): Number of K-dimension tiles loaded and consumed per pipeline stage; bothnum_pipeline_stagesand the total K extent must be multiples of this value (defaults to 1). - βswapAB (
Bool): Whether to swap the A and B operand roles in the output writer for the small-M strategy, transposing the tile and block coordinate mapping (defaults toFalse).
Implemented traitsβ
comptime membersβ
a_smem_layoutβ
comptime a_smem_layout = tile_layout_k_major[a_type, block_tile_shape[Int(0)], block_tile_shape[Int(2)], a_swizzle]()
accum_typeβ
comptime accum_type = get_accum_type[a_type]()
AccumRegTileβ
comptime AccumRegTile = LayoutTensor[HopperMatmulSM90Kernel[a_type, b_type, c_type, a_layout, b_layout, c_layout, c_smem_layout, block_tile_shape, wgmma_shape, cluster_shape, num_pipeline_stages, num_threads, transpose_b, a_swizzle, b_swizzle, c_swizzle, partitioned_multicast, use_tma_store, promotion_frequency, pdl_level, elementwise_lambda_fn, elementwise_compute_lambda_fn, hilbert_swizzle, k_group_size, swapAB].accum_type, Layout.row_major(Int((mul (block_tile_shape[Int(1)] // wgmma_shape[Int(1)]), ((block_tile_shape[Int(0)] // wgmma_shape[Int(0)]) // Int((add (num_threads // Int(128)), -1))))), (Int((mul wgmma_shape[Int(0)], wgmma_shape[Int(1)])) // Int(128))), MutAnyOrigin, address_space=AddressSpace.LOCAL]
adjusted_num_pipeline_stagesβ
comptime adjusted_num_pipeline_stages = (num_pipeline_stages // k_group_size)
b_smem_layoutβ
comptime b_smem_layout = tile_layout_k_major[b_type, block_tile_shape[Int(1)], block_tile_shape[Int(2)], b_swizzle]()
BKβ
comptime BK = block_tile_shape[Int(2)]
BMβ
comptime BM = block_tile_shape[Int(0)]
BNβ
comptime BN = block_tile_shape[Int(1)]
c_frag_sizeβ
comptime c_frag_size = (Int((mul wgmma_shape[Int(0)], wgmma_shape[Int(1)])) // Int(128))
cluster_sizeβ
comptime cluster_size = SIMD(((cluster_shape[Int(0)] * cluster_shape[Int(1)]) * cluster_shape[Int(2)]))
num_consumerβ
comptime num_consumer = ((num_threads // Int(128)) - Int(1))
num_consumer_threadsβ
comptime num_consumer_threads = (HopperMatmulSM90Kernel[a_type, b_type, c_type, a_layout, b_layout, c_layout, c_smem_layout, block_tile_shape, wgmma_shape, cluster_shape, num_pipeline_stages, num_threads, transpose_b, a_swizzle, b_swizzle, c_swizzle, partitioned_multicast, use_tma_store, promotion_frequency, pdl_level, elementwise_lambda_fn, elementwise_compute_lambda_fn, hilbert_swizzle, k_group_size, swapAB].num_consumer * Int(128))
num_m_mmasβ
comptime num_m_mmas = ((block_tile_shape[Int(0)] // wgmma_shape[Int(0)]) // HopperMatmulSM90Kernel[a_type, b_type, c_type, a_layout, b_layout, c_layout, c_smem_layout, block_tile_shape, wgmma_shape, cluster_shape, num_pipeline_stages, num_threads, transpose_b, a_swizzle, b_swizzle, c_swizzle, partitioned_multicast, use_tma_store, promotion_frequency, pdl_level, elementwise_lambda_fn, elementwise_compute_lambda_fn, hilbert_swizzle, k_group_size, swapAB].num_consumer)
num_n_mmasβ
comptime num_n_mmas = (block_tile_shape[Int(1)] // wgmma_shape[Int(1)])
SMemβ
comptime SMem = HopperMatmulSM90Kernel_SMem[a_type, b_type, c_type, block_tile_shape[Int(0)], block_tile_shape[Int(1)], block_tile_shape[Int(2)], c_smem_layout.shape[0].value(), c_smem_layout.shape[1].value(), num_pipeline_stages, k_group_size]
TMABarrierβ
comptime TMABarrier = TMABarrierHandler[(Int((add (mul Layout(Coord(Coord(ComptimeInt(), ComptimeInt()), Coord(ComptimeInt(), ComptimeInt())), Coord(Coord(ComptimeInt(), ComptimeInt()), Coord(ComptimeInt(), ComptimeInt()))).product(), size_of[a_type](), num_pipeline_stages), (mul Layout(Coord(Coord(ComptimeInt(), ComptimeInt()), Coord(ComptimeInt(), ComptimeInt())), Coord(Coord(ComptimeInt(), ComptimeInt()), Coord(ComptimeInt(), ComptimeInt()))).product(), size_of[b_type](), num_pipeline_stages))) // HopperMatmulSM90Kernel[a_type, b_type, c_type, a_layout, b_layout, c_layout, c_smem_layout, block_tile_shape, wgmma_shape, cluster_shape, num_pipeline_stages, num_threads, transpose_b, a_swizzle, b_swizzle, c_swizzle, partitioned_multicast, use_tma_store, promotion_frequency, pdl_level, elementwise_lambda_fn, elementwise_compute_lambda_fn, hilbert_swizzle, k_group_size, swapAB].adjusted_num_pipeline_stages)]
WgmmaOpβ
comptime WgmmaOp = TensorCoreAsync[HopperMatmulSM90Kernel[a_type, b_type, c_type, a_layout, b_layout, c_layout, c_smem_layout, block_tile_shape, wgmma_shape, cluster_shape, num_pipeline_stages, num_threads, transpose_b, a_swizzle, b_swizzle, c_swizzle, partitioned_multicast, use_tma_store, promotion_frequency, pdl_level, elementwise_lambda_fn, elementwise_compute_lambda_fn, hilbert_swizzle, k_group_size, swapAB].accum_type, a_type, b_type, wgmma_shape, a_swizzle, b_swizzle, transpose_b]
Methodsβ
validate_constraintsβ
static def validate_constraints()
Validate common constraints for all kernel variants.
pipeline_initβ
static def pipeline_init()
Initialize pipeline synchronization barriers.
This function ensures that all pipeline initialization (barriers, shared memory) is visible to all thread blocks in the cluster before proceeding. This is critical for correct producer-consumer synchronization.
For multi-cluster configurations, uses fence and cluster sync. For single block, uses a simple barrier.
finalize_kernelβ
static def finalize_kernel()
Common finalization for all kernel variants.
multicast_maskβ
static def multicast_mask(rank_m: Int, rank_n: Int) -> Tuple[Int32, Int32]
Returns:
common_kernel_initβ
static def common_kernel_init() -> Tuple[Int, Int, Int, Int, Int, Bool]
Common initialization for all kernel variants.
Returns:
Tuple[Int, Int, Int, Int, Int, Bool]: Tuple of (warp_group_idx, warp_group_thread_idx,
rank_m, rank_n, warp_id, lane_predicate).
setup_producerβ
static def setup_producer() -> Int
Setup producer warp group by deallocating registers.
Returns:
Int: Number of registers deallocated.
setup_consumerβ
static def setup_consumer(warp_group_idx: Int) -> Tuple[Int, LayoutTensor[Self.accum_type, Layout.row_major(Int((mul (block_tile_shape[Int(1)] // wgmma_shape[Int(1)]), ((block_tile_shape[Int(0)] // wgmma_shape[Int(0)]) // Int((add (num_threads // Int(128)), -1))))), (Int((mul wgmma_shape[Int(0)], wgmma_shape[Int(1)])) // Int(128))), MutAnyOrigin, address_space=AddressSpace.LOCAL], LayoutTensor[Self.accum_type, Layout.row_major(Int((mul (block_tile_shape[Int(1)] // wgmma_shape[Int(1)]), ((block_tile_shape[Int(0)] // wgmma_shape[Int(0)]) // Int((add (num_threads // Int(128)), -1))))), (Int((mul wgmma_shape[Int(0)], wgmma_shape[Int(1)])) // Int(128))), MutAnyOrigin, address_space=AddressSpace.LOCAL]]
Setup consumer warp group.
Returns:
consumer_arrive_empty_barriersβ
static def consumer_arrive_empty_barriers(warp_group_thread_idx: Int, mut pipeline: ProducerConsumerPipeline[Self.adjusted_num_pipeline_stages])
Signal initial empty barrier arrival for all pipeline stages.
Must be called by consumer warp groups before the main loop so the producer knows it can start filling stages.
Args:
- βwarp_group_thread_idx (
Int): Thread index within the warp group, used to select which threads signal the barrier. - βpipeline (
ProducerConsumerPipeline[Self.adjusted_num_pipeline_stages]): Producer-consumer pipeline whose empty barriers are signaled (modified in place).
get_block_swizzleβ
static def get_block_swizzle(lut_ptr: OptionalReg[Pointer[UInt32, MutAnyOrigin, _safe=False]] = None) -> IndexList[Int(2), element_type=DType.uint32]
Calculate block swizzle for better L2 cache locality.
Args:
- βlut_ptr (
OptionalReg[Pointer[UInt32, MutAnyOrigin, _safe=False]]): Lookup table for Hilbert curve block scheduling (optional).
Returns:
IndexList[Int(2), element_type=DType.uint32]: Swizzled block indices.
consumer_outputβ
static def consumer_output[custom_elementwise_lambda_fn: Optional[def[dtype: DType, width: SIMDLength, *, alignment: Int = Int(1)](IndexList[Int(2)], SIMD[dtype, width]) capturing thin -> None] = elementwise_lambda_fn](c_tma_op: TMATensorTile[c_type], c: TileTensor[c_type, Storage=c.Storage, linear_idx_type=c.linear_idx_type], c_tile: TileTensor[c_type, Layout[*?, *?], MutAnyOrigin, address_space=AddressSpace.SHARED], output_reg_tile: LayoutTensor[Self.accum_type, Layout.row_major(Int((mul (block_tile_shape[Int(1)] // wgmma_shape[Int(1)]), ((block_tile_shape[Int(0)] // wgmma_shape[Int(0)]) // Int((add (num_threads // Int(128)), -1))))), (Int((mul wgmma_shape[Int(0)], wgmma_shape[Int(1)])) // Int(128))), MutAnyOrigin, address_space=AddressSpace.LOCAL], warp_group_thread_idx: Int, local_warp_group_idx: Int, local_thread_idx: Int, block_y: Int, block_x: Int)
Handle consumer output by writing GEMM results to global memory.
Parameters:
- βcustom_elementwise_lambda_fn (
Optional[def[dtype: DType, width: SIMDLength, *, alignment: Int = Int(1)](IndexList[Int(2)], SIMD[dtype, width]) capturing thin -> None]): Optional epilogue function applied to output elements (defaults to the struct'selementwise_lambda_fn).
Args:
- βc_tma_op (
TMATensorTile[c_type]): TMA descriptor for the output matrix C, used for TMA stores. - βc (
TileTensor[c_type, Storage=c.Storage, linear_idx_type=c.linear_idx_type]): Writable output matrix C tile tensor. - βc_tile (
TileTensor[c_type, Layout[*?, *?], MutAnyOrigin, address_space=AddressSpace.SHARED]): Shared memory tile staging the output before the global write. - βoutput_reg_tile (
LayoutTensor[Self.accum_type, Layout.row_major(Int((mul (block_tile_shape[Int(1)] // wgmma_shape[Int(1)]), ((block_tile_shape[Int(0)] // wgmma_shape[Int(0)]) // Int((add (num_threads // Int(128)), -1))))), (Int((mul wgmma_shape[Int(0)], wgmma_shape[Int(1)])) // Int(128))), MutAnyOrigin, address_space=AddressSpace.LOCAL]): Register tile holding the accumulated GEMM result to write. - βwarp_group_thread_idx (
Int): Thread index within the warp group. - βlocal_warp_group_idx (
Int): Index of this consumer warp group (0-based). - βlocal_thread_idx (
Int): Thread index within the consumer warp group. - βblock_y (
Int): Block-level M coordinate (row) of the output tile. - βblock_x (
Int): Block-level N coordinate (column) of the output tile.
build_tma_loadersβ
static def build_tma_loaders[a_tma_rank: Int, b_tma_rank: Int, a_tile_shape: IndexList[a_tma_rank], b_tile_shape: IndexList[b_tma_rank], a_desc_shape: IndexList[a_tma_rank], b_desc_shape: IndexList[b_tma_rank], //](a_tma_op: TMATensorTile[a_type, a_tma_rank, a_tile_shape, a_desc_shape], b_tma_op: TMATensorTile[b_type, b_tma_rank, b_tile_shape, b_desc_shape], rank_m: Int, rank_n: Int) -> Tuple[TileLoaderTMA[origin_of(a_tma_op), a_type, a_tma_rank, a_tile_shape, a_desc_shape, BK=block_tile_shape[Int(2)], cluster_size=cluster_shape[Int(0)], use_partitioned_multicast=partitioned_multicast], TileLoaderTMA[origin_of(b_tma_op), b_type, b_tma_rank, b_tile_shape, b_desc_shape, BK=block_tile_shape[Int(2)], cluster_size=cluster_shape[Int(1)], use_partitioned_multicast=partitioned_multicast]]
Returns:
build_cpasync_loadersβ
static def build_cpasync_loaders[k_align: Int, vector_size: Int = (k_align // size_of[a_type]()), num_threads_per_row: Int = (block_tile_shape[Int(2)] // vector_size), thread_layout: Layout[thread_layout.shape_types, thread_layout.stride_types] = row_major[(_resolve_warpgroup_size() // num_threads_per_row), num_threads_per_row]()](a: TileTensor[a_type, a_layout, ImmutAnyOrigin], b: TileTensor[b_type, b_layout, ImmutAnyOrigin]) -> Tuple[TileLoaderCPAsync[a_type, a_layout, thread_layout, a_swizzle, vector_size], TileLoaderCPAsync[b_type, b_layout, thread_layout, b_swizzle, vector_size]]
Returns:
producer_main_loop_pipelineβ
static def producer_main_loop_pipeline[a_loader_type: TileLoader, b_loader_type: TileLoader, barrier_handler_type: BarrierHandler, //, num_k_iters: Int](m_coord: Int, n_coord: Int, k_coord: Int, a_loader: a_loader_type, b_loader: b_loader_type, barrier_handler: barrier_handler_type, mut pipeline: ProducerConsumerPipeline[Self.adjusted_num_pipeline_stages], a_tiles: SMemTileArrayWithLayout[a_type, Layout(Coord(Coord(ComptimeInt(), ComptimeInt()), Coord(ComptimeInt(), ComptimeInt())), Coord(Coord(ComptimeInt(), ComptimeInt()), Coord(ComptimeInt(), ComptimeInt()))), num_pipeline_stages], b_tiles: SMemTileArrayWithLayout[b_type, Layout(Coord(Coord(ComptimeInt(), ComptimeInt()), Coord(ComptimeInt(), ComptimeInt())), Coord(Coord(ComptimeInt(), ComptimeInt()), Coord(ComptimeInt(), ComptimeInt()))), num_pipeline_stages])
runβ
static def run[a_tma_rank: Int, b_tma_rank: Int, c_tma_rank: Int, a_tile_shape: IndexList[a_tma_rank], b_tile_shape: IndexList[b_tma_rank], c_tile_shape: IndexList[c_tma_rank], a_desc_shape: IndexList[a_tma_rank], b_desc_shape: IndexList[b_tma_rank], c_desc_shape: IndexList[c_tma_rank], a_tensor_layout: TensorLayout, b_tensor_layout: TensorLayout, c_tensor_layout: TensorLayout](a_tma_op: TMATensorTile[a_type, a_tma_rank, a_tile_shape, a_desc_shape], b_tma_op: TMATensorTile[b_type, b_tma_rank, b_tile_shape, b_desc_shape], c_tma_op: TMATensorTile[c_type, c_tma_rank, c_tile_shape, c_desc_shape], a: TileTensor[a_type, a_tensor_layout, ImmutAnyOrigin], b: TileTensor[b_type, b_tensor_layout, ImmutAnyOrigin], c: TileTensor[c_type, c_tensor_layout, MutAnyOrigin], lut_ptr: Pointer[UInt32, MutAnyOrigin, _safe=False])
Main kernel entry point for matrix multiplication.
This kernel implements a producer-consumer pattern where:
- One warp group (producer) loads tiles from global memory using TMA
- Multiple warp groups (consumers) perform matrix multiplication using tensor cores
The kernel uses software pipelining to overlap memory transfers with computation, achieving high throughput on Hopper GPUs.
Parameters:
- βa_tma_rank (
Int): Number of dimensions in the TMA descriptor for matrix A. - βb_tma_rank (
Int): Number of dimensions in the TMA descriptor for matrix B. - βc_tma_rank (
Int): Number of dimensions in the TMA descriptor for matrix C. - βa_tile_shape (
IndexList[a_tma_rank]): Shape of each A tile loaded by TMA. - βb_tile_shape (
IndexList[b_tma_rank]): Shape of each B tile loaded by TMA. - βc_tile_shape (
IndexList[c_tma_rank]): Shape of each C tile stored by TMA. - βa_desc_shape (
IndexList[a_tma_rank]): Full shape of matrix A as described by the TMA descriptor. - βb_desc_shape (
IndexList[b_tma_rank]): Full shape of matrix B as described by the TMA descriptor. - βc_desc_shape (
IndexList[c_tma_rank]): Full shape of matrix C as described by the TMA descriptor. - βa_tensor_layout (
TensorLayout): Memory layout of input matrix A. - βb_tensor_layout (
TensorLayout): Memory layout of input matrix B. - βc_tensor_layout (
TensorLayout): Memory layout of output matrix C.
Args:
- βa_tma_op (
TMATensorTile[a_type, a_tma_rank, a_tile_shape, a_desc_shape]): TMA descriptor for matrix A. - βb_tma_op (
TMATensorTile[b_type, b_tma_rank, b_tile_shape, b_desc_shape]): TMA descriptor for matrix B. - βc_tma_op (
TMATensorTile[c_type, c_tma_rank, c_tile_shape, c_desc_shape]): TMA descriptor for matrix C. - βa (
TileTensor[a_type, a_tensor_layout, ImmutAnyOrigin]): Input matrix A. - βb (
TileTensor[b_type, b_tensor_layout, ImmutAnyOrigin]): Input matrix B. - βc (
TileTensor[c_type, c_tensor_layout, MutAnyOrigin]): Output matrix C. - βlut_ptr (
Pointer[UInt32, MutAnyOrigin, _safe=False]): Lookup table for Hilbert curve block scheduling (optional).
run_splitkβ
static def run_splitk[a_tma_rank: Int, b_tma_rank: Int, c_tma_rank: Int, a_tile_shape: IndexList[a_tma_rank], b_tile_shape: IndexList[b_tma_rank], c_tile_shape: IndexList[c_tma_rank], a_desc_shape: IndexList[a_tma_rank], b_desc_shape: IndexList[b_tma_rank], c_desc_shape: IndexList[c_tma_rank], splits: Int, raster_order: RasterOrder, c_tensor_layout: TensorLayout](a_tma_op: TMATensorTile[a_type, a_tma_rank, a_tile_shape, a_desc_shape], b_tma_op: TMATensorTile[b_type, b_tma_rank, b_tile_shape, b_desc_shape], c_tma_op: TMATensorTile[c_type, c_tma_rank, c_tile_shape, c_desc_shape], c: TileTensor[c_type, c_tensor_layout, MutAnyOrigin], workspace_ptr: Pointer[Scalar[Self.accum_type], MutAnyOrigin, _safe=False], locks_ptr: Pointer[UInt8, MutAnyOrigin, _safe=False], problem_shape: IndexList[Int(3)])
Split-K variant of the kernel for better load balancing on small problems.
Parameters:
- βa_tma_rank (
Int): Number of dimensions in the TMA descriptor for matrix A. - βb_tma_rank (
Int): Number of dimensions in the TMA descriptor for matrix B. - βc_tma_rank (
Int): Number of dimensions in the TMA descriptor for matrix C. - βa_tile_shape (
IndexList[a_tma_rank]): Shape of each A tile loaded by TMA. - βb_tile_shape (
IndexList[b_tma_rank]): Shape of each B tile loaded by TMA. - βc_tile_shape (
IndexList[c_tma_rank]): Shape of each C tile stored by TMA. - βa_desc_shape (
IndexList[a_tma_rank]): Full shape of matrix A as described by the TMA descriptor. - βb_desc_shape (
IndexList[b_tma_rank]): Full shape of matrix B as described by the TMA descriptor. - βc_desc_shape (
IndexList[c_tma_rank]): Full shape of matrix C as described by the TMA descriptor. - βsplits (
Int): Number of equal chunks the K dimension is divided into for parallel reduction. Each block processes one chunk per output tile. - βraster_order (
RasterOrder): Tile rasterization order used by the split-K scheduler to assign output tiles to blocks. - βc_tensor_layout (
TensorLayout): Memory layout of output matrix C.
Args:
- βa_tma_op (
TMATensorTile[a_type, a_tma_rank, a_tile_shape, a_desc_shape]): TMA descriptor for matrix A. - βb_tma_op (
TMATensorTile[b_type, b_tma_rank, b_tile_shape, b_desc_shape]): TMA descriptor for matrix B. - βc_tma_op (
TMATensorTile[c_type, c_tma_rank, c_tile_shape, c_desc_shape]): TMA descriptor for matrix C. - βc (
TileTensor[c_type, c_tensor_layout, MutAnyOrigin]): Output matrix C. - βworkspace_ptr (
Pointer[Scalar[Self.accum_type], MutAnyOrigin, _safe=False]): Pointer to the reduction workspace storing partial accumulations from each split. - βlocks_ptr (
Pointer[UInt8, MutAnyOrigin, _safe=False]): Pointer to the lock array coordinating split-K synchronization across blocks. - βproblem_shape (
IndexList[Int(3)]): Full GEMM problem dimensions[M, N, K].
run_groupedβ
static def run_grouped[a_tma_rank: Int, b_tma_rank: Int, c_tma_rank: Int, a_tile_shape: IndexList[a_tma_rank], b_tile_shape: IndexList[b_tma_rank], c_tile_shape: IndexList[c_tma_rank], a_desc_shape: IndexList[a_tma_rank], b_desc_shape: IndexList[b_tma_rank], c_desc_shape: IndexList[c_tma_rank], AOffsetsLayout: TensorLayout, ExpertIdsLayout: TensorLayout, c_tensor_layout: TensorLayout](a_tma_op: TMATensorTile[a_type, a_tma_rank, a_tile_shape, a_desc_shape], b_tma_op: TMATensorTile[b_type, b_tma_rank, b_tile_shape, b_desc_shape], c_tma_op: TMATensorTile[c_type, c_tma_rank, c_tile_shape, c_desc_shape], a_offsets: TileTensor[DType.uint32, AOffsetsLayout, ImmUnsafeAnyOrigin], expert_ids: TileTensor[DType.int32, ExpertIdsLayout, ImmUnsafeAnyOrigin], c: TileTensor[c_type, c_tensor_layout, MutAnyOrigin])
Grouped matmul variant for MoE (Mixture of Experts) models.
This variant handles multiple experts where each expert processes a subset of tokens. The a_offsets array indicates token boundaries for each expert.
Parameters:
- βa_tma_rank (
Int): Number of dimensions in the TMA descriptor for matrix A. - βb_tma_rank (
Int): Number of dimensions in the TMA descriptor for matrix B. - βc_tma_rank (
Int): Number of dimensions in the TMA descriptor for matrix C. - βa_tile_shape (
IndexList[a_tma_rank]): Shape of each A tile loaded by TMA. - βb_tile_shape (
IndexList[b_tma_rank]): Shape of each B tile loaded by TMA. - βc_tile_shape (
IndexList[c_tma_rank]): Shape of each C tile stored by TMA. - βa_desc_shape (
IndexList[a_tma_rank]): Full shape of matrix A as described by the TMA descriptor. - βb_desc_shape (
IndexList[b_tma_rank]): Full shape of matrix B as described by the TMA descriptor. - βc_desc_shape (
IndexList[c_tma_rank]): Full shape of matrix C as described by the TMA descriptor. - βAOffsetsLayout (
TensorLayout): Memory layout of thea_offsetstensor. - βExpertIdsLayout (
TensorLayout): Memory layout of theexpert_idstensor. - βc_tensor_layout (
TensorLayout): Memory layout of output matrix C.
Args:
- βa_tma_op (
TMATensorTile[a_type, a_tma_rank, a_tile_shape, a_desc_shape]): TMA descriptor for matrix A. - βb_tma_op (
TMATensorTile[b_type, b_tma_rank, b_tile_shape, b_desc_shape]): TMA descriptor for matrix B. - βc_tma_op (
TMATensorTile[c_type, c_tma_rank, c_tile_shape, c_desc_shape]): TMA descriptor for matrix C. - βa_offsets (
TileTensor[DType.uint32, AOffsetsLayout, ImmUnsafeAnyOrigin]): Starting row offsets into matrix A for each expert. The token count for expertiis `a_offsets[i+1]- a_offsets[i]
, indexed byblock_idx.z`.
- a_offsets[i]
- βexpert_ids (
TileTensor[DType.int32, ExpertIdsLayout, ImmUnsafeAnyOrigin]): Expert index selected by each block.expert_ids[block_idx.z]picks the row block in B for this block, and -1 marks an inactive block whose output is zeroed. - βc (
TileTensor[c_type, c_tensor_layout, MutAnyOrigin]): Output matrix C.
consumer_main_loop_pipelineβ
static def consumer_main_loop_pipeline[num_k_iters: Int](wgmma_op: TensorCoreAsync[Self.accum_type, a_type, b_type, wgmma_shape, a_swizzle, b_swizzle, transpose_b], local_warp_group_idx: Int, final_c_reg_tile: LayoutTensor[Self.accum_type, Layout.row_major(Int((mul (block_tile_shape[Int(1)] // wgmma_shape[Int(1)]), ((block_tile_shape[Int(0)] // wgmma_shape[Int(0)]) // Int((add (num_threads // Int(128)), -1))))), (Int((mul wgmma_shape[Int(0)], wgmma_shape[Int(1)])) // Int(128))), MutAnyOrigin, address_space=AddressSpace.LOCAL], c_reg_tile: LayoutTensor[Self.accum_type, Layout.row_major(Int((mul (block_tile_shape[Int(1)] // wgmma_shape[Int(1)]), ((block_tile_shape[Int(0)] // wgmma_shape[Int(0)]) // Int((add (num_threads // Int(128)), -1))))), (Int((mul wgmma_shape[Int(0)], wgmma_shape[Int(1)])) // Int(128))), MutAnyOrigin, address_space=AddressSpace.LOCAL], mut pipeline: ProducerConsumerPipeline[Self.adjusted_num_pipeline_stages], a_tiles: SMemTileArrayWithLayout[a_type, Layout(Coord(Coord(ComptimeInt(), ComptimeInt()), Coord(ComptimeInt(), ComptimeInt())), Coord(Coord(ComptimeInt(), ComptimeInt()), Coord(ComptimeInt(), ComptimeInt()))), num_pipeline_stages], b_tiles: SMemTileArrayWithLayout[b_type, Layout(Coord(Coord(ComptimeInt(), ComptimeInt()), Coord(ComptimeInt(), ComptimeInt())), Coord(Coord(ComptimeInt(), ComptimeInt()), Coord(ComptimeInt(), ComptimeInt()))), num_pipeline_stages], warp_group_thread_idx: Int)
Pipeline-based consumer loop using ProducerConsumerPipeline.
This is an alternative implementation of consumer_main_loop that uses the SM100 ProducerConsumerPipeline for synchronization instead of RingBuffer.
Parameters:
- βnum_k_iters (
Int): Number of K-dimension tiles the consumer processes in this loop.
Args:
- βwgmma_op (
TensorCoreAsync[Self.accum_type, a_type, b_type, wgmma_shape, a_swizzle, b_swizzle, transpose_b]): Tensor core operator for matrix multiplication. - βlocal_warp_group_idx (
Int): Index of this consumer warp group (0-based). - βfinal_c_reg_tile (
LayoutTensor[Self.accum_type, Layout.row_major(Int((mul (block_tile_shape[Int(1)] // wgmma_shape[Int(1)]), ((block_tile_shape[Int(0)] // wgmma_shape[Int(0)]) // Int((add (num_threads // Int(128)), -1))))), (Int((mul wgmma_shape[Int(0)], wgmma_shape[Int(1)])) // Int(128))), MutAnyOrigin, address_space=AddressSpace.LOCAL]): Final accumulation register tile (for FP8 promotion). - βc_reg_tile (
LayoutTensor[Self.accum_type, Layout.row_major(Int((mul (block_tile_shape[Int(1)] // wgmma_shape[Int(1)]), ((block_tile_shape[Int(0)] // wgmma_shape[Int(0)]) // Int((add (num_threads // Int(128)), -1))))), (Int((mul wgmma_shape[Int(0)], wgmma_shape[Int(1)])) // Int(128))), MutAnyOrigin, address_space=AddressSpace.LOCAL]): Working accumulation register tile. - βpipeline (
ProducerConsumerPipeline[Self.adjusted_num_pipeline_stages]): ProducerConsumerPipeline for synchronized tile access. - βa_tiles (
SMemTileArrayWithLayout[a_type, Layout(Coord(Coord(ComptimeInt(), ComptimeInt()), Coord(ComptimeInt(), ComptimeInt())), Coord(Coord(ComptimeInt(), ComptimeInt()), Coord(ComptimeInt(), ComptimeInt()))), num_pipeline_stages]): Tile array for A matrix in shared memory. - βb_tiles (
SMemTileArrayWithLayout[b_type, Layout(Coord(Coord(ComptimeInt(), ComptimeInt()), Coord(ComptimeInt(), ComptimeInt())), Coord(Coord(ComptimeInt(), ComptimeInt()), Coord(ComptimeInt(), ComptimeInt()))), num_pipeline_stages]): Tile array for B matrix in shared memory. - βwarp_group_thread_idx (
Int): Thread index within the warp group.
promote_to_cuda_coresβ
static def promote_to_cuda_cores(c_reg_tile: LayoutTensor[Self.accum_type, Layout.row_major(Int((mul (block_tile_shape[Int(1)] // wgmma_shape[Int(1)]), ((block_tile_shape[Int(0)] // wgmma_shape[Int(0)]) // Int((add (num_threads // Int(128)), -1))))), (Int((mul wgmma_shape[Int(0)], wgmma_shape[Int(1)])) // Int(128))), MutAnyOrigin, address_space=AddressSpace.LOCAL], final_c_reg_tile: LayoutTensor[Self.accum_type, Layout.row_major(Int((mul (block_tile_shape[Int(1)] // wgmma_shape[Int(1)]), ((block_tile_shape[Int(0)] // wgmma_shape[Int(0)]) // Int((add (num_threads // Int(128)), -1))))), (Int((mul wgmma_shape[Int(0)], wgmma_shape[Int(1)])) // Int(128))), MutAnyOrigin, address_space=AddressSpace.LOCAL])
Promote FP8 accumulation to higher precision using CUDA cores.
When using FP8 data types, tensor cores accumulate in limited precision. To maintain accuracy over many accumulations, we periodically add the intermediate results to a higher-precision accumulator using CUDA cores.
This technique is commonly used in production libraries like cuBLAS to achieve both high performance (from FP8 tensor cores) and good accuracy.
Args:
- βc_reg_tile (
LayoutTensor[Self.accum_type, Layout.row_major(Int((mul (block_tile_shape[Int(1)] // wgmma_shape[Int(1)]), ((block_tile_shape[Int(0)] // wgmma_shape[Int(0)]) // Int((add (num_threads // Int(128)), -1))))), (Int((mul wgmma_shape[Int(0)], wgmma_shape[Int(1)])) // Int(128))), MutAnyOrigin, address_space=AddressSpace.LOCAL]): Current accumulation from tensor cores. - βfinal_c_reg_tile (
LayoutTensor[Self.accum_type, Layout.row_major(Int((mul (block_tile_shape[Int(1)] // wgmma_shape[Int(1)]), ((block_tile_shape[Int(0)] // wgmma_shape[Int(0)]) // Int((add (num_threads // Int(128)), -1))))), (Int((mul wgmma_shape[Int(0)], wgmma_shape[Int(1)])) // Int(128))), MutAnyOrigin, address_space=AddressSpace.LOCAL]): Higher-precision accumulator (updated in place).
wgmmaβ
static def wgmma(wgmma_op: TensorCoreAsync[Self.accum_type, a_type, b_type, wgmma_shape, a_swizzle, b_swizzle, transpose_b], local_warp_group_idx: Int, a_tile: TileTensor[a_type, Layout[*?, *?], MutAnyOrigin, address_space=AddressSpace.SHARED], b_tile: TileTensor[b_type, Layout[*?, *?], MutAnyOrigin, address_space=AddressSpace.SHARED], c_reg_tile: LayoutTensor[Self.accum_type, Layout.row_major(Int((mul (block_tile_shape[Int(1)] // wgmma_shape[Int(1)]), ((block_tile_shape[Int(0)] // wgmma_shape[Int(0)]) // Int((add (num_threads // Int(128)), -1))))), (Int((mul wgmma_shape[Int(0)], wgmma_shape[Int(1)])) // Int(128))), MutAnyOrigin, address_space=AddressSpace.LOCAL])
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!