For the complete documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /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.