Unverified Commit 90fc7f91 authored by Harry Mellor's avatar Harry Mellor Committed by GitHub
Browse files

Fix custom processors that use deleted behaviour for Transformers v5 (#35107)


Signed-off-by: default avatarHarry Mellor <19981378+hmellor@users.noreply.github.com>
parent 675ec59a
...@@ -44,7 +44,39 @@ def _transformers_v4_compatibility_import(): ...@@ -44,7 +44,39 @@ def _transformers_v4_compatibility_import():
processing_utils.ChatTemplateLoadKwargs = new_import processing_utils.ChatTemplateLoadKwargs = new_import
def _transformers_v4_compatibility_init() -> Any:
"""Some remote code processors may define `optional_attributes` in their
`ProcessorMixin` subclass, and then pass these arbitrary attributes directly to
`ProcessorMixin.__init__`, which is no longer allowed in Transformers v5. For
backward compatibility, we intercept these optional attributes and set them on the
processor instance before calling the original `ProcessorMixin.__init__`.
This can be removed if `Molmo2ForConditionalGeneration` is upstreamed to
Transformers."""
# Transformers v4
if hasattr(ProcessorMixin, "optional_attributes"):
return
# Transformers v5
if hasattr(ProcessorMixin.__init__, "_vllm_patched"):
return
original_init = ProcessorMixin.__init__
def __init__(self, *args, **kwargs):
for optional_attribute in getattr(self, "optional_attributes", []):
if optional_attribute in kwargs:
setattr(self, optional_attribute, kwargs.pop(optional_attribute))
original_init(self, *args, **kwargs)
# Only patch if ProcessorMixin is not mocked (for docs builds)
if not hasattr(ProcessorMixin, "_mock_name"):
__init__._vllm_patched = True # type: ignore[attr-defined]
ProcessorMixin.__init__ = __init__
_transformers_v4_compatibility_import() _transformers_v4_compatibility_import()
_transformers_v4_compatibility_init()
_P = TypeVar("_P", bound=ProcessorMixin, default=ProcessorMixin) _P = TypeVar("_P", bound=ProcessorMixin, default=ProcessorMixin)
_V = TypeVar("_V", bound=BaseVideoProcessor, default=BaseVideoProcessor) _V = TypeVar("_V", bound=BaseVideoProcessor, default=BaseVideoProcessor)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment