'''simple docstring'''






from typing import Tuple, Union

from ...modeling_outputs import BackboneOutput
from ...modeling_utils import PreTrainedModel
from ...utils import is_timm_available, is_torch_available, requires_backends
from ...utils.backbone_utils import BackboneMixin
from .configuration_timm_backbone import TimmBackboneConfig


if is_timm_available():
    import timm


if is_torch_available():
    from torch import Tensor




class    a (       __lowerCAmelCase						,  __lowerCAmelCase				):
  __lowerCAmelCase				      =					"""pixel_values"""
  __lowerCAmelCase				      =					False
  __lowerCAmelCase				      =					TimmBackboneConfig







  def __init__(							self   ,    snake_case_   ,    **snake_case_					):

    '''simple docstring'''







    requires_backends(self   ,    """timm"""					)
    super().__init__(snake_case_					)
    __UpperCAmelCase:					str		= config

    if config.backbone is None:
      raise ValueError("""backbone is not set in the config. Please set it to a timm model name."""					)

    if config.backbone not in timm.list_models():
      raise ValueError(F'''backbone {config.backbone} is not supported by timm.'''					)

    if hasattr(snake_case_   ,    """out_features"""					) and config.out_features is not None:
      raise ValueError("""out_features is not supported by TimmBackbone. Please use out_indices instead."""					)

    __UpperCAmelCase:					str		= getattr(snake_case_   ,    """use_pretrained_backbone"""   ,    snake_case_					)
    if pretrained is None:
      raise ValueError("""use_pretrained_backbone is not set in the config. Please set it to True or False."""					)

    # We just take the final layer by default. This matches the default for the transformers models.
    __UpperCAmelCase:					str		= config.out_indices if getattr(snake_case_   ,    """out_indices"""   ,    snake_case_					) is not None else (-1,)

    __UpperCAmelCase:					Optional[int]		= timm.create_model(
        config.backbone   ,    pretrained=snake_case_   ,    features_only=config.features_only   ,    in_chans=config.num_channels   ,    out_indices=snake_case_   ,    **snake_case_   ,    )
    # These are used to control the output of the model when called. If output_hidden_states is True, then
    # return_layers is modified to include all layers.
    __UpperCAmelCase:					Any		= self._backbone.return_layers
    __UpperCAmelCase:					Any		= {layer["""module"""]: str(snake_case_					) for i, layer in enumerate(self._backbone.feature_info.info					)}
    super()._init_backbone(snake_case_					)







  @classmethod
  def    lowercase_  (							cls   ,    snake_case_   ,    *snake_case_   ,    **snake_case_					):

    '''simple docstring'''







    requires_backends(cls   ,    ["""vision""", """timm"""]					)
    from ...models.timm_backbone import TimmBackboneConfig

    __UpperCAmelCase:					Any		= kwargs.pop("""config"""   ,    TimmBackboneConfig()					)

    __UpperCAmelCase:					str		= kwargs.pop("""use_timm_backbone"""   ,    snake_case_					)
    if not use_timm:
      raise ValueError("""use_timm_backbone must be True for timm backbones"""					)

    __UpperCAmelCase:					List[Any]		= kwargs.pop("""num_channels"""   ,    config.num_channels					)
    __UpperCAmelCase:					int		= kwargs.pop("""features_only"""   ,    config.features_only					)
    __UpperCAmelCase:					Any		= kwargs.pop("""use_pretrained_backbone"""   ,    config.use_pretrained_backbone					)
    __UpperCAmelCase:					Union[str, Any]		= kwargs.pop("""out_indices"""   ,    config.out_indices					)
    __UpperCAmelCase:					Optional[int]		= TimmBackboneConfig(
        backbone=snake_case_   ,    num_channels=snake_case_   ,    features_only=snake_case_   ,    use_pretrained_backbone=snake_case_   ,    out_indices=snake_case_   ,    )
    return super()._from_config(snake_case_   ,    **snake_case_					)







  def    lowercase_  (							self   ,    snake_case_					):

    '''simple docstring'''







    pass







  def    lowercase_  (							self   ,    snake_case_   ,    snake_case_=None   ,    snake_case_=None   ,    snake_case_=None   ,    **snake_case_					):

    '''simple docstring'''







    __UpperCAmelCase:					List[str]		= return_dict if return_dict is not None else self.config.use_return_dict
    __UpperCAmelCase:					List[str]		= (
        output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
    )
    __UpperCAmelCase:					List[Any]		= output_attentions if output_attentions is not None else self.config.output_attentions

    if output_attentions:
      raise ValueError("""Cannot output attentions for timm backbones at the moment"""					)

    if output_hidden_states:
      # We modify the return layers to include all the stages of the backbone
      __UpperCAmelCase:					int		= self._all_layers
      __UpperCAmelCase:					Optional[int]		= self._backbone(snake_case_   ,    **snake_case_					)
      __UpperCAmelCase:					List[Any]		= self._return_layers
      __UpperCAmelCase:					int		= tuple(hidden_states[i] for i in self.out_indices					)
    else:
      __UpperCAmelCase:					List[str]		= self._backbone(snake_case_   ,    **snake_case_					)
      __UpperCAmelCase:					List[Any]		= None

    __UpperCAmelCase:					Tuple		= tuple(snake_case_					)
    __UpperCAmelCase:					str		= tuple(snake_case_					) if hidden_states is not None else None

    if not return_dict:
      __UpperCAmelCase:					Tuple		= (feature_maps,)
      if output_hidden_states:
        __UpperCAmelCase:					Dict		= output + (hidden_states,)
      return output

    return BackboneOutput(feature_maps=snake_case_   ,    hidden_states=snake_case_   ,    attentions=snake_case_					)
