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_			(snake_case__					):
      '''simple docstring'''

      __UpperCamelCase:						torch.FloatTensor

class 			lowerCamelCase_			(snake_case__							,				snake_case__					):
      '''simple docstring'''

      @register_to_config
      def __init__( self		:       Optional[Any]		,      A		:       int = 16		,      A		:       int = 88		,      A		:       Optional[int] = None		,      A		:       Optional[int] = None		,      A		:       int = 1		,      A		:       float = 0.0		,      A		:       int = 32		,      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		,      ):
             super().__init__()
             _UpperCAmelCase  :						Union[str, Any]							   =					use_linear_projection
             _UpperCAmelCase  :						Tuple							   =					num_attention_heads
             _UpperCAmelCase  :						Union[str, Any]							   =					attention_head_dim
             _UpperCAmelCase  :						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
             _UpperCAmelCase  :						Optional[Any]							   =					(in_channels is not None) and (patch_size is None)
             _UpperCAmelCase  :						str							   =					num_vector_embeds is not None
             _UpperCAmelCase  :						str							   =					in_channels is not None and patch_size is not None

             if norm_type == "layer_norm" and num_embeds_ada_norm is not None:
                    _UpperCAmelCase  :						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 )
                    _UpperCAmelCase  :						Dict							   =					"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:
                    _UpperCAmelCase  :						str							   =					in_channels

                    _UpperCAmelCase  :						List[Any]							   =					torch.nn.GroupNorm(num_groups=A		,      num_channels=A		,      eps=1E-6		,      affine=A )
                    if use_linear_projection:
                           _UpperCAmelCase  :						Union[str, Any]							   =					nn.Linear(A		,      A )
                    else:
                           _UpperCAmelCase  :						Optional[Any]							   =					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"

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

                    _UpperCAmelCase  :						Optional[int]							   =					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"

                    _UpperCAmelCase  :						str							   =					sample_size
                    _UpperCAmelCase  :						Optional[Any]							   =					sample_size

                    _UpperCAmelCase  :						Dict							   =					patch_size
                    _UpperCAmelCase  :						int							   =					PatchEmbed(
                        height=A		,      width=A		,      patch_size=A		,      in_channels=A		,      embed_dim=A		,      )

             # 3. Define transformers blocks
             _UpperCAmelCase  :						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
             _UpperCAmelCase  :						List[Any]							   =					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:
                           _UpperCAmelCase  :						Any							   =					nn.Linear(A		,      A )
                    else:
                           _UpperCAmelCase  :						Any							   =					nn.Convad(A		,      A		,      kernel_size=1		,      stride=1		,      padding=0 )
             elif self.is_input_vectorized:
                    _UpperCAmelCase  :						Tuple							   =					nn.LayerNorm(A )
                    _UpperCAmelCase  :						Optional[Any]							   =					nn.Linear(A		,      self.num_vector_embeds - 1 )
             elif self.is_input_patches:
                    _UpperCAmelCase  :						Tuple							   =					nn.LayerNorm(A		,      elementwise_affine=A		,      eps=1E-6 )
                    _UpperCAmelCase  :						Optional[Any]							   =					nn.Linear(A		,      2 * inner_dim )
                    _UpperCAmelCase  :						Optional[Any]							   =					nn.Linear(A		,      patch_size * patch_size * self.out_channels )





      def   _A			( self		:       Union[str, Any]		,      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		,      ):
             # ensure attention_mask is a bias, and give it a singleton query_tokens dimension.
             #   we may have done this conversion already, e.g. if we came here via UNet2DConditionModel#forward.
             #   we can tell by counting dims; if ndim == 2: it's a mask rather than a bias.
             # expects mask of shape:
             #   [batch, key_tokens]
             # adds singleton query_tokens dimension:
             #   [batch,                    1, key_tokens]
             # this helps to broadcast it as a bias over attention scores, which will be in one of the following shapes:
             #   [batch,  heads, query_tokens, key_tokens] (e.g. torch sdp attn)
             #   [batch * heads, query_tokens, key_tokens] (e.g. xformers or classic attn)
             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)
                    _UpperCAmelCase  :						List[str]							   =					(1 - attention_mask.to(hidden_states.dtype )) * -10000.0
                    _UpperCAmelCase  :						Dict							   =					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:
                    _UpperCAmelCase  :						Union[str, Any]							   =					(1 - encoder_attention_mask.to(hidden_states.dtype )) * -10000.0
                    _UpperCAmelCase  :						Dict							   =					encoder_attention_mask.unsqueeze(1 )

             # 1. Input
             if self.is_input_continuous:
                    _UpperCAmelCase  :						List[str]							   =					hidden_states.shape
                    _UpperCAmelCase  :						Dict							   =					hidden_states

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

             # 2. Blocks
             for block in self.transformer_blocks:
                    _UpperCAmelCase  :						str							   =					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:
                           _UpperCAmelCase  :						List[Any]							   =					hidden_states.reshape(A		,      A		,      A		,      A ).permute(0		,      3		,      1		,      2 ).contiguous()
                           _UpperCAmelCase  :						List[str]							   =					self.proj_out(A )
                    else:
                           _UpperCAmelCase  :						Dict							   =					self.proj_out(A )
                           _UpperCAmelCase  :						List[Any]							   =					hidden_states.reshape(A		,      A		,      A		,      A ).permute(0		,      3		,      1		,      2 ).contiguous()

                    _UpperCAmelCase  :						str							   =					hidden_states + residual
             elif self.is_input_vectorized:
                    _UpperCAmelCase  :						List[Any]							   =					self.norm_out(A )
                    _UpperCAmelCase  :						Dict							   =					self.out(A )
                    # (batch, self.num_vector_embeds - 1, self.num_latent_pixels)
                    _UpperCAmelCase  :						Any							   =					logits.permute(0		,      2		,      1 )

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

                    # unpatchify
                    _UpperCAmelCase  :						Any							   =					int(hidden_states.shape[1] ** 0.5 )
                    _UpperCAmelCase  :						str							   =					hidden_states.reshape(
                        shape=(-1, height, width, self.patch_size, self.patch_size, self.out_channels) )
                    _UpperCAmelCase  :						Optional[int]							   =					torch.einsum("nhwpqc->nchpwq"		,      A )
                    _UpperCAmelCase  :						str							   =					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 typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
)


__SCREAMING_SNAKE_CASE :  Union[str, Any]  =						{
    """configuration_gptsan_japanese""": ["""GPTSAN_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GPTSanJapaneseConfig"""],
    """tokenization_gptsan_japanese""": ["""GPTSanJapaneseTokenizer"""],
}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __SCREAMING_SNAKE_CASE :  Optional[int]  =						[
      """GPTSAN_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """GPTSanJapaneseForConditionalGeneration""",
      """GPTSanJapaneseModel""",
      """GPTSanJapanesePreTrainedModel""",
  ]
  __SCREAMING_SNAKE_CASE :  Union[str, Any]  =						[
      """GPTSanJapaneseTokenizer""",
  ]


if TYPE_CHECKING:
  from .configuration_gptsan_japanese import GPTSAN_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTSanJapaneseConfig
  from .tokenization_gptsan_japanese import GPTSanJapaneseTokenizer

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_gptsan_japanese import (
        GPTSAN_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST,
        GPTSanJapaneseForConditionalGeneration,
        GPTSanJapaneseModel,
        GPTSanJapanesePreTrainedModel,
    )
    from .tokenization_gptsan_japanese import GPTSanJapaneseTokenizer


else:
  import sys

  __SCREAMING_SNAKE_CASE :  Union[str, Any]  =						_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

