|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | +# patched by vLLM-Kunlun |
| 4 | + |
| 5 | +from typing import Literal, get_args |
| 6 | + |
| 7 | +from vllm.logger import init_logger |
| 8 | +from vllm.model_executor.layers.quantization.base_config import QuantizationConfig |
| 9 | +from vllm.platforms import current_platform |
| 10 | + |
| 11 | +logger = init_logger(__name__) |
| 12 | + |
| 13 | +QuantizationMethods = Literal[ |
| 14 | + "awq", |
| 15 | + "fp8", |
| 16 | + "ptpc_fp8", |
| 17 | + "fbgemm_fp8", |
| 18 | + # "fp_quant", |
| 19 | + "modelopt", |
| 20 | + "modelopt_fp4", |
| 21 | + "bitblas", |
| 22 | + "gguf", |
| 23 | + "gptq_marlin_24", |
| 24 | + "gptq_marlin", |
| 25 | + "gptq_bitblas", |
| 26 | + "awq_marlin", |
| 27 | + "gptq", |
| 28 | + "compressed-tensors", |
| 29 | + "bitsandbytes", |
| 30 | + "experts_int8", |
| 31 | + "ipex", |
| 32 | + "quark", |
| 33 | + "moe_wna16", |
| 34 | + "torchao", |
| 35 | + "inc", |
| 36 | + "mxfp4", |
| 37 | + "petit_nvfp4", |
| 38 | + "cpu_awq", |
| 39 | +] |
| 40 | +QUANTIZATION_METHODS: list[str] = list(get_args(QuantizationMethods)) |
| 41 | + |
| 42 | +DEPRECATED_QUANTIZATION_METHODS = [ |
| 43 | + "tpu_int8", |
| 44 | + "ptpc_fp8", |
| 45 | + "fbgemm_fp8", |
| 46 | + # "fp_quant", |
| 47 | + "bitblas", |
| 48 | + "gptq_marlin_24", |
| 49 | + "gptq_bitblas", |
| 50 | + "experts_int8", |
| 51 | + "ipex", |
| 52 | + "petit_nvfp4", |
| 53 | +] |
| 54 | + |
| 55 | +# The customized quantization methods which will be added to this dict. |
| 56 | +_CUSTOMIZED_METHOD_TO_QUANT_CONFIG = {} |
| 57 | + |
| 58 | + |
| 59 | +def register_quantization_config(quantization: str): |
| 60 | + """Register a customized vllm quantization config. |
| 61 | +
|
| 62 | + When a quantization method is not supported by vllm, you can register a customized |
| 63 | + quantization config to support it. |
| 64 | +
|
| 65 | + Args: |
| 66 | + quantization (str): The quantization method name. |
| 67 | +
|
| 68 | + Examples: |
| 69 | + >>> from vllm.model_executor.layers.quantization import ( |
| 70 | + ... register_quantization_config, |
| 71 | + ... ) |
| 72 | + >>> from vllm.model_executor.layers.quantization import get_quantization_config |
| 73 | + >>> from vllm.model_executor.layers.quantization.base_config import ( |
| 74 | + ... QuantizationConfig, |
| 75 | + ... ) |
| 76 | + >>> |
| 77 | + >>> @register_quantization_config("my_quant") |
| 78 | + ... class MyQuantConfig(QuantizationConfig): |
| 79 | + ... pass |
| 80 | + >>> |
| 81 | + >>> get_quantization_config("my_quant") |
| 82 | + <class 'MyQuantConfig'> |
| 83 | + """ # noqa: E501 |
| 84 | + |
| 85 | + def _wrapper(quant_config_cls): |
| 86 | + if quantization in QUANTIZATION_METHODS: |
| 87 | + logger.warning( |
| 88 | + "The quantization method '%s' already exists and will be " |
| 89 | + "overwritten by the quantization config %s.", |
| 90 | + quantization, |
| 91 | + quant_config_cls, |
| 92 | + ) |
| 93 | + else: |
| 94 | + QUANTIZATION_METHODS.append(quantization) |
| 95 | + # Automatically assume the custom quantization config is supported |
| 96 | + if sq := current_platform.supported_quantization: |
| 97 | + sq.append(quantization) |
| 98 | + |
| 99 | + if not issubclass(quant_config_cls, QuantizationConfig): |
| 100 | + raise ValueError( |
| 101 | + "The quantization config must be a subclass of `QuantizationConfig`." |
| 102 | + ) |
| 103 | + _CUSTOMIZED_METHOD_TO_QUANT_CONFIG[quantization] = quant_config_cls |
| 104 | + return quant_config_cls |
| 105 | + |
| 106 | + return _wrapper |
| 107 | + |
| 108 | + |
| 109 | +def get_quantization_config(quantization: str) -> type[QuantizationConfig]: |
| 110 | + if quantization not in QUANTIZATION_METHODS: |
| 111 | + raise ValueError(f"Invalid quantization method: {quantization}") |
| 112 | + |
| 113 | + # lazy import to avoid triggering `torch.compile` too early |
| 114 | + from vllm.model_executor.layers.quantization.awq import AWQConfig |
| 115 | + from vllm.model_executor.layers.quantization.awq_marlin import AWQMarlinConfig |
| 116 | + from vllm.model_executor.layers.quantization.bitblas import BitBLASConfig |
| 117 | + from vllm.model_executor.layers.quantization.bitsandbytes import BitsAndBytesConfig |
| 118 | + from vllm.model_executor.layers.quantization.compressed_tensors.compressed_tensors import ( |
| 119 | + CompressedTensorsConfig, |
| 120 | + ) |
| 121 | + from vllm.model_executor.layers.quantization.cpu_wna16 import CPUAWQConfig |
| 122 | + from vllm.model_executor.layers.quantization.experts_int8 import ExpertsInt8Config |
| 123 | + from vllm.model_executor.layers.quantization.fbgemm_fp8 import FBGEMMFp8Config |
| 124 | + from vllm.model_executor.layers.quantization.fp8 import Fp8Config |
| 125 | + |
| 126 | + # from vllm.model_executor.layers.quantization.fp_quant import FPQuantConfig |
| 127 | + from vllm.model_executor.layers.quantization.gguf import GGUFConfig |
| 128 | + from vllm.model_executor.layers.quantization.gptq import GPTQConfig |
| 129 | + from vllm.model_executor.layers.quantization.gptq_bitblas import GPTQBitBLASConfig |
| 130 | + from vllm.model_executor.layers.quantization.gptq_marlin import GPTQMarlinConfig |
| 131 | + from vllm.model_executor.layers.quantization.gptq_marlin_24 import ( |
| 132 | + GPTQMarlin24Config, |
| 133 | + ) |
| 134 | + from vllm.model_executor.layers.quantization.inc import INCConfig |
| 135 | + from vllm.model_executor.layers.quantization.ipex_quant import IPEXConfig |
| 136 | + from vllm.model_executor.layers.quantization.modelopt import ( |
| 137 | + ModelOptFp8Config, |
| 138 | + ModelOptNvFp4Config, |
| 139 | + ) |
| 140 | + from vllm.model_executor.layers.quantization.moe_wna16 import MoeWNA16Config |
| 141 | + from vllm.model_executor.layers.quantization.mxfp4 import Mxfp4Config |
| 142 | + from vllm.model_executor.layers.quantization.petit import PetitNvFp4Config |
| 143 | + from vllm.model_executor.layers.quantization.ptpc_fp8 import PTPCFp8Config |
| 144 | + from vllm.model_executor.layers.quantization.quark.quark import QuarkConfig |
| 145 | + from vllm.model_executor.layers.quantization.torchao import TorchAOConfig |
| 146 | + |
| 147 | + method_to_config: dict[str, type[QuantizationConfig]] = { |
| 148 | + "awq": AWQConfig, |
| 149 | + "fp8": Fp8Config, |
| 150 | + "fbgemm_fp8": FBGEMMFp8Config, |
| 151 | + # "fp_quant": FPQuantConfig, |
| 152 | + "modelopt": ModelOptFp8Config, |
| 153 | + "modelopt_fp4": ModelOptNvFp4Config, |
| 154 | + "bitblas": BitBLASConfig, |
| 155 | + "gguf": GGUFConfig, |
| 156 | + "gptq_marlin_24": GPTQMarlin24Config, |
| 157 | + "gptq_marlin": GPTQMarlinConfig, |
| 158 | + "gptq_bitblas": GPTQBitBLASConfig, |
| 159 | + "awq_marlin": AWQMarlinConfig, |
| 160 | + "gptq": GPTQConfig, |
| 161 | + "compressed-tensors": CompressedTensorsConfig, |
| 162 | + "bitsandbytes": BitsAndBytesConfig, |
| 163 | + "ptpc_fp8": PTPCFp8Config, |
| 164 | + "experts_int8": ExpertsInt8Config, |
| 165 | + "ipex": IPEXConfig, |
| 166 | + "quark": QuarkConfig, |
| 167 | + "moe_wna16": MoeWNA16Config, |
| 168 | + "torchao": TorchAOConfig, |
| 169 | + "auto-round": INCConfig, |
| 170 | + "inc": INCConfig, |
| 171 | + "mxfp4": Mxfp4Config, |
| 172 | + "petit_nvfp4": PetitNvFp4Config, |
| 173 | + "cpu_awq": CPUAWQConfig, |
| 174 | + } |
| 175 | + # Update the `method_to_config` with customized quantization methods. |
| 176 | + method_to_config.update(_CUSTOMIZED_METHOD_TO_QUANT_CONFIG) |
| 177 | + |
| 178 | + return method_to_config[quantization] |
| 179 | + |
| 180 | + |
| 181 | +__all__ = [ |
| 182 | + "QuantizationConfig", |
| 183 | + "QuantizationMethods", |
| 184 | + "get_quantization_config", |
| 185 | + "register_quantization_config", |
| 186 | + "QUANTIZATION_METHODS", |
| 187 | +] |
0 commit comments