From dbb8e5c2cbe769186010551ba3399fff30f939a1 Mon Sep 17 00:00:00 2001 From: Flakes342 Date: Fri, 21 Nov 2025 02:10:03 +0530 Subject: [PATCH 1/5] nqt fixed --- src/transformers/models/blip_2/processing_blip_2.py | 11 +++++++++-- tests/models/blip_2/test_processing_blip_2.py | 12 ++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/transformers/models/blip_2/processing_blip_2.py b/src/transformers/models/blip_2/processing_blip_2.py index 71f79583c77e..3984b77dc7f5 100644 --- a/src/transformers/models/blip_2/processing_blip_2.py +++ b/src/transformers/models/blip_2/processing_blip_2.py @@ -115,8 +115,15 @@ def __call__( return_tensors = output_kwargs["text_kwargs"].pop("return_tensors", None) max_length = output_kwargs["text_kwargs"].pop("max_length", None) if max_length is not None: - output_kwargs["text_kwargs"]["max_length"] = max_length - self.num_query_tokens - + # Treating None as 0 to avoid TypeError during subtraction. + num_query_tokens = self.num_query_tokens + if num_query_tokens is None: + logger.warning( + "Blip2Processor.num_query_tokens is None. Treating as 0 for max_length calculations. " + "Consider updating the processor to set num_query_tokens explicitly." + ) + num_query_tokens = 0 + output_kwargs["text_kwargs"]["max_length"] = max_length - int(num_query_tokens) encoding = BatchFeature(tensor_type=return_tensors) if text is not None: if isinstance(text, str): diff --git a/tests/models/blip_2/test_processing_blip_2.py b/tests/models/blip_2/test_processing_blip_2.py index e5c17a11ce02..4d03ab179644 100644 --- a/tests/models/blip_2/test_processing_blip_2.py +++ b/tests/models/blip_2/test_processing_blip_2.py @@ -118,3 +118,15 @@ def test_tokenizer_decode(self): decoded_tok = tokenizer.batch_decode(predicted_ids) self.assertListEqual(decoded_tok, decoded_processor) + + def test_none_num_query_tokens_is_handled(self): + image_processor = self.get_image_processor() + tokenizer = self.get_tokenizer() + + processor = Blip2Processor(tokenizer=tokenizer, image_processor=image_processor, num_query_tokens=None) + + input_str = "hello world" + + outputs = processor(text=input_str, max_length=20, return_tensors="np") + self.assertIn("input_ids", outputs) + self.assertIn("attention_mask", outputs) From 4db134fbc39a1f52c5b12c197644f4cc2e117fa2 Mon Sep 17 00:00:00 2001 From: Flakes342 Date: Tue, 25 Nov 2025 03:40:34 +0530 Subject: [PATCH 2/5] Defaults to 32 and prevents negative truncation values --- tests/models/blip_2/test_processing_blip_2.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/models/blip_2/test_processing_blip_2.py b/tests/models/blip_2/test_processing_blip_2.py index 4d03ab179644..6590804c0035 100644 --- a/tests/models/blip_2/test_processing_blip_2.py +++ b/tests/models/blip_2/test_processing_blip_2.py @@ -128,5 +128,6 @@ def test_none_num_query_tokens_is_handled(self): input_str = "hello world" outputs = processor(text=input_str, max_length=20, return_tensors="np") + self.assertEqual(processor.num_query_tokens, 32) self.assertIn("input_ids", outputs) self.assertIn("attention_mask", outputs) From adabecdffe989b85a0a3031bebe0cd6f1711ba8c Mon Sep 17 00:00:00 2001 From: Flakes342 Date: Tue, 25 Nov 2025 03:45:03 +0530 Subject: [PATCH 3/5] Defaults to 32 and prevents negative truncation values --- .../models/blip_2/processing_blip_2.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/transformers/models/blip_2/processing_blip_2.py b/src/transformers/models/blip_2/processing_blip_2.py index 3984b77dc7f5..2654b310e54a 100644 --- a/src/transformers/models/blip_2/processing_blip_2.py +++ b/src/transformers/models/blip_2/processing_blip_2.py @@ -73,7 +73,8 @@ def __init__(self, image_processor, tokenizer, num_query_tokens=None, **kwargs): tokenizer.add_tokens([self.image_token], special_tokens=True) else: self.image_token = tokenizer.image_token - self.num_query_tokens = num_query_tokens + # Default to 32 if missing, matching official BLIP-2 checkpoints + self.num_query_tokens = num_query_tokens if num_query_tokens is not None else 32 super().__init__(image_processor, tokenizer) @@ -115,15 +116,9 @@ def __call__( return_tensors = output_kwargs["text_kwargs"].pop("return_tensors", None) max_length = output_kwargs["text_kwargs"].pop("max_length", None) if max_length is not None: - # Treating None as 0 to avoid TypeError during subtraction. - num_query_tokens = self.num_query_tokens - if num_query_tokens is None: - logger.warning( - "Blip2Processor.num_query_tokens is None. Treating as 0 for max_length calculations. " - "Consider updating the processor to set num_query_tokens explicitly." - ) - num_query_tokens = 0 - output_kwargs["text_kwargs"]["max_length"] = max_length - int(num_query_tokens) + adjusted_max_length = max_length - self.num_query_tokens + if adjusted_max_length > 0: + output_kwargs["text_kwargs"]["max_length"] = adjusted_max_length encoding = BatchFeature(tensor_type=return_tensors) if text is not None: if isinstance(text, str): From 0934c4b7743cfc57d501dcd60625238a6fbec57b Mon Sep 17 00:00:00 2001 From: Flakes342 Date: Tue, 2 Dec 2025 01:51:32 +0530 Subject: [PATCH 4/5] default fixed --- src/transformers/models/blip_2/processing_blip_2.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/transformers/models/blip_2/processing_blip_2.py b/src/transformers/models/blip_2/processing_blip_2.py index 49afb22ecb65..64e86aef926d 100644 --- a/src/transformers/models/blip_2/processing_blip_2.py +++ b/src/transformers/models/blip_2/processing_blip_2.py @@ -60,15 +60,14 @@ class Blip2Processor(ProcessorMixin): Number of tokens used by the Qformer as queries, should be same as in model's config. """ - def __init__(self, image_processor, tokenizer, num_query_tokens=None, **kwargs): + def __init__(self, image_processor, tokenizer, num_query_tokens=32, **kwargs): tokenizer.return_token_type_ids = False if not hasattr(tokenizer, "image_token"): self.image_token = AddedToken("", normalized=False, special=True) tokenizer.add_tokens([self.image_token], special_tokens=True) else: self.image_token = tokenizer.image_token - # Default to 32 if missing, matching official BLIP-2 checkpoints - self.num_query_tokens = num_query_tokens if num_query_tokens is not None else 32 + self.num_query_tokens = num_query_tokens super().__init__(image_processor, tokenizer) From 9a4303a89890b8656b919f57ccd3b660e8485b6e Mon Sep 17 00:00:00 2001 From: Flakes342 Date: Thu, 4 Dec 2025 17:52:55 +0530 Subject: [PATCH 5/5] redundant checks removed from modeling code --- src/transformers/models/blip_2/modeling_blip_2.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/transformers/models/blip_2/modeling_blip_2.py b/src/transformers/models/blip_2/modeling_blip_2.py index 4107f448717b..3d883c37a5a2 100644 --- a/src/transformers/models/blip_2/modeling_blip_2.py +++ b/src/transformers/models/blip_2/modeling_blip_2.py @@ -2022,13 +2022,7 @@ def forward( if use_image_text_matching_head: query_tokens = self.query_tokens.expand(image_embeds.shape[0], -1, -1) - if self.config.image_token_index is not None: - input_ids = input_ids[:, self.config.num_query_tokens :] - else: - query_attention_mask = torch.ones( - query_tokens.size()[:-1], dtype=torch.long, device=query_tokens.device - ) - attention_mask = torch.cat([query_attention_mask, attention_mask], dim=1) + input_ids = input_ids[:, self.config.num_query_tokens :] query_embeds = self.embeddings( input_ids=input_ids, @@ -2060,9 +2054,8 @@ def forward( image_embeds = query_outputs[0] if not return_dict else query_outputs.last_hidden_state image_embeds = image_embeds.to(dtype=self.vision_projection.weight.dtype) - if self.config.image_token_index is not None: - input_ids = input_ids[:, self.config.num_query_tokens :] - attention_mask = attention_mask[:, self.config.num_query_tokens :] + input_ids = input_ids[:, self.config.num_query_tokens :] + attention_mask = attention_mask[:, self.config.num_query_tokens :] query_embeds = self.embeddings( input_ids=input_ids,