Skip to content

Fix: Guard against None num_query_tokens in Blip2Processor (to avoid TypeError)#42311

Open
Flakes342 wants to merge 13 commits intohuggingface:mainfrom
Flakes342:bug/nqt
Open

Fix: Guard against None num_query_tokens in Blip2Processor (to avoid TypeError)#42311
Flakes342 wants to merge 13 commits intohuggingface:mainfrom
Flakes342:bug/nqt

Conversation

@Flakes342
Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #42203

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?

Fixed a bug where Blip2Processor assumes num_query_tokens is an int and does max_length - self.num_query_tokens which raises TypeError when num_query_tokens is None.

This change:

  • Treats num_query_tokens as 0 when it is None (with a warning).
  • Adds a minimal unit test to prevent regression.

Who can review?

@Rocketknight1

@Rocketknight1
Copy link
Copy Markdown
Member

cc @zucchini-nlp! I think it's fine without the warning message, but deferring to her on what behaviour we want from processors 😅

Copy link
Copy Markdown
Member

@zucchini-nlp zucchini-nlp left a comment

Choose a reason for hiding this comment

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

@Flakes342 thanks for the PR!

To support older models from the hub which weren't updated, I think we should update the default value of num_query_tokens.

The modeling code assumes that input_ids have special image placeholders and with self.num_query_tokens=None the placeholders will not be added in the input text. Therefore the model will not see an input image

Let's add the default value of 32 which is the only value used in all official checkpoints. Ping me for another review when ready :)

@HuggingFaceDocBuilderDev
Copy link
Copy Markdown

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.

@Flakes342
Copy link
Copy Markdown
Contributor Author

@zucchini-nlp thr pr is ready for your review

@zucchini-nlp
Copy link
Copy Markdown
Member

run-slow: blip_2

Comment thread src/transformers/models/blip_2/processing_blip_2.py Outdated
@github-actions
Copy link
Copy Markdown
Contributor

This comment contains run-slow, running the specified jobs:

models: ["models/blip_2"]
quantizations: []

@github-actions
Copy link
Copy Markdown
Contributor

CI Results

Workflow Run ⚙️

Model CI Report

❌ Failed tests

  • blip_2:
    tests/models/blip_2/test_modeling_blip_2.py::Blip2ModelIntegrationTest::test_inference_itm
    tests/models/blip_2/test_modeling_blip_2.py::Blip2ModelIntegrationTest::test_inference_itm_fp16

@zucchini-nlp
Copy link
Copy Markdown
Member

run-slow: blip_2

1 similar comment
@zucchini-nlp
Copy link
Copy Markdown
Member

run-slow: blip_2

@zucchini-nlp
Copy link
Copy Markdown
Member

Can you also fix the modeling code to be in line with the changes? In some places we check if the model config was updated or not with if self.config.image_token_index is not None but after this PR all model inputs will be prepared in "updated" format. So we can assume it safely in modeling code

It can be later tested with RUN_SLOW=1 pytest tests/models/blip_2/test_modeling_blip_2.py::Blip2ModelIntegrationTest::test_inference_itm

Finally, you need to run make fix-copies and make style to pass the CI checks

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Dec 4, 2025

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

run-slow: blip_2

@zucchini-nlp
Copy link
Copy Markdown
Member

@bot /style

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Dec 4, 2025

Style fix is beginning .... View the workflow run here.

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.

num_query_tokens of blip2 might be None

4 participants