





"""simple docstring"""




from typing import Any, Dict, Optional

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

from ..utils import maybe_allow_in_graph
from .activations import get_activation
from .attention_processor import Attention
from .embeddings import CombinedTimestepLabelEmbeddings


@maybe_allow_in_graph
class 			__UpperCamelCase							(		nn.Module		):






							def __init__(  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__=0.0 ,   lowerCAmelCase__ = None ,   lowerCAmelCase__ = "geglu" ,   lowerCAmelCase__ = None ,   lowerCAmelCase__ = False ,   lowerCAmelCase__ = False ,   lowerCAmelCase__ = False ,   lowerCAmelCase__ = False ,   lowerCAmelCase__ = True ,   lowerCAmelCase__ = "layer_norm" ,   lowerCAmelCase__ = False ,   )      ->					Optional[int]:
								super().__init__()
								a :						Union[str, Any]      =							only_cross_attention

								a :						Union[str, Any]      =							(num_embeds_ada_norm is not None) and norm_type == "ada_norm_zero"
								a :						str      =							(num_embeds_ada_norm is not None) and norm_type == "ada_norm"

								if norm_type in ("ada_norm", "ada_norm_zero") and num_embeds_ada_norm is None:
									raise ValueError(
									    f"""`norm_type` is set to {norm_type}, but `num_embeds_ada_norm` is not defined. Please make sure to"""
									    f""" define `num_embeds_ada_norm` if setting `norm_type` to {norm_type}.""" )

								# Define 3 blocks. Each block has its own normalization layer.
								# 1. Self-Attn
								if self.use_ada_layer_norm:
									a :						Tuple      =							AdaLayerNorm(lowerCAmelCase__ ,   lowerCAmelCase__ )
								elif self.use_ada_layer_norm_zero:
									a :						Union[str, Any]      =							AdaLayerNormZero(lowerCAmelCase__ ,   lowerCAmelCase__ )
								else:
									a :						List[Any]      =							nn.LayerNorm(lowerCAmelCase__ ,   elementwise_affine=lowerCAmelCase__ )
								a :						Optional[int]      =							Attention(
								    query_dim=lowerCAmelCase__ ,   heads=lowerCAmelCase__ ,   dim_head=lowerCAmelCase__ ,   dropout=lowerCAmelCase__ ,   bias=lowerCAmelCase__ ,   cross_attention_dim=cross_attention_dim if only_cross_attention else None ,   upcast_attention=lowerCAmelCase__ ,   )

								# 2. Cross-Attn
								if cross_attention_dim is not None or double_self_attention:
									# We currently only use AdaLayerNormZero for self attention where there will only be one attention block.
									# I.e. the number of returned modulation chunks from AdaLayerZero would not make sense if returned during
									# the second cross attention block.
									a :						int      =							(
									    AdaLayerNorm(lowerCAmelCase__ ,   lowerCAmelCase__ )
									    if self.use_ada_layer_norm
									    else nn.LayerNorm(lowerCAmelCase__ ,   elementwise_affine=lowerCAmelCase__ )
									)
									a :						Dict      =							Attention(
									    query_dim=lowerCAmelCase__ ,   cross_attention_dim=cross_attention_dim if not double_self_attention else None ,   heads=lowerCAmelCase__ ,   dim_head=lowerCAmelCase__ ,   dropout=lowerCAmelCase__ ,   bias=lowerCAmelCase__ ,   upcast_attention=lowerCAmelCase__ ,   )  # is self-attn if encoder_hidden_states is none
								else:
									a :						Any      =							None
									a :						List[Any]      =							None

								# 3. Feed-forward
								a :						Any      =							nn.LayerNorm(lowerCAmelCase__ ,   elementwise_affine=lowerCAmelCase__ )
								a :						Any      =							FeedForward(lowerCAmelCase__ ,   dropout=lowerCAmelCase__ ,   activation_fn=lowerCAmelCase__ ,   final_dropout=lowerCAmelCase__ )

								# let chunk size default to None
								a :						Any      =							None
								a :						Tuple      =							0






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					Optional[Any]:
								# Sets chunk feed-forward
								a :						List[str]      =							chunk_size
								a :						Optional[int]      =							dim






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ = None ,   lowerCAmelCase__ = None ,   lowerCAmelCase__ = None ,   lowerCAmelCase__ = None ,   lowerCAmelCase__ = None ,   lowerCAmelCase__ = None ,   )      ->					Any:
								# Notice that normalization is always applied before the real computation in the following blocks.
								# 1. Self-Attention
								if self.use_ada_layer_norm:
									a :						Union[str, Any]      =							self.norma(lowerCAmelCase__ ,   lowerCAmelCase__ )
								elif self.use_ada_layer_norm_zero:
									a,			a,			a,			a,			a :						Dict      =							self.norma(
									    lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   hidden_dtype=hidden_states.dtype )
								else:
									a :						Union[str, Any]      =							self.norma(lowerCAmelCase__ )

								a :						List[str]      =							cross_attention_kwargs if cross_attention_kwargs is not None else {}

								a :						Optional[Any]      =							self.attna(
								    lowerCAmelCase__ ,   encoder_hidden_states=encoder_hidden_states if self.only_cross_attention else None ,   attention_mask=lowerCAmelCase__ ,   **lowerCAmelCase__ ,   )
								if self.use_ada_layer_norm_zero:
									a :						Any      =							gate_msa.unsqueeze(1 ) * attn_output
								a :						str      =							attn_output + hidden_states

								# 2. Cross-Attention
								if self.attna is not None:
									a :						Tuple      =							(
									    self.norma(lowerCAmelCase__ ,   lowerCAmelCase__ ) if self.use_ada_layer_norm else self.norma(lowerCAmelCase__ )
									)

									a :						List[str]      =							self.attna(
									    lowerCAmelCase__ ,   encoder_hidden_states=lowerCAmelCase__ ,   attention_mask=lowerCAmelCase__ ,   **lowerCAmelCase__ ,   )
									a :						int      =							attn_output + hidden_states

								# 3. Feed-forward
								a :						Optional[Any]      =							self.norma(lowerCAmelCase__ )

								if self.use_ada_layer_norm_zero:
									a :						Any      =							norm_hidden_states * (1 + scale_mlp[:, None]) + shift_mlp[:, None]

								if self._chunk_size is not None:
									# "feed_forward_chunk_size" can be used to save memory
									if norm_hidden_states.shape[self._chunk_dim] % self._chunk_size != 0:
										raise ValueError(
										    f"""`hidden_states` dimension to be chunked: {norm_hidden_states.shape[self._chunk_dim]} has to be divisible by chunk size: {self._chunk_size}. Make sure to set an appropriate `chunk_size` when calling `unet.enable_forward_chunking`.""" )

									a :						str      =							norm_hidden_states.shape[self._chunk_dim] // self._chunk_size
									a :						Optional[int]      =							torch.cat(
									    [self.ff(lowerCAmelCase__ ) for hid_slice in norm_hidden_states.chunk(lowerCAmelCase__ ,   dim=self._chunk_dim )] ,   dim=self._chunk_dim ,   )
								else:
									a :						int      =							self.ff(lowerCAmelCase__ )

								if self.use_ada_layer_norm_zero:
									a :						str      =							gate_mlp.unsqueeze(1 ) * ff_output

								a :						Dict      =							ff_output + hidden_states

								return hidden_states


