fix(lora): validate LoRA rank is positive in PEFTHelper (#48437)

Signed-off-by: ErenAta16 <erena6466@gmail.com>
This commit is contained in:
ErenAta16
2026-07-16 05:22:52 +00:00
committed by GitHub
parent 6a9f24aa8c
commit 59b964f37d
2 changed files with 19 additions and 0 deletions
+17
View File
@@ -22,6 +22,8 @@ ERROR_CASES = [
{"modules_to_save": ["lm_head"]},
"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(
test_dir, max_position_embeddings=4096
).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"])
+2
View File
@@ -51,6 +51,8 @@ class PEFTHelper:
return error_msg
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:
logger.info_once("Loading LoRA weights trained with rsLoRA.")
self.vllm_lora_scaling_factor = self.lora_alpha / math.sqrt(self.r)