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

Fix KeyError when initialize the model with ignore_mismatched_sizes=True #16920

Merged
merged 1 commit into from
Apr 26, 2022

Conversation

tricktreat
Copy link
Contributor

@tricktreat tricktreat commented Apr 25, 2022

What does this PR do?

KeyError is thrown when the model is initialized from pre-trained weights with ignore_mismatched_sizes=True. Reproduced as follows:

>>> import transformers
>>> transformers.BertModel.from_pretrained("bert-base-cased", ignore_mismatched_sizes=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/shenyl/miniconda/envs/cuda111/lib/python3.8/site-packages/transformers/modeling_utils.py", line 1882, in from_pretrained
    model, missing_keys, unexpected_keys, mismatched_keys, error_msgs = cls._load_pretrained_model(
  File "/home/shenyl/miniconda/envs/cuda111/lib/python3.8/site-packages/transformers/modeling_utils.py", line 2003, in _load_pretrained_model
    and state_dict[checkpoint_key].shape != model_state_dict[model_key].shape
KeyError: 'bert.embeddings.LayerNorm.weight'

The cause is that the key modified by function _fix_key is not found in state_dict.

The solution is to use the original loaded keys when finding the mismatched keys.

@tricktreat tricktreat changed the title use original loaded keys to find mismatched keys Use original loaded keys to find mismatched keys Apr 25, 2022
@tricktreat tricktreat changed the title Use original loaded keys to find mismatched keys Fix KeyError when initialize a model from pre-trained weights with ignore_mismatched_sizes=True. Apr 25, 2022
@tricktreat tricktreat changed the title Fix KeyError when initialize a model from pre-trained weights with ignore_mismatched_sizes=True. Fix KeyError when initialize the model from pre-trained weights with ignore_mismatched_sizes=True. Apr 25, 2022
@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Apr 25, 2022

The documentation is not available anymore as the PR was closed or merged.

@tricktreat tricktreat changed the title Fix KeyError when initialize the model from pre-trained weights with ignore_mismatched_sizes=True. Fix KeyError when initialize the model with ignore_mismatched_sizes=True Apr 26, 2022
@LysandreJik LysandreJik requested a review from sgugger April 26, 2022 21:02
Copy link
Collaborator

@sgugger sgugger left a comment

Choose a reason for hiding this comment

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

Confirming this fixes the issue, thanks a lot for your PR!

@sgugger sgugger merged commit 2d91e3c into huggingface:main Apr 26, 2022
chamidullinr pushed a commit to chamidullinr/transformers that referenced this pull request Apr 28, 2022
elusenji pushed a commit to elusenji/transformers that referenced this pull request Jun 12, 2022
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.

3 participants