Skip to content

Fix unexpected position_ids keys when loading OwlViT models#44508

Open
KartikPawade wants to merge 2 commits intohuggingface:mainfrom
KartikPawade:fix/owlvit-ignore-position-ids-unexpected-keys
Open

Fix unexpected position_ids keys when loading OwlViT models#44508
KartikPawade wants to merge 2 commits intohuggingface:mainfrom
KartikPawade:fix/owlvit-ignore-position-ids-unexpected-keys

Conversation

@KartikPawade
Copy link

What does this PR do?

Older OwlViT checkpoints stored position_ids as buffers in the text and vision embedding modules. These tensors are simple integer ranges (0 → max sequence length) and are now recomputed dynamically during initialization.

As a result, when loading models such as google/owlvit-base-patch32, the loader reports the following unexpected keys:

  • owlvit.text_model.embeddings.position_ids
  • owlvit.vision_model.embeddings.position_ids

These buffers are not required for correct model behavior and can be safely ignored.

This PR suppresses the warnings by adding the corresponding patterns to _keys_to_ignore_on_load_unexpected in OwlViTPreTrainedModel.

This change does not affect model performance or weights and only prevents unnecessary loading warnings when using older checkpoints.

Fixes #44493

Before submitting

  • This PR fixes a bug (unexpected keys warning when loading OwlViT checkpoints).
  • Did you read the contributor guideline? Yes.
  • Was this discussed/approved via a Github issue or the forum? Yes, discussed in Many models started showing UNEXPECTED Key with position id #44493.
  • Did you make sure to update the documentation with your changes? Not required.
  • Did you write any new necessary tests? Not required (warning suppression only).

Who can review?

@Rocketknight1
@yonigozlan

Older OwlViT checkpoints saved `position_ids` as buffers in the text and
vision embedding modules. These tensors are simple integer ranges and are
now recomputed dynamically during initialization.

This results in `UNEXPECTED` key warnings when loading models such as
`google/owlvit-base-patch32`.

Add the corresponding patterns to `_keys_to_ignore_on_load_unexpected`
to suppress these warnings.
@github-actions
Copy link
Contributor

github-actions bot commented Mar 6, 2026

[For maintainers] Suggested jobs to run (before merge)

run-slow: owlv2, owlvit

@KartikPawade
Copy link
Author

Hi @Rocketknight1 , CI is passing now. Happy to make any changes if needed. Thanks!

Copy link
Member

@Rocketknight1 Rocketknight1 left a comment

Choose a reason for hiding this comment

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

LGTM but please escape the . in the key names when it's a literal .!

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.

Many models started showing UNEXPECTED Key with position id

3 participants