






"""simple docstring"""



import enum
import inspect
from typing import Iterable, List, Optional, Tuple, Union

class 			a	(  enum.Enum      ):
    UpperCAmelCase_					:	Tuple      	="timm"
    UpperCAmelCase_					:	Any      	="transformers"


def 		_SCREAMING_SNAKE_CASE					(       __snake_case     :    Optional[Iterable[str]]						,   __snake_case     :    Optional[Iterable[int]]						,   __snake_case     :    Optional[Iterable[str]]       ):





  '''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(__snake_case						,   (list,)       ):
      raise ValueError(f'out_features must be a list {type(__snake_case       )}'       )
    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(__snake_case						,   (list, tuple)       ):
      raise ValueError(f'out_indices must be a list or tuple, got {type(__snake_case       )}'       )
    if any(idx >= len(__snake_case       ) 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(__snake_case       ) != len(__snake_case       ):
      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 		_SCREAMING_SNAKE_CASE					(       __snake_case     :    Optional[List[str]]						,   __snake_case     :    Optional[Union[List[int], Tuple[int]]]						,   __snake_case     :    List[str]						,   ):





  '''simple docstring'''





  if out_indices is None and out_features is None:
    lowercase				       =			[len(__snake_case       ) - 1]
    lowercase				       =			[stage_names[-1]]
  elif out_indices is None and out_features is not None:
    lowercase				       =			[stage_names.index(__snake_case       ) for layer in out_features]
  elif out_features is None and out_indices is not None:
    lowercase				       =			[stage_names[idx] for idx in out_indices]
  return out_features, out_indices
def 		_SCREAMING_SNAKE_CASE					(       __snake_case     :    Optional[List[str]]						,   __snake_case     :    Optional[Union[List[int], Tuple[int]]]						,   __snake_case     :    List[str]						,   ):





  '''simple docstring'''





  verify_out_features_out_indices(out_features=__snake_case						,   out_indices=__snake_case						,   stage_names=__snake_case       )
  lowercase       ,	lowercase				       =			_align_output_features_output_indices(
      out_features=__snake_case						,   out_indices=__snake_case						,   stage_names=__snake_case       )
  # Verify that the aligned out_features and out_indices are valid
  verify_out_features_out_indices(out_features=__snake_case						,   out_indices=__snake_case						,   stage_names=__snake_case       )
  return output_features, output_indices

class 			a	:
    UpperCAmelCase_					:	Optional[BackboneType]      	=None







    def      UpperCamelCase_ (     self				,						_lowerCamelCase			):
      if getattr(self				,						'_backbone'				,						_lowerCamelCase			) 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']
      lowercase				       =			[stage['module'] for stage in self._backbone.feature_info.info]
      lowercase				       =			[stage['num_chs'] for stage in self._backbone.feature_info.info]
      lowercase				       =			self._backbone.feature_info.out_indices
      lowercase				       =			self._backbone.feature_info.module_name()

      # We verify the out indices and out features are valid
      verify_out_features_out_indices(
          out_features=_lowerCamelCase				,						out_indices=_lowerCamelCase				,						stage_names=self.stage_names			)
      lowercase       ,	lowercase				       =			out_features, out_indices







    def      UpperCamelCase_ (     self				,						_lowerCamelCase			):
      lowercase				       =			getattr(_lowerCamelCase				,						'stage_names'			)
      lowercase				       =			getattr(_lowerCamelCase				,						'out_features'				,						_lowerCamelCase			)
      lowercase				       =			getattr(_lowerCamelCase				,						'out_indices'				,						_lowerCamelCase			)

      lowercase				       =			stage_names
      lowercase       ,	lowercase				       =			get_aligned_output_features_output_indices(
          out_features=_lowerCamelCase				,						out_indices=_lowerCamelCase				,						stage_names=_lowerCamelCase			)
      # Number of channels for each stage. This is set in the transformer backbone model init
      lowercase				       =			None







    def      UpperCamelCase_ (     self				,						_lowerCamelCase			):
      lowercase				       =			config

      lowercase				       =			getattr(_lowerCamelCase				,						'use_timm_backbone'				,						_lowerCamelCase			)
      lowercase				       =			BackboneType.TIMM if self.use_timm_backbone else BackboneType.TRANSFORMERS

      if self.backbone_type == BackboneType.TIMM:
        self._init_timm_backbone(_lowerCamelCase			)
      elif self.backbone_type == BackboneType.TRANSFORMERS:
        self._init_transformers_backbone(_lowerCamelCase			)
      else:
        raise ValueError(F'backbone_type {self.backbone_type} not supported.'			)







    @property
    def      UpperCamelCase_ (     self			):
      return self._out_features







    @out_features.setter
    def      UpperCamelCase_ (     self				,						_lowerCamelCase			):
      lowercase       ,	lowercase				       =			get_aligned_output_features_output_indices(
          out_features=_lowerCamelCase				,						out_indices=_lowerCamelCase				,						stage_names=self.stage_names			)







    @property
    def      UpperCamelCase_ (     self			):
      return self._out_indices







    @out_indices.setter
    def      UpperCamelCase_ (     self				,						_lowerCamelCase			):
      lowercase       ,	lowercase				       =			get_aligned_output_features_output_indices(
          out_features=_lowerCamelCase				,						out_indices=_lowerCamelCase				,						stage_names=self.stage_names			)







    @property
    def      UpperCamelCase_ (     self			):
      # the current backbones will output the number of channels for each stage
      # even if that stage is not in the out_features list.
      return {stage: self.num_features[i] for i, stage in enumerate(self.stage_names			)}







    @property
    def      UpperCamelCase_ (     self			):
      return [self.out_feature_channels[name] for name in self.out_features]







    def      UpperCamelCase_ (     self				,						*_lowerCamelCase				,						**_lowerCamelCase			):
      lowercase				       =			dict(inspect.signature(self.forward			).parameters			)
      lowercase				       =			{k: v for k, v in kwargs.items() if k in signature}
      return self(*_lowerCamelCase				,						**_lowerCamelCase			)







    def      UpperCamelCase_ (     self				,						_lowerCamelCase				,						_lowerCamelCase = None				,						_lowerCamelCase = None				,						_lowerCamelCase = None				,						):
      raise NotImplementedError('This method should be implemented by the derived class.'			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			super().to_dict()
      lowercase				       =			output.pop('_out_features'			)
      lowercase				       =			output.pop('_out_indices'			)
      return output


class 			a	:







    @property
    def      UpperCamelCase_ (     self			):
      return self._out_features







    @out_features.setter
    def      UpperCamelCase_ (     self				,						_lowerCamelCase			):
      lowercase       ,	lowercase				       =			get_aligned_output_features_output_indices(
          out_features=_lowerCamelCase				,						out_indices=_lowerCamelCase				,						stage_names=self.stage_names			)







    @property
    def      UpperCamelCase_ (     self			):
      return self._out_indices







    @out_indices.setter
    def      UpperCamelCase_ (     self				,						_lowerCamelCase			):
      lowercase       ,	lowercase				       =			get_aligned_output_features_output_indices(
          out_features=_lowerCamelCase				,						out_indices=_lowerCamelCase				,						stage_names=self.stage_names			)







    def      UpperCamelCase_ (     self			):
      lowercase				       =			super().to_dict()
      lowercase				       =			output.pop('_out_features'			)
      lowercase				       =			output.pop('_out_indices'			)
      return output









'''simple docstring'''






from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


snake_case_     			=						logging.get_logger(__name__)

snake_case_     			=						{
    """apple/mobilevit-small""": """https://huggingface.co/apple/mobilevit-small/resolve/main/config.json""",
    """apple/mobilevit-x-small""": """https://huggingface.co/apple/mobilevit-x-small/resolve/main/config.json""",
    """apple/mobilevit-xx-small""": """https://huggingface.co/apple/mobilevit-xx-small/resolve/main/config.json""",
    """apple/deeplabv3-mobilevit-small""": (
        """https://huggingface.co/apple/deeplabv3-mobilevit-small/resolve/main/config.json"""
    ),
    """apple/deeplabv3-mobilevit-x-small""": (
        """https://huggingface.co/apple/deeplabv3-mobilevit-x-small/resolve/main/config.json"""
    ),
    """apple/deeplabv3-mobilevit-xx-small""": (
        """https://huggingface.co/apple/deeplabv3-mobilevit-xx-small/resolve/main/config.json"""
    ),
    # See all MobileViT models at https://huggingface.co/models?filter=mobilevit
}


class 	_lowercase    (				a					):
   _UpperCamelCase									= """mobilevit"""


   def __init__(			self     , _UpperCAmelCase=3     , _UpperCAmelCase=256     , _UpperCAmelCase=2     , _UpperCAmelCase=[144, 192, 240]     , _UpperCAmelCase=[16, 32, 64, 96, 128, 160, 640]     , _UpperCAmelCase=4     , _UpperCAmelCase=2.0     , _UpperCAmelCase=4.0     , _UpperCAmelCase="silu"     , _UpperCAmelCase=3     , _UpperCAmelCase=32     , _UpperCAmelCase=0.1     , _UpperCAmelCase=0.0     , _UpperCAmelCase=0.1     , _UpperCAmelCase=0.02     , _UpperCAmelCase=1E-5     , _UpperCAmelCase=True     , _UpperCAmelCase=256     , _UpperCAmelCase=[6, 12, 18]     , _UpperCAmelCase=0.1     , _UpperCAmelCase=255     , **_UpperCAmelCase     , ):
       super().__init__(**_UpperCAmelCase       )

       A							:							Union[str, Any]				=    num_channels
       A							:							Union[str, Any]				=    image_size
       A							:							List[str]				=    patch_size
       A							:							Any				=    hidden_sizes
       A							:							Dict				=    neck_hidden_sizes
       A							:							int				=    num_attention_heads
       A							:							List[str]				=    mlp_ratio
       A							:							List[Any]				=    expand_ratio
       A							:							Dict				=    hidden_act
       A							:							List[Any]				=    conv_kernel_size
       A							:							List[str]				=    output_stride
       A							:							Dict				=    hidden_dropout_prob
       A							:							List[str]				=    attention_probs_dropout_prob
       A							:							Optional[Any]				=    classifier_dropout_prob
       A							:							Any				=    initializer_range
       A							:							Union[str, Any]				=    layer_norm_eps
       A							:							Union[str, Any]				=    qkv_bias

       # decode head attributes for semantic segmentation
       A							:							int				=    aspp_out_channels
       A							:							Any				=    atrous_rates
       A							:							Tuple				=    aspp_dropout_prob
       A							:							List[Any]				=    semantic_loss_ignore_index







class 	_lowercase    (				a					):
   _UpperCamelCase									= version.parse("""1.11"""					)


   @property
   def        snake_case					(			self       ):
       return OrderedDict([('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''})]       )


   @property
   def        snake_case					(			self       ):
       if self.task == "image-classification":
           return OrderedDict([('''logits''', {0: '''batch'''})]       )
       else:
           return OrderedDict([('''last_hidden_state''', {0: '''batch'''}), ('''pooler_output''', {0: '''batch'''})]       )


   @property
   def        snake_case					(			self       ):
       return 1E-4
