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
[core
] Add hub utils
#39
Conversation
- from_pretrained support for config - from_pretrained support for loramodel - todo: tests - todo: push_to_hub
@younesbelkada would it make more sense to retrieve the base model from the LoRA model id? Something like so?
A full-fledged example is here: https://huggingface.co/docs/diffusers/main/en/training/lora |
Yes makes totally sense, will work on that! |
- push to hub method works - add tests - add config super class - add Lora support for `from_pretrained`
I have now a working v1 that includes also the feature you have asked @sayakpaul so that it can be adapted / used in
The latest changes adds also nice tests, for config and adapter models Here is an example of pushed adapter weights of from transformers import AutoModelForCausalLM
from peft import LoraConfig, LoraModel
from huggingface_hub.repocard import RepoCard
lora_model_id = "ybelkada/test-opt-lora"
card = RepoCard.load(lora_model_id)
model_id = card.data.to_dict()["base_model"]
# Load the config & model
config = LoraConfig.from_pretrained(lora_model_id)
model = AutoModelForCausalLM.from_pretrained(model_id)
# Load the Lora model
model = LoraModel.from_pretrained(model, lora_model_id) cc @sayakpaul @pacman100 this PR is now ready for review |
Love it. Thank you, Younes! |
src/peft/tuners/lora.py
Outdated
@@ -72,7 +86,7 @@ def __post_init__(self): | |||
self.peft_type = PeftType.LORA | |||
|
|||
|
|||
class LoraModel(torch.nn.Module): | |||
class LoraModel(PushToHubMixin, torch.nn.Module): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there any specific advantage to using transformers.PushToHubMixin
vs huggingface_hub
's? the latter means you only have to implement a single _save_pretrained()
method, but perhaps you need more flexibility
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was not aware that the class you propose is simpler, I will have a look now and potentially replace it !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried it and in both cases I needed to re-implement from_pretrained
method, also it seems that push_to_hub
from PushToHubMixin
first clones a repo with the weights before pushing on the Hub, maybe I'll just stick on transformers.PushToHubMixin
for now :D
@younesbelkada I spun up this Colab Notebook quickly to showcase LoRA (from PEFT) for image classification and wow: https://colab.research.google.com/drive/1GtKXiVyINz2rRnd5FSMh2WnTjt7nOTEn?usp=sharing ❤️ I guess after this PR is merged, we should incorporate Hub-related utilities in the Notebook. WDYT? Thanks to @pacman100 for his guidance and help. |
Thanks a lot for sharing the notebook @sayakpaul ! |
Cool, I am looking forward to it. Next, I will try SegFormer. My plan is to come up with the faithful benchmarks that users can refer to while using PEFT for computer vision to really see the benefits. |
src/peft/utils/config.py
Outdated
peft_type: Union[str, PeftType] = field(default=None, metadata={"help": "Peft type"}) | ||
task_type: Union[str, TaskType] = field(default=None, metadata={"help": "Task type"}) | ||
inference_mode: bool = field(default=False, metadata={"help": "Whether to use inference mode"}) | ||
|
||
|
||
@dataclass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is required else similar error as seen in the previous comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! In any case I added tests to test that the script you shared above will not fail
Hello @younesbelkada, Thanks a lot for adding hf hub utils and test 🤗. This would help the users a lot by being able to load and save peft modules to HF Hub 🚀. Few general comments:
{
"base_model_name_or_path": "facebook/opt-350m",
"bias": "none",
"enable_lora": null,
"fan_in_fan_out": false,
"inference_mode": false,
"lora_alpha": 32,
"lora_dropout": 0.05,
"merge_weights": false,
"peft_type": "LORA",
"r": 16,
"target_modules": [
"q_proj",
"v_proj"
],
"task_type": "SEQ_CLS"
} With this one can load the model: # Load the config & model
config = LoraConfig.from_pretrained(lora_model_id)
model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path)
# Load the Lora model
model = LoraModel.from_pretrained(model, lora_model_id)
Saving
Loading
Left a few other suggestions and comments. |
Co-authored-by: Sourab Mangrulkar <13534540+pacman100@users.noreply.github.com>
- remove `README` - inherit from `dataclass` - add new test
Thanks a lot for the feedback @pacman100 ! |
Hello @younesbelkada, Thank You for reiterating! 🤗 Wrt tests for remaining peft methods, I will address them in a follow up PR. This PR is good to go 🚀. |
What does this PR do?
This PR adds some enhancements on
peft
by being able to share trained LoRA weights and configs. With this PR the API looks like the following:This PR adds:
from_pretrained
support forxxConfig
andLoraModel
save_pretrained
support forxxConfig
andLoraModel
TODOs:
push_to_hub
supportcc @pacman100