class 			__UpperCamelCase							(		nn.Module		):






							def __init__(  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ = None ,   lowerCAmelCase__ = 4 ,   lowerCAmelCase__ = 0.0 ,   lowerCAmelCase__ = "geglu" ,   lowerCAmelCase__ = False ,   )      ->					List[str]:
								super().__init__()
								a :						int      =							int(dim * mult )
								a :						Union[str, Any]      =							dim_out if dim_out is not None else dim

								if activation_fn == "gelu":
									a :						List[str]      =							GELU(lowerCAmelCase__ ,   lowerCAmelCase__ )
								if activation_fn == "gelu-approximate":
									a :						Tuple      =							GELU(lowerCAmelCase__ ,   lowerCAmelCase__ ,   approximate="tanh" )
								elif activation_fn == "geglu":
									a :						Tuple      =							GEGLU(lowerCAmelCase__ ,   lowerCAmelCase__ )
								elif activation_fn == "geglu-approximate":
									a :						Tuple      =							ApproximateGELU(lowerCAmelCase__ ,   lowerCAmelCase__ )

								a :						List[str]      =							nn.ModuleList([] )
								# project in
								self.net.append(lowerCAmelCase__ )
								# project dropout
								self.net.append(nn.Dropout(lowerCAmelCase__ ) )
								# project out
								self.net.append(nn.Linear(lowerCAmelCase__ ,   lowerCAmelCase__ ) )
								# FF as used in Vision Transformer, MLP-Mixer, etc. have a final dropout
								if final_dropout:
									self.net.append(nn.Dropout(lowerCAmelCase__ ) )






							def        __a  (  self ,   lowerCAmelCase__ )      ->					int:
								for module in self.net:
									a :						str      =							module(lowerCAmelCase__ )
								return hidden_states


