Skip to content

Fix lora_only bias serialization#3344

Closed
loginov-kirill wants to merge 1 commit into
huggingface:mainfrom
loginov-kirill:fix-lora-only-bias
Closed

Fix lora_only bias serialization#3344
loginov-kirill wants to merge 1 commit into
huggingface:mainfrom
loginov-kirill:fix-lora-only-bias

Conversation

@loginov-kirill

Copy link
Copy Markdown

I noticed a bug when using LoraConfig(bias="lora_only"). Even though the base layer's bias is correctly marked as trainable during training, it gets dropped when calling get_peft_model_state_dict() or save_pretrained().

This happens because the bias parameter of the wrapped layer is named {prefix}.{module_name}.base_layer.bias in the state dict. However, the serialization code in get_peft_model_state_dict() splits the LoRA weight key on "lora_" and appends "bias", looking for {prefix}.{module_name}.bias. Since it doesn't find this key, it skips the bias completely.

This PR adds a fallback check for {prefix}.{module_name}.base_layer.bias in the bias == "lora_only" logic so that these trained biases are saved correctly.

Fixes #3306

@BenjaminBossan BenjaminBossan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tackling that issue. For us to be sure that this fixes the bug and to prevent future regressions, we need to add a unit test though.

I checked if any of the existing tests can be extended, and indeed we have test_parameters_after_loading_model here:

def test_parameters_after_loading_model(self, test_name, model_id, config_cls, config_kwargs):

Here we could either 1) create a similar test and check LoRA with bias="lora_only" or 2) extend the existing TEST_CASES (this would need to be for this test only, as other tests won't work with bias enabled).

if bias_name in state_dict:
to_return[bias_name] = state_dict[bias_name]
else:
bias_name = k.split("lora_")[0] + "base_layer.bias"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a comment to explain why this name needs to be checked.

@BenjaminBossan

Copy link
Copy Markdown
Member

Oh, I just saw that we already have a PR for this, #3307.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LoraConfig(bias="lora_only") trains base_layer.bias but save_pretrained/get_peft_model_state_dict drops it

2 participants