[LoRA] feat: Support LoRA for DeepSeek V4 (#53361)

Signed-off-by: Hollow Man <hollowman@opensuse.org>
Co-authored-by: Jee Jee Li <pandaleefree@gmail.com>
This commit is contained in:
ℍ𝕠𝕝𝕝𝕠𝕨 𝕄𝕒𝕟
2026-08-24 03:57:57 +00:00
committed by GitHub
co-authored by Jee Jee Li
parent e8888b2d68
commit 702e1d7186
3 changed files with 70 additions and 5 deletions
+35 -1
View File
@@ -55,6 +55,7 @@ from vllm.model_executor.models.interfaces import (
EagleModelMixin,
MixtureOfExperts,
SupportsEagle3,
SupportsLoRA,
SupportsPP,
)
from vllm.model_executor.models.utils import (
@@ -1468,6 +1469,11 @@ class DeepseekV4Model(nn.Module, EagleModelMixin):
if is_pp_missing_parameter(name, self):
break
if name not in params_dict:
head, _, leaf = name.rpartition(".")
suffixed = f"{head}.base_layer.{leaf}"
if suffixed in params_dict:
name = suffixed
param = params_dict[name]
weight_loader = param.weight_loader
weight_loader(param, loaded_weight, shard_id)
@@ -1522,6 +1528,13 @@ class DeepseekV4Model(nn.Module, EagleModelMixin):
else:
if is_pp_missing_parameter(name, self):
continue
# Non-LoRA params on a LoRA-wrapped module live at
# ``<head>.base_layer.<leaf>``; the checkpoint is plain.
if name not in params_dict:
head, _, leaf = name.rpartition(".")
suffixed = f"{head}.base_layer.{leaf}"
if suffixed in params_dict:
name = suffixed
param = params_dict[name]
weight_loader = getattr(
param, "weight_loader", default_weight_loader
@@ -1598,6 +1611,10 @@ def _make_deepseek_v4_weights_mapper(expert_dtype: str) -> WeightsMapper:
# shared experts use Fp8LinearMethod's block scales, which
# register as ``weight_scale_inv``.
scale_regex = {
# ``.base_layer.``-namespace variant (LoRA-wrapped experts).
re.compile(
r"(\.experts\.\d+\.w[123]\.base_layer)\.scale$"
): r"\1.weight_scale",
re.compile(r"(\.experts\.\d+\.w[123])\.scale$"): r"\1.weight_scale",
re.compile(r"\.scale$"): ".weight_scale_inv",
}
@@ -1606,6 +1623,10 @@ def _make_deepseek_v4_weights_mapper(expert_dtype: str) -> WeightsMapper:
# scales as ``w{13,2}_weight_scale_inv``. Map all ``.scale`` keys
# there.
scale_regex = {
# ``.base_layer.``-namespace variant of the above.
re.compile(
r"(\.experts\.\d+\.w[123]\.base_layer)\.scale$"
): r"\1.weight_scale_inv",
re.compile(r"\.scale$"): ".weight_scale_inv",
}
return WeightsMapper(
@@ -1667,7 +1688,11 @@ class DeepseekV4MixtureOfExperts(MixtureOfExperts):
class DeepseekV4ForCausalLM(
nn.Module, SupportsPP, SupportsEagle3, DeepseekV4MixtureOfExperts
nn.Module,
SupportsPP,
SupportsEagle3,
SupportsLoRA,
DeepseekV4MixtureOfExperts,
):
model_cls = DeepseekV4Model
@@ -1675,6 +1700,15 @@ class DeepseekV4ForCausalLM(
# Overridden per-instance in __init__ when expert_dtype != "fp4".
hf_to_vllm_mapper = _make_deepseek_v4_weights_mapper("fp4")
packed_modules_mapping = {
"gate_up_proj": ["w1", "w3"],
"fused_wqa_wkv": ["wq_a", "wkv"],
"fused_wkv_wgate": ["wkv", "wgate"],
}
# The MTP draft head is not LoRA-adapted.
lora_skip_prefixes = ["mtp."]
def __init__(self, *, vllm_config: VllmConfig, prefix: str = ""):
super().__init__()