Skip to content

Commit

Permalink
Fix pipeline tests blip2
Browse files Browse the repository at this point in the history
  • Loading branch information
yonigozlan committed Oct 24, 2024
1 parent 0a8afeb commit 6d44f3c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
10 changes: 4 additions & 6 deletions src/transformers/models/kosmos2/processing_kosmos2.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ class Kosmos2ProcessorKwargs(ProcessingKwargs, total=False):
"verbose": True,
"add_eos_token": False,
},
"images_kwargs": {
"num_image_tokens": 64,
},
}


Expand All @@ -84,11 +81,11 @@ class Kosmos2Processor(ProcessorMixin):
"""

attributes = ["image_processor", "tokenizer"]
valid_kwargs = ["num_patch_index_tokens"]
valid_kwargs = ["num_patch_index_tokens", "num_image_tokens"]
image_processor_class = "CLIPImageProcessor"
tokenizer_class = "AutoTokenizer"

def __init__(self, image_processor, tokenizer, num_patch_index_tokens=1024, *kwargs):
def __init__(self, image_processor, tokenizer, num_patch_index_tokens=1024, num_image_tokens=64, *kwargs):
tokenizer.return_token_type_ids = False

self.eod_token = "</doc>"
Expand Down Expand Up @@ -125,6 +122,7 @@ def __init__(self, image_processor, tokenizer, num_patch_index_tokens=1024, *kwa

self.num_patch_index_tokens = num_patch_index_tokens
patch_index_tokens = [f"<patch_index_{str(x).zfill(4)}>" for x in range(self.num_patch_index_tokens)]
self.num_image_tokens = num_image_tokens

tokens_to_add = []
for token in self.tag_tokens + patch_index_tokens:
Expand Down Expand Up @@ -171,7 +169,7 @@ def __call__(
)

bboxes = output_kwargs["images_kwargs"].pop("bboxes", None)
num_image_tokens = output_kwargs["images_kwargs"].pop("num_image_tokens", 64)
num_image_tokens = output_kwargs["images_kwargs"].pop("num_image_tokens", self.num_image_tokens)
first_image_token_id = output_kwargs["images_kwargs"].pop("first_image_token_id", None)
add_eos_token = output_kwargs["text_kwargs"].pop("add_eos_token", False)

Expand Down
8 changes: 8 additions & 0 deletions src/transformers/pipelines/image_text_to_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ def _sanitize_parameters(
" please use only one"
)
forward_kwargs["generate_kwargs"]["max_new_tokens"] = max_new_tokens
else:
if "generate_kwargs" not in forward_kwargs:
forward_kwargs["generate_kwargs"] = {}
if "max_new_tokens" not in forward_kwargs["generate_kwargs"]:
logger.warning_once(
"The `max_new_tokens` parameter is not set. By default, the model will generate up to 20 tokens."
)
forward_kwargs["generate_kwargs"]["max_new_tokens"] = 20

if return_full_text is not None and return_type is None:
if return_tensors is not None:
Expand Down
6 changes: 3 additions & 3 deletions tests/pipelines/test_pipelines_image_text_to_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ class ImageTextToTextPipelineTests(unittest.TestCase):

def get_test_pipeline(self, model, tokenizer, processor, image_processor, torch_dtype="float32"):
pipe = ImageTextToTextPipeline(model=model, processor=processor, torch_dtype=torch_dtype)
image_token = processor.image_token if hasattr(processor, "image_token") else "<image>"
image_token = getattr(processor.tokenizer, "image_token", "")
examples = [
{
"images": Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"),
"text": f"{image_token} This is a ",
"text": f"{image_token}This is a ",
},
{
"images": "./tests/fixtures/tests_samples/COCO/000000039769.png",
"text": f"{image_token} Here I see a ",
"text": f"{image_token}Here I see a ",
},
]
return pipe, examples
Expand Down

0 comments on commit 6d44f3c

Please sign in to comment.