Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/transformers/models/blip_2/processing_blip_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,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):
Expand Down
12 changes: 12 additions & 0 deletions tests/models/blip_2/test_processing_blip_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)