Skip to content
Merged
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
16 changes: 12 additions & 4 deletions src/transformers/image_processing_base.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a warning to let remote code model users know that this is happening?

Also if you were feeling fancy you could iterate over kwargs in reverse and pop them in a single loop

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the warning! And I did feel fancy :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh since we're using list() again I don't think reversed is necessary, since the list is a copy (I think)

Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,18 @@ def from_dict(cls, image_processor_dict: dict[str, Any], **kwargs):
image_processor_dict.update({k: v for k, v in kwargs.items() if k in cls.valid_kwargs.__annotations__})
image_processor = cls(**image_processor_dict)

# Remove kwargs that are used to initialize the image processor attributes
for key in list(kwargs):
if hasattr(image_processor, key):
kwargs.pop(key)
# Apply extra kwargs to instance (BC for remote code, e.g. phi4_multimodal)
extra_keys = []
for key in reversed(list(kwargs.keys())):
if hasattr(image_processor, key) and key not in cls.valid_kwargs.__annotations__:
setattr(image_processor, key, kwargs.pop(key, None))
extra_keys.append(key)
if extra_keys:
logger.warning_once(
f"Image processor {cls.__name__}: kwargs {extra_keys} were applied for backward compatibility. "
f"To avoid this warning, add them to valid_kwargs: create a custom TypedDict extending "
f"ImagesKwargs with these keys and set it as the `valid_kwargs` class attribute."
)

logger.info(f"Image processor {image_processor}")
if return_unused_kwargs:
Expand Down