| 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.marlin_utils_fp4 import ( |
| 8 | apply_fp4_marlin_linear, |
| 9 | is_fp4_marlin_supported, |
| 10 | prepare_fp4_layer_for_marlin, |
| 11 | ) |
| 12 | |
| 13 | from .base import NvFp4LinearKernel, NvFp4LinearLayerConfig |
| 14 | |
| 15 | logger = init_logger(__name__) |
| 16 | |
| 17 | |
| 18 | class MarlinNvFp4LinearKernel(NvFp4LinearKernel): |
| 19 | """NVFP4 weight-only GEMM via Marlin (W4A16).""" |
| 20 | |
| 21 | @classmethod |
| 22 | def is_supported( |
| 23 | cls, compute_capability: int | None = None |
| 24 | ) -> tuple[bool, str | None]: |
| 25 | if is_fp4_marlin_supported(): |
| 26 | return True, None |
| 27 | return False, "Marlin FP4 not available" |
| 28 | |
| 29 | @classmethod |
| 30 | def can_implement(cls, config: NvFp4LinearLayerConfig) -> tuple[bool, str | None]: |
| 31 | return True, None |
| 32 | |
| 33 | def process_weights_after_loading(self, layer: torch.nn.Module) -> None: |
| 34 | logger.warning_once( |
| 35 | "Your GPU does not have native support for FP4 computation but " |
| 36 | "FP4 quantization is being used. Weight-only FP4 compression " |
| 37 | "will be used leveraging the Marlin kernel. This may degrade " |
| 38 | "performance for compute-heavy workloads." |
| 39 | ) |
| 40 | prepare_fp4_layer_for_marlin(layer) |
| 41 | |
| 42 | def apply_weights( |
| 43 | self, |
| 44 | layer: torch.nn.Module, |
| 45 | x: torch.Tensor, |
| 46 | bias: torch.Tensor | None = None, |
| 47 | ) -> torch.Tensor: |
| 48 | return apply_fp4_marlin_linear( |
| 49 | input=x, |
| 50 | weight=layer.weight, |
| 51 | weight_scale=layer.weight_scale, |
| 52 | weight_global_scale=layer.weight_global_scale, |
| 53 | workspace=None, |
| 54 | size_n=layer.output_size_per_partition, |
| 55 | size_k=layer.input_size_per_partition, |
| 56 | bias=bias, |
| 57 | ) |