fix(lora): validate LoRA rank is positive in PEFTHelper (#48437)
Signed-off-by: ErenAta16 <erena6466@gmail.com>
ErenAta16 committed
Jul 16, 2026 at 08:22 UTC
59b964f37d38c3b78df56dd2cf32e1e4925276ae
2 files changed
+19
tests/lora/test_peft_helper.py
+17
@@ -22,6 +22,8 @@ ERROR_CASES = [
22
{"modules_to_save": ["lm_head"]},
23
"only supports modules_to_save being None",
24
),
25
+ ("test_rank_zero", {"r": 0}, "must be a positive integer"),
26
+ ("test_rank_negative", {"r": -8}, "must be a positive integer"),
27
]
28
29
@@ -97,3 +99,18 @@ def test_peft_helper_error(
99
PEFTHelper.from_local_dir(
100
test_dir, max_position_embeddings=4096
101
).validate_legal(lora_config)
102
+
103
+
104
+@pytest.mark.parametrize("bad_rank", [0, -1, -8])
105
+def test_peft_helper_invalid_rank_direct(bad_rank: int):
106
+ """Regression test: constructing a PEFTHelper with a non-positive rank
107
+ must raise a clear ValueError instead of crashing with an unrelated
108
+ ZeroDivisionError (r=0) or silently succeeding with a sign-flipped
109
+ scaling factor that validate_legal() never catches (r<0, since its only
110
+ rank check is the upper bound against max_lora_rank).
111
+
112
+ Network-free: constructs PEFTHelper directly rather than going through
113
+ from_local_dir(), which needs an on-disk adapter_config.json.
114
+ """
115
+ with pytest.raises(ValueError, match="must be a positive integer"):
116
+ PEFTHelper(r=bad_rank, lora_alpha=16, target_modules=["q_proj"])
vllm/lora/peft_helper.py
+2
@@ -51,6 +51,8 @@ class PEFTHelper:
51
return error_msg
52
53
def __post_init__(self):
54
+ if self.r <= 0:
55
+ raise ValueError(f"LoRA rank `r` must be a positive integer, got {self.r}.")
56
if self.use_rslora:
57
logger.info_once("Loading LoRA weights trained with rsLoRA.")
58
self.vllm_lora_scaling_factor = self.lora_alpha / math.sqrt(self.r)