Signed-off-by: roG0d <rodgarcas98@gmail.com> Signed-off-by: roG0d <baonudesifeizhai@gmail.com> Signed-off-by: baonudesifeizhai <85092850+baonudesifeizhai@users.noreply.github.com> Co-authored-by: roG0d <rodgarcas98@gmail.com>
This commit is contained in:
1 parent
40a07e0d80
commit
c4a0990044
26 files changed
+1214
-176
No files matched your search
@@ -60,17 +60,83 @@ _OVERRIDES: dict[str, Callable[..., QuantizationConfig]] = {
|
||||
"int8": _build_int8,
|
||||
"inc": _build_inc,
|
||||
"auto-round": _build_inc,
|
||||
"auto_round": _build_inc,
|
||||
}
|
||||
|
||||
SUPPORTED_QUANTIZATION_METHODS: list[str] = list(dict.fromkeys(QUANTIZATION_METHODS + list(_OVERRIDES.keys())))
|
||||
|
||||
|
||||
_MODEL_OPT_METHODS = {
|
||||
"modelopt",
|
||||
}
|
||||
_MODEL_OPT_FP8_ALGOS = {
|
||||
"FP8",
|
||||
"FP8_PER_CHANNEL_PER_TOKEN",
|
||||
}
|
||||
|
||||
|
||||
def _normalize_method_name(method: Any) -> str:
|
||||
return str(method).lower().replace("-", "_")
|
||||
|
||||
|
||||
def _detect_modelopt_method(config: Mapping[str, Any]) -> str | None:
|
||||
quantization = config.get("quantization")
|
||||
if isinstance(quantization, Mapping):
|
||||
quant_algo = str(quantization.get("quant_algo", "")).upper()
|
||||
else:
|
||||
quant_algo = str(config.get("quant_algo", "")).upper()
|
||||
|
||||
method = config.get("method", config.get("quant_method"))
|
||||
normalized_method = _normalize_method_name(method) if method is not None else None
|
||||
|
||||
producer = config.get("producer")
|
||||
is_modelopt_config = normalized_method in _MODEL_OPT_METHODS or (
|
||||
isinstance(producer, Mapping) and str(producer.get("name", "")).lower() == "modelopt"
|
||||
)
|
||||
|
||||
if not is_modelopt_config:
|
||||
return None
|
||||
|
||||
if quant_algo:
|
||||
if quant_algo in _MODEL_OPT_FP8_ALGOS:
|
||||
return "modelopt"
|
||||
return None
|
||||
|
||||
if method is not None:
|
||||
if normalized_method in _MODEL_OPT_METHODS:
|
||||
return normalized_method
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def _build_modelopt_from_config(method: str, config: Mapping[str, Any]) -> QuantizationConfig:
|
||||
config_cls = get_quantization_config(method)
|
||||
normalized_config = dict(config)
|
||||
normalized_config.setdefault("quant_method", method)
|
||||
return config_cls.from_config(normalized_config)
|
||||
|
||||
|
||||
def _pop_method_name(spec: dict[str, Any]) -> str | None:
|
||||
method = spec.pop("method", None)
|
||||
if method is None:
|
||||
method = spec.pop("quant_method", None)
|
||||
return method
|
||||
|
||||
|
||||
def _build_from_method_and_config(method: str, config: Mapping[str, Any]) -> QuantizationConfig:
|
||||
normalized_config = {"quant_method": method, **config}
|
||||
modelopt_method = _detect_modelopt_method(normalized_config)
|
||||
if modelopt_method is not None:
|
||||
return _build_modelopt_from_config(modelopt_method, normalized_config)
|
||||
return _build_single(method, **config)
|
||||
|
||||
|
||||
def _build_single(method: str, **kwargs: Any) -> QuantizationConfig:
|
||||
"""Build a single QuantizationConfig by method name.
|
||||
|
||||
Resolution: _OVERRIDES first, then vLLM registry via from_config().
|
||||
"""
|
||||
method = method.lower()
|
||||
method = _normalize_method_name(method)
|
||||
|
||||
if method in _OVERRIDES:
|
||||
return _OVERRIDES[method](**kwargs)
|
||||
@@ -92,16 +158,17 @@ def _build_single(method: str, **kwargs: Any) -> QuantizationConfig:
|
||||
def _is_per_component_dict(spec: dict[str, Any]) -> bool:
|
||||
"""Check if a dict describes per-component quantization.
|
||||
|
||||
A per-component dict has no "method" key and all values are
|
||||
A per-component dict has no "method" / "quant_method" key and all values are
|
||||
str, dict, or None. To avoid misdetecting a flat config with
|
||||
all-string values (e.g. {"activation_scheme": "static"}), we
|
||||
require at least one value to be None or a dict with "method".
|
||||
require at least one value to be None or a dict with "method" /
|
||||
"quant_method".
|
||||
"""
|
||||
if "method" in spec:
|
||||
if "method" in spec or "quant_method" in spec:
|
||||
return False
|
||||
if not all(isinstance(v, (dict, str, type(None))) for v in spec.values()):
|
||||
return False
|
||||
return any(v is None or (isinstance(v, dict) and "method" in v) for v in spec.values())
|
||||
return any(v is None or (isinstance(v, dict) and ("method" in v or "quant_method" in v)) for v in spec.values())
|
||||
|
||||
|
||||
def _build_component_config(spec: dict[str, Any]) -> ComponentQuantizationConfig:
|
||||
@@ -116,10 +183,10 @@ def _build_component_config(spec: dict[str, Any]) -> ComponentQuantizationConfig
|
||||
config = _build_single(value)
|
||||
elif isinstance(value, dict):
|
||||
value = dict(value) # avoid mutating caller's dict
|
||||
method = value.pop("method", None)
|
||||
method = _pop_method_name(value)
|
||||
if method is None:
|
||||
raise ValueError(f"Component '{prefix}' config dict must have a 'method' key")
|
||||
config = _build_single(method, **value)
|
||||
raise ValueError(f"Component '{prefix}' config dict must have a 'method' or 'quant_method' key")
|
||||
config = _build_from_method_and_config(method, value)
|
||||
else:
|
||||
raise TypeError(f"Component '{prefix}' config must be str, dict, or None, got {type(value).__name__}")
|
||||
|
||||
@@ -164,14 +231,19 @@ def build_quant_config(
|
||||
if _is_per_component_dict(spec):
|
||||
return _build_component_config(spec)
|
||||
|
||||
method = spec.pop("method", None)
|
||||
modelopt_method = _detect_modelopt_method(spec)
|
||||
if modelopt_method is not None:
|
||||
logger.info("Building quantization config: %s", modelopt_method)
|
||||
return _build_modelopt_from_config(modelopt_method, spec)
|
||||
|
||||
method = _pop_method_name(spec)
|
||||
if method is None:
|
||||
raise ValueError(
|
||||
"Dict quantization config must have a 'method' key or "
|
||||
"Dict quantization config must have a 'method' or 'quant_method' key or "
|
||||
"be a per-component config with component prefixes as keys."
|
||||
)
|
||||
merged = {**spec, **kwargs}
|
||||
logger.info("Building quantization config: %s", method)
|
||||
return _build_single(method, **merged)
|
||||
return _build_from_method_and_config(method, merged)
|
||||
|
||||
raise TypeError(f"quantization config must be str, dict, QuantizationConfig, or None, got {type(spec).__name__}")
|
||||
Reference in new issue
Block a user