Issues setting trainable parameters in LoRA adapter #2217
-
Hi all, consider the following model: import torch
from peft import LoraConfig
from transformers import PretrainedConfig, PreTrainedModel
class FooConfig(PretrainedConfig):
model_type: str = "foo"
def __init__(self, input_size: int, **kwargs):
super().__init__(**kwargs)
self.input_size = input_size
class FooPreTrainedModel(PreTrainedModel):
config_class = FooConfig
base_model_prefix = "model"
def _init_weights(self, module):
if isinstance(module, torch.nn.Linear):
module.weight.data.normal_(mean=0.0, std=0.02)
elif isinstance(module, torch.nn.Parameter):
module.data.normal_(mean=0.0, std=0.01)
class FooComposite(torch.nn.Module):
def __init__(self, config: FooConfig):
super().__init__()
self.linear1 = torch.nn.Linear(config.input_size, config.hidden_size)
self.linear2 = torch.nn.Linear(config.hidden_size, config.hidden_size)
self.other_param = torch.nn.Parameter(torch.empty(config.hidden_size))
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.linear2(self.linear1(x) + self.other_param)
class FooModel(FooPreTrainedModel):
def __init__(self, config: FooConfig):
super().__init__(config)
self.composite = FooComposite(config)
self.linear3 = torch.nn.Linear(config.hidden_size, config.hidden_size)
def forward(self, x: torch.Tensor) -> torch.Tensor:
return self.linear3(self.composite(x)) Suppose I want to create a LoRA adapter with the following properties:
how am I supposed to define my
NOTE: I know one solution would be to wrap "other_param" in a this is the script I'm using to check which parameters are trainable after creating the adapter: config = FooConfig(input_size=8, hidden_size=16)
model = FooModel(config)
lora_config = LoraConfig(
r=8,
lora_alpha=32,
target_modules=["linear3"],
modules_to_save=...,
exclude_modules=...
)
peft_model = get_peft_model(
model,
peft_config=lora_config,
adapter_name="dummy",
)
peft_model.set_adapter("dummy")
_ = [print(p.requires_grad, name) for name, p in peft_model.named_parameters()] |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Okay, so IIUC, the core of the problem is that you would like to treat In theory, you can just manually set the The reason why we cannot simply allow |
Beta Was this translation helpful? Give feedback.
Okay, so IIUC, the core of the problem is that you would like to treat
other_param
the same as the othermodules_to_save
but it doesn't work asmodules_to_save
can only target modules but not parameters.In theory, you can just manually set the
requires_grad
argument on this parameter, e.g.model.base_model.model.other_param.requires_grad = True
. Then this parameter should train. However, when you then callmodel.save_pretrained
, it will not be included in the checkpoint. Therefore, you would have to save it separately and also load it separately.The reason why we cannot simply allow
modules_to_save
to work onnn.Parameter
s is that we cannot control how they're being used by the model. W…