| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | from typing import Any |
| 5 | |
| 6 | import torch |
| 7 | |
| 8 | from vllm.model_executor.layers.fused_moe import ( |
| 9 | RoutedExperts, |
| 10 | ) |
| 11 | from vllm.model_executor.layers.linear import LinearBase, UnquantizedLinearMethod |
| 12 | from vllm.model_executor.layers.quantization import QuantizationMethods |
| 13 | from vllm.model_executor.layers.quantization.base_config import ( |
| 14 | QuantizationConfig, |
| 15 | QuantizeMethodBase, |
| 16 | ) |
| 17 | from vllm.model_executor.layers.quantization.online.int8 import ( |
| 18 | Int8OnlineMoEMethod, |
| 19 | ) |
| 20 | |
| 21 | |
| 22 | class ExpertsInt8Config(QuantizationConfig): |
| 23 | """Online int8 quantization for MoE expert weights. |
| 24 | Linear layers are left unquantized. |
| 25 | |
| 26 | Backward-compatible config for ``--quantization experts_int8``. |
| 27 | Prefer ``--quantization int8_per_channel`` |
| 28 | """ |
| 29 | |
| 30 | def __init__(self) -> None: |
| 31 | super().__init__() |
| 32 | |
| 33 | @classmethod |
| 34 | def get_name(cls) -> QuantizationMethods: |
| 35 | return "experts_int8" |
| 36 | |
| 37 | @classmethod |
| 38 | def get_supported_act_dtypes(cls) -> list[torch.dtype]: |
| 39 | return [torch.bfloat16, torch.half] |
| 40 | |
| 41 | @classmethod |
| 42 | def get_min_capability(cls) -> int: |
| 43 | return 80 |
| 44 | |
| 45 | @classmethod |
| 46 | def get_config_filenames(cls) -> list[str]: |
| 47 | return [] |
| 48 | |
| 49 | @classmethod |
| 50 | def from_config(cls, config: dict[str, Any]) -> "ExpertsInt8Config": |
| 51 | return cls() |
| 52 | |
| 53 | def get_quant_method( |
| 54 | self, layer: torch.nn.Module, prefix: str |
| 55 | ) -> "QuantizeMethodBase | None": |
| 56 | if isinstance(layer, LinearBase): |
| 57 | return UnquantizedLinearMethod() |
| 58 | elif isinstance(layer, RoutedExperts): |
| 59 | return Int8OnlineMoEMethod(moe=layer.moe_config) |
| 60 | return None |