|
| 1 | +import logging |
| 2 | + |
| 3 | +import torch |
| 4 | + |
| 5 | +from sglang.srt.utils import cpu_has_amx_support |
| 6 | + |
| 7 | +logger = logging.getLogger(__name__) |
| 8 | + |
| 9 | + |
| 10 | +def amx_process_weight_after_loading(weight): |
| 11 | + if weight.device != torch.device("cpu"): |
| 12 | + return weight |
| 13 | + if not cpu_has_amx_support(): |
| 14 | + return weight |
| 15 | + |
| 16 | + return torch.ops.sgl_kernel.convert_weight_packed(weight) |
| 17 | + |
| 18 | + |
| 19 | +# TODO: currently gemm kernel has the below requirements: |
| 20 | +# OC % TILE_N == 0, where TILE_N = 16 |
| 21 | +# IC % TILE_K == 0, where TILE_K = 32 |
| 22 | +def dim_is_supported(weight): |
| 23 | + TILE_N = 16 |
| 24 | + TILE_K = 32 |
| 25 | + ndim = weight.ndim |
| 26 | + OC = weight.size(1) if ndim == 3 else weight.size(0) |
| 27 | + IC = weight.size(2) if ndim == 3 else weight.size(1) |
| 28 | + return OC % TILE_N == 0 and IC % TILE_K == 0 |
| 29 | + |
| 30 | + |
| 31 | +def _amx_process_weight_after_loading( |
| 32 | + module, weight_names, transpose_dims=None |
| 33 | +) -> None: |
| 34 | + # Pack weight for get better performance on CPU |
| 35 | + devices = {getattr(module, weight_name).device for weight_name in weight_names} |
| 36 | + assert len(devices) == 1, f"Expects all weights to be on the same device" |
| 37 | + device = devices.pop() |
| 38 | + |
| 39 | + if transpose_dims: |
| 40 | + assert len(weight_names) == len( |
| 41 | + transpose_dims |
| 42 | + ), "len(weight_names) should be equal to len(transpose_dims)" |
| 43 | + |
| 44 | + for i, weight_name in enumerate(weight_names): |
| 45 | + weight_tensor = getattr(module, weight_name) |
| 46 | + |
| 47 | + if transpose_dims and transpose_dims[i]: |
| 48 | + weight_tensor = weight_tensor.transpose(*transpose_dims[i]) |
| 49 | + |
| 50 | + # We don't pack weight or use intel amx backend if any weight of this module has unsupported dim. |
| 51 | + if not dim_is_supported(weight_tensor): |
| 52 | + logger.warning( |
| 53 | + f"Unsupported dimension for prepacking for weight '{weight_name}' with shape {weight_tensor.shape} in {module}. " |
| 54 | + f"The derived (OC, IC) dimensions must be divisible by (16, 32). " |
| 55 | + ) |
| 56 | + module.use_intel_amx_backend = False |
| 57 | + return |
| 58 | + |
| 59 | + packed_weight = torch.nn.Parameter( |
| 60 | + amx_process_weight_after_loading(weight_tensor), |
| 61 | + requires_grad=False, |
| 62 | + ) |
| 63 | + packed_weight.__dict__ = weight_tensor.__dict__ |
| 64 | + setattr(module, weight_name, packed_weight) |
| 65 | + |
| 66 | + module.use_intel_amx_backend = ( |
| 67 | + device == torch.device("cpu") and cpu_has_amx_support() |
| 68 | + ) |
| 69 | + |
| 70 | + if ( |
| 71 | + module.use_intel_amx_backend |
| 72 | + and hasattr(module, "bias") |
| 73 | + and module.bias is not None |
| 74 | + ): |
| 75 | + module.bias = torch.nn.Parameter(module.bias.data.float(), requires_grad=False) |
| 76 | + |
| 77 | + |
| 78 | +class PackWeightMethod: |
| 79 | + def __init__(self, weight_names, transpose_dims=None): |
| 80 | + self.weight_names = weight_names |
| 81 | + self.transpose_dims = transpose_dims |
| 82 | + |
| 83 | + def process_weights_after_loading(self, module) -> None: |
| 84 | + _amx_process_weight_after_loading( |
| 85 | + module, self.weight_names, self.transpose_dims |
| 86 | + ) |
0 commit comments