-
Notifications
You must be signed in to change notification settings - Fork 26.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uniformize kwargs for chameleon processor #32181
base: main
Are you sure you want to change the base?
Changes from all commits
2cdc473
43febe0
f59ca5a
dcbfd17
80fb7bb
e57e988
ed0e8aa
ae5d537
b252643
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import tempfile | ||
import unittest | ||
|
||
from transformers import ChameleonProcessor | ||
|
||
from ...test_processing_common import ProcessorTesterMixin | ||
|
||
|
||
class ChameleonProcessorTest(ProcessorTesterMixin, unittest.TestCase): | ||
from_pretrained_id = "leloy/Anole-7b-v0.1-hf" | ||
processor_class = ChameleonProcessor | ||
|
||
def setUp(self): | ||
self.tmpdirname = tempfile.mkdtemp() | ||
processor = self.processor_class.from_pretrained(self.from_pretrained_id) | ||
processor.save_pretrained(self.tmpdirname) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,8 @@ | |
@require_torch | ||
class ProcessorTesterMixin: | ||
processor_class = None | ||
text_data_arg_name = "input_ids" | ||
images_data_arg_name = "pixel_values" | ||
|
||
def prepare_processor_dict(self): | ||
return {} | ||
|
@@ -136,14 +138,14 @@ def test_tokenizer_defaults_preserved_by_kwargs(self): | |
image_input = self.prepare_image_inputs() | ||
|
||
inputs = processor(text=input_str, images=image_input, return_tensors="pt") | ||
self.assertEqual(len(inputs["input_ids"][0]), 117) | ||
self.assertEqual(len(inputs[self.text_data_arg_name][0]), 117) | ||
|
||
@require_torch | ||
@require_vision | ||
def test_image_processor_defaults_preserved_by_image_kwargs(self): | ||
if "image_processor" not in self.processor_class.attributes: | ||
self.skipTest(f"image_processor attribute not present in {self.processor_class}") | ||
image_processor = self.get_component("image_processor", size=(234, 234)) | ||
image_processor = self.get_component("image_processor", size=(234, 234), crop_size=(234, 234)) | ||
tokenizer = self.get_component("tokenizer", max_length=117, padding="max_length") | ||
|
||
processor = self.processor_class(tokenizer=tokenizer, image_processor=image_processor) | ||
|
@@ -153,7 +155,7 @@ def test_image_processor_defaults_preserved_by_image_kwargs(self): | |
image_input = self.prepare_image_inputs() | ||
|
||
inputs = processor(text=input_str, images=image_input) | ||
self.assertEqual(len(inputs["pixel_values"][0][0]), 234) | ||
self.assertEqual(len(inputs[self.images_data_arg_name][0][0]), 234) | ||
|
||
@require_vision | ||
@require_torch | ||
|
@@ -171,7 +173,7 @@ def test_kwargs_overrides_default_tokenizer_kwargs(self): | |
inputs = processor( | ||
text=input_str, images=image_input, return_tensors="pt", max_length=112, padding="max_length" | ||
) | ||
self.assertEqual(len(inputs["input_ids"][0]), 112) | ||
self.assertEqual(len(inputs[self.text_data_arg_name][0]), 112) | ||
|
||
@require_torch | ||
@require_vision | ||
|
@@ -187,8 +189,8 @@ def test_kwargs_overrides_default_image_processor_kwargs(self): | |
input_str = "lower newer" | ||
image_input = self.prepare_image_inputs() | ||
|
||
inputs = processor(text=input_str, images=image_input, size=[224, 224]) | ||
self.assertEqual(len(inputs["pixel_values"][0][0]), 224) | ||
inputs = processor(text=input_str, images=image_input, size=[224, 224], crop_size=(224, 224)) | ||
self.assertEqual(len(inputs[self.images_data_arg_name][0][0]), 224) | ||
|
||
@require_torch | ||
@require_vision | ||
|
@@ -208,12 +210,13 @@ def test_unstructured_kwargs(self): | |
images=image_input, | ||
return_tensors="pt", | ||
size={"height": 214, "width": 214}, | ||
crop_size={"height": 214, "width": 214}, | ||
padding="max_length", | ||
max_length=76, | ||
) | ||
|
||
self.assertEqual(inputs["pixel_values"].shape[2], 214) | ||
self.assertEqual(len(inputs["input_ids"][0]), 76) | ||
self.assertEqual(inputs[self.images_data_arg_name].shape[2], 214) | ||
self.assertEqual(len(inputs[self.text_data_arg_name][0]), 76) | ||
|
||
@require_torch | ||
@require_vision | ||
|
@@ -233,13 +236,14 @@ def test_unstructured_kwargs_batched(self): | |
images=image_input, | ||
return_tensors="pt", | ||
size={"height": 214, "width": 214}, | ||
crop_size={"height": 214, "width": 214}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @yonigozlan i think you removed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @zucchini-nlp Yes but actually it would be nice to have both here. @molbap had some CI tests crash because |
||
padding="longest", | ||
max_length=76, | ||
) | ||
|
||
self.assertEqual(inputs["pixel_values"].shape[2], 214) | ||
self.assertEqual(inputs[self.images_data_arg_name].shape[2], 214) | ||
|
||
self.assertEqual(len(inputs["input_ids"][0]), 6) | ||
self.assertEqual(len(inputs[self.text_data_arg_name][0]), 6) | ||
|
||
@require_torch | ||
@require_vision | ||
|
@@ -260,6 +264,7 @@ def test_doubly_passed_kwargs(self): | |
images=image_input, | ||
images_kwargs={"size": {"height": 222, "width": 222}}, | ||
size={"height": 214, "width": 214}, | ||
crop_size={"height": 214, "width": 214}, | ||
) | ||
|
||
@require_torch | ||
|
@@ -279,16 +284,19 @@ def test_structured_kwargs_nested(self): | |
# Define the kwargs for each modality | ||
all_kwargs = { | ||
"common_kwargs": {"return_tensors": "pt"}, | ||
"images_kwargs": {"size": {"height": 214, "width": 214}}, | ||
"images_kwargs": { | ||
"size": {"height": 214, "width": 214}, | ||
"crop_size": {"height": 214, "width": 214}, | ||
}, | ||
"text_kwargs": {"padding": "max_length", "max_length": 76}, | ||
} | ||
|
||
inputs = processor(text=input_str, images=image_input, **all_kwargs) | ||
self.skip_processor_without_typed_kwargs(processor) | ||
|
||
self.assertEqual(inputs["pixel_values"].shape[2], 214) | ||
self.assertEqual(inputs[self.images_data_arg_name].shape[2], 214) | ||
|
||
self.assertEqual(len(inputs["input_ids"][0]), 76) | ||
self.assertEqual(len(inputs[self.text_data_arg_name][0]), 76) | ||
|
||
@require_torch | ||
@require_vision | ||
|
@@ -307,14 +315,17 @@ def test_structured_kwargs_nested_from_dict(self): | |
# Define the kwargs for each modality | ||
all_kwargs = { | ||
"common_kwargs": {"return_tensors": "pt"}, | ||
"images_kwargs": {"size": {"height": 214, "width": 214}}, | ||
"images_kwargs": { | ||
"size": {"height": 214, "width": 214}, | ||
"crop_size": {"height": 214, "width": 214}, | ||
}, | ||
"text_kwargs": {"padding": "max_length", "max_length": 76}, | ||
} | ||
|
||
inputs = processor(text=input_str, images=image_input, **all_kwargs) | ||
self.assertEqual(inputs["pixel_values"].shape[2], 214) | ||
self.assertEqual(inputs[self.images_data_arg_name].shape[2], 214) | ||
|
||
self.assertEqual(len(inputs["input_ids"][0]), 76) | ||
self.assertEqual(len(inputs[self.text_data_arg_name][0]), 76) | ||
|
||
|
||
class MyProcessor(ProcessorMixin): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw @zucchini-nlp we might need to increase prio for this PR because of this
I have this change in my other PR too, but I forgot we haven't merged it yet
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I was out for a while. Yes, I think some other contributor also reported the issue and wanted to open a PR to fix the conversion script. Feel free to open a PR if there isn't any, as this issue isn't at all related to processor kwargs