'''simple docstring'''






from ...configuration_utils import PretrainedConfig
from ...utils import logging


SCREAMING_SNAKE_CASE_	   =			logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_	   =			{
    'facebook/timesformer': 'https://huggingface.co/facebook/timesformer/resolve/main/config.json',
}




class    a (       __lowerCAmelCase				):



  """simple docstring"""
  __lowerCAmelCase				      =					"""timesformer"""







  def __init__(							self   ,    snake_case_=224   ,    snake_case_=16   ,    snake_case_=3   ,    snake_case_=8   ,    snake_case_=768   ,    snake_case_=12   ,    snake_case_=12   ,    snake_case_=3072   ,    snake_case_="gelu"   ,    snake_case_=0.0   ,    snake_case_=0.0   ,    snake_case_=0.0_2   ,    snake_case_=1e-6   ,    snake_case_=True   ,    snake_case_="divided_space_time"   ,    snake_case_=0   ,    **snake_case_   ,    ):

    '''simple docstring'''







    super().__init__(**snake_case_					)

    __UpperCAmelCase:					Tuple		= image_size
    __UpperCAmelCase:					List[Any]		= patch_size
    __UpperCAmelCase:					Optional[Any]		= num_channels
    __UpperCAmelCase:					int		= num_frames

    __UpperCAmelCase:					List[str]		= hidden_size
    __UpperCAmelCase:					List[str]		= num_hidden_layers
    __UpperCAmelCase:					Dict		= num_attention_heads
    __UpperCAmelCase:					List[str]		= intermediate_size
    __UpperCAmelCase:					Union[str, Any]		= hidden_act
    __UpperCAmelCase:					Dict		= hidden_dropout_prob
    __UpperCAmelCase:					Optional[Any]		= attention_probs_dropout_prob
    __UpperCAmelCase:					Union[str, Any]		= initializer_range
    __UpperCAmelCase:					List[Any]		= layer_norm_eps
    __UpperCAmelCase:					int		= qkv_bias

    __UpperCAmelCase:					str		= attention_type
    __UpperCAmelCase:					Tuple		= drop_path_rate