From 59b964f37d38c3b78df56dd2cf32e1e4925276ae Mon Sep 17 00:00:00 2001 From: ErenAta16 Date: Thu, 16 Jul 2026 08:22:52 +0300 Subject: [PATCH] fix(lora): validate LoRA rank is positive in PEFTHelper (#48437) Signed-off-by: ErenAta16 --- tests/lora/test_peft_helper.py | 17 +++++++++++++++++ vllm/lora/peft_helper.py | 2 ++ 2 files changed, 19 insertions(+) diff --git a/tests/lora/test_peft_helper.py b/tests/lora/test_peft_helper.py index e3035b00e9..23a8aad1a5 100644 --- a/tests/lora/test_peft_helper.py +++ b/tests/lora/test_peft_helper.py @@ -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"]) diff --git a/vllm/lora/peft_helper.py b/vllm/lora/peft_helper.py index 1443efd4f0..a0bb8bf3c2 100644 --- a/vllm/lora/peft_helper.py +++ b/vllm/lora/peft_helper.py @@ -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)