-
Notifications
You must be signed in to change notification settings - Fork 2.1k
[core] Add safetensors integration
#553
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
Conversation
|
The documentation is not available anymore as the PR was closed or merged. |
Narsil
left a 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.
Shouldnt' you make the safetensors loading optional ? Just because there are already some PEFT pickled files out there already ? (Maybe it's handled elsewhere)
| filename = hf_hub_download( | ||
| model_id, SAFETENSORS_WEIGHTS_NAME, subfolder=kwargs.get("subfolder", None), **kwargs | ||
| ) | ||
| except: # noqa | ||
| try: | ||
| filename = hf_hub_download( | ||
| model_id, WEIGHTS_NAME, subfolder=kwargs.get("subfolder", None), **kwargs | ||
| ) | ||
| except: # noqa | ||
| raise ValueError( | ||
| f"Can't find weights for {model_id} in {model_id} or in the Hugging Face Hub. " | ||
| f"Please check that the file {WEIGHTS_NAME} or {SAFETENSORS_WEIGHTS_NAME} is present at {model_id}." | ||
| ) |
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 don't like the double try / except will find a better workaround
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.
using EntryNotFoundError for now
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.
Will use cached_file method from transformers: https://github.com/huggingface/transformers/blob/02fe3af275f0ad935441b813ef2e58b94d09788a/src/transformers/utils/hub.py#L300
src/peft/peft_model.py
Outdated
| @classmethod | ||
| def from_pretrained(cls, model, model_id, adapter_name="default", is_trainable=False, **kwargs): | ||
| def from_pretrained( | ||
| cls, model, model_id, adapter_name="default", is_trainable=False, load_safetensors=False, **kwargs |
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.
| cls, model, model_id, adapter_name="default", is_trainable=False, load_safetensors=False, **kwargs | |
| cls, model, model_id, adapter_name="default", is_trainable=False, use_safetensors=False, **kwargs |
For consistency with transformers and diffusers. And it should probably be None instead of False if we want to make it the default.
src/peft/peft_model.py
Outdated
|
|
||
| if os.path.exists(os.path.join(path, WEIGHTS_NAME)): | ||
| # load_safetensors is only used for remote weights | ||
| remote_weights_name = SAFETENSORS_WEIGHTS_NAME if load_safetensors else WEIGHTS_NAME |
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.
Since the default is False this will always use the pickled file iiuc unless user specifically asks for 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.
Thank you @younesbelkada for adding support to safetensors and related nice tests 🔐🤗, left a couple comments
|
This is PR is ready for a final review! I have also updated the PR description a bit |
I don't know if we should add a ternary logic here as safetensors becomes a core dependency on this PR. With these changes |
pacman100
left a 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.
Thank you @younesbelkada for iterating, LGTM, super cool feature addition!
| isinstance(whisper_8bit.base_model.model.model.decoder.layers[0].self_attn.v_proj, Linear8bitLt) | ||
| ) | ||
|
|
||
| @require_bitsandbytes |
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.
Nice test!
src/peft/peft_model.py
Outdated
| def active_peft_config(self): | ||
| return self.peft_config[self.active_adapter] | ||
|
|
||
| def push_to_hub( |
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.
As huggingface/transformers#24074 will be merged maybe we should remove this method? @pacman100
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.
EDT: let's leave it as it is and remove it on the next transformers release
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.
let's remove it as it is taken care in transformers and the next release is in couple of days
|
Thanks a lot @pacman100 ! I just left one open question: #553 (comment) |
pacman100
left a 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.
Thank you!
src/peft/peft_model.py
Outdated
| def active_peft_config(self): | ||
| return self.peft_config[self.active_adapter] | ||
|
|
||
| def push_to_hub( |
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.
let's remove it as it is taken care in transformers and the next release is in couple of days
* add v1 * clean up * more improvements * add device * final adjustements * use `EntryNotFoundError` * better checks * add tests and final fixes * make style && make quality * remove `push_to_hub` because of the release
What does this PR do?
This PR integrates safetensors into peft
The logic now is as follows:
safe_serialization=Truetosave_pretrainedmethodpush_to_hubmethod, by default we push on the Hub pickle file, except if a user callssafe_serialization=Trueto that method.What do you think?
An example of safetensors adapter weights on the Hub: https://huggingface.co/ybelkada/test-st-lora/tree/main
Fixes #546
cc @Narsil @pacman100