| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | from abc import ABC, abstractmethod |
| 5 | from typing import TYPE_CHECKING |
| 6 | |
| 7 | if TYPE_CHECKING: |
| 8 | import torch |
| 9 | |
| 10 | from vllm.model_executor.layers.fused_moe import FusedMoEMethodBase |
| 11 | from vllm.model_executor.layers.linear import LinearMethodBase |
| 12 | |
| 13 | from ..config_parser import INCLayerConfig |
| 14 | from ..inc import INCConfig |
| 15 | |
| 16 | |
| 17 | class INCScheme(ABC): |
| 18 | """One class per quant type. Single registration point for the factory. |
| 19 | |
| 20 | Each subclass defines: |
| 21 | - can_handle(): when does this scheme apply? |
| 22 | - get_linear_method(): required — how to quantize Linear layers |
| 23 | - get_moe_method(): optional — how to quantize MoE layers |
| 24 | |
| 25 | Schemes that don't support MoE/KVCache inherit the default raise. |
| 26 | """ |
| 27 | |
| 28 | @staticmethod |
| 29 | @abstractmethod |
| 30 | def can_handle(layer_config: "INCLayerConfig") -> bool: |
| 31 | raise NotImplementedError |
| 32 | |
| 33 | @abstractmethod |
| 34 | def get_linear_method( |
| 35 | self, |
| 36 | config: "INCConfig", |
| 37 | layer: "torch.nn.Module", |
| 38 | prefix: str, |
| 39 | layer_config: "INCLayerConfig", |
| 40 | ) -> "LinearMethodBase": |
| 41 | raise NotImplementedError |
| 42 | |
| 43 | def get_moe_method( |
| 44 | self, |
| 45 | config: "INCConfig", |
| 46 | layer: "torch.nn.Module", |
| 47 | prefix: str, |
| 48 | layer_config: "INCLayerConfig", |
| 49 | ) -> "FusedMoEMethodBase | None": |
| 50 | """Optional. Override if this scheme supports MoE. |
| 51 | Default raises NotImplementedError.""" |
| 52 | raise NotImplementedError( |
| 53 | f"{type(self).__name__} does not support MoE layers. " |
| 54 | f"Layer config: {layer_config}" |
| 55 | ) |
| 56 | |
| 57 | |
| 58 | class INCLinearScheme(ABC): |
| 59 | @classmethod |
| 60 | @abstractmethod |
| 61 | def get_min_capability(cls) -> int: |
| 62 | raise NotImplementedError |
| 63 | |
| 64 | @abstractmethod |
| 65 | def create_weights( |
| 66 | self, |
| 67 | layer: "torch.nn.Module", |
| 68 | input_size_per_partition: int, |
| 69 | output_partition_sizes: list[int], |
| 70 | input_size: int, |
| 71 | output_size: int, |
| 72 | params_dtype: "torch.dtype", |
| 73 | **extra_weight_attrs, |
| 74 | ) -> None: |
| 75 | raise NotImplementedError |
| 76 | |
| 77 | @abstractmethod |
| 78 | def process_weights_after_loading(self, layer: "torch.nn.Module") -> None: |
| 79 | raise NotImplementedError |
| 80 | |
| 81 | @abstractmethod |
| 82 | def apply_weights( |
| 83 | self, |
| 84 | layer: "torch.nn.Module", |
| 85 | x: "torch.Tensor", |
| 86 | bias: "torch.Tensor | None" = None, |
| 87 | ) -> "torch.Tensor": |
| 88 | raise NotImplementedError |