FIX BdLoraConfig.nblocks default value#3252
Conversation
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`.
There was a problem hiding this comment.
Pull request overview
Fixes BD-LoRA configuration initialization by correcting BdLoraConfig.nblocks to default to the integer 1 (instead of an accidental 1-tuple), and adds regression tests to ensure get_peft_model works when nblocks is not explicitly provided.
Changes:
- Fix
BdLoraConfig.nblocksdataclass field definition so the default is a properfield(default=1, ...). - Add tests covering the default
nblocksvalue and successful BD-LoRA model creation relying on that default.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/peft/tuners/lora/config.py |
Corrects BdLoraConfig.nblocks default to be an int via a proper dataclasses.field. |
tests/test_initialization.py |
Adds regression tests asserting the default nblocks is 1 and that get_peft_model succeeds without explicitly setting it. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 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." | ||
| }, | ||
| ) |
BenjaminBossan
left a comment
There was a problem hiding this comment.
You have correctly identified an error. The fix is correct but let's test it differently.
| with pytest.raises(ValueError, match="not divisible by"): | ||
| get_peft_model(model, config) | ||
|
|
||
| def test_bdlora_default_nblocks_is_int(self): |
There was a problem hiding this comment.
We don't need dedicated tests for this. Our normal tests should already surface the error but they don't because they all override nblocks=2, so the default is never used:
peft/tests/test_custom_models.py
Lines 1014 to 1040 in 1b3d06d
So to trigger the error, we can just remove nblocks=2 from one of these settings.
| 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." | ||
| }, | ||
| ) |
|
@ATOM00blue Are you still on this? |
What this fixes
BdLoraConfig.nblocksis meant to default to the integer1, but a misplaced trailing comma wrapped thefield(...)call in a tuple:Because the assigned value is a
tuplerather than aFieldinstance, the dataclass machinery treats it as a literal default. SoBdLoraConfig().nblocksis the one-element tuple(Field(...),)instead of1.This breaks
get_peft_modelwhenever a BD-LoRA config is used without explicitly settingnblocks, because the value flows intoBlockDiagonalLinearwhere it is used for%and//:The existing BD-LoRA tests all pass an explicit
nblocks, so they did not catch the broken default.The fix
Remove the extra parentheses and trailing comma so
nblocksis a regular integer field defaulting to1, consistent with the other fields inBdLoraConfig.Tests
Added two tests in
TestLoraInitialization(both fail before the fix, pass after):test_bdlora_default_nblocks_is_intchecksBdLoraConfig().nblocks == 1.test_bdlora_get_peft_model_with_default_nblocksbuilds a BD-LoRA model relying on the defaultnblocksand checks the resulting block-diagonal layer hasnblocks == 1.ruff checkandruff format --checkare clean on the changed files.Developed with AI coding assistance; reviewed and submitted by the author.