Skip to content

Fix multimodal processor get duplicate arguments when receive kwargs for initialization #39125

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

Merged
merged 7 commits into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 7 additions & 3 deletions src/transformers/processing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1097,9 +1097,13 @@ def from_args_and_dict(cls, args, processor_dict: dict[str, Any], **kwargs):
processor_config=processor_dict, valid_kwargs=accepted_args_and_kwargs
)

# remove args that are in processor_dict to avoid duplicate arguments
args_to_remove = [i for i, arg in enumerate(accepted_args_and_kwargs) if arg in processor_dict]
args = [arg for i, arg in enumerate(args) if i not in args_to_remove]
# update args that are already in processor_dict to avoid duplicate arguments
args_to_update = {
i: valid_kwargs.pop(arg)
for i, arg in enumerate(accepted_args_and_kwargs)
if (arg in valid_kwargs and i < len(args))
}
args = [arg if i not in args_to_update else args_to_update[i] for i, arg in enumerate(args)]

# instantiate processor with used (and valid) kwargs only
processor = cls(*args, **valid_kwargs)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_processing_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,18 @@ def test_doubly_passed_kwargs(self):
return_tensors="pt",
)

def test_args_overlap_kwargs(self):
if "image_processor" not in self.processor_class.attributes:
self.skipTest(f"image_processor attribute not present in {self.processor_class}")
processor_first = self.get_processor()
image_processor = processor_first.image_processor
image_processor.is_override = True

with tempfile.TemporaryDirectory() as tmpdirname:
processor_first.save_pretrained(tmpdirname)
processor_second = self.processor_class.from_pretrained(tmpdirname, image_processor=image_processor)
self.assertTrue(processor_second.image_processor.is_override)

def test_structured_kwargs_nested(self):
if "image_processor" not in self.processor_class.attributes:
self.skipTest(f"image_processor attribute not present in {self.processor_class}")
Expand Down