class 			__UpperCamelCase							(		nn.Module		):






							def __init__(  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ = "none" )      ->					Optional[Any]:
								super().__init__()
								a :						str      =							nn.Linear(lowerCAmelCase__ ,   lowerCAmelCase__ )
								a :						Tuple      =							approximate






							def        __a  (  self ,   lowerCAmelCase__ )      ->					Tuple:
								if gate.device.type != "mps":
									return F.gelu(lowerCAmelCase__ ,   approximate=self.approximate )
								# mps: gelu is not implemented for float16
								return F.gelu(gate.to(dtype=torch.floataa ) ,   approximate=self.approximate ).to(dtype=gate.dtype )






							def        __a  (  self ,   lowerCAmelCase__ )      ->					List[Any]:
								a :						Optional[int]      =							self.proj(lowerCAmelCase__ )
								a :						Tuple      =							self.gelu(lowerCAmelCase__ )
								return hidden_states


class 			__UpperCamelCase							(		nn.Module		):






							def __init__(  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					List[str]:
								super().__init__()
								a :						Optional[Any]      =							nn.Linear(lowerCAmelCase__ ,   dim_out * 2 )






							def        __a  (  self ,   lowerCAmelCase__ )      ->					Optional[Any]:
								if gate.device.type != "mps":
									return F.gelu(lowerCAmelCase__ )
								# mps: gelu is not implemented for float16
								return F.gelu(gate.to(dtype=torch.floataa ) ).to(dtype=gate.dtype )






							def        __a  (  self ,   lowerCAmelCase__ )      ->					List[str]:
								a,			a :						Tuple      =							self.proj(lowerCAmelCase__ ).chunk(2 ,   dim=-1 )
								return hidden_states * self.gelu(lowerCAmelCase__ )


class 			__UpperCamelCase							(		nn.Module		):






							def __init__(  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					str:
								super().__init__()
								a :						Optional[int]      =							nn.Linear(lowerCAmelCase__ ,   lowerCAmelCase__ )






							def        __a  (  self ,   lowerCAmelCase__ )      ->					List[Any]:
								a :						str      =							self.proj(lowerCAmelCase__ )
								return x * torch.sigmoid(1.702 * x )


class 			__UpperCamelCase							(		nn.Module		):






							def __init__(  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					str:
								super().__init__()
								a :						List[str]      =							nn.Embedding(lowerCAmelCase__ ,   lowerCAmelCase__ )
								a :						Optional[Any]      =							nn.SiLU()
								a :						List[Any]      =							nn.Linear(lowerCAmelCase__ ,   embedding_dim * 2 )
								a :						Union[str, Any]      =							nn.LayerNorm(lowerCAmelCase__ ,   elementwise_affine=lowerCAmelCase__ )






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					int:
								a :						Any      =							self.linear(self.silu(self.emb(lowerCAmelCase__ ) ) )
								a,			a :						Tuple      =							torch.chunk(lowerCAmelCase__ ,   2 )
								a :						Any      =							self.norm(lowerCAmelCase__ ) * (1 + scale) + shift
								return x


class 			__UpperCamelCase							(		nn.Module		):






							def __init__(  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					Union[str, Any]:
								super().__init__()

								a :						int      =							CombinedTimestepLabelEmbeddings(lowerCAmelCase__ ,   lowerCAmelCase__ )

								a :						Any      =							nn.SiLU()
								a :						List[str]      =							nn.Linear(lowerCAmelCase__ ,   6 * embedding_dim ,   bias=lowerCAmelCase__ )
								a :						Dict      =							nn.LayerNorm(lowerCAmelCase__ ,   elementwise_affine=lowerCAmelCase__ ,   eps=1E-6 )






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__=None )      ->					int:
								a :						Optional[Any]      =							self.linear(self.silu(self.emb(lowerCAmelCase__ ,   lowerCAmelCase__ ,   hidden_dtype=lowerCAmelCase__ ) ) )
								a,			a,			a,			a,			a,			a :						Dict      =							emb.chunk(6 ,   dim=1 )
								a :						Dict      =							self.norm(lowerCAmelCase__ ) * (1 + scale_msa[:, None]) + shift_msa[:, None]
								return x, gate_msa, shift_mlp, scale_mlp, gate_mlp


class 			__UpperCamelCase							(		nn.Module		):






							def __init__(  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ = None ,   lowerCAmelCase__ = 1E-5 )      ->					List[str]:
								super().__init__()
								a :						str      =							num_groups
								a :						List[str]      =							eps

								if act_fn is None:
									a :						List[Any]      =							None
								else:
									a :						str      =							get_activation(lowerCAmelCase__ )

								a :						int      =							nn.Linear(lowerCAmelCase__ ,   out_dim * 2 )






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ )      ->					Optional[Any]:
								if self.act:
									a :						Optional[int]      =							self.act(lowerCAmelCase__ )
								a :						Optional[int]      =							self.linear(lowerCAmelCase__ )
								a :						Optional[int]      =							emb[:, :, None, None]
								a,			a :						Tuple      =							emb.chunk(2 ,   dim=1 )

								a :						Any      =							F.group_norm(lowerCAmelCase__ ,   self.num_groups ,   eps=self.eps )
								a :						Optional[Any]      =							x * (1 + scale) + shift
								return x









"""simple docstring"""




from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import center_crop, normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
		import PIL


a		:					int	      =     logging.get_logger(__name__)


class 			__UpperCamelCase							(		a__		):
							lowerCamelCase  :   Optional[int]    =["""pixel_values"""]






							def __init__(  self ,   lowerCAmelCase__ = True ,   lowerCAmelCase__ = None ,   lowerCAmelCase__ = PIL.Image.BICUBIC ,   lowerCAmelCase__ = True ,   lowerCAmelCase__ = None ,   lowerCAmelCase__ = 1 / 255 ,   lowerCAmelCase__ = True ,   lowerCAmelCase__ = True ,   lowerCAmelCase__ = None ,   lowerCAmelCase__ = None ,   **lowerCAmelCase__ ,   )      ->					None:
								super().__init__(**lowerCAmelCase__ )
								a :						Optional[int]      =							size if size is not None else {"height": 256, "width": 256}
								a :						Tuple      =							get_size_dict(lowerCAmelCase__ )
								a :						Any      =							crop_size if crop_size is not None else {"height": 224, "width": 224}
								a :						Dict      =							get_size_dict(lowerCAmelCase__ ,   param_name="crop_size" )

								a :						Tuple      =							do_resize
								a :						int      =							size
								a :						List[str]      =							resample
								a :						List[Any]      =							do_center_crop
								a :						List[Any]      =							crop_size
								a :						Union[str, Any]      =							do_rescale
								a :						List[Any]      =							rescale_factor
								a :						Union[str, Any]      =							do_normalize
								a :						Union[str, Any]      =							image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
								a :						List[str]      =							image_std if image_std is not None else IMAGENET_STANDARD_STD






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ = PIL.Image.BICUBIC ,   lowerCAmelCase__ = None ,   **lowerCAmelCase__ ,   )      ->					np.ndarray:
								a :						Optional[int]      =							get_size_dict(lowerCAmelCase__ )
								if "height" not in size or "width" not in size:
									raise ValueError(f"""The size dictionary must have keys 'height' and 'width'. Got {size.keys()}""" )
								return resize(
								    lowerCAmelCase__ ,   size=(size["height"], size["width"]) ,   resample=lowerCAmelCase__ ,   data_format=lowerCAmelCase__ ,   **lowerCAmelCase__ )






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ = None ,   **lowerCAmelCase__ ,   )      ->					np.ndarray:
								a :						List[str]      =							get_size_dict(lowerCAmelCase__ )
								if "height" not in size or "width" not in size:
									raise ValueError(f"""The size dictionary must have keys 'height' and 'width'. Got {size.keys()}""" )
								return center_crop(lowerCAmelCase__ ,   size=(size["height"], size["width"]) ,   data_format=lowerCAmelCase__ ,   **lowerCAmelCase__ )






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ = None ,   **lowerCAmelCase__ ,   )      ->					Union[str, Any]:
								return rescale(lowerCAmelCase__ ,   scale=lowerCAmelCase__ ,   data_format=lowerCAmelCase__ ,   **lowerCAmelCase__ )






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ ,   lowerCAmelCase__ = None ,   **lowerCAmelCase__ ,   )      ->					np.ndarray:
								return normalize(lowerCAmelCase__ ,   mean=lowerCAmelCase__ ,   std=lowerCAmelCase__ ,   data_format=lowerCAmelCase__ ,   **lowerCAmelCase__ )






							def        __a  (  self ,   lowerCAmelCase__ ,   lowerCAmelCase__ = None ,   lowerCAmelCase__ = None ,   lowerCAmelCase__=None ,   lowerCAmelCase__ = None ,   lowerCAmelCase__ = None ,   lowerCAmelCase__ = None ,   lowerCAmelCase__ = None ,   lowerCAmelCase__ = None ,   lowerCAmelCase__ = None ,   lowerCAmelCase__ = None ,   lowerCAmelCase__ = None ,   lowerCAmelCase__ = ChannelDimension.FIRST ,   **lowerCAmelCase__ ,   )      ->					PIL.Image.Image:
								a :						List[Any]      =							do_resize if do_resize is not None else self.do_resize
								a :						str      =							resample if resample is not None else self.resample
								a :						Dict      =							do_center_crop if do_center_crop is not None else self.do_center_crop
								a :						Union[str, Any]      =							do_rescale if do_rescale is not None else self.do_rescale
								a :						str      =							rescale_factor if rescale_factor is not None else self.rescale_factor
								a :						Dict      =							do_normalize if do_normalize is not None else self.do_normalize
								a :						List[str]      =							image_mean if image_mean is not None else self.image_mean
								a :						Union[str, Any]      =							image_std if image_std is not None else self.image_std

								a :						str      =							size if size is not None else self.size
								a :						Any      =							get_size_dict(lowerCAmelCase__ )
								a :						Dict      =							crop_size if crop_size is not None else self.crop_size
								a :						List[str]      =							get_size_dict(lowerCAmelCase__ ,   param_name="crop_size" )

								a :						str      =							make_list_of_images(lowerCAmelCase__ )

								if not valid_images(lowerCAmelCase__ ):
									raise ValueError(
									    "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
									    "torch.Tensor, tf.Tensor or jax.ndarray." )

								if do_resize and size is None or resample is None:
									raise ValueError("Size and resample must be specified if do_resize is True." )

								if do_center_crop and crop_size is None:
									raise ValueError("Crop size must be specified if do_center_crop is True." )

								if do_rescale and rescale_factor is None:
									raise ValueError("Rescale factor must be specified if do_rescale is True." )

								if do_normalize and (image_mean is None or image_std is None):
									raise ValueError("Image mean and std must be specified if do_normalize is True." )

								# All transformations expect numpy arrays.
								a :						Optional[Any]      =							[to_numpy_array(lowerCAmelCase__ ) for image in images]

								if do_resize:
									a :						Optional[int]      =							[self.resize(image=lowerCAmelCase__ ,   size=lowerCAmelCase__ ,   resample=lowerCAmelCase__ ) for image in images]

								if do_center_crop:
									a :						Dict      =							[self.center_crop(image=lowerCAmelCase__ ,   size=lowerCAmelCase__ ) for image in images]

								if do_rescale:
									a :						Any      =							[self.rescale(image=lowerCAmelCase__ ,   scale=lowerCAmelCase__ ) for image in images]

								if do_normalize:
									a :						Optional[Any]      =							[self.normalize(image=lowerCAmelCase__ ,   mean=lowerCAmelCase__ ,   std=lowerCAmelCase__ ) for image in images]

								a :						Optional[Any]      =							[to_channel_dimension_format(lowerCAmelCase__ ,   lowerCAmelCase__ ) for image in images]

								a :						Tuple      =							{"pixel_values": images}
								return BatchFeature(data=lowerCAmelCase__ ,   tensor_type=lowerCAmelCase__ )


