Fix loading models with mismatched sizes#36463
Merged
Merged
Conversation
qubvel
commented
Feb 27, 2025
Comment on lines
+4910
to
4913
| mismatched_names = [name for name, _, _ in mismatched_keys] | ||
| fixed_state_dict = {k: v for k, v in state_dict.items() if k not in mismatched_names} | ||
| fixed_state_dict = model_to_load._fix_state_dict_keys_on_load(fixed_state_dict) | ||
| model_to_load.load_state_dict(fixed_state_dict, strict=False, assign=assign_to_params_buffers) |
Contributor
Author
There was a problem hiding this comment.
strict=False does not allow loading weights with mismatched sizes, filter those keys first
|
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. |
muellerzr
approved these changes
Feb 27, 2025
Contributor
muellerzr
left a comment
There was a problem hiding this comment.
Thanks! This is a much better solution than the try/catch I initially was doing.
ArthurZucker
approved these changes
Feb 28, 2025
Collaborator
ArthurZucker
left a comment
There was a problem hiding this comment.
Super nice fix merging to get this asap
4 tasks
garrett361
pushed a commit
to garrett361/transformers
that referenced
this pull request
Mar 4, 2025
* Fix loading model with mismatched sizes * trigger tests
garrett361
pushed a commit
to garrett361/transformers
that referenced
this pull request
Mar 4, 2025
* Fix loading model with mismatched sizes * trigger tests
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this PR do?
Fix loading model with mismatched sizes, the bug was introduced since:
Reported in
Fixes: #36464
Minimal reproducing example:
cc @ArthurZucker