diff --git a/src/transformers/processing_utils.py b/src/transformers/processing_utils.py index bb1344a43dcf..31360883dbf3 100644 --- a/src/transformers/processing_utils.py +++ b/src/transformers/processing_utils.py @@ -17,6 +17,7 @@ import bisect import copy +import functools import inspect import json import os @@ -571,6 +572,15 @@ def __getitem__(self, key): raise AttributeError(f"{self.__class__.__name__} has no attribute {key}") +@functools.lru_cache(maxsize=8) +def _merge_typed_dict(preprocessor_typed_dict: type, modality_typed_dict: type) -> type: + return TypedDict( + "merged_typed_dict", + {**preprocessor_typed_dict.__annotations__, **modality_typed_dict.__annotations__}, + total=False, + ) + + class ProcessorMixin(PushToHubMixin): """ This is a mixin used to provide saving/loading functionality for all processor classes. @@ -1369,11 +1379,7 @@ class MyProcessingKwargs(ProcessingKwargs, CommonKwargs, TextKwargs, ImagesKwarg if preprocessor is None or getattr(preprocessor, "valid_kwargs", None) is None: continue preprocessor_typed_dict_obj = getattr(preprocessor, "valid_kwargs") - typed_dict_obj = TypedDict( - "merged_typed_dict", - {**preprocessor_typed_dict_obj.__annotations__, **typed_dict_obj.__annotations__}, - total=False, - ) + typed_dict_obj = _merge_typed_dict(preprocessor_typed_dict_obj, typed_dict_obj) validate_typed_dict(typed_dict_obj, output_kwargs[key]) return output_kwargs