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
AppleM5Int8MatMul
struct AppleM5Int8MatMul[c_type: DType = DType.bfloat16, *, has_bias: Bool = False, BM: Int = Int(64), BN: Int = Int(64), BK: Int = Int(64), TTI32: Bool = False]
W8A8 GEMM: int8 A x int8 B^T -> int32 accum -> per-row/col dequant.
run is the GPU kernel entry (TileTensor operands + M/N/K). Launch via
enqueue_apple_int8_matmul.
Parametersβ
- βc_type (
DType): Output element type (bf16 / fp16 / fp32). Accumulation is int32; the dequant multiply is done in fp32 then cast toc_type. - βhas_bias (
Bool): If True, add a per-output-column bias (inc_type) after dequant. - βBM (
Int): Threadgroup M-tile height (multiple ofSG_M). - βBN (
Int): Threadgroup N-tile width (multiple ofSG_N). - βBK (
Int): K-strip depth per accumulate step (multiple ofMMA_K= 16; default 64 = four 16-wide K-blocks). - βTTI32 (
Bool): If True, use int32load_linearfor the interior A/B loads (faster on NA-bound shapes, numerically identical). Auto-selected byenqueue_apple_int8_matmulwhenmax(M*K, N*K) < 2^31, else i64.
Implemented traitsβ
comptime membersβ
Mmaβ
comptime Mma = MmaOpApple[DType.int32, DType.int8, Int(2), Int(2), transpose_b=True]
MMA_Kβ
comptime MMA_K = 16
MMA_Mβ
comptime MMA_M = 16
MMA_Nβ
comptime MMA_N = 16
NUM_MMA_Mβ
comptime NUM_MMA_M = 2
NUM_MMA_Nβ
comptime NUM_MMA_N = 2
NUM_SGβ
comptime NUM_SG = (AppleM5Int8MatMul[c_type, has_bias=has_bias, BM=BM, BN=BN, BK=BK, TTI32=TTI32].NUM_SG_M * AppleM5Int8MatMul[c_type, has_bias=has_bias, BM=BM, BN=BN, BK=BK, TTI32=TTI32].NUM_SG_N)
NUM_SG_Mβ
comptime NUM_SG_M = (BM // Int(32))
NUM_SG_Nβ
comptime NUM_SG_N = (BN // Int(32))
SG_Mβ
comptime SG_M = 32
SG_Nβ
comptime SG_N = 32
THREADS_PER_BLOCKβ
comptime THREADS_PER_BLOCK = (AppleM5Int8MatMul[c_type, has_bias=has_bias, BM=BM, BN=BN, BK=BK, TTI32=TTI32].NUM_SG * _resolve_warp_size())
Methodsβ
morton_decode_2dβ
static def morton_decode_2d(flat_idx: UInt32) -> Tuple[UInt32, UInt32]
Returns:
morton_decode_2d_rectβ
static def morton_decode_2d_rect(flat_idx: UInt32, log2_m: UInt32, log2_n: UInt32) -> Tuple[UInt32, UInt32]
Returns:
runβ
static def run[c_layout: TensorLayout, a_layout: TensorLayout, b_layout: TensorLayout, as_layout: TensorLayout, bs_layout: TensorLayout, bias_layout: TensorLayout](c: TileTensor[c_type, c_layout, MutAnyOrigin], a: TileTensor[DType.int8, a_layout, ImmutAnyOrigin], b: TileTensor[DType.int8, b_layout, ImmutAnyOrigin], a_scale: TileTensor[DType.float32, as_layout, ImmutAnyOrigin], b_scale: TileTensor[DType.float32, bs_layout, ImmutAnyOrigin], bias: TileTensor[c_type, bias_layout, ImmutAnyOrigin], log2_grid_m: UInt32, log2_grid_n: UInt32)
W8A8 kernel entry. C (M, N), A (M, K) int8, B (N, K) int8 (transpose_b), a_scale (M,), b_scale (N,), bias (N,) (used iff has_bias). Grid is (1<<log2_grid_m) * (1<<log2_grid_n) threadgroups; OOB threadgroups early-return after Morton decode.
Parameters:
- βc_layout (
TensorLayout):TensorLayoutof the C outputTileTensor. - βa_layout (
TensorLayout):TensorLayoutof the int8 ATileTensor. - βb_layout (
TensorLayout):TensorLayoutof the int8 BTileTensor. - βas_layout (
TensorLayout):TensorLayoutof the per-row activation scaleTileTensor. - βbs_layout (
TensorLayout):TensorLayoutof the per-column weight scaleTileTensor. - βbias_layout (
TensorLayout):TensorLayoutof the per-column biasTileTensor.
Args:
- βc (
TileTensor[c_type, c_layout, MutAnyOrigin]): OutputTileTensorof shape(M, N)inc_type. - βa (
TileTensor[DType.int8, a_layout, ImmutAnyOrigin]): Int8 activationTileTensorof shape(M, K). - βb (
TileTensor[DType.int8, b_layout, ImmutAnyOrigin]): Int8 weightTileTensorof shape(N, K)(used transposed). - βa_scale (
TileTensor[DType.float32, as_layout, ImmutAnyOrigin]): Per-row fp32 activation scale, shape(M,). - βb_scale (
TileTensor[DType.float32, bs_layout, ImmutAnyOrigin]): Per-column fp32 weight scale, shape(N,). - βbias (
TileTensor[c_type, bias_layout, ImmutAnyOrigin]): Per-column bias inc_type, shape(N,)(used iffhas_bias). - βlog2_grid_m (
UInt32): Base-2 log of the power-of-two-padded M-side grid extent. - βlog2_grid_n (
UInt32): Base-2 log of the power-of-two-padded N-side grid extent.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!