






"""simple docstring"""






import enum
import inspect
from typing import Iterable, List, Optional, Tuple, Union
class 						__lowerCAmelCase   (			enum.Enum       ):







  '''simple docstring'''


  __lowerCamelCase       :    List[Any]     	=			'''timm'''
  __lowerCamelCase       :    Any     	=			'''transformers'''




def 						_UpperCamelCase			(  A    ,       A    ,       A			):

   '''simple docstring'''





   if stage_names is None:
      raise ValueError("Stage_names must be set for transformers backbones"			)

   if out_features is not None:
      if not isinstance(__lowerCAmelCase    ,       (list,)			):
         raise ValueError(f"""out_features must be a list {type(__lowerCAmelCase			)}"""			)
      if any(feat not in stage_names for feat in out_features			):
         raise ValueError(f"""out_features must be a subset of stage_names: {stage_names} got {out_features}"""			)

   if out_indices is not None:
      if not isinstance(__lowerCAmelCase    ,       (list, tuple)			):
         raise ValueError(f"""out_indices must be a list or tuple, got {type(__lowerCAmelCase			)}"""			)
      if any(idx >= len(__lowerCAmelCase			) for idx in out_indices			):
         raise ValueError("out_indices must be valid indices for stage_names {stage_names}, got {out_indices}"			)

   if out_features is not None and out_indices is not None:
      if len(__lowerCAmelCase			) != len(__lowerCAmelCase			):
         raise ValueError("out_features and out_indices should have the same length if both are set"			)
      if out_features != [stage_names[idx] for idx in out_indices]:
         raise ValueError("out_features and out_indices should correspond to the same stages if both are set"			)




def 						_UpperCamelCase			(  A    ,       A    ,       A    ,       ):

   '''simple docstring'''





   if out_indices is None and out_features is None:
      UpperCamelCase_     =[len(__lowerCAmelCase			) - 1]
      UpperCamelCase_     =[stage_names[-1]]
   elif out_indices is None and out_features is not None:
      UpperCamelCase_     =[stage_names.index(__lowerCAmelCase			) for layer in out_features]
   elif out_features is None and out_indices is not None:
      UpperCamelCase_     =[stage_names[idx] for idx in out_indices]
   return out_features, out_indices




def 						_UpperCamelCase			(  A    ,       A    ,       A    ,       ):

   '''simple docstring'''





   verify_out_features_out_indices(out_features=__lowerCAmelCase    ,       out_indices=__lowerCAmelCase    ,       stage_names=__lowerCAmelCase			)
   UpperCamelCase_     =_align_output_features_output_indices(
       out_features=__lowerCAmelCase    ,       out_indices=__lowerCAmelCase    ,       stage_names=__lowerCAmelCase			)
   # Verify that the aligned out_features and out_indices are valid
   verify_out_features_out_indices(out_features=__lowerCAmelCase    ,       out_indices=__lowerCAmelCase    ,       stage_names=__lowerCAmelCase			)
   return output_features, output_indices
