[Bugfix][ROCm] AMD-Quark mixed-precision DeepSeek-V4.1 support (#57071)

Signed-off-by: Xiao Yu <xiao.yu.dc@outlook.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
xiao-llm
2026-09-25 20:51:18 -05:00
committed by GitHub
co-authored by Cursor
parent b6d2b4fc25
commit 5840d95284
11 changed files with 645 additions and 100 deletions
@@ -45,6 +45,8 @@ from vllm.model_executor.layers.quantization.quark.utils import (
from vllm.model_executor.layers.quantization.utils.ocp_mx_utils import (
_ACTIVATION_QUANT_KEY_MAP,
_WEIGHT_QUANT_KEY_MAP,
OCP_MX_BLOCK_SIZE,
ocp_mx_weight_dtype_and_rows,
)
from vllm.model_executor.layers.quantization.utils.quant_utils import (
QuantKey,
@@ -158,6 +160,20 @@ class QuarkConfig(QuantizationConfig):
self.quant_config = quant_config_with_hf_to_vllm_mapper
def _scale_block_rows(self, prefix: str, layer_type: type[torch.nn.Module]) -> int:
"""How many weight rows one checkpoint scale row covers.
1 for the canonical 1-D per-group MX spelling; ``block_size[0]`` for
MXFP8 exports that spell the scale 2-D (DeepSeek-V4.1 uses 32). The
scheme cannot work this out itself: it has no handle on the layer name,
and tensor-parallel sharding narrows the loaded scale so the ratio is
not recoverable from shapes either.
"""
config = self._find_matched_config(prefix, layer_type)
weight = self._unwrap_single_quant_config(config.get("weight"))
normalized = ocp_mx_weight_dtype_and_rows(weight)
return normalized[1] if normalized is not None else 1
def get_quant_method(
self, layer: torch.nn.Module, prefix: str
) -> "QuantizeMethodBase | None":
@@ -175,6 +191,11 @@ class QuarkConfig(QuantizationConfig):
scheme_cls,
weight_quant_key=weight_quant_key,
activation_quant_key=activation_quant_key,
scale_block_rows=(
self._scale_block_rows(prefix, type(layer))
if scheme_cls is QuarkOCP_MX
else 1
),
weight_config=self._find_matched_config(prefix, type(layer)).get(
"weight"
),
@@ -640,52 +661,18 @@ class QuarkConfig(QuantizationConfig):
The rationale for checking only the weight type is that
the model loading concept and process primarily concerns the weights themselves.
"""
# Confirm weights quantized.
if not isinstance(weight_quant, dict):
# Confirm weights quantized, and normalize the two OCP MX config
# dialects (1-D per_group, and MXFP8's 2-D per_block) to a dtype.
normalized = ocp_mx_weight_dtype_and_rows(weight_quant)
if normalized is None:
logger.debug(
"Quark model's weight quantization is incompatible with OCP_MX format: "
"weight_quant is not a dictionary."
"Quark model's weight quantization is incompatible with OCP MX "
"format: %s",
weight_quant,
)
return QuantKeyMatch(False, None, None)
# Input and weight qscheme needs to be per group.
if weight_quant.get("qscheme") != "per_group":
logger.debug(
"Quark model's weight quantization is incompatible with OCP MX format: "
"weight is not per_group."
)
return QuantKeyMatch(False, None, None)
# Input and weight group size needs to be 32.
if weight_quant.get("group_size") != 32:
logger.debug(
"Quark model's weight quantization is incompatible with OCP MX format: "
"group_size of weight is not 32."
)
return QuantKeyMatch(False, None, None)
# Activations and weight scales need to be in e8m0 format.
if weight_quant.get("scale_format") != "e8m0":
logger.debug(
"Quark model's weight quantization is incompatible with OCP MX format: "
"scale_format of weight is not e8m0."
)
return QuantKeyMatch(False, None, None)
# Input and weight dtypes need to be any of fp4,
# fp6_e3m2 or fp6_e3m2, possibly mixed.
if weight_quant.get("dtype") not in {
"fp4",
"fp6_e3m2",
"fp6_e2m3",
}:
logger.debug(
"Quark model's weight quantization is incompatible with OCP MX format: "
"dtype is not in {fp4, fp6_e3m2, fp6_e2m3}."
)
return QuantKeyMatch(False, None, None)
weight_dtype = weight_quant["dtype"].replace("fp", "mxfp")
weight_dtype, _ = normalized
weight_quant_key = _WEIGHT_QUANT_KEY_MAP[weight_dtype]
if input_quant is None:
activation_quant_key = None
@@ -703,6 +690,14 @@ class QuarkConfig(QuantizationConfig):
"input scales."
)
return QuantKeyMatch(False, None, None)
elif (
input_quant["dtype"] == "fp8_e4m3"
and input_quant.get("qscheme") == "per_group"
and input_quant.get("group_size") == OCP_MX_BLOCK_SIZE
):
# MXFP8 activations. Must be checked before the per-tensor FP8
# branch below, which would otherwise claim them.
activation_quant_key = _ACTIVATION_QUANT_KEY_MAP["mxfp8_e4m3"]
elif input_quant["dtype"] == "fp8_e4m3":
activation_quant_key = kFp8DynamicTensorSym
else:
@@ -903,6 +898,7 @@ class QuarkConfig(QuantizationConfig):
scheme_cls: type["QuarkScheme"],
weight_quant_key: QuantKey | None,
activation_quant_key: QuantKey | None,
scale_block_rows: int = 1,
weight_config: dict[str, Any] | None = None,
) -> "QuarkScheme":
"""Construct a Quark scheme selected by get_scheme_cls."""
@@ -942,6 +938,9 @@ class QuarkConfig(QuantizationConfig):
):
kwargs["weight_quant_key"] = weight_quant_key
if scheme_cls is QuarkOCP_MX:
kwargs["scale_block_rows"] = scale_block_rows
scheme = scheme_cls(**kwargs)
# Raise error if device does not support the scheme