




'''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 					lowerCamelCase_							(	UpperCAmelCase_							):




					'''simple docstring'''






					lowercase__   =			42





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

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

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

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

									_snake_case     	=	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"

									_snake_case     	=	sample_size
									_snake_case     	=	sample_size

									_snake_case     	=	patch_size
									_snake_case     	=	PatchEmbed(
									    height=__UpperCamelCase,  width=__UpperCamelCase,  patch_size=__UpperCamelCase,  in_channels=__UpperCamelCase,  embed_dim=__UpperCamelCase,  )

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






					def     __SCREAMING_SNAKE_CASE     (   self,  __UpperCamelCase,  __UpperCamelCase = None,  __UpperCamelCase = None,  __UpperCamelCase = None,  __UpperCamelCase = None,  __UpperCamelCase = None,  __UpperCamelCase = None,  __UpperCamelCase = 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)
									_snake_case     	=	(1 - attention_mask.to(hidden_states.dtype			)) * -1_00_00.0
									_snake_case     	=	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:
									_snake_case     	=	(1 - encoder_attention_mask.to(hidden_states.dtype			)) * -1_00_00.0
									_snake_case     	=	encoder_attention_mask.unsqueeze(1			)

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

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

							# 2. Blocks
							for block in self.transformer_blocks:
									_snake_case     	=	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:
											_snake_case     	=	hidden_states.reshape(__UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase			).permute(0,  3,  1,  2			).contiguous()
											_snake_case     	=	self.proj_out(__UpperCamelCase			)
									else:
											_snake_case     	=	self.proj_out(__UpperCamelCase			)
											_snake_case     	=	hidden_states.reshape(__UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase			).permute(0,  3,  1,  2			).contiguous()

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

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

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











'''simple docstring'''







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


_lowerCamelCase							:						List[Any]							     =  {
    'configuration_mobilenet_v1': [
        'MOBILENET_V1_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'MobileNetV1Config',
        'MobileNetV1OnnxConfig',
    ],
}

try:
						if not is_vision_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_lowerCamelCase							:						str							     =  ['MobileNetV1FeatureExtractor']
						_lowerCamelCase							:						Tuple							     =  ['MobileNetV1ImageProcessor']

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						_lowerCamelCase							:						List[Any]							     =  [
						    'MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST',
						    'MobileNetV1ForImageClassification',
						    'MobileNetV1Model',
						    'MobileNetV1PreTrainedModel',
						    'load_tf_weights_in_mobilenet_v1',
						]


if TYPE_CHECKING:
						from .configuration_mobilenet_va import (
						    MOBILENET_V1_PRETRAINED_CONFIG_ARCHIVE_MAP,
						    MobileNetVaConfig,
						    MobileNetVaOnnxConfig,
						)

						try:
												if not is_vision_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .feature_extraction_mobilenet_va import MobileNetVaFeatureExtractor
												from .image_processing_mobilenet_va import MobileNetVaImageProcessor

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_mobilenet_va import (
												    MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST,
												    MobileNetVaForImageClassification,
												    MobileNetVaModel,
												    MobileNetVaPreTrainedModel,
												    load_tf_weights_in_mobilenet_va,
												)


else:
						import sys

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





