






"""simple docstring"""


from dataclasses import dataclass
from typing import Any, Dict, Optional

import torch
import torch.nn.functional as F
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..models.embeddings import ImagePositionalEmbeddings
from ..utils import BaseOutput, deprecate
from .attention import BasicTransformerBlock
from .embeddings import PatchEmbed
from .modeling_utils import ModelMixin






@dataclass
class 		snake_case    (    UpperCAmelCase			):
    __magic_name__       	=	42






class 		snake_case    (    UpperCAmelCase  ,					UpperCAmelCase			):





    @register_to_config
    def __init__(				self      :   Optional[Any]							,						A      :   int = 1_6							,						A      :   int = 8_8							,						A      :   Optional[int] = None							,						A      :   Optional[int] = None							,						A      :   int = 1							,						A      :   float = 0.0							,						A      :   int = 3_2							,						A      :   Optional[int] = None							,						A      :   bool = False							,						A      :   Optional[int] = None							,						A      :   Optional[int] = None							,						A      :   Optional[int] = None							,						A      :   str = "geglu"							,						A      :   Optional[int] = None							,						A      :   bool = False							,						A      :   bool = False							,						A      :   bool = False							,						A      :   str = "layer_norm"							,						A      :   bool = True							,						):


      '''simple docstring'''




      super().__init__()
      a	:     Dict							=  use_linear_projection
      a	:     Dict							=  num_attention_heads
      a	:     int							=  attention_head_dim
      a	:     List[str]							=  num_attention_heads * attention_head_dim

      # 1. Transformer2DModel can process both standard continuous images of shape `(batch_size, num_channels, width, height)` as well as quantized image embeddings of shape `(batch_size, num_image_vectors)`
      # Define whether input is continuous or discrete depending on configuration
      a	:     List[Any]							=  (in_channels is not None) and (patch_size is None)
      a	:     str							=  num_vector_embeds is not None
      a	:     int							=  in_channels is not None and patch_size is not None

      if norm_type == "layer_norm" and num_embeds_ada_norm is not None:
        a	:     List[Any]							=  (
            F'''The configuration file of this model: {self.__class__} is outdated. `norm_type` is either not set or'''
            ' incorrectly set to `\'layer_norm\'`.Make sure to set `norm_type` to `\'ada_norm\'` in the config.'
            ' Please make sure to update the config accordingly as leaving `norm_type` might led to incorrect'
            ' results in future versions. If you have downloaded this checkpoint from the Hugging Face Hub, it'
            ' would be very nice if you could open a Pull request for the `transformer/config.json` file'
        )
        deprecate('norm_type!=num_embeds_ada_norm'							,						'1.0.0'							,						A							,						standard_warn=A     )
        a	:     List[str]							=  'ada_norm'

      if self.is_input_continuous and self.is_input_vectorized:
        raise ValueError(
            F'''Cannot define both `in_channels`: {in_channels} and `num_vector_embeds`: {num_vector_embeds}. Make'''
            ' sure that either `in_channels` or `num_vector_embeds` is None.'     )
      elif self.is_input_vectorized and self.is_input_patches:
        raise ValueError(
            F'''Cannot define both `num_vector_embeds`: {num_vector_embeds} and `patch_size`: {patch_size}. Make'''
            ' sure that either `num_vector_embeds` or `num_patches` is None.'     )
      elif not self.is_input_continuous and not self.is_input_vectorized and not self.is_input_patches:
        raise ValueError(
            F'''Has to define `in_channels`: {in_channels}, `num_vector_embeds`: {num_vector_embeds}, or patch_size:'''
            F''' {patch_size}. Make sure that `in_channels`, `num_vector_embeds` or `num_patches` is not None.'''     )

      # 2. Define input layers
      if self.is_input_continuous:
        a	:     Optional[Any]							=  in_channels

        a	:     str							=  torch.nn.GroupNorm(num_groups=A							,						num_channels=A							,						eps=1E-6							,						affine=A     )
        if use_linear_projection:
          a	:     Optional[int]							=  nn.Linear(A							,						A     )
        else:
          a	:     Dict							=  nn.Convad(A							,						A							,						kernel_size=1							,						stride=1							,						padding=0     )
      elif self.is_input_vectorized:
        assert sample_size is not None, "Transformer2DModel over discrete input must provide sample_size"
        assert num_vector_embeds is not None, "Transformer2DModel over discrete input must provide num_embed"

        a	:     Tuple							=  sample_size
        a	:     Optional[Any]							=  sample_size
        a	:     Optional[int]							=  num_vector_embeds
        a	:     List[str]							=  self.height * self.width

        a	:     Any							=  ImagePositionalEmbeddings(
            num_embed=A							,						embed_dim=A							,						height=self.height							,						width=self.width     )
      elif self.is_input_patches:
        assert sample_size is not None, "Transformer2DModel over patched input must provide sample_size"

        a	:     str							=  sample_size
        a	:     Dict							=  sample_size

        a	:     List[str]							=  patch_size
        a	:     Dict							=  PatchEmbed(
            height=A							,						width=A							,						patch_size=A							,						in_channels=A							,						embed_dim=A							,						)

      # 3. Define transformers blocks
      a	:     Optional[int]							=  nn.ModuleList(
          [
              BasicTransformerBlock(
                  A							,						A							,						A							,						dropout=A							,						cross_attention_dim=A							,						activation_fn=A							,						num_embeds_ada_norm=A							,						attention_bias=A							,						only_cross_attention=A							,						upcast_attention=A							,						norm_type=A							,						norm_elementwise_affine=A							,						)
              for d in range(A     )
          ]     )

      # 4. Define output layers
      a	:     List[str]							=  in_channels if out_channels is None else out_channels
      if self.is_input_continuous:
        # TODO: should use out_channels for continuous projections
        if use_linear_projection:
          a	:     Tuple							=  nn.Linear(A							,						A     )
        else:
          a	:     Any							=  nn.Convad(A							,						A							,						kernel_size=1							,						stride=1							,						padding=0     )
      elif self.is_input_vectorized:
        a	:     Optional[Any]							=  nn.LayerNorm(A     )
        a	:     Optional[int]							=  nn.Linear(A							,						self.num_vector_embeds - 1     )
      elif self.is_input_patches:
        a	:     Optional[int]							=  nn.LayerNorm(A							,						elementwise_affine=A							,						eps=1E-6     )
        a	:     Any							=  nn.Linear(A							,						2 * inner_dim     )
        a	:     str							=  nn.Linear(A							,						patch_size * patch_size * self.out_channels     )





    def 							lowerCamelCase__ (				self      :   Dict							,						A      :   torch.Tensor							,						A      :   Optional[torch.Tensor] = None							,						A      :   Optional[torch.LongTensor] = None							,						A      :   Optional[torch.LongTensor] = None							,						A      :   Dict[str, Any] = None							,						A      :   Optional[torch.Tensor] = None							,						A      :   Optional[torch.Tensor] = None							,						A      :   bool = True							,						):


      '''simple docstring'''




      if attention_mask is not None and attention_mask.ndim == 2:
        # assume that mask is expressed as:
        #   (1 = keep,      0 = discard)
        # convert mask into a bias that can be added to attention scores:
        #       (keep = +0,     discard = -10000.0)
        a	:     str							=  (1 - attention_mask.to(hidden_states.dtype     )) * -1_00_00.0
        a	:     Tuple							=  attention_mask.unsqueeze(1     )

      # convert encoder_attention_mask to a bias the same way we do for attention_mask
      if encoder_attention_mask is not None and encoder_attention_mask.ndim == 2:
        a	:     Any							=  (1 - encoder_attention_mask.to(hidden_states.dtype     )) * -1_00_00.0
        a	:     str							=  encoder_attention_mask.unsqueeze(1     )

      # 1. Input
      if self.is_input_continuous:
        a,	a,	a,	a	:     str							=  hidden_states.shape
        a	:     List[Any]							=  hidden_states

        a	:     str							=  self.norm(A     )
        if not self.use_linear_projection:
          a	:     List[str]							=  self.proj_in(A     )
          a	:     Dict							=  hidden_states.shape[1]
          a	:     Tuple							=  hidden_states.permute(0							,						2							,						3							,						1     ).reshape(A							,						height * width							,						A     )
        else:
          a	:     int							=  hidden_states.shape[1]
          a	:     Dict							=  hidden_states.permute(0							,						2							,						3							,						1     ).reshape(A							,						height * width							,						A     )
          a	:     Optional[Any]							=  self.proj_in(A     )
      elif self.is_input_vectorized:
        a	:     Optional[Any]							=  self.latent_image_embedding(A     )
      elif self.is_input_patches:
        a	:     Dict							=  self.pos_embed(A     )

      # 2. Blocks
      for block in self.transformer_blocks:
        a	:     Dict							=  block(
            A							,						attention_mask=A							,						encoder_hidden_states=A							,						encoder_attention_mask=A							,						timestep=A							,						cross_attention_kwargs=A							,						class_labels=A							,						)

      # 3. Output
      if self.is_input_continuous:
        if not self.use_linear_projection:
          a	:     Optional[Any]							=  hidden_states.reshape(A							,						A							,						A							,						A     ).permute(0							,						3							,						1							,						2     ).contiguous()
          a	:     Dict							=  self.proj_out(A     )
        else:
          a	:     Any							=  self.proj_out(A     )
          a	:     Optional[Any]							=  hidden_states.reshape(A							,						A							,						A							,						A     ).permute(0							,						3							,						1							,						2     ).contiguous()

        a	:     int							=  hidden_states + residual
      elif self.is_input_vectorized:
        a	:     str							=  self.norm_out(A     )
        a	:     int							=  self.out(A     )
        # (batch, self.num_vector_embeds - 1, self.num_latent_pixels)
        a	:     str							=  logits.permute(0							,						2							,						1     )

        # log(p(x_0))
        a	:     Dict							=  F.log_softmax(logits.double()							,						dim=1     ).float()
      elif self.is_input_patches:
        # TODO: cleanup!
        a	:     List[str]							=  self.transformer_blocks[0].norma.emb(
            A							,						A							,						hidden_dtype=hidden_states.dtype     )
        a,	a	:     Any							=  self.proj_out_a(F.silu(A     )     ).chunk(2							,						dim=1     )
        a	:     Union[str, Any]							=  self.norm_out(A     ) * (1 + scale[:, None]) + shift[:, None]
        a	:     Dict							=  self.proj_out_a(A     )

        # unpatchify
        a	:     Dict							=  int(hidden_states.shape[1] ** 0.5     )
        a	:     List[str]							=  hidden_states.reshape(
            shape=(-1, height, width, self.patch_size, self.patch_size, self.out_channels)     )
        a	:     Optional[int]							=  torch.einsum('nhwpqc->nchpwq'							,						A     )
        a	:     Optional[Any]							=  hidden_states.reshape(
            shape=(-1, self.out_channels, height * self.patch_size, width * self.patch_size)     )

      if not return_dict:
        return (output,)

      return TransformeraDModelOutput(sample=A     )












