From 799ad41c9f2736d9b5e133f64268be993c1d7f2f Mon Sep 17 00:00:00 2001 From: ATOM00blue <219721791+ATOM00blue@users.noreply.github.com> Date: Fri, 22 May 2026 08:17:15 +0530 Subject: [PATCH] FIX BdLoraConfig.nblocks default value A misplaced trailing comma wrapped the `field(...)` call for `BdLoraConfig.nblocks` in a tuple, so the default value was a one-element tuple containing a `Field` object instead of the integer `1`. As a result, calling `get_peft_model` with a BD-LoRA config that did not explicitly set `nblocks` raised `TypeError` when the value was used for modulo and floor-division in `BlockDiagonalLinear`. Remove the extra parentheses and trailing comma so `nblocks` defaults to `1` as documented. Add tests that build a BD-LoRA model relying on the default `nblocks` and check the default is the integer `1`. --- src/peft/tuners/lora/config.py | 14 ++++++-------- tests/test_initialization.py | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/peft/tuners/lora/config.py b/src/peft/tuners/lora/config.py index a376c0e065..51b5317d45 100644 --- a/src/peft/tuners/lora/config.py +++ b/src/peft/tuners/lora/config.py @@ -216,14 +216,12 @@ class BdLoraConfig: "Example: ['out_proj', 'down_proj']" }, ) - nblocks: int = ( - field( - default=1, - metadata={ - "help": "Number of blocks each block-diagonal matrix has. If using BD-LoRA to speed up inference, " - "set it to be equal to the desired sharding degree during serving." - }, - ), + nblocks: int = field( + default=1, + metadata={ + "help": "Number of blocks each block-diagonal matrix has. If using BD-LoRA to speed up inference, " + "set it to be equal to the desired sharding degree during serving." + }, ) match_strict: bool = field( default=True, diff --git a/tests/test_initialization.py b/tests/test_initialization.py index df73f8ae08..f8ba7e8c11 100644 --- a/tests/test_initialization.py +++ b/tests/test_initialization.py @@ -65,7 +65,7 @@ ) from peft.mapping import PEFT_TYPE_TO_PREFIX_MAPPING from peft.tuners.lokr.layer import LoKrLayer -from peft.tuners.lora.config import CordaConfig +from peft.tuners.lora.config import BdLoraConfig, CordaConfig from peft.tuners.lora.corda import preprocess_corda from peft.tuners.lora.layer import LoraLayer from peft.utils import infer_device @@ -1233,6 +1233,23 @@ def test_bdlora_feature_size_non_divisible_by_blocksize_raises(self): with pytest.raises(ValueError, match="not divisible by"): get_peft_model(model, config) + def test_bdlora_default_nblocks_is_int(self): + # The default value of BdLoraConfig.nblocks must be the integer 1, not a wrapped field object. A misplaced + # trailing comma previously turned the default into a 1-tuple, which broke get_peft_model whenever the user + # did not pass nblocks explicitly. + assert BdLoraConfig().nblocks == 1 + + def test_bdlora_get_peft_model_with_default_nblocks(self): + # Building a BD-LoRA model without specifying nblocks must work and rely on the documented default of 1. + # This used to raise a TypeError because nblocks defaulted to a tuple instead of an int. + model = self.get_model() + + bdlora_config = {"target_modules_bd_a": ["linear"], "target_modules_bd_b": []} + config = LoraConfig(target_modules=["linear"], use_bdlora=bdlora_config) + model = get_peft_model(model, config) + + assert model.linear.lora_A["default"].nblocks == 1 + @pytest.fixture def mha_cls(self): class ModelMha(nn.Module):