|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +""" |
| 8 | +metal::gather_qmv custom op for MoE expert-indexed quantized matmul. |
| 9 | +
|
| 10 | +Performs y[i] = W[expert_idx[i]] @ x[i] with INT4 quantized expert weights. |
| 11 | +The Metal fallback kernel is in runtime/ops/op_gather_qmv.mm. |
| 12 | +""" |
| 13 | + |
| 14 | +import torch |
| 15 | +from torch import Tensor |
| 16 | + |
| 17 | + |
| 18 | +@torch.library.custom_op("metal::gather_qmv", mutates_args=()) |
| 19 | +def gather_qmv( |
| 20 | + x: Tensor, # [P, K] — activations (P = num token-expert pairs) |
| 21 | + w: Tensor, # [E, N, K_packed] — packed INT4 expert weights |
| 22 | + scales: Tensor, # [E, N, K/gs] — per-group scales |
| 23 | + biases: Tensor, # [E, N, K/gs] — per-group biases |
| 24 | + expert_indices: Tensor, # [P] — expert index per pair |
| 25 | + group_size: int, |
| 26 | +) -> Tensor: |
| 27 | + """Reference implementation for tracing and CPU testing.""" |
| 28 | + P, K = x.shape |
| 29 | + E, N, K_packed = w.shape |
| 30 | + |
| 31 | + y = torch.zeros(P, N, dtype=x.dtype, device=x.device) |
| 32 | + for i in range(P): |
| 33 | + eidx = expert_indices[i].item() |
| 34 | + w_e = w[eidx] # [N, K_packed] |
| 35 | + s_e = scales[eidx] # [N, K/gs] |
| 36 | + b_e = biases[eidx] # [N, K/gs] |
| 37 | + |
| 38 | + # Dequantize: unpack INT4, apply affine dequant |
| 39 | + w_unpacked = _dequantize_int4_affine(w_e, s_e, b_e, K, group_size) |
| 40 | + y[i] = w_unpacked @ x[i] |
| 41 | + |
| 42 | + return y |
| 43 | + |
| 44 | + |
| 45 | +def _quantize_int4_affine(w: Tensor, group_size: int) -> tuple[Tensor, Tensor, Tensor]: |
| 46 | + """Quantize float weights to packed INT4 using MLX affine format. |
| 47 | +
|
| 48 | + Args: |
| 49 | + w: [..., K] float weight tensor (last dim is quantized). |
| 50 | + group_size: Number of elements per quantization group. |
| 51 | +
|
| 52 | + Returns: |
| 53 | + (packed, scales, biases) where: |
| 54 | + - packed: [..., K//2] uint8, two INT4 values per byte. |
| 55 | + - scales: [..., K//group_size] per-group scales. |
| 56 | + - biases: [..., K//group_size] per-group biases (zero points). |
| 57 | +
|
| 58 | + The affine mapping is: dequantized = raw_uint4 * scale + bias, |
| 59 | + where raw_uint4 is in [0, 15]. |
| 60 | + """ |
| 61 | + *leading, K = w.shape |
| 62 | + w_groups = w.reshape(*leading, K // group_size, group_size) |
| 63 | + g_min = w_groups.amin(dim=-1) |
| 64 | + g_max = w_groups.amax(dim=-1) |
| 65 | + scales = ((g_max - g_min) / 15.0).clamp(min=1e-8) |
| 66 | + biases = g_min |
| 67 | + w_int = ( |
| 68 | + ((w_groups - biases.unsqueeze(-1)) / scales.unsqueeze(-1)) |
| 69 | + .round() |
| 70 | + .clamp(0, 15) |
| 71 | + .to(torch.uint8) |
| 72 | + .reshape(*leading, K) |
| 73 | + ) |
| 74 | + packed = w_int[..., 0::2] | (w_int[..., 1::2] << 4) |
| 75 | + return packed, scales, biases |
| 76 | + |
| 77 | + |
| 78 | +def _dequantize_int4_affine( |
| 79 | + w_packed: Tensor, scales: Tensor, biases: Tensor, K: int, group_size: int |
| 80 | +) -> Tensor: |
| 81 | + """Dequantize packed INT4 weights using MLX affine format.""" |
| 82 | + N = w_packed.shape[0] |
| 83 | + w_bytes = w_packed.to(torch.int16) |
| 84 | + low = w_bytes & 0x0F |
| 85 | + high = (w_bytes >> 4) & 0x0F |
| 86 | + w_int = torch.stack([low, high], dim=-1).reshape(N, K).float() |
| 87 | + |
| 88 | + scales_expanded = scales.float().repeat_interleave(group_size, dim=-1)[:, :K] |
| 89 | + biases_expanded = biases.float().repeat_interleave(group_size, dim=-1)[:, :K] |
| 90 | + |
| 91 | + return (w_int * scales_expanded + biases_expanded).to(scales.dtype) |
| 92 | + |
| 93 | + |
| 94 | +@torch.library.register_fake("metal::gather_qmv") |
| 95 | +def gather_qmv_fake( |
| 96 | + x: Tensor, |
| 97 | + w: Tensor, |
| 98 | + scales: Tensor, |
| 99 | + biases: Tensor, |
| 100 | + expert_indices: Tensor, |
| 101 | + group_size: int, |
| 102 | +) -> Tensor: |
| 103 | + P = x.shape[0] |
| 104 | + N = w.shape[1] |
| 105 | + return torch.empty(P, N, dtype=x.dtype, device=x.device) |
| 106 | + |
| 107 | + |
| 108 | +# C shim mapping for AOTInductor code generation. |
| 109 | +# Maps the torch op to the C function name that the generated wrapper calls. |
| 110 | +metal_gather_qmv_c_shim = { |
| 111 | + torch.ops.metal.gather_qmv.default: [ |
| 112 | + "AOTITorchError aoti_torch_mps_gather_qmv(" |
| 113 | + "AtenTensorHandle X, AtenTensorHandle W, AtenTensorHandle S, " |
| 114 | + "AtenTensorHandle Z, AtenTensorHandle ExpertIndices, " |
| 115 | + "int64_t group_size, AtenTensorHandle* ret)" |
| 116 | + ], |
| 117 | +} |
0 commit comments