-
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
Different Results When Predicting with Multiple LoRA Adapters in a Loop VS. Using only One LoRA #2270
Comments
Update: I identified the problem. The first time after
Then if I load another lora by
There are two lora adapters in the model, which is beyond my expectation, since I thought the |
Seems like its related to issue #2184. However, I didn't see any warnings when I tried to load multiple PiSSA lora adapters. |
Please note that after loading the first adapter with
Okay, so you're using PiSSA. Please follow the advice in the linked issue to convert them to normal LoRA adapters and then they should not interfere with one another. Regarding the missing warning, it was added in a later PEFT version, if you upgrade PEFT you should see it. |
@BenjaminBossan Thanks for your reply, happy to meet you in the timeline :) base_model = MyModelForClassification.from_pretrained(base_model_path, num_labels=3,ignore_mismatched_sizes=True)
lora_name = 'm1'
D = {
"m1": "trained_model/pissa_to_lora/TP16to19_seed2",
"m2": "trained_model/pissa_to_lora/TP16to19_seed3",
"m3": "trained_model/pissa_to_lora/TP20to23_seed2",
"m4": "trained_model/pissa_to_lora/TP20to23_seed3",
}
print('loading pissa...')
model = PeftModel.from_pretrained(base_model, lora_map[lora_name])
output_dir = D[lora_name]
print('saving pissa...')
model.save_pretrained(output_dir)
model.save_pretrained(output_dir, convert_pissa_to_lora="pissa_init")
print('Saved!') and got this error:
|
Note that the
according to the docs. (Note that the |
I don't quite understand what is "path to the initialized adapter, which is obtained after initializing the model with PiSSA or OLoRA and before performing any training." Now I have a trained PiSSA adapter saved at I tried both, but found that if I load from the saved path, the model is still doing SVD. I checked this issue: #1929, which seems to tell me the path should be created before training. So how could I convert an already trained PiSSA model? |
No, it's neither of those. What you need to do is to save the adapter after it has been initialized but before training of the model has started, as e.g. shown here: peft/examples/pissa_finetuning/preprocess.py Lines 52 to 65 in ec92cdc
I know that you have already trained your model, so perhaps it's too late. But if you still have your training script and if you have fixed all random seeds, you should be able to restore that state exactly as it was before you did your first training run. So try to restore this state and save the model somewhere. Then use that model path as the argument for |
Thanks a lot for your help and detailed information! I will try to train my models again and see if all problem solved. I will report tomorrow and close the issue then. |
Good luck. As mentioned, if you can precisely restore the model state to before training started, there should be no need for retraining, otherwise it is unfortunately required. |
Hi @BenjaminBossan I'm back again. I fixed all random seeds during previous training, so I tried to restore the model state without retraining. Here's how I save the initial pissa weights: from transformers import set_seed
from transformers import BertForSequenceClassification
from peft import LoraConfig, get_peft_model, TaskType
set_seed(42) # <---------- same with training
base_model_path = '../../llm_hub/Erlangshen-Roberta-330M-Sentiment'
class MyModelForClassification(BertForSequenceClassification):
...
base_model = MyModelForClassification.from_pretrained(base_model_path, num_labels=3,ignore_mismatched_sizes=True)
peft_config = LoraConfig(init_lora_weights="pissa", task_type=TaskType.SEQ_CLS, inference_mode=False, r=8, lora_alpha=32, lora_dropout=0.1) # <---------- same with training
init_pissa_model = get_peft_model(base_model, peft_config)
init_pissa_model.peft_config["default"].init_lora_weights = True
init_pissa_model.save_pretrained('trained_model/pissa_init_seed42') Then I try to convert pissa to lora with: trained_pissa_path = '...' # -----> this is where I save the model during training, the original pissa weights
class MyModelForClassification(BertForSequenceClassification):
...
base_model = MyModelForClassification.from_pretrained(base_model_path, num_labels=3,ignore_mismatched_sizes=True)
converted_pissa_path = '...' # -----> this is where I want to save the converted version
trained_pissa_model = PeftModel.from_pretrained(base_model, trained_pissa_path )
trained_pissa_model.save_pretrained(converted_pissa_path , path_initial_model_for_weight_conversion='trained_model/pissa_init_seed42') Finally I load the model with: model = PeftModel.from_pretrained(base_model, converted_pissa_path) and found its still doing SVD. Note that the above three scripts are seperate python programs. Checking #1929 (comment) again, I guess this is due to my PEFT version (0.11.1). Maybe I have to upgrade the version and try again. I'm not sure if a new version can work, since my trained model is based on the old version. What confuses me during these trials is that everythings runs fine without throwing errors or warnings, so I can't know whether a correct converted pissa is saved until I finally load it. I guess more checks can be done internally in the Anyway, thank you @BenjaminBossan for your help these days! Best regards! |
Thanks for trying out the suggestion. I'm not quite sure if your second code block is correct. Where is the actual training happening? The intent is that you can start training at the end of the first code block, after the
Could you paste the
Please try if upgrading resolves the issue for you. In general, it is save to upgrade PEFT, as we avoid making changes that make older checkpoints invalid. There are rare occasions where upgrading PEFT can lead to different results, but we document that in our release notes.
We don't know if there is any error in the conversion here, I suspect it's either that the necessary steps have not been performed or it is indeed required to upgrade PEFT. |
System Info
Linux, Python 3.8
A two-H100 node.
Name: transformers
Version: 4.34.1
Name: peft
Version: 0.11.1
Who can help?
@BenjaminBossan
Information
Tasks
examples
folderReproduction
Description
I encountered a strange issue while using PEFT LoRA adapters with the Hugging Face Trainer. When predicting using different LoRA adapters in a loop, the predictions are different compared to when using the same LoRA adapter (e.g.,
m4
) individually. The issue arises when I predict using multiple LoRA adapters sequentially, and then compare the results of them4
adapter between the two scenarios.Steps to Reproduce
lora_map
that maps LoRA adapter names to their respective paths.lora_map
and predicts using each LoRA adapter:the
lora_map
is likelora_map={'m1':xxx,'m2':xxx,...}
I found the results in
final_pred_df[final_pred_df.lora == 'm4']
is different from predicting with loadingm4
only. But the results form1
is the same, probably because its the first in the lora_map.What could be the problem? What happend when I load the second adapter using
PeftModel.from_pretrained
?I'm sorry I can't share my lora weights (it was trained with PiSSA) since its a private model.
Expected behavior
Same results.
The text was updated successfully, but these errors were encountered: