






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


UpperCamelCase											=      logging.get_logger(__name__)

UpperCamelCase											=      {
    '''apple/mobilevitv2-1.0''': '''https://huggingface.co/apple/mobilevitv2-1.0/resolve/main/config.json''',
}




class 	lowerCamelCase__				(							UpperCAmelCase ):
      lowerCamelCase_							:   Optional[Any]											=	'mobilevitv2'






      def __init__(self					:    int		,		_snake_case					:    Dict=3		,		_snake_case					:    int=256		,		_snake_case					:    int=2		,		_snake_case					:    Union[str, Any]=2.0		,		_snake_case					:    Union[str, Any]="swish"		,		_snake_case					:    List[str]=3		,		_snake_case					:    List[Any]=32		,		_snake_case					:    Union[str, Any]=0.1		,		_snake_case					:    str=0.02		,		_snake_case					:    Optional[int]=1e-5		,		_snake_case					:    Optional[int]=512		,		_snake_case					:    Any=[6, 12, 18]		,		_snake_case					:    int=0.1		,		_snake_case					:    int=255		,		_snake_case					:    Union[str, Any]=[2, 4, 3]		,		_snake_case					:    List[Any]=[128, 192, 256]		,		_snake_case					:    Optional[int]=1.0		,		_snake_case					:    Optional[Any]=2		,		_snake_case					:    List[str]=0.0		,		_snake_case					:    List[str]=0.0		,		**_snake_case					:    Dict		,		)       ->   Any:


       """simple docstring"""






       super().__init__(**_snake_case		)

       lowerCamelCase_							:    int							=     num_channels
       lowerCamelCase_							:    Any							=     image_size
       lowerCamelCase_							:    List[str]							=     patch_size
       lowerCamelCase_							:    Any							=     expand_ratio
       lowerCamelCase_							:    Any							=     hidden_act
       lowerCamelCase_							:    Tuple							=     conv_kernel_size
       lowerCamelCase_							:    int							=     output_stride
       lowerCamelCase_							:    Dict							=     initializer_range
       lowerCamelCase_							:    Tuple							=     layer_norm_eps
       lowerCamelCase_							:    Tuple							=     n_attn_blocks
       lowerCamelCase_							:    Optional[int]							=     base_attn_unit_dims
       lowerCamelCase_							:    Optional[Any]							=     width_multiplier
       lowerCamelCase_							:    List[str]							=     ffn_multiplier
       lowerCamelCase_							:    int							=     ffn_dropout
       lowerCamelCase_							:    Optional[int]							=     attn_dropout
       lowerCamelCase_							:    Optional[Any]							=     classifier_dropout_prob

       # decode head attributes for semantic segmentation
       lowerCamelCase_							:    List[str]							=     aspp_out_channels
       lowerCamelCase_							:    Union[str, Any]							=     atrous_rates
       lowerCamelCase_							:    List[str]							=     aspp_dropout_prob
       lowerCamelCase_							:    Optional[int]							=     semantic_loss_ignore_index




class 	lowerCamelCase__				(							UpperCAmelCase ):
      lowerCamelCase_							:   Dict											=	version.parse('1.11' )






      @property
      def      UpperCAmelCase_     (self					:    Optional[Any]		)       ->   Mapping[str, Mapping[int, str]]:


       """simple docstring"""






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






      @property
      def      UpperCAmelCase_     (self					:    Any		)       ->   Mapping[str, Mapping[int, str]]:


       """simple docstring"""






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






      @property
      def      UpperCAmelCase_     (self					:    Optional[Any]		)       ->   float:


       """simple docstring"""






       return 1e-4








from functools import partial
from typing import Any, Callable, Dict, List, Type, TypeVar, Union, overload

