code: 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 _lowercase					(       _UpperCAmelCase						):
  
  
  
  
  
  							"""simple docstring"""
  
  
  
  
  							lowerCAmelCase__       =					42
  
  
  
  
  
  
  class _lowercase					(       _UpperCAmelCase   ,				_UpperCAmelCase						):
  
  
  
  
  
  							"""simple docstring"""
  
  
  
  
  							@register_to_config
  							def __init__(    self     ,   UpperCAmelCase = 16     ,   UpperCAmelCase = 88     ,   UpperCAmelCase = None     ,   UpperCAmelCase = None     ,   UpperCAmelCase = 1     ,   UpperCAmelCase = 0.0     ,   UpperCAmelCase = 32     ,   UpperCAmelCase = None     ,   UpperCAmelCase = False     ,   UpperCAmelCase = None     ,   UpperCAmelCase = None     ,   UpperCAmelCase = None     ,   UpperCAmelCase = "geglu"     ,   UpperCAmelCase = None     ,   UpperCAmelCase = False     ,   UpperCAmelCase = False     ,   UpperCAmelCase = False     ,   UpperCAmelCase = "layer_norm"     ,   UpperCAmelCase = 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"""     ,   UpperCAmelCase     ,   standard_warn=UpperCAmelCase  )
  																					_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=UpperCAmelCase     ,   num_channels=UpperCAmelCase     ,   eps=1e-6     ,   affine=UpperCAmelCase  )
  																					if use_linear_projection:
  																												_lowercase				=	nn.Linear(UpperCAmelCase     ,   UpperCAmelCase  )
  																					else:
  																												_lowercase				=	nn.Convad(UpperCAmelCase     ,   UpperCAmelCase     ,   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=UpperCAmelCase     ,   embed_dim=UpperCAmelCase     ,   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=UpperCAmelCase     ,   width=UpperCAmelCase     ,   patch_size=UpperCAmelCase     ,   in_channels=UpperCAmelCase     ,   embed_dim=UpperCAmelCase     ,   )
  
  														# 3. Define transformers blocks
  														_lowercase				=	nn.ModuleList(
  														    [
  														        BasicTransformerBlock(
  														            UpperCAmelCase     ,   UpperCAmelCase     ,   UpperCAmelCase     ,   dropout=UpperCAmelCase     ,   cross_attention_dim=UpperCAmelCase     ,   activation_fn=UpperCAmelCase     ,   num_embeds_ada_norm=UpperCAmelCase     ,   attention_bias=UpperCAmelCase     ,   only_cross_attention=UpperCAmelCase     ,   upcast_attention=UpperCAmelCase     ,   norm_type=UpperCAmelCase     ,   norm_elementwise_affine=UpperCAmelCase     ,   )
  														        for d in range(UpperCAmelCase  )
  														    ]  )
  
  														# 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(UpperCAmelCase     ,   UpperCAmelCase  )
  																					else:
  																												_lowercase				=	nn.Convad(UpperCAmelCase     ,   UpperCAmelCase     ,   kernel_size=1     ,   stride=1     ,   padding=0  )
  														elif self.is_input_vectorized:
  																					_lowercase				=	nn.LayerNorm(UpperCAmelCase  )
  																					_lowercase				=	nn.Linear(UpperCAmelCase     ,   self.num_vector_embeds - 1  )
  														elif self.is_input_patches:
  																					_lowercase				=	nn.LayerNorm(UpperCAmelCase     ,   elementwise_affine=UpperCAmelCase     ,   eps=1e-6  )
  																					_lowercase				=	nn.Linear(UpperCAmelCase     ,   2 * inner_dim  )
  																					_lowercase				=	nn.Linear(UpperCAmelCase     ,   patch_size * patch_size * self.out_channels  )
  
  
  
  							def     _UpperCAmelCase       (    self     ,   UpperCAmelCase     ,   UpperCAmelCase = None     ,   UpperCAmelCase = None     ,   UpperCAmelCase = None     ,   UpperCAmelCase = None     ,   UpperCAmelCase = None     ,   UpperCAmelCase = None     ,   UpperCAmelCase = 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  )) * -10_000.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  )) * -10_000.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(UpperCAmelCase  )
  																					if not self.use_linear_projection:
  																												_lowercase				=	self.proj_in(UpperCAmelCase  )
  																												_lowercase				=	hidden_states.shape[1]
  																												_lowercase				=	hidden_states.permute(0     ,   2     ,   3     ,   1  ).reshape(UpperCAmelCase     ,   height * width     ,   UpperCAmelCase  )
  																					else:
  																												_lowercase				=	hidden_states.shape[1]
  																												_lowercase				=	hidden_states.permute(0     ,   2     ,   3     ,   1  ).reshape(UpperCAmelCase     ,   height * width     ,   UpperCAmelCase  )
  																												_lowercase				=	self.proj_in(UpperCAmelCase  )
  														elif self.is_input_vectorized:
  																					_lowercase				=	self.latent_image_embedding(UpperCAmelCase  )
  														elif self.is_input_patches:
  																					_lowercase				=	self.pos_embed(UpperCAmelCase  )
  
  														# 2. Blocks
  														for block in self.transformer_blocks:
  																					_lowercase				=	block(
  																					    UpperCAmelCase     ,   attention_mask=UpperCAmelCase     ,   encoder_hidden_states=UpperCAmelCase     ,   encoder_attention_mask=UpperCAmelCase     ,   timestep=UpperCAmelCase     ,   cross_attention_kwargs=UpperCAmelCase     ,   class_labels=UpperCAmelCase     ,   )
  
  														# 3. Output
  														if self.is_input_continuous:
  																					if not self.use_linear_projection:
  																												_lowercase				=	hidden_states.reshape(UpperCAmelCase     ,   UpperCAmelCase     ,   UpperCAmelCase     ,   UpperCAmelCase  ).permute(0     ,   3     ,   1     ,   2  ).contiguous()
  																												_lowercase				=	self.proj_out(UpperCAmelCase  )
  																					else:
  																												_lowercase				=	self.proj_out(UpperCAmelCase  )
  																												_lowercase				=	hidden_states.reshape(UpperCAmelCase     ,   UpperCAmelCase     ,   UpperCAmelCase     ,   UpperCAmelCase  ).permute(0     ,   3     ,   1     ,   2  ).contiguous()
  
  																					_lowercase				=	hidden_states + residual
  														elif self.is_input_vectorized:
  																					_lowercase				=	self.norm_out(UpperCAmelCase  )
  																					_lowercase				=	self.out(UpperCAmelCase  )
  																					# (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(
  																					    UpperCAmelCase     ,   UpperCAmelCase     ,   hidden_dtype=hidden_states.dtype  )
  																					_lowercase				,       _lowercase				=	self.proj_out_a(F.silu(UpperCAmelCase  )  ).chunk(2     ,   dim=1  )
  																					_lowercase				=	self.norm_out(UpperCAmelCase  ) * (1 + scale[:, None]) + shift[:, None]
  																					_lowercase				=	self.proj_out_a(UpperCAmelCase  )
  
  																					# 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"""     ,   UpperCAmelCase  )
  																					_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=UpperCAmelCase  )
  
  
  
style_context: from ctypes import c_float, sizeof
  from enum import Enum
  from typing import TYPE_CHECKING, Optional, Union
  
  
  if TYPE_CHECKING:
  				from .. import AutoFeatureExtractor, AutoProcessor, AutoTokenizer  # tests_ignore
  
  
  class _lowercase					(       _UpperCAmelCase						):
  
  
  
  
  
  							"""simple docstring"""
  
  
  
  
  							lowerCAmelCase__       =					c_float
  
  
  
  							@property
  							def     _UpperCAmelCase       (    self  ):
  
  
  
  
  
  
  
  														'''simple docstring'''
  
  
  
  
  
  
  														return sizeof(self.value  )
  
  
  
  
  
  
  def 					__lowerCAmelCase			(  _A ,_A ,_A = 0					):
  
  
  
  							"""simple docstring"""
  
  
  							if dimension <= 0:
  														_lowercase				=	fixed_dimension
  
  							dimension -= num_token_to_add
  							return dimension
  
  
  def 					__lowerCAmelCase			(  _A ,_A					):
  
  
  
  							"""simple docstring"""
  
  
  							return num_parameters * dtype.size
  
  
  
  
  
  
  
  def 					__lowerCAmelCase			(  _A					):
  
  
  
  							"""simple docstring"""
  
  
  							from .. import AutoFeatureExtractor, AutoProcessor, AutoTokenizer  # tests_ignore
  
  							try:
  														return AutoProcessor.from_pretrained(_A					)
  							except (ValueError, OSError, KeyError):
  														_lowercase				,       _lowercase				=	None, None
  														try:
  																					_lowercase				=	AutoTokenizer.from_pretrained(_A					)
  														except (OSError, KeyError):
  																					pass
  														try:
  																					_lowercase				=	AutoFeatureExtractor.from_pretrained(_A					)
  														except (OSError, KeyError):
  																					pass
  
  														if tokenizer is not None and feature_extractor is not None:
  																					raise ValueError(
  																					    f'''Couldn\'t auto-detect preprocessor for {model_name}. Found both a tokenizer and a feature extractor.'''					)
  														elif tokenizer is None and feature_extractor is None:
  																					return None
  														elif tokenizer is not None:
  																					return tokenizer
  														else:
  																					return feature_extractor
  
  
  