main
py 65 lines 2.18 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.logger import init_logger
7 from vllm.model_executor.layers.quantization.utils.quant_utils import kMxfp4Dynamic
8
9 from .base import MxFp4LinearKernel, MxFp4LinearLayerConfig
10
11 logger = init_logger(__name__)
12
13
14 class MarlinMxFp4LinearKernel(MxFp4LinearKernel):
15 @classmethod
16 def is_supported(
17 cls, compute_capability: int | None = None
18 ) -> tuple[bool, str | None]:
19 from vllm.model_executor.layers.quantization.utils.marlin_utils_fp4 import (
20 is_fp4_marlin_supported,
21 )
22
23 if is_fp4_marlin_supported():
24 return True, None
25 return False, "Marlin FP4 not available"
26
27 @classmethod
28 def can_implement(cls, config: MxFp4LinearLayerConfig) -> tuple[bool, str | None]:
29 if config.activation_quant_key not in (None, kMxfp4Dynamic):
30 return False, "only supports MXFP4 dynamic or unquantized activations"
31 if config.activation_quant_key is not None:
32 logger.warning_once(
33 "MarlinMxFp4LinearKernel is a weight-only (A16) kernel; "
34 "the requested activation quantization (%s) is ignored.",
35 config.activation_quant_key,
36 )
37 return True, None
38
39 def process_weights_after_loading(self, layer: torch.nn.Module) -> None:
40 from vllm.model_executor.layers.quantization.utils.marlin_utils_fp4 import (
41 prepare_fp4_layer_for_marlin,
42 )
43
44 prepare_fp4_layer_for_marlin(layer)
45
46 def apply_weights(
47 self,
48 layer: torch.nn.Module,
49 x: torch.Tensor,
50 bias: torch.Tensor | None = None,
51 ) -> torch.Tensor:
52 from vllm.model_executor.layers.quantization.utils.marlin_utils_fp4 import (
53 apply_fp4_marlin_linear,
54 )
55
56 return apply_fp4_marlin_linear(
57 input=x,
58 weight=layer.weight,
59 weight_scale=layer.weight_scale,
60 weight_global_scale=None,
61 workspace=None,
62 size_n=layer.output_size_per_partition,
63 size_k=layer.input_size_per_partition,
64 bias=bias,
65 )