[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
+7 -7
View File
@@ -135,15 +135,15 @@ class DeepseekV4FP8Config(Fp8Config):
def override_quantization_method(
cls, hf_quant_cfg, user_quant, hf_config=None
) -> QuantizationMethods | None:
# Quark checkpoints are always handled by QuarkConfig.
if (
isinstance(hf_quant_cfg, dict)
and hf_quant_cfg.get("quant_method") == "quark"
):
return None
if not (
isinstance(hf_quant_cfg, dict)
and (
hf_quant_cfg.get("quant_method") in ("fp8", "deepseek_v4_fp8")
or (
hf_quant_cfg.get("quant_method") == "quark"
and cls._is_quark_mxfp4_ocp(hf_quant_cfg)
)
)
and hf_quant_cfg.get("quant_method") in ("fp8", "deepseek_v4_fp8")
):
return None
model_type = getattr(hf_config, "model_type", None)
+10
View File
@@ -895,6 +895,11 @@ def _make_deepseek_v4_weights_mapper(
# renames the engram fp8 table but not its scale; route the
# scale explicitly to the same module.
re.compile(r"(engram\.embed)\.scale$"): r"\1_tokens.weight_scale_inv",
# Quark exports spell the same tensor ``embed.weight_scale``,
# which the ``\.scale$`` rules never match.
re.compile(
r"(engram\.embed)\.weight_scale$"
): r"\1_tokens.weight_scale_inv",
re.compile(r"\.scale$"): f".{linear_scale_name}",
}
else:
@@ -908,6 +913,11 @@ def _make_deepseek_v4_weights_mapper(
): r"\1.weight_scale_inv",
# Same engram reroute as the fp4 branch above.
re.compile(r"(engram\.embed)\.scale$"): r"\1_tokens.weight_scale_inv",
# Quark exports spell the same tensor ``embed.weight_scale``,
# which the ``\.scale$`` rules never match.
re.compile(
r"(engram\.embed)\.weight_scale$"
): r"\1_tokens.weight_scale_inv",
re.compile(r"\.scale$"): f".{linear_scale_name}",
}
return WeightsMapper(
+26 -8
View File
@@ -79,20 +79,22 @@ class DeepseekV4VLImagePixelInputs(TensorSchema):
types: Annotated[torch.Tensor, TensorShape("ns", dynamic_dims={"ns"})]
_VL_PREFIX_MAPPING: dict[str, str | None] = {
"layers.": "language_model.model.layers.",
"embed.": "language_model.model.embed.",
"norm.": "language_model.model.norm.",
"hc_head": "language_model.model.hc_head",
"mtp.": "language_model.model.mtp.",
}
def _make_deepseek_v4_vl_weights_mapper(
expert_dtype: str, linear_scale_name: str
) -> WeightsMapper:
"""Text-checkpoint mapping rules re-rooted under ``language_model.``."""
base = _make_deepseek_v4_weights_mapper(expert_dtype, linear_scale_name)
orig_to_new_prefix: dict[str, str | None] = {
"layers.": "language_model.model.layers.",
"embed.": "language_model.model.embed.",
"norm.": "language_model.model.norm.",
"hc_head": "language_model.model.hc_head",
"mtp.": "language_model.model.mtp.",
}
return WeightsMapper(
orig_to_new_prefix=orig_to_new_prefix,
orig_to_new_prefix=dict(_VL_PREFIX_MAPPING),
orig_to_new_regex=base.orig_to_new_regex,
orig_to_new_suffix={
"head.weight": "language_model.lm_head.weight",
@@ -131,6 +133,22 @@ class DeepseekV41ForCausalLM(
supports_encoder_tp_data = True
# Both of these are read off the *class* by
# ``configure_quant_config``/``SupportsQuant``, before ``__init__`` runs,
# to translate a quantization config's checkpoint-style module names into
# runtime prefixes. ``__init__`` later overwrites the instance
# ``hf_to_vllm_mapper`` with the full weight-loading mapper; only the
# prefix rules matter here. Without the class attributes, per-layer specs
# naming ``layers.N.attn.wq_a`` never match this wrapper's
# ``language_model.model.layers.N.attn.wq_a`` and silently fall back to
# the global spec.
packed_modules_mapping = DeepseekV41LLMForCausalLM.packed_modules_mapping
hf_to_vllm_mapper = WeightsMapper(
orig_to_new_prefix=dict(_VL_PREFIX_MAPPING),
orig_to_new_substr={".shared_experts.w2": ".shared_experts.down_proj"},
)
# The MoE router needs raw token ids to detect image-span tokens
# (all carrying image_token_id, see common/mm_preprocess.py) and apply
# bias_vl.
+9 -7
View File
@@ -139,15 +139,17 @@ class DeepseekV4FP8Config(Fp8Config):
def override_quantization_method(
cls, hf_quant_cfg, user_quant, hf_config=None
) -> QuantizationMethods | None:
# Quark checkpoints are always handled by QuarkConfig, which knows
# how to dispatch each per-layer scheme (MXFP4, MXFP8, etc.).
# ``from_config`` below rewrites the config into a single global FP8
# scheme, which would discard per-layer specs — so never claim Quark.
if isinstance(hf_quant_cfg, dict) and (
hf_quant_cfg.get("quant_method") == "quark"
):
return None
if not (
isinstance(hf_quant_cfg, dict)
and (
hf_quant_cfg.get("quant_method") in ("fp8", "deepseek_v4_fp8")
or (
hf_quant_cfg.get("quant_method") == "quark"
and cls._is_quark_mxfp4_ocp(hf_quant_cfg)
)
)
and hf_quant_cfg.get("quant_method") in ("fp8", "deepseek_v4_fp8")
):
return None
model_type = getattr(hf_config, "model_type", None)