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 loading models with mismatched sizes #36463

Merged
merged 2 commits into from
Feb 28, 2025

Conversation

qubvel
Copy link
Member

@qubvel qubvel commented Feb 27, 2025

What does this PR do?

Fix loading model with mismatched sizes, the bug was introduced since:

Reported in

Fixes: #36464

Minimal reproducing example:

from transformers import AutoModelForObjectDetection

model = AutoModelForObjectDetection.from_pretrained(
    "PekingU/rtdetr_r18vd",
    id2label={0: "person"},
    label2id={"person": 0},
    ignore_mismatched_sizes=True
)

cc @ArthurZucker

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)
Copy link
Member Author

Choose a reason for hiding this comment

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

strict=False does not allow loading weights with mismatched sizes, filter those keys first

@qubvel qubvel marked this pull request as ready for review February 27, 2025 20:41
@qubvel qubvel requested a review from ArthurZucker February 27, 2025 20:41
@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.

@muellerzr muellerzr mentioned this pull request Feb 27, 2025
5 tasks
Copy link
Contributor

@muellerzr muellerzr left a comment

Choose a reason for hiding this comment

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

Thanks! This is a much better solution than the try/catch I initially was doing.

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

Super nice fix merging to get this asap

@qubvel qubvel marked this pull request as draft February 28, 2025 10:17
@qubvel qubvel marked this pull request as ready for review February 28, 2025 10:17
@ArthurZucker ArthurZucker merged commit 02776d2 into huggingface:main Feb 28, 2025
20 of 23 checks passed
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
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.

AutoModelForObjectDetection isnt working due to wrong output size
4 participants