
'''simple docstring'''

import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
     import transformer_engine.pytorch as te


def 		_UpperCAmelCase			(					_lowerCamelCase  :    Any					, _lowerCamelCase  :    List[str]=True					, _lowerCamelCase  :    Tuple=True					, _lowerCamelCase  :    Dict=True						)      ->  Tuple:
  if not is_fpa_available():
    raise ImportError("""Using `convert_model` requires transformer_engine to be installed."""						)
  for name, module in model.named_children():
    if isinstance(_lowerCAmelCase					, nn.Linear						) and to_transformer_engine and _convert_linear:
      # Return early if the linear layer weights are not multiples of 16
      if any(p % 16 != 0 for p in module.weight.shape						):
        return
      _lowerCAmelCase    :					int							=   module.bias is not None
      _lowerCAmelCase    :					int							=   te.Linear(
          module.in_features					, module.out_features					, bias=_lowerCAmelCase					, params_dtype=module.weight.dtype						)
      _lowerCAmelCase    :					Optional[Any]							=   module.weight.data.clone()
      if has_bias:
        _lowerCAmelCase    :					Union[str, Any]							=   module.bias.data.clone()

      setattr(_lowerCAmelCase					, _lowerCAmelCase					, _lowerCAmelCase						)
    elif isinstance(_lowerCAmelCase					, nn.LayerNorm						) and to_transformer_engine and _convert_ln:
      _lowerCAmelCase    :					Optional[int]							=   te.LayerNorm(module.normalized_shape[0]					, eps=module.eps					, params_dtype=module.weight.dtype						)
      _lowerCAmelCase    :					Any							=   module.weight.data.clone()
      _lowerCAmelCase    :					Optional[int]							=   module.bias.data.clone()

      setattr(_lowerCAmelCase					, _lowerCAmelCase					, _lowerCAmelCase						)
    elif isinstance(_lowerCAmelCase					, te.Linear						) and not to_transformer_engine and _convert_linear:
      _lowerCAmelCase    :					Dict							=   module.bias is not None
      _lowerCAmelCase    :					Optional[int]							=   nn.Linear(
          module.in_features					, module.out_features					, bias=_lowerCAmelCase					, params_dtype=module.weight.dtype						)
      _lowerCAmelCase    :					int							=   module.weight.data.clone()
      if has_bias:
        _lowerCAmelCase    :					str							=   module.bias.data.clone()

      setattr(_lowerCAmelCase					, _lowerCAmelCase					, _lowerCAmelCase						)
    elif isinstance(_lowerCAmelCase					, te.LayerNorm						) and not to_transformer_engine and _convert_ln:
      _lowerCAmelCase    :					Tuple							=   nn.LayerNorm(module.normalized_shape[0]					, eps=module.eps					, params_dtype=module.weight.dtype						)
      _lowerCAmelCase    :					Optional[int]							=   module.weight.data.clone()
      _lowerCAmelCase    :					Dict							=   module.bias.data.clone()

      setattr(_lowerCAmelCase					, _lowerCAmelCase					, _lowerCAmelCase						)
    else:
      convert_model(
          _lowerCAmelCase					, to_transformer_engine=_lowerCAmelCase					, _convert_linear=_convert_linear					, _convert_ln=_convert_ln					, )



def 		_UpperCAmelCase			(					_lowerCamelCase  :    Optional[Any]						)      ->  Any:
  if not is_fpa_available():
    raise ImportError("""Using `has_transformer_engine_layers` requires transformer_engine to be installed."""						)
  for m in model.modules():
    if isinstance(_lowerCAmelCase					, (te.LayerNorm, te.Linear)						):
      return True
  return False




'''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


UpperCamelCase_            =    logging.get_logger(__name__)

UpperCamelCase_            =    {
    """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 					a_					(_a      ):
       __lowerCAmelCase      :			List[str]      = """mobilevit"""




       def __init__(       self				,				snake_case_=3				,				snake_case_=2_5_6				,				snake_case_=2				,				snake_case_=[1_4_4, 1_9_2, 2_4_0]				,				snake_case_=[1_6, 3_2, 6_4, 9_6, 1_2_8, 1_6_0, 6_4_0]				,				snake_case_=4				,				snake_case_=2.0				,				snake_case_=4.0				,				snake_case_="silu"				,				snake_case_=3				,				snake_case_=3_2				,				snake_case_=0.1				,				snake_case_=0.0				,				snake_case_=0.1				,				snake_case_=0.02				,				snake_case_=1E-5				,				snake_case_=True				,				snake_case_=2_5_6				,				snake_case_=[6, 1_2, 1_8]				,				snake_case_=0.1				,				snake_case_=2_5_5				,				**snake_case_				,				):
         super().__init__(**snake_case_					)

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

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



class 					a_					(_a      ):
       __lowerCAmelCase      :			Optional[int]      = version.parse("""1.11"""      )




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




       @property
       def 							__UpperCamelCase    (       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 							__UpperCamelCase    (       self					):
         return 1E-4


