-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
'set_adapter()' throws "ValueError: Adapter not found in odict_keys" after 'load_adapter()' #1574
Comments
That's indeed very strange, it looks like the steps you take are indeed correct. Could you please paste the full error message? Is it possible to access your adapters publicly somewhere so that I can try to reproduce? |
Sure, here is the full error message, just several lines:
I have upload the adapter_model.bin of two models I want to merge to
Thanks a lot! |
Thanks for the additional context. However, I could not reproduce the error. Below is a code snippet. Note that I used my own LoRA adapters, as yours are .bin files and not safetensors (just as a safety measure), but that shouldn't really change things. from transformers import AutoModelForSequenceClassification
from peft import get_peft_model, LoraConfig, PeftModel
# creating adapters
config0 = LoraConfig(init_lora_weights=False)
config1 = LoraConfig(init_lora_weights=False)
base_model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased")
peft_model = get_peft_model(base_model, config0)
peft_model.add_adapter("other", config1)
peft_model.save_pretrained("/tmp/peft/bert")
# same as your code
base_model = AutoModelForSequenceClassification.from_pretrained("bert-base-uncased")
merged_model = PeftModel.from_pretrained(base_model, "/tmp/peft/bert/", adapter_name="backdoor_model")
merged_model.load_adapter("/tmp/peft/bert/other", adapter_name="clean_model")
adapters = ["clean_model", "backdoor_model"]
weights = [1.0, 1.0]
adapter_name = "merge_model"
density = 0.2
combination_type = "ties"
merged_model.add_weighted_adapter(adapters, weights, adapter_name, combination_type=combination_type, density=density)
merged_model.set_adapter(adapter_name) # works
print(merged_model.active_adapters) # shows ['merge_model'] What's also strange is that your error message refers to "id_1" and "id_2" but your code uses different adapter names. Are you sure that there isn't something else going on? |
Sorry, I just manually changed the name in the error msg for consistency with the initial query, but forgot to change the later code. Here id_1 stands for backdoor_model and id_2 for merge_model. Could I ask for your log and Pytorch version? When running my code, I saw this:
Not sure whether it is related. |
I have torch v2.2.0, transformers v4.39.0 and latest PEFT. My log is a bit different to yours:
This is expected, since we use |
Apologies for my late reply; I was preoccupied with something else last week. I notice the differences between your code and mine.
In your code, here, these two adapters are used for training the "same" model, i.e., the What I did is somewhat similar to:
and
and then
I guess this is where the problem I encountered arises? |
I have tested it, and it appears not to be the reason. Both work. I might figure out the problem myself. Thanks! |
I just want to confirm that this is not necessary and should not be the reason for the problem you encounter.
Feel free to share new insights or questions that you may have in this issue. |
Thank you for your patience. I have identified where the problem lies, but I do not know how to solve it: If we specify the
However, if we remove the
|
Thanks for digging further. I can reproduce the error and this is indeed caused by a bug in PEFT, which actually runs deeper than what is reported in this issue. I'll work on a bugfix and will get back to you once it's ready. |
Partially addresses huggingface#1574 This fixes an issue that occurs when we have multiple adapters with modules_to_save. Previously, the modules_to_save of the 2nd adapter which is added with add_adapter was not being honored. This is fixed now.
I'm working on a solution for this. Some parts are addressed in the PR linked above but there is also another issue which is not easily fixed. The problem with your example is that if you want to use sequence classification, it means that we also need to train the classifier head. This isn't done with LoRA, instead the classifier head is trained using full fine-tuning (based on a copy of the weights of the original classifier head). When you have multiple adapters, each will have its own classifier head (once the PR is merged). Now the problem is that you want to merge the two adapters. For the LoRA weights, there are a few ways that they can be merged. But these classifier heads cannot be merged, as they each have a fine-tuned copy of the full weights. Therefore, even with the bugfix, your original problem wouldn't be solved. In fact, we may even start to explicitly check this during merging and raise an error. |
System Info
peft-0.9.0
Who can help?
@pacman100
Information
Tasks
examples
folderReproduction
Saved two finetuned models to
peft_model_id_1
andpeft_model_id_2
in this way:Load the saved two adapters:
When trying to set adapter:
Got error:
Expected behavior
merged_model.peft_config
and got:and both two adapters occur in
merged_mode
l ifprint(merged_model)
.merged_model.add_weighted_adapter
can also work.merged_model.set_adapter("id_2")
has problems:ValueError: Adapter id_2 not found in odict_keys(['id_1'])
. It seems that only the first adapter can be seen inodict_keys
The text was updated successfully, but these errors were encountered: