Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose bias to to ModulesToSaveWrapper #2081

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/peft/utils/other.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ def weight(self):
return self.original_module.weight
return self.modules_to_save[self.active_adapter].weight

@property
def bias(self):
if self.active_adapter not in self.modules_to_save:
return self.original_module.bias
return self.modules_to_save[self.active_adapter].bias

def update(self, adapter_name):
context_manager = nullcontext()
for _, param in self.original_module.named_parameters():
Expand Down
8 changes: 6 additions & 2 deletions tests/test_low_level_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(self):
super().__init__()
self.embedding = torch.nn.Embedding(10, 10)
self.linear = torch.nn.Linear(10, 10)
self.linear2 = torch.nn.Linear(10, 10, bias=True)
self.lm_head = torch.nn.Linear(10, 10)

def forward(self, input_ids):
Expand Down Expand Up @@ -74,7 +75,7 @@ def test_modules_to_save(self):
r=64,
bias="none",
target_modules=["linear"],
modules_to_save=["embedding"],
modules_to_save=["embedding", "linear2"],
)

self.model = inject_adapter_in_model(lora_config, self.model)
Expand All @@ -83,11 +84,14 @@ def test_modules_to_save(self):
if name == "linear":
assert hasattr(module, "lora_A")
assert hasattr(module, "lora_B")
elif name == "embedding":
elif name in ["embedding", "linear2"]:
assert isinstance(module, ModulesToSaveWrapper)

state_dict = get_peft_model_state_dict(self.model)

assert "embedding.weight" in state_dict.keys()

assert hasattr(self.model.embedding, "weight")

assert hasattr(self.model.linear2, "weight")
assert hasattr(self.model.linear2, "bias")
Loading