[Misc][LoRA] Add --lora-target-modules to restrict LoRA to specific modules (#34984)

Signed-off-by: Bhoomit Vasani <bhoomit.2010@gmail.com>
Co-authored-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
Bhoomit
2026-03-17 14:36:41 +00:00
committed by GitHub
co-authored by Harry Mellor
parent ecfcdd2ce4
commit 3717a4dd47
9 changed files with 404 additions and 10 deletions
+8
View File
@@ -43,6 +43,10 @@ class LoRAConfig:
`max_loras`."""
lora_dtype: torch.dtype | LoRADType = "auto"
"""Data type for LoRA. If auto, will default to base model dtype."""
target_modules: list[str] | None = None
"""Restrict LoRA to specific module suffixes (e.g., ["o_proj", "qkv_proj"]).
If None, all supported LoRA modules are used. This allows deployment-time
control over which modules have LoRA applied, useful for performance tuning."""
default_mm_loras: dict[str, str] | None = None
"""Dictionary mapping specific modalities to LoRA model paths; this field
is only applicable to multimodal models and should be leveraged when a
@@ -84,6 +88,10 @@ class LoRAConfig:
factors.append(self.fully_sharded_loras)
factors.append(self.lora_dtype)
factors.append(self.enable_tower_connector_lora)
# target_modules affects which modules get LoRA applied
factors.append(
tuple(sorted(self.target_modules)) if self.target_modules else None
)
hash_str = safe_hash(str(factors).encode(), usedforsecurity=False).hexdigest()
return hash_str
+5
View File
@@ -506,6 +506,7 @@ class EngineArgs:
fully_sharded_loras: bool = LoRAConfig.fully_sharded_loras
max_cpu_loras: int | None = LoRAConfig.max_cpu_loras
lora_dtype: str | torch.dtype | None = LoRAConfig.lora_dtype
lora_target_modules: list[str] | None = LoRAConfig.target_modules
enable_tower_connector_lora: bool = LoRAConfig.enable_tower_connector_lora
specialize_active_lora: bool = LoRAConfig.specialize_active_lora
@@ -1107,6 +1108,9 @@ class EngineArgs:
lora_group.add_argument(
"--fully-sharded-loras", **lora_kwargs["fully_sharded_loras"]
)
lora_group.add_argument(
"--lora-target-modules", **lora_kwargs["target_modules"]
)
lora_group.add_argument("--default-mm-loras", **lora_kwargs["default_mm_loras"])
lora_group.add_argument(
"--specialize-active-lora", **lora_kwargs["specialize_active_lora"]
@@ -1800,6 +1804,7 @@ class EngineArgs:
default_mm_loras=self.default_mm_loras,
fully_sharded_loras=self.fully_sharded_loras,
lora_dtype=self.lora_dtype,
target_modules=self.lora_target_modules,
enable_tower_connector_lora=self.enable_tower_connector_lora,
specialize_active_lora=self.specialize_active_lora,
max_cpu_loras=self.max_cpu_loras
+19 -9
View File
@@ -5,7 +5,6 @@ import math
from collections.abc import Callable
from typing import TypeVar
import regex as re
import torch
from torch import nn
@@ -25,7 +24,9 @@ from vllm.lora.utils import (
from_layer,
from_layer_logits_processor,
get_supported_lora_modules,
is_in_target_modules,
is_moe_model,
is_supported_lora_module,
process_packed_modules_mapping,
replace_submodule,
)
@@ -541,14 +542,23 @@ class LoRAModelManager:
model.loras[module_name] = lora
return model
def _match_target_modules(self, module_name: str):
return any(
re.match(
r".*\.{target_module}$".format(target_module=target_module), module_name
)
or target_module == module_name
for target_module in self.supported_lora_modules
)
def _match_target_modules(self, module_name: str) -> bool:
"""Check if a module should have LoRA applied.
This method first checks if the module is in vLLM's supported LoRA
modules, then applies deployment-time restrictions based on
LoRAConfig.target_modules.
Args:
module_name: Full dot-separated module name (e.g.,
"model.layers.0.self_attn.o_proj")
Returns:
True if LoRA should be applied to this module, False otherwise.
"""
if not is_supported_lora_module(module_name, self.supported_lora_modules):
return False
return is_in_target_modules(module_name, self.lora_config.target_modules)
def _get_punica_wrapper(self, module_name: str) -> PunicaWrapperBase | None:
"""
+52
View File
@@ -5,6 +5,7 @@ import os
from typing import TYPE_CHECKING
import huggingface_hub
import regex as re
from huggingface_hub.utils import HfHubHTTPError, HFValidationError
from torch import nn
from transformers import PretrainedConfig
@@ -226,6 +227,57 @@ def get_supported_lora_modules(model: nn.Module) -> list[str]:
return list(supported_lora_modules)
def is_supported_lora_module(
module_name: str,
supported_lora_modules: list[str],
) -> bool:
"""Check if a module is in the model's supported LoRA modules.
Uses regex suffix matching against the model-defined supported modules
list (e.g., matching "model.layers.0.self_attn.o_proj" against
"o_proj").
Args:
module_name: Full dot-separated module name.
supported_lora_modules: List of module suffixes supported by the
model.
Returns:
True if the module is supported, False otherwise.
"""
return any(
re.match(
r".*\.{target_module}$".format(target_module=target_module),
module_name,
)
or target_module == module_name
for target_module in supported_lora_modules
)
def is_in_target_modules(
module_name: str,
target_modules: list[str] | None,
) -> bool:
"""Check if a module passes the deployment-time target_modules filter.
When target_modules is None (no restriction), all modules pass.
Otherwise, the module's suffix must be in the target_modules list.
Args:
module_name: Full dot-separated module name.
target_modules: Optional deployment-time restriction list from
LoRAConfig.target_modules.
Returns:
True if the module passes the filter, False otherwise.
"""
if target_modules is None:
return True
module_suffix = module_name.split(".")[-1]
return module_suffix in set(target_modules)
def get_adapter_absolute_path(lora_path: str) -> str:
"""
Resolves the given lora_path to an absolute local path.
+28 -1
View File
@@ -17,7 +17,11 @@ from vllm.lora.model_manager import (
)
from vllm.lora.peft_helper import PEFTHelper
from vllm.lora.request import LoRARequest
from vllm.lora.utils import get_adapter_absolute_path
from vllm.lora.utils import (
get_adapter_absolute_path,
is_in_target_modules,
is_supported_lora_module,
)
logger = init_logger(__name__)
@@ -142,6 +146,29 @@ class WorkerLoRAManager:
skip_prefixes=lora_skip_prefixes,
)
# Warn about adapter modules that will be ignored.
target_modules = self.lora_config.target_modules
for module_name in lora.loras:
if not is_supported_lora_module(module_name, supported_lora_modules):
logger.warning_once(
"LoRA module '%s' in adapter '%s' is not in the "
"model's supported LoRA target modules [%s]. "
"These parameters will be ignored, which may "
"cause abnormal model behavior.",
module_name,
lora_request.lora_path,
", ".join(sorted(supported_lora_modules)),
)
elif not is_in_target_modules(module_name, target_modules):
logger.warning_once(
"LoRA module '%s' in adapter '%s' is not in the "
"deployment-time target_modules restriction [%s]."
" These parameters will be ignored.",
module_name,
lora_request.lora_path,
", ".join(sorted(target_modules)),
)
except FileNotFoundError as e:
# FileNotFoundError should be raised if both
# - No adapter found to download from huggingface (or in