| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import torch |
| 5 | |
| 6 | from vllm.model_executor.layers.quantization.utils.humming import ( |
| 7 | apply_humming_linear, |
| 8 | convert_linear_layer_to_humming_standard, |
| 9 | get_humming_linear_compute_config, |
| 10 | prepare_humming_linear_layer_config, |
| 11 | quant_key_to_input_schema, |
| 12 | ) |
| 13 | from vllm.model_executor.layers.quantization.utils.quant_utils import kMxfp8Dynamic |
| 14 | from vllm.platforms import current_platform |
| 15 | from vllm.utils.import_utils import has_humming |
| 16 | |
| 17 | from .Mxfp8LinearKernel import Mxfp8LinearKernel, Mxfp8LinearLayerConfig |
| 18 | |
| 19 | |
| 20 | class HummingMxfp8LinearKernel(Mxfp8LinearKernel): |
| 21 | """Humming GEMM Kernel for MXFP8.""" |
| 22 | |
| 23 | @classmethod |
| 24 | def is_supported( |
| 25 | cls, compute_capability: int | None = None |
| 26 | ) -> tuple[bool, str | None]: |
| 27 | if not current_platform.is_cuda(): |
| 28 | return False, "Humming only supported on CUDA" |
| 29 | |
| 30 | if not has_humming(): |
| 31 | return False, "Humming is not installed" |
| 32 | |
| 33 | if not current_platform.has_device_capability(75): |
| 34 | return False, "Humming only supported on SM75+" |
| 35 | |
| 36 | return True, None |
| 37 | |
| 38 | @classmethod |
| 39 | def can_implement(cls, c: Mxfp8LinearLayerConfig) -> tuple[bool, str | None]: |
| 40 | return True, None |
| 41 | |
| 42 | def process_weights_after_loading(self, layer: torch.nn.Module) -> None: |
| 43 | layer.weight_scale.data = layer.weight_scale.data.view(torch.float8_e8m0fnu) |
| 44 | name_map = {"weight": "weight", "weight_scale": "weight_scale"} |
| 45 | |
| 46 | quant_config = { |
| 47 | "quant_method": "humming", |
| 48 | "dtype": "float8e4m3", |
| 49 | "scale_dtype": "float8e8m0", |
| 50 | "group_size": 32, |
| 51 | "weight_scale_type": "group", |
| 52 | } |
| 53 | |
| 54 | convert_linear_layer_to_humming_standard(layer=layer, name_map=name_map) |
| 55 | input_schema = quant_key_to_input_schema(kMxfp8Dynamic) |
| 56 | self.layer_config = prepare_humming_linear_layer_config( |
| 57 | layer, quant_config, input_schema=input_schema |
| 58 | ) |
| 59 | self.compute_config = get_humming_linear_compute_config() |
| 60 | self.locks = torch.zeros(1024, dtype=torch.int32, device=layer.weight.device) |
| 61 | |
| 62 | def apply_weights( |
| 63 | self, |
| 64 | layer: torch.nn.Module, |
| 65 | x: torch.Tensor, |
| 66 | bias: torch.Tensor | None = None, |
| 67 | ) -> torch.Tensor: |
| 68 | return apply_humming_linear( |
| 69 | layer, |
| 70 | x, |
| 71 | skip_bias_add=bias is None, |
| 72 | layer_config=self.layer_config, |
| 73 | compute_config=self.compute_config, |
| 74 | locks=self.locks, |
| 75 | ) |