[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:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user