class 						__lowerCAmelCase   :







  '''simple docstring'''


  __lowerCamelCase       :    Optional[BackboneType]     	=			None
  def 					UpperCamelCase__   (      self:		str				,			UpperCamelCase_:		Dict					):






     if getattr(self				,			"_backbone"				,			UpperCamelCase_					) is None:
        raise ValueError("self._backbone must be set before calling _init_timm_backbone"					)

     # These will diagree with the defaults for the transformers models e.g. for resnet50
     # the transformer model has out_features = ['stem', 'stage1', 'stage2', 'stage3', 'stage4']
     # the timm model has out_features = ['act', 'layer1', 'layer2', 'layer3', 'layer4']
     UpperCamelCase_     =[stage["module"] for stage in self._backbone.feature_info.info]
     UpperCamelCase_     =[stage["num_chs"] for stage in self._backbone.feature_info.info]
     UpperCamelCase_     =self._backbone.feature_info.out_indices
     UpperCamelCase_     =self._backbone.feature_info.module_name()

     # We verify the out indices and out features are valid
     verify_out_features_out_indices(
         out_features=UpperCamelCase_				,			out_indices=UpperCamelCase_				,			stage_names=self.stage_names					)
     UpperCamelCase_     =out_features, out_indices
  def 					UpperCamelCase__   (      self:		int				,			UpperCamelCase_:		Union[str, Any]					):
     UpperCamelCase_     =getattr(UpperCamelCase_				,			"stage_names"					)
     UpperCamelCase_     =getattr(UpperCamelCase_				,			"out_features"				,			UpperCamelCase_					)
     UpperCamelCase_     =getattr(UpperCamelCase_				,			"out_indices"				,			UpperCamelCase_					)

     UpperCamelCase_     =stage_names
     UpperCamelCase_     =get_aligned_output_features_output_indices(
         out_features=UpperCamelCase_				,			out_indices=UpperCamelCase_				,			stage_names=UpperCamelCase_					)
     # Number of channels for each stage. This is set in the transformer backbone model init
     UpperCamelCase_     =None
  def 					UpperCamelCase__   (      self:		Optional[int]				,			UpperCamelCase_:		List[str]					):
     UpperCamelCase_     =config

     UpperCamelCase_     =getattr(UpperCamelCase_				,			"use_timm_backbone"				,			UpperCamelCase_					)
     UpperCamelCase_     =BackboneType.TIMM if self.use_timm_backbone else BackboneType.TRANSFORMERS

     if self.backbone_type == BackboneType.TIMM:
        self._init_timm_backbone(UpperCamelCase_					)
     elif self.backbone_type == BackboneType.TRANSFORMERS:
        self._init_transformers_backbone(UpperCamelCase_					)
     else:
        raise ValueError(f"""backbone_type {self.backbone_type} not supported."""					)
  @property
  def 					UpperCamelCase__   (      self:		List[Any]					):
     return self._out_features
  @out_features.setter
  def 					UpperCamelCase__   (      self:		Dict				,			UpperCamelCase_:		List[str]					):
     UpperCamelCase_     =get_aligned_output_features_output_indices(
         out_features=UpperCamelCase_				,			out_indices=UpperCamelCase_				,			stage_names=self.stage_names					)
  @property
  def 					UpperCamelCase__   (      self:		Optional[Any]					):
     return self._out_indices
  @out_indices.setter
  def 					UpperCamelCase__   (      self:		Optional[int]				,			UpperCamelCase_:		Union[Tuple[int], List[int]]					):
     UpperCamelCase_     =get_aligned_output_features_output_indices(
         out_features=UpperCamelCase_				,			out_indices=UpperCamelCase_				,			stage_names=self.stage_names					)
  @property
  def 					UpperCamelCase__   (      self:		int					):
     return {stage: self.num_features[i] for i, stage in enumerate(self.stage_names					)}
  @property
  def 					UpperCamelCase__   (      self:		Optional[int]					):
     return [self.out_feature_channels[name] for name in self.out_features]
  def 					UpperCamelCase__   (      self:		List[str]				,			*UpperCamelCase_:		List[str]				,			**UpperCamelCase_:		str					):
     UpperCamelCase_     =dict(inspect.signature(self.forward					).parameters					)
     UpperCamelCase_     ={k: v for k, v in kwargs.items() if k in signature}
     return self(*UpperCamelCase_				,			**UpperCamelCase_					)
  def 					UpperCamelCase__   (      self:		List[str]				,			UpperCamelCase_:		List[str]				,			UpperCamelCase_:		Optional[bool] = None				,			UpperCamelCase_:		Optional[bool] = None				,			UpperCamelCase_:		Optional[bool] = None				,			):
     raise NotImplementedError("This method should be implemented by the derived class."					)





  def 					UpperCamelCase__   (      self:		Tuple					):
     UpperCamelCase_     =super().to_dict()
     UpperCamelCase_     =output.pop("_out_features"					)
     UpperCamelCase_     =output.pop("_out_indices"					)
     return output




