main
py 84 lines 2.86 KB
Raw
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 kMxfp6E2M3Static
14 from vllm.platforms import current_platform
15 from vllm.utils.import_utils import has_humming
16
17 from .base import MxFp6LinearKernel, MxFp6LinearLayerConfig
18
19
20 class HummingMxFp6LinearKernel(MxFp6LinearKernel):
21 """Humming GEMM for packed MXFP6 E2M3 and E3M2 weights."""
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, config: MxFp6LinearLayerConfig) -> tuple[bool, str | None]:
40 try:
41 quant_key_to_input_schema(config.activation_quant_key)
42 except ValueError as error:
43 return False, str(error)
44 return True, None
45
46 def process_weights_after_loading(self, layer: torch.nn.Module) -> None:
47 weight_dtype = "float6e3m2"
48 if self.config.weight_quant_key == kMxfp6E2M3Static:
49 weight_dtype = "float6e2m3"
50
51 quant_config = {
52 "quant_method": "humming",
53 "dtype": weight_dtype,
54 "scale_dtype": "float8e8m0",
55 "group_size": 32,
56 "weight_scale_type": "group",
57 }
58 layer.weight_scale.data = layer.weight_scale.data.view(torch.float8_e8m0fnu)
59
60 convert_linear_layer_to_humming_standard(
61 layer=layer,
62 name_map={"weight": "weight", "weight_scale": "weight_scale"},
63 )
64 input_schema = quant_key_to_input_schema(self.config.activation_quant_key)
65 self.layer_config = prepare_humming_linear_layer_config(
66 layer, quant_config, input_schema=input_schema
67 )
68 self.compute_config = get_humming_linear_compute_config()
69 self.locks = torch.zeros(1024, dtype=torch.int32, device=layer.weight.device)
70
71 def apply_weights(
72 self,
73 layer: torch.nn.Module,
74 x: torch.Tensor,
75 bias: torch.Tensor | None = None,
76 ) -> torch.Tensor:
77 return apply_humming_linear(
78 layer,
79 x,
80 skip_bias_add=bias is None,
81 layer_config=self.layer_config,
82 compute_config=self.compute_config,
83 locks=self.locks,
84 )