fix(lora): validate LoRA rank is positive in PEFTHelper (#48437)
Signed-off-by: ErenAta16 <erena6466@gmail.com>
This commit is contained in:
@@ -22,6 +22,8 @@ ERROR_CASES = [
|
|||||||
{"modules_to_save": ["lm_head"]},
|
{"modules_to_save": ["lm_head"]},
|
||||||
"only supports modules_to_save being None",
|
"only supports modules_to_save being None",
|
||||||
),
|
),
|
||||||
|
("test_rank_zero", {"r": 0}, "must be a positive integer"),
|
||||||
|
("test_rank_negative", {"r": -8}, "must be a positive integer"),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@@ -97,3 +99,18 @@ def test_peft_helper_error(
|
|||||||
PEFTHelper.from_local_dir(
|
PEFTHelper.from_local_dir(
|
||||||
test_dir, max_position_embeddings=4096
|
test_dir, max_position_embeddings=4096
|
||||||
).validate_legal(lora_config)
|
).validate_legal(lora_config)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("bad_rank", [0, -1, -8])
|
||||||
|
def test_peft_helper_invalid_rank_direct(bad_rank: int):
|
||||||
|
"""Regression test: constructing a PEFTHelper with a non-positive rank
|
||||||
|
must raise a clear ValueError instead of crashing with an unrelated
|
||||||
|
ZeroDivisionError (r=0) or silently succeeding with a sign-flipped
|
||||||
|
scaling factor that validate_legal() never catches (r<0, since its only
|
||||||
|
rank check is the upper bound against max_lora_rank).
|
||||||
|
|
||||||
|
Network-free: constructs PEFTHelper directly rather than going through
|
||||||
|
from_local_dir(), which needs an on-disk adapter_config.json.
|
||||||
|
"""
|
||||||
|
with pytest.raises(ValueError, match="must be a positive integer"):
|
||||||
|
PEFTHelper(r=bad_rank, lora_alpha=16, target_modules=["q_proj"])
|
||||||
|
|||||||
@@ -51,6 +51,8 @@ class PEFTHelper:
|
|||||||
return error_msg
|
return error_msg
|
||||||
|
|
||||||
def __post_init__(self):
|
def __post_init__(self):
|
||||||
|
if self.r <= 0:
|
||||||
|
raise ValueError(f"LoRA rank `r` must be a positive integer, got {self.r}.")
|
||||||
if self.use_rslora:
|
if self.use_rslora:
|
||||||
logger.info_once("Loading LoRA weights trained with rsLoRA.")
|
logger.info_once("Loading LoRA weights trained with rsLoRA.")
|
||||||
self.vllm_lora_scaling_factor = self.lora_alpha / math.sqrt(self.r)
|
self.vllm_lora_scaling_factor = self.lora_alpha / math.sqrt(self.r)
|
||||||
|
|||||||
Reference in New Issue
Block a user