






import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding

class        snake_case_				(							lowercase_   ):







   """simple docstring"""



   __lowerCAmelCase  =    ["""image_processor""", """tokenizer"""]
   __lowerCAmelCase  =    """VideoMAEImageProcessor"""
   __lowerCAmelCase  =    ("""CLIPTokenizer""", """CLIPTokenizerFast""")

   def __init__(     self:						List[str] ,							snake_case__:						List[str]=None ,							snake_case__:						Any=None ,							**snake_case__:						Optional[int]						)	->		Union[str, Any]:


       """simple docstring"""



       _lowercase     			=						None
       if "feature_extractor" in kwargs:
           warnings.warn(
               'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
               ' instead.' ,							snake_case__ ,							)
           _lowercase     			=						kwargs.pop('feature_extractor'						)

       _lowercase     			=						image_processor if image_processor is not None else feature_extractor
       if image_processor is None:
           raise ValueError('You need to specify an `image_processor`.'						)
       if tokenizer is None:
           raise ValueError('You need to specify a `tokenizer`.'						)

       super().__init__(snake_case__ ,							snake_case__						)
       _lowercase     			=						self.image_processor

   def __call__(     self:						Dict ,							snake_case__:						Union[str, Any]=None ,							snake_case__:						List[Any]=None ,							snake_case__:						Any=None ,							**snake_case__:						Dict						)	->		Any:


       """simple docstring"""



       if text is None and videos is None:
           raise ValueError('You have to specify either text or videos. Both cannot be none.'						)

       if text is not None:
           _lowercase     			=						self.tokenizer(snake_case__ ,							return_tensors=snake_case__ ,							**snake_case__						)

       if videos is not None:
           _lowercase     			=						self.image_processor(snake_case__ ,							return_tensors=snake_case__ ,							**snake_case__						)

       if text is not None and videos is not None:
           _lowercase     			=						image_features.pixel_values
           return encoding
       elif text is not None:
           return encoding
       else:
           return BatchEncoding(data=dict(**snake_case__						) ,							tensor_type=snake_case__						)

   def       SCREAMING_SNAKE_CASE__					(     self:						Union[str, Any] ,							*snake_case__:						List[Any] ,							**snake_case__:						Union[str, Any]						)	->		Union[str, Any]:


       """simple docstring"""



       return self.tokenizer.batch_decode(*snake_case__ ,							**snake_case__						)

   def       SCREAMING_SNAKE_CASE__					(     self:						Tuple ,							*snake_case__:						Optional[int] ,							**snake_case__:						str						)	->		Optional[Any]:


       """simple docstring"""



       return self.tokenizer.decode(*snake_case__ ,							**snake_case__						)

   @property
   def       SCREAMING_SNAKE_CASE__					(     self:						List[Any]						)	->		str:


       """simple docstring"""



       return ["input_ids", "attention_mask", "position_ids", "pixel_values"]

   @property
   def       SCREAMING_SNAKE_CASE__					(     self:						Optional[Any]						)	->		Optional[int]:


       """simple docstring"""



       warnings.warn(
           '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.' ,							snake_case__ ,							)
       return self.image_processor_class

   @property
   def       SCREAMING_SNAKE_CASE__					(     self:						int						)	->		Dict:


       """simple docstring"""



       warnings.warn(
           '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.' ,							snake_case__ ,							)
       return self.image_processor








import torch

from accelerate import Accelerator, DistributedType

class        snake_case_				:







   """simple docstring"""



   def __enter__(     self:						int						)	->		Tuple:


       """simple docstring"""



       if self.enabled:
           _lowercase     			=						self.model.no_sync()
           self.model_sync_obj.__enter__()

       return self

   def __exit__(     self:						str ,							snake_case__:						Tuple ,							snake_case__:						int ,							snake_case__:						Tuple						)	->		Any:


       """simple docstring"""



       if self.enabled:
           # Average all models on exit
           self._sync_and_avg_model_params()
           self.model_sync_obj.__exit__(snake_case__ ,							snake_case__ ,							snake_case__						)

   def __init__(     self:						int ,							snake_case__:						Accelerator ,							snake_case__:						torch.nn.Module ,							snake_case__:						int ,							snake_case__:						bool = True						)	->		Optional[Any]:


       """simple docstring"""



       if accelerator.distributed_type not in [
           DistributedType.NO,
           DistributedType.MULTI_CPU,
           DistributedType.MULTI_GPU,
       ]:
           raise NotImplementedError('LocalSGD is supported only for CPUs and GPUs (no DeepSpeed or MegatronLM)'						)
       _lowercase     			=						enabled and accelerator.distributed_type != DistributedType.NO
       _lowercase     			=						0
       if self.enabled:
           _lowercase     			=						accelerator
           _lowercase     			=						model
           _lowercase     			=						local_sgd_steps

   def       SCREAMING_SNAKE_CASE__					(     self:						str						)	->		List[Any]:


       """simple docstring"""



       self.num_steps += 1
       if not self.enabled:
           return

       if self.num_steps % self.local_sgd_steps == 0:
           self._sync_and_avg_model_params()

   def       SCREAMING_SNAKE_CASE__					(     self:						Optional[int]						)	->		Dict:


       """simple docstring"""



       self.accelerator.wait_for_everyone()
       with self.accelerator.autocast():
           for param in self.model.parameters():
               _lowercase     			=						self.accelerator.reduce(param.data ,							reduction='mean'						)
