IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /max/get-started.md). For the complete documentation index, see llms.txt.
Skip to main content
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 function

matmul_kernel_naive

def matmul_kernel_naive[c_type: DType, a_type: DType, b_type: DType, c_layout_type: TensorLayout, a_layout_type: TensorLayout, b_layout_type: TensorLayout, BLOCK_DIM: Int, transpose_b: 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, s_type: DType = get_accum_type[c_type](), c_storage: TensorStorage = PointerStorage, a_storage: TensorStorage = PointerStorage, b_storage: TensorStorage = PointerStorage](c: TileTensor[c_type, c_layout_type, MutAnyOrigin, Storage=c_storage], a: TileTensor[a_type, a_layout_type, ImmutAnyOrigin, Storage=a_storage], b: TileTensor[b_type, b_layout_type, ImmutAnyOrigin, Storage=b_storage], m: Int, n: Int, k: Int)

Computes one output element of a matrix multiply per thread by iterating over the K dimension.

Each thread maps to a single (x, y) coordinate of c and accumulates the dot product of the corresponding row of a and column of b (or row of b when transpose_b is set). When elementwise_lambda_fn is supplied the accumulated value is passed through it instead of being stored directly.

Parameters:

  • ​c_type (DType): DType of the output tile c elements.
  • ​a_type (DType): DType of the input tile a elements.
  • ​b_type (DType): DType of the input tile b elements.
  • ​c_layout_type (TensorLayout): Tensor layout of the output tile c.
  • ​a_layout_type (TensorLayout): Tensor layout of the input tile a.
  • ​b_layout_type (TensorLayout): Tensor layout of the input tile b.
  • ​BLOCK_DIM (Int): Thread-block dimension used when launching the kernel.
  • ​transpose_b (Bool): Whether b is accessed transposed, so its row y is read as b[y, i] instead of b[i, y] (defaults to False).
  • ​elementwise_lambda_fn (Optional[def[dtype: DType, width: SIMDLength, *, alignment: Int = Int(1)](IndexList[Int(2)], SIMD[dtype, width]) capturing thin -> None]): Optional epilogue applied to the accumulated value before it is stored to c (defaults to None, which stores the raw accumulation).
  • ​s_type (DType): DType used for the inner accumulation (defaults to the accumulator type for c_type).
  • ​c_storage (TensorStorage): Storage type of the output tile c (defaults to PointerStorage[element_width=1]).
  • ​a_storage (TensorStorage): Storage type of the input tile a (defaults to PointerStorage[element_width=1]).
  • ​b_storage (TensorStorage): Storage type of the input tile b (defaults to PointerStorage[element_width=1]).

Args:

Was this page helpful?