You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
diffusers doesn't save and load the LoraConfig, resulting wrong lora_alpha during inference. #6087
When I run train_dreambooth_lora_sdxl.py, I find the StableDiffusionXLLoraLoaderMixin (and LoraLoaderMixin) doesn't save & load the LoraConfig.
The default lora_alpha is 8 in LoraConfig but is 1 in peft Linear. The lora_alpha is 8 during training but is 1 when I load the weights from a checkpoint as the XXLoraLoaderMixin cannot parse the correct lora_alpha from the saved Lora weights. The wrong lora_alpha gives the wrong scaling in the peft Linear layer.
I manually reset the scaling to get the correct result, but I think this is a bug and should be fixed.
Reproduction
Clone the diffusers repo and install it. Then prepare any one image in a folder.
For example, I try to overfit the first image. The second image is the validation image at epoch 250 (tensorboard). The third one is the result after the pipeline loads Lora weights from the saved checkpoint (epoch 300, also available in tensorboard).
Logs
No response
System Info
Copy-and-paste the text below in your GitHub issue and FILL OUT the two last points.
I think I'm getting similar problems with examples/text_to_image/train_text_to_image_lora.py.
The tensorboard validation results are good as the model is training, but the final validation (when the model is saved and reloaded) is bad.
Loading the checkpoints afterwards also gives poor results
I think I'm getting similar problems with examples/text_to_image/train_text_to_image_lora.py. The tensorboard validation results are good as the model is training, but the final validation (when the model is saved and reloaded) is bad. Loading the checkpoints afterwards also gives poor results
@PhilippeNguyen My current solution is the following, hope it's helpful:
During the training, I save the LoraConfig, e.g.:
@sayakpaul I am getting the same when running the examples/dreambooth/train_dreambooth_lora.py script. At training time It seems to be using the default lora_alpha of 8 without an argument that controls it. At inference, when calling pipe.load_lora_weights(lora_model_path)lora_alpha is set to be lora rank (extracted from the lora state_dict).
It fixes it, but I am not sure this is the best solution. Why not allow the user to specify the lora_alpha as an argument and only if they don't default it to the rank?
The issue with the above suggestion is that one would need to save the alpha value and load it when loading the lora weights.
Got it, the results I am getting when training on rank=alpha=16 are worse than rank=16, alpha=8. I think (but not sure, running experiments now) that higher alpha should result in lower LR. Maybe it's worth noting this somewhere in the readme
Activity
sayakpaul commentedon Dec 7, 2023
@younesbelkada could you take a look here?
PhilippeNguyen commentedon Dec 7, 2023
I think I'm getting similar problems with examples/text_to_image/train_text_to_image_lora.py.
The tensorboard validation results are good as the model is training, but the final validation (when the model is saved and reloaded) is bad.
Loading the checkpoints afterwards also gives poor results
YiqunChen1999 commentedon Dec 8, 2023
@PhilippeNguyen My current solution is the following, hope it's helpful:
During the training, I save the LoraConfig, e.g.:
During inference, I load the LoraConfig and reset the
lora_alphaandscaling:The functions are defined as follows:
The solution might not be the best, but it works for me.
sayakpaul commentedon Dec 11, 2023
@YiqunChen1999 #6135 does this help?
YiqunChen1999 commentedon Dec 11, 2023
@sayakpaul Yes, it works for me. Thanks for your efforts!
sayakpaul commentedon Dec 19, 2023
@YiqunChen1999 we had to close #6135. Does #6225 help?
YiqunChen1999 commentedon Dec 19, 2023
@sayakpaul Yes, it works well in my case!
eliahuhorwitz commentedon Dec 21, 2023
@sayakpaul I am getting the same when running the
examples/dreambooth/train_dreambooth_lora.pyscript. At training time It seems to be using the defaultlora_alphaof 8 without an argument that controls it. At inference, when callingpipe.load_lora_weights(lora_model_path)lora_alphais set to be lora rank (extracted from the lora state_dict).sayakpaul commentedon Dec 21, 2023
Ah, now I see. Applying the changes from #6225 should fix that. Can you confirm?
eliahuhorwitz commentedon Dec 21, 2023
It fixes it, but I am not sure this is the best solution. Why not allow the user to specify the lora_alpha as an argument and only if they don't default it to the rank?
The issue with the above suggestion is that one would need to save the alpha value and load it when loading the lora weights.
sayakpaul commentedon Dec 21, 2023
To keep it harmonized with the previous method of training. #6135 did what you just said but we eventually decided not to that route for now.
eliahuhorwitz commentedon Dec 21, 2023
Got it, the results I am getting when training on rank=alpha=16 are worse than rank=16, alpha=8. I think (but not sure, running experiments now) that higher alpha should result in lower LR. Maybe it's worth noting this somewhere in the readme