Skip to content
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

Conversation

amyeroberts
Copy link
Collaborator

What does this PR do?

Fixes #31649

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@troy-baker-aumni
Copy link

Looks good! Thank you so much for your help! @amyeroberts
It is so, so appreciated.

@amyeroberts amyeroberts requested a review from ydshieh July 11, 2024 18:42
@ydshieh ydshieh self-assigned this Jul 12, 2024
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 (
Copy link
Collaborator

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)

            if is_local:
                if from_tf and os.path.isfile(
                    os.path.join(pretrained_model_name_or_path, subfolder, TF_WEIGHTS_NAME + ".index")
                ):

Comment on lines +3374 to +3425
elif not use_safetensors and os.path.isfile(
os.path.join(pretrained_model_name_or_path, subfolder, FLAX_WEIGHTS_NAME)
):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

@ydshieh
Copy link
Collaborator

ydshieh commented Jul 12, 2024

@amyeroberts Thank you. Although I think the changes make some sense, I am not 100% sure if the current design is explicitly aiming to fail only when it tries to load from a remote repository. And when it is local, it is designed to not fail (i.e. always trust a local file).

Therefore I would prefer @LysandreJik to confirm it is a design or it is just a miss.

@amyeroberts
Copy link
Collaborator Author

@ydshieh Thanks for the review!

And when it is local, it is designed to not fail (i.e. always trust a local file).

That's a good point. If that's the case, then I think this is a bit more involved, as I think we'd need to update documentation to make it clearer and possibly revise some of the flags in from_pretrained as this means trust_remote_code, local_files_only and use_safetensors have overlap in control. I think the behaviour is clearer if use_safetensors just controls whether .safetensors files are used.

@ydshieh
Copy link
Collaborator

ydshieh commented Jul 12, 2024

Yes, I agree.

Copy link
Member

@LysandreJik LysandreJik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense! Thanks for your PR @amyeroberts !

@LysandreJik
Copy link
Member

And regarding the two messages above, I don't think we want to

And when it is local, it is designed to not fail (i.e. always trust a local file).

We want local execution to behave exactly the same as it would with a remote repository. Otherwise, it can get very messy IMO.

@@ -815,6 +815,62 @@ def test_checkpoint_variant_local_sharded_safe(self):
for p1, p2 in zip(model.parameters(), new_model.parameters()):
self.assertTrue(torch.allclose(p1, p2))

def test_checkpoint_safetensors(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good (to all of us) that we write a short comment about why we add a test.

As I am sure we already have some tests that are about saving/loading. And when we add these 2 new tests, the main test case is about failing the loading if a format is specified (as we don't switch to other format).

Reading the test names here doesn't tell me what is the main objective, and I will be confused why we still add new saving/loading tests.

Copy link
Collaborator

@ydshieh ydshieh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only have 2 minor comments otherwise it's good!

(Comments for the tests' goal would be very nice.)

@amyeroberts amyeroberts force-pushed the 31649-respect-use-safetensors-flag branch 2 times, most recently from 5b6ee7b to fd0a5cc Compare July 19, 2024 18:35
@amyeroberts amyeroberts force-pushed the 31649-respect-use-safetensors-flag branch from fd0a5cc to 70b2e26 Compare July 22, 2024 16:07
@amyeroberts amyeroberts merged commit 817a676 into huggingface:main Jul 22, 2024
21 checks passed
@amyeroberts amyeroberts deleted the 31649-respect-use-safetensors-flag branch July 22, 2024 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

"use_safetensors" not enforced with "local_files_only", loads bin file
5 participants