main
py 58 lines 1.85 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.mxfp4_utils import (
7 xpu_mxfp4_quantize as quant_mxfp4,
8 )
9 from vllm.model_executor.layers.quantization.utils.quant_utils import (
10 kMxfp4Dynamic,
11 )
12 from vllm.model_executor.utils import replace_parameter
13 from vllm.platforms import current_platform
14
15 from .base import MxFp4LinearKernel, MxFp4LinearLayerConfig
16
17
18 class XPUMxFp4LinearKernel(MxFp4LinearKernel):
19 """MXFP4 W4A4 GEMM on XPU."""
20
21 @classmethod
22 def is_supported(
23 cls, compute_capability: int | None = None
24 ) -> tuple[bool, str | None]:
25 if not current_platform.is_xpu():
26 return False, "XPUMxFp4 only support on XPU"
27 return True, None
28
29 @classmethod
30 def can_implement(cls, config: MxFp4LinearLayerConfig) -> tuple[bool, str | None]:
31 if config.activation_quant_key != kMxfp4Dynamic:
32 return False, "only supports MXFP4 dynamic activation"
33 return True, None
34
35 def process_weights_after_loading(self, layer: torch.nn.Module) -> None:
36 weight = layer.weight.view(torch.float4_e2m1fn_x2)
37 replace_parameter(layer, "weight", weight.data.t())
38
39 weight_scale = layer.weight_scale.view(torch.float8_e8m0fnu)
40 weight_scale = weight_scale.t().contiguous()
41 replace_parameter(layer, "weight_scale", weight_scale.data)
42
43 def apply_weights(
44 self,
45 layer: torch.nn.Module,
46 x: torch.Tensor,
47 bias: torch.Tensor | None = None,
48 ) -> torch.Tensor:
49 out_dtype = x.dtype
50 x_fp4, x_blockscale = quant_mxfp4(x)
51 return torch.ops._xpu_C.fp4_gemm(
52 x_fp4,
53 layer.weight,
54 x_blockscale,
55 layer.weight_scale,
56 out_dtype,
57 bias,
58 )