-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Adding use_safetensors
argument to give more control to users
#2123
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
Merged
patrickvonplaten
merged 12 commits into
huggingface:main
from
Narsil:add_use_safetensors
Mar 16, 2023
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
864c00e
Adding `use_safetensors` argument to give more control to users
Narsil b2236b8
Doc style.
Narsil b2ef1de
Rebased (not functional).
Narsil 8185f88
Rebased and functional with tests.
Narsil 7eb3a48
Style.
Narsil dd066f9
Merge branch 'main' into add_use_safetensors
patrickvonplaten 756063f
Apply suggestions from code review
patrickvonplaten f86261b
Style.
Narsil 3a2899f
Addressing comments.
Narsil 9d35f16
Update tests/test_pipelines.py
Narsil 09a2ae1
Merge branch 'main' into add_use_safetensors
Narsil 89dadb7
Black ???
Narsil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -392,6 +392,10 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P | |
variant (`str`, *optional*): | ||
If specified load weights from `variant` filename, *e.g.* pytorch_model.<variant>.bin. `variant` is | ||
ignored when using `from_flax`. | ||
use_safetensors (`bool`, *optional* ): | ||
If set to `True`, the pipeline will forcibly load the models from `safetensors` weights. If set to | ||
`None` (the default). The pipeline will load using `safetensors` if safetensors weights are available | ||
*and* if `safetensors` is installed. If the to `False` the pipeline will *not* use `safetensors`. | ||
Comment on lines
+395
to
+398
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool! |
||
|
||
<Tip> | ||
|
||
|
@@ -423,6 +427,17 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P | |
device_map = kwargs.pop("device_map", None) | ||
low_cpu_mem_usage = kwargs.pop("low_cpu_mem_usage", _LOW_CPU_MEM_USAGE_DEFAULT) | ||
variant = kwargs.pop("variant", None) | ||
use_safetensors = kwargs.pop("use_safetensors", None) | ||
|
||
if use_safetensors and not is_safetensors_available(): | ||
raise ValueError( | ||
"`use_safetensors`=True but safetensors is not installed. Please install safetensors with `pip install safetenstors" | ||
) | ||
|
||
allow_pickle = False | ||
if use_safetensors is None: | ||
use_safetensors = is_safetensors_available() | ||
allow_pickle = True | ||
|
||
if low_cpu_mem_usage and not is_accelerate_available(): | ||
low_cpu_mem_usage = False | ||
|
@@ -509,7 +524,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P | |
|
||
model = load_flax_checkpoint_in_pytorch_model(model, model_file) | ||
else: | ||
if is_safetensors_available(): | ||
if use_safetensors: | ||
try: | ||
model_file = _get_model_file( | ||
pretrained_model_name_or_path, | ||
|
@@ -525,7 +540,9 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P | |
user_agent=user_agent, | ||
commit_hash=commit_hash, | ||
) | ||
except: # noqa: E722 | ||
except IOError as e: | ||
if not allow_pickle: | ||
raise e | ||
pass | ||
if model_file is None: | ||
model_file = _get_model_file( | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@patrickvonplaten before you merge can you double check this error change is ok?