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

Refactor initialization tests to check init from from_pretrained #30451

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

amyeroberts
Copy link
Collaborator

What does this PR do?

Adds a new test which checks that all model weights are correctly initialized when creating a model using from_pretrained. This was done by:

  • Adding a new test_initialization_from_pretrained test
  • Abstraction out the common logic between test_initialization_from_pretrained and test_initialization into _test_models_weight_initialization

Motivation

A weight init bug in CLIP was highlighted in #30374.

When a model is created via a config, the layer weights are initialized with their default torch initialization i.e. with parameter.reset_parameters().

When a model is created with a checkpoint using from_pretrained, the model weights are not initialized, and are instead torch.empty arrays. This is to speed up model creation: there's no need to initialize if we're going to use the checkpoint's weight.

However, if the weight isn't in the checkpoint and doesn't have init behaviour specified in the model's _init_weight method, then the weight will remain empty.

This can be tricky to catch if only part of the layer's weights are initialized. For example, in CLIP, the bias initialization is specified but not the weight. This means the Linear module will be marked as initialized even if this is partly True.

The initialization bug wasn't caught in our test suite, despite test_initialization, because this test creates the model from the config, not from a checkpoint.

As this is difficult to correctly handle within the code itself, I propose adding a test which will at least catch when weights aren't properly covered in the from_pretrained scenario

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?

@@ -482,33 +482,28 @@ def test_model_common_attributes(self):
pass

# override as the `temperature` parameter initilization is different for ALIGN
def test_initialization(self):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Suggestion for reviewer to look at the changes in test_modeling_common.py as this will provide more context for the changes in the other model's tests :)

@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.

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.

None yet

2 participants