





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 					lowerCAmelCase							(   lowercase_					):
  __lowerCamelCase		=			42





class 					lowerCAmelCase							(   lowercase_							,							lowercase_					):







  @register_to_config
  def __init__(			self  :List[Any]    ,							_lowercase  :int = 16    ,							_lowercase  :int = 88    ,							_lowercase  :Optional[int] = None    ,							_lowercase  :Optional[int] = None    ,							_lowercase  :int = 1    ,							_lowercase  :float = 0.0    ,							_lowercase  :int = 32    ,							_lowercase  :Optional[int] = None    ,							_lowercase  :bool = False    ,							_lowercase  :Optional[int] = None    ,							_lowercase  :Optional[int] = None    ,							_lowercase  :Optional[int] = None    ,							_lowercase  :str = "geglu"    ,							_lowercase  :Optional[int] = None    ,							_lowercase  :bool = False    ,							_lowercase  :bool = False    ,							_lowercase  :bool = False    ,							_lowercase  :str = "layer_norm"    ,							_lowercase  :bool = True    ,							):






     '''simple docstring'''





     super().__init__()
     lowercase__    =  use_linear_projection
     lowercase__    =  num_attention_heads
     lowercase__    =  attention_head_dim
     lowercase__    =  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
     lowercase__    =  (in_channels is not None) and (patch_size is None)
     lowercase__    =  num_vector_embeds is not None
     lowercase__    =  in_channels is not None and patch_size is not None

     if norm_type == "layer_norm" and num_embeds_ada_norm is not None:
        lowercase__    =  (
            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"    ,							_lowercase    ,							standard_warn=_lowercase					)
        lowercase__    =  "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:
        lowercase__    =  in_channels

        lowercase__    =  torch.nn.GroupNorm(num_groups=_lowercase    ,							num_channels=_lowercase    ,							eps=1e-6    ,							affine=_lowercase					)
        if use_linear_projection:
           lowercase__    =  nn.Linear(_lowercase    ,							_lowercase					)
        else:
           lowercase__    =  nn.Convad(_lowercase    ,							_lowercase    ,							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"

        lowercase__    =  sample_size
        lowercase__    =  sample_size
        lowercase__    =  num_vector_embeds
        lowercase__    =  self.height * self.width

        lowercase__    =  ImagePositionalEmbeddings(
            num_embed=_lowercase    ,							embed_dim=_lowercase    ,							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"

        lowercase__    =  sample_size
        lowercase__    =  sample_size

        lowercase__    =  patch_size
        lowercase__    =  PatchEmbed(
            height=_lowercase    ,							width=_lowercase    ,							patch_size=_lowercase    ,							in_channels=_lowercase    ,							embed_dim=_lowercase    ,							)

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

     # 4. Define output layers
     lowercase__    =  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:
           lowercase__    =  nn.Linear(_lowercase    ,							_lowercase					)
        else:
           lowercase__    =  nn.Convad(_lowercase    ,							_lowercase    ,							kernel_size=1    ,							stride=1    ,							padding=0					)
     elif self.is_input_vectorized:
        lowercase__    =  nn.LayerNorm(_lowercase					)
        lowercase__    =  nn.Linear(_lowercase    ,							self.num_vector_embeds - 1					)
     elif self.is_input_patches:
        lowercase__    =  nn.LayerNorm(_lowercase    ,							elementwise_affine=_lowercase    ,							eps=1e-6					)
        lowercase__    =  nn.Linear(_lowercase    ,							2 * inner_dim					)
        lowercase__    =  nn.Linear(_lowercase    ,							patch_size * patch_size * self.out_channels					)







  def   UpperCAmelCase     (			self  :Union[str, Any]    ,							_lowercase  :torch.Tensor    ,							_lowercase  :Optional[torch.Tensor] = None    ,							_lowercase  :Optional[torch.LongTensor] = None    ,							_lowercase  :Optional[torch.LongTensor] = None    ,							_lowercase  :Dict[str, Any] = None    ,							_lowercase  :Optional[torch.Tensor] = None    ,							_lowercase  :Optional[torch.Tensor] = None    ,							_lowercase  :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)
        lowercase__    =  (1 - attention_mask.to(hidden_states.dtype					)) * -10000.0
        lowercase__    =  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:
        lowercase__    =  (1 - encoder_attention_mask.to(hidden_states.dtype					)) * -10000.0
        lowercase__    =  encoder_attention_mask.unsqueeze(1					)

     # 1. Input
     if self.is_input_continuous:
        lowercase__   ,							lowercase__   ,							lowercase__   ,							lowercase__    =  hidden_states.shape
        lowercase__    =  hidden_states

        lowercase__    =  self.norm(_lowercase					)
        if not self.use_linear_projection:
           lowercase__    =  self.proj_in(_lowercase					)
           lowercase__    =  hidden_states.shape[1]
           lowercase__    =  hidden_states.permute(0    ,							2    ,							3    ,							1					).reshape(_lowercase    ,							height * width    ,							_lowercase					)
        else:
           lowercase__    =  hidden_states.shape[1]
           lowercase__    =  hidden_states.permute(0    ,							2    ,							3    ,							1					).reshape(_lowercase    ,							height * width    ,							_lowercase					)
           lowercase__    =  self.proj_in(_lowercase					)
     elif self.is_input_vectorized:
        lowercase__    =  self.latent_image_embedding(_lowercase					)
     elif self.is_input_patches:
        lowercase__    =  self.pos_embed(_lowercase					)

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

     # 3. Output
     if self.is_input_continuous:
        if not self.use_linear_projection:
           lowercase__    =  hidden_states.reshape(_lowercase    ,							_lowercase    ,							_lowercase    ,							_lowercase					).permute(0    ,							3    ,							1    ,							2					).contiguous()
           lowercase__    =  self.proj_out(_lowercase					)
        else:
           lowercase__    =  self.proj_out(_lowercase					)
           lowercase__    =  hidden_states.reshape(_lowercase    ,							_lowercase    ,							_lowercase    ,							_lowercase					).permute(0    ,							3    ,							1    ,							2					).contiguous()

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

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

        # unpatchify
        lowercase__    =  lowercase__    =  int(hidden_states.shape[1] ** 0.5					)
        lowercase__    =  hidden_states.reshape(
            shape=(-1, height, width, self.patch_size, self.patch_size, self.out_channels)					)
        lowercase__    =  torch.einsum("nhwpqc->nchpwq"    ,							_lowercase					)
        lowercase__    =  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=_lowercase					)













from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_snake_case				= {"""configuration_dinat""": ["""DINAT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """DinatConfig"""]}


try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  _snake_case				= [
      """DINAT_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """DinatForImageClassification""",
      """DinatModel""",
      """DinatPreTrainedModel""",
      """DinatBackbone""",
  ]

if TYPE_CHECKING:
  from .configuration_dinat import DINAT_PRETRAINED_CONFIG_ARCHIVE_MAP, DinatConfig

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_dinat import (
        DINAT_PRETRAINED_MODEL_ARCHIVE_LIST,
        DinatBackbone,
        DinatForImageClassification,
        DinatModel,
        DinatPreTrainedModel,
    )

else:
  import sys

  _snake_case				= _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)