import torch
import torch.nn as nn
import torch.types




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)  ->      torch.Tensor:
 # The first operation in a checkpoint can't be in-place, but it's
 # nice to have in-place addition during inference. Thus...
 if not inplace:
  lowerCamelCase_							:    Tuple							=     ma + ma
 else:
  ma += ma

 return ma




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__				)  ->      torch.Tensor:
 lowerCamelCase_							:    Union[str, Any]							=     -1 * len(lowerCamelCase__				)
 lowerCamelCase_							:    Optional[Any]							=     list(range(len(tensor.shape[:zero_index]				)				)				)
 return tensor.permute(first_inds + [zero_index + i for i in inds]				)




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__				)  ->      torch.Tensor:
 return t.reshape(t.shape[:-no_dims] + (-1,)				)




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__ = 1e-4				)  ->      torch.Tensor:
 lowerCamelCase_							:    Optional[int]							=     mask.expand(*value.shape				)
 return torch.sum(mask * value ,			dim=lowerCamelCase__				) / (eps + torch.sum(lowerCamelCase__ ,			dim=lowerCamelCase__				))




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__ = 2.3125 ,			lowerCamelCase__ = 21.6875 ,			lowerCamelCase__ = 64				)  ->      torch.Tensor:
 lowerCamelCase_							:    int							=     torch.linspace(lowerCamelCase__ ,			lowerCamelCase__ ,			no_bins - 1 ,			device=pts.device				)
 lowerCamelCase_							:    Tuple							=     torch.sqrt(torch.sum((pts.unsqueeze(-2				) - pts.unsqueeze(-3				)) ** 2 ,			dim=-1				)				)
 return torch.bucketize(lowerCamelCase__ ,			lowerCamelCase__				)




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__				)  ->      dict:
 lowerCamelCase_							:    Optional[Any]							=     dicts[0]
 lowerCamelCase_							:    Optional[Any]							=     {}
 for k, v in first.items():
  lowerCamelCase_							:    Tuple							=     [d[k] for d in dicts]
  if isinstance(lowerCamelCase__ ,			lowerCamelCase__				):
   lowerCamelCase_							:    Optional[int]							=     dict_multimap(lowerCamelCase__ ,			lowerCamelCase__				)
  else:
   lowerCamelCase_							:    List[Any]							=     fn(lowerCamelCase__				)

 return new_dict




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__				)  ->      torch.Tensor:
 lowerCamelCase_							:    str							=     v_bins.view(((1,) * len(x.shape				)) + (len(lowerCamelCase__				),)				)
 lowerCamelCase_							:    List[Any]							=     x[..., None] - reshaped_bins
 lowerCamelCase_							:    List[Any]							=     torch.argmin(torch.abs(lowerCamelCase__				) ,			dim=-1				)
 return nn.functional.one_hot(lowerCamelCase__ ,			num_classes=len(lowerCamelCase__				)				).float()




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__ = 0 ,			lowerCamelCase__ = 0				)  ->      torch.Tensor:
 lowerCamelCase_							:    List[Union[slice, torch.Tensor]]							=     []
 for i, s in enumerate(data.shape[:no_batch_dims]				):
  lowerCamelCase_							:    int							=     torch.arange(lowerCamelCase__				)
  lowerCamelCase_							:    Any							=     r.view(*(*((1,) * i), -1, *((1,) * (len(inds.shape				) - i - 1)))				)
  ranges.append(lowerCamelCase__				)

 lowerCamelCase_							:    List[Union[slice, torch.Tensor]]							=     [slice(lowerCamelCase__				) for _ in range(len(data.shape				) - no_batch_dims				)]
 lowerCamelCase_							:    Optional[int]							=     inds
 ranges.extend(lowerCamelCase__				)
 # Matt note: Editing this to get around the behaviour of using a list as an array index changing
 # in recent Numpy versions
 return data[tuple(lowerCamelCase__				)]


UpperCamelCase											=      TypeVar('''T''')




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)  ->      Dict[Any, Union[dict, list, tuple, Any]]:
 lowerCamelCase_							:    Dict[Any, Union[dict, list, tuple, Any]]							=     {}
 for k, v in dic.items():
  if isinstance(lowerCamelCase__ ,			lowerCamelCase__				):
   lowerCamelCase_							:    Optional[Any]							=     dict_map(lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)
  else:
   lowerCamelCase_							:    Dict							=     tree_map(lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)

 return new_dict




@overload
def       _a   (							lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)  ->      Any:
 ...




@overload
def       _a   (							lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)  ->      dict:
 ...




@overload
def       _a   (							lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)  ->      list:
 ...




@overload
def       _a   (							lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)  ->      tuple:
 ...




def       _a   (							lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)  ->      Any:
 if isinstance(lowerCamelCase__ ,			lowerCamelCase__				):
  return dict_map(lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				)
 elif isinstance(lowerCamelCase__ ,			lowerCamelCase__				):
  return [tree_map(lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				) for x in tree]
 elif isinstance(lowerCamelCase__ ,			lowerCamelCase__				):
  return tuple(tree_map(lowerCamelCase__ ,			lowerCamelCase__ ,			lowerCamelCase__				) for x in tree				)
 elif isinstance(lowerCamelCase__ ,			lowerCamelCase__				):
  return fn(lowerCamelCase__				)
 else:
  print(type(lowerCamelCase__				)				)
  raise ValueError('Not supported'				)





UpperCamelCase											=      partial(tree_map, leaf_type=torch.Tensor)
