-
Notifications
You must be signed in to change notification settings - Fork 27.5k
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
Don't default to other weights file when use_safetensors=True #31874
Merged
amyeroberts
merged 6 commits into
huggingface:main
from
amyeroberts:31649-respect-use-safetensors-flag
Jul 22, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a8432c3
Don't default to other weights file when use_safetensors=True
amyeroberts 126de6e
Add tests
amyeroberts 1e5c960
Update tests/utils/test_modeling_utils.py
amyeroberts e9e6fb3
Add clarifying comments to tests
amyeroberts ff76be3
Update tests/utils/test_modeling_utils.py
amyeroberts 70b2e26
Update tests/utils/test_modeling_utils.py
amyeroberts 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 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 |
---|---|---|
|
@@ -3395,14 +3395,14 @@ def from_pretrained( | |
pretrained_model_name_or_path, subfolder, _add_variant(SAFE_WEIGHTS_INDEX_NAME, variant) | ||
) | ||
is_sharded = True | ||
elif os.path.isfile( | ||
elif not use_safetensors and os.path.isfile( | ||
os.path.join(pretrained_model_name_or_path, subfolder, _add_variant(WEIGHTS_NAME, variant)) | ||
): | ||
# Load from a PyTorch checkpoint | ||
archive_file = os.path.join( | ||
pretrained_model_name_or_path, subfolder, _add_variant(WEIGHTS_NAME, variant) | ||
) | ||
elif os.path.isfile( | ||
elif not use_safetensors and os.path.isfile( | ||
os.path.join(pretrained_model_name_or_path, subfolder, _add_variant(WEIGHTS_INDEX_NAME, variant)) | ||
): | ||
# Load from a sharded PyTorch checkpoint | ||
|
@@ -3411,15 +3411,18 @@ def from_pretrained( | |
) | ||
is_sharded = True | ||
# At this stage we don't have a weight file so we will raise an error. | ||
elif os.path.isfile( | ||
os.path.join(pretrained_model_name_or_path, subfolder, TF_WEIGHTS_NAME + ".index") | ||
) or os.path.isfile(os.path.join(pretrained_model_name_or_path, subfolder, TF2_WEIGHTS_NAME)): | ||
elif not use_safetensors and ( | ||
os.path.isfile(os.path.join(pretrained_model_name_or_path, subfolder, TF_WEIGHTS_NAME + ".index")) | ||
or os.path.isfile(os.path.join(pretrained_model_name_or_path, subfolder, TF2_WEIGHTS_NAME)) | ||
): | ||
raise EnvironmentError( | ||
f"Error no file named {_add_variant(WEIGHTS_NAME, variant)} found in directory" | ||
f" {pretrained_model_name_or_path} but there is a file for TensorFlow weights. Use" | ||
" `from_tf=True` to load this model from those weights." | ||
) | ||
elif os.path.isfile(os.path.join(pretrained_model_name_or_path, subfolder, FLAX_WEIGHTS_NAME)): | ||
elif not use_safetensors and os.path.isfile( | ||
os.path.join(pretrained_model_name_or_path, subfolder, FLAX_WEIGHTS_NAME) | ||
): | ||
Comment on lines
+3423
to
+3425
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. same |
||
raise EnvironmentError( | ||
f"Error no file named {_add_variant(WEIGHTS_NAME, variant)} found in directory" | ||
f" {pretrained_model_name_or_path} but there is a file for Flax weights. Use `from_flax=True`" | ||
|
This file contains 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
Oops, something went wrong.
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.
Maybe use
if from_tf and ...
here, to correspond to (several lines above)