class 						__lowerCAmelCase   :







  '''simple docstring'''


  @property
  def 					UpperCamelCase__   (      self:		Union[str, Any]					):
     return self._out_features
  @out_features.setter
  def 					UpperCamelCase__   (      self:		Any				,			UpperCamelCase_:		List[str]					):
     UpperCamelCase_     =get_aligned_output_features_output_indices(
         out_features=UpperCamelCase_				,			out_indices=UpperCamelCase_				,			stage_names=self.stage_names					)
  @property
  def 					UpperCamelCase__   (      self:		Optional[int]					):
     return self._out_indices
  @out_indices.setter
  def 					UpperCamelCase__   (      self:		List[str]				,			UpperCamelCase_:		Union[Tuple[int], List[int]]					):
     UpperCamelCase_     =get_aligned_output_features_output_indices(
         out_features=UpperCamelCase_				,			out_indices=UpperCamelCase_				,			stage_names=self.stage_names					)





  def 					UpperCamelCase__   (      self:		Union[str, Any]					):
     UpperCamelCase_     =super().to_dict()
     UpperCamelCase_     =output.pop("_out_features"					)
     UpperCamelCase_     =output.pop("_out_indices"					)
     return output








"""simple docstring"""






import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
class 						__lowerCAmelCase   (			UpperCAmelCase       ):







  '''simple docstring'''


  __lowerCamelCase       :    Optional[Any]     	=			["image_processor", "tokenizer"]
  __lowerCamelCase       :    List[str]     	=			"VideoMAEImageProcessor"
  __lowerCamelCase       :    Dict     	=			("CLIPTokenizer", "CLIPTokenizerFast")
  def __init__(      self:		str				,			UpperCamelCase_:		Dict=None				,			UpperCamelCase_:		str=None				,			**UpperCamelCase_:		str					):
     UpperCamelCase_     =None
     if "feature_extractor" in kwargs:
        warnings.warn(
            "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
            " instead."				,			UpperCamelCase_				,			)
        UpperCamelCase_     =kwargs.pop("feature_extractor"					)

     UpperCamelCase_     =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__(UpperCamelCase_				,			UpperCamelCase_					)
     UpperCamelCase_     =self.image_processor
  def __call__(      self:		Tuple				,			UpperCamelCase_:		str=None				,			UpperCamelCase_:		str=None				,			UpperCamelCase_:		Optional[Any]=None				,			**UpperCamelCase_:		int					):

     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:
        UpperCamelCase_     =self.tokenizer(UpperCamelCase_				,			return_tensors=UpperCamelCase_				,			**UpperCamelCase_					)

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

     if text is not None and videos is not None:
        UpperCamelCase_     =image_features.pixel_values
        return encoding
     elif text is not None:
        return encoding
     else:
        return BatchEncoding(data=dict(**UpperCamelCase_					)				,			tensor_type=UpperCamelCase_					)
  def 					UpperCamelCase__   (      self:		Any				,			*UpperCamelCase_:		List[str]				,			**UpperCamelCase_:		Optional[int]					):
     return self.tokenizer.batch_decode(*UpperCamelCase_				,			**UpperCamelCase_					)
  def 					UpperCamelCase__   (      self:		Optional[Any]				,			*UpperCamelCase_:		List[str]				,			**UpperCamelCase_:		Tuple					):
     return self.tokenizer.decode(*UpperCamelCase_				,			**UpperCamelCase_					)
  @property
  def 					UpperCamelCase__   (      self:		List[Any]					):
     return ["input_ids", "attention_mask", "position_ids", "pixel_values"]
  @property
  def 					UpperCamelCase__   (      self:		str					):
     warnings.warn(
         "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."				,			UpperCamelCase_				,			)
     return self.image_processor_class





  @property
  def 					UpperCamelCase__   (      self:		Optional[Any]					):
     warnings.warn(
         "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."				,			UpperCamelCase_				,			)
     return self.image_processor