"""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							:       Optional[Any]       					=		logging.get_logger(__name__)

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






class 		snake_case    (    UpperCAmelCase			):
    __magic_name__       	=	'''mobilevitv2'''





    def __init__(				self      :   Any							,						A      :   Any=3							,						A      :   Any=2_5_6							,						A      :   Optional[int]=2							,						A      :   str=2.0							,						A      :   Union[str, Any]="swish"							,						A      :   str=3							,						A      :   Tuple=3_2							,						A      :   Optional[int]=0.1							,						A      :   str=0.02							,						A      :   int=1E-5							,						A      :   List[Any]=5_1_2							,						A      :   str=[6, 1_2, 1_8]							,						A      :   Any=0.1							,						A      :   Union[str, Any]=2_5_5							,						A      :   Optional[Any]=[2, 4, 3]							,						A      :   List[Any]=[1_2_8, 1_9_2, 2_5_6]							,						A      :   Tuple=1.0							,						A      :   Union[str, Any]=2							,						A      :   List[Any]=0.0							,						A      :   int=0.0							,						**A      :   Optional[Any]							,						):


      '''simple docstring'''




      super().__init__(**A     )

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

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






class 		snake_case    (    UpperCAmelCase			):
    __magic_name__       	=	version.parse('''1.11'''			)





    @property
    def 							lowerCamelCase__ (				self      :   List[str]     ):


      '''simple docstring'''




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





    @property
    def 							lowerCamelCase__ (				self      :   int     ):


      '''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 							lowerCamelCase__ (				self      :   Union[str, Any]     ):


      '''simple docstring'''




      return 1E-4




