

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,
    convert_to_rgb,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


A__						=							logging.get_logger(__name__)


if is_vision_available():
						import PIL




class 				__UpperCamelCase				(					SCREAMING_SNAKE_CASE			):
	_lowercase  :   Optional[int]       				=  ["pixel_values"]







	def __init__(   self: List[str]  , __UpperCamelCase: bool = True  , __UpperCamelCase: Dict[str, int] = None  , __UpperCamelCase: PILImageResampling = PILImageResampling.BICUBIC  , __UpperCamelCase: bool = True  , __UpperCamelCase: Dict[str, int] = None  , __UpperCamelCase: bool = True  , __UpperCamelCase: Union[int, float] = 1 / 2_55  , __UpperCamelCase: bool = True  , __UpperCamelCase: Optional[Union[float, List[float]]] = None  , __UpperCamelCase: Optional[Union[float, List[float]]] = None  , __UpperCamelCase: bool = True  , **__UpperCamelCase: Dict  , ):

						'''simple docstring'''





						super().__init__(**UpperCamelCase_      )
						__magic_name__							=    size if size is not None else {'shortest_edge': 2_24}
						__magic_name__							=    get_size_dict(UpperCamelCase_  , default_to_square=UpperCamelCase_      )
						__magic_name__							=    crop_size if crop_size is not None else {'height': 2_24, 'width': 2_24}
						__magic_name__							=    get_size_dict(UpperCamelCase_  , default_to_square=UpperCamelCase_  , param_name='crop_size'      )

						__magic_name__							=    do_resize
						__magic_name__							=    size
						__magic_name__							=    resample
						__magic_name__							=    do_center_crop
						__magic_name__							=    crop_size
						__magic_name__							=    do_rescale
						__magic_name__							=    rescale_factor
						__magic_name__							=    do_normalize
						__magic_name__							=    image_mean if image_mean is not None else OPENAI_CLIP_MEAN
						__magic_name__							=    image_std if image_std is not None else OPENAI_CLIP_STD
						__magic_name__							=    do_convert_rgb







	def 	_SCREAMING_SNAKE_CASE							(   self: Optional[int]  , __UpperCamelCase: np.ndarray  , __UpperCamelCase: Dict[str, int]  , __UpperCamelCase: PILImageResampling = PILImageResampling.BICUBIC  , __UpperCamelCase: Optional[Union[str, ChannelDimension]] = None  , **__UpperCamelCase: Dict  , ):

						'''simple docstring'''





						__magic_name__							=    get_size_dict(UpperCamelCase_  , default_to_square=UpperCamelCase_      )
						if "shortest_edge" not in size:
											raise ValueError(F'The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}'      )
						__magic_name__							=    get_resize_output_image_size(UpperCamelCase_  , size=size['shortest_edge']  , default_to_square=UpperCamelCase_      )
						return resize(UpperCamelCase_  , size=UpperCamelCase_  , resample=UpperCamelCase_  , data_format=UpperCamelCase_  , **UpperCamelCase_      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Tuple  , __UpperCamelCase: np.ndarray  , __UpperCamelCase: Dict[str, int]  , __UpperCamelCase: Optional[Union[str, ChannelDimension]] = None  , **__UpperCamelCase: Tuple  , ):

						'''simple docstring'''





						__magic_name__							=    get_size_dict(UpperCamelCase_      )
						if "height" not in size or "width" not in size:
											raise ValueError(F'The `size` parameter must contain the keys (height, width). Got {size.keys()}'      )
						return center_crop(UpperCamelCase_  , size=(size['height'], size['width'])  , data_format=UpperCamelCase_  , **UpperCamelCase_      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Tuple  , __UpperCamelCase: np.ndarray  , __UpperCamelCase: Union[int, float]  , __UpperCamelCase: Optional[Union[str, ChannelDimension]] = None  , **__UpperCamelCase: List[str]  , ):

						'''simple docstring'''





						return rescale(UpperCamelCase_  , scale=UpperCamelCase_  , data_format=UpperCamelCase_  , **UpperCamelCase_      )







	def 	_SCREAMING_SNAKE_CASE							(   self: Tuple  , __UpperCamelCase: np.ndarray  , __UpperCamelCase: Union[float, List[float]]  , __UpperCamelCase: Union[float, List[float]]  , __UpperCamelCase: Optional[Union[str, ChannelDimension]] = None  , **__UpperCamelCase: Tuple  , ):

						'''simple docstring'''





						return normalize(UpperCamelCase_  , mean=UpperCamelCase_  , std=UpperCamelCase_  , data_format=UpperCamelCase_  , **UpperCamelCase_      )







	def 	_SCREAMING_SNAKE_CASE							(   self: List[Any]  , __UpperCamelCase: ImageInput  , __UpperCamelCase: bool = None  , __UpperCamelCase: Dict[str, int] = None  , __UpperCamelCase: PILImageResampling = None  , __UpperCamelCase: bool = None  , __UpperCamelCase: int = None  , __UpperCamelCase: bool = None  , __UpperCamelCase: float = None  , __UpperCamelCase: bool = None  , __UpperCamelCase: Optional[Union[float, List[float]]] = None  , __UpperCamelCase: Optional[Union[float, List[float]]] = None  , __UpperCamelCase: bool = None  , __UpperCamelCase: Optional[Union[str, TensorType]] = None  , __UpperCamelCase: Optional[ChannelDimension] = ChannelDimension.FIRST  , **__UpperCamelCase: Optional[Any]  , ):

						'''simple docstring'''





						__magic_name__							=    do_resize if do_resize is not None else self.do_resize
						__magic_name__							=    size if size is not None else self.size
						__magic_name__							=    get_size_dict(UpperCamelCase_  , param_name='size'  , default_to_square=UpperCamelCase_      )
						__magic_name__							=    resample if resample is not None else self.resample
						__magic_name__							=    do_center_crop if do_center_crop is not None else self.do_center_crop
						__magic_name__							=    crop_size if crop_size is not None else self.crop_size
						__magic_name__							=    get_size_dict(UpperCamelCase_  , param_name='crop_size'  , default_to_square=UpperCamelCase_      )
						__magic_name__							=    do_rescale if do_rescale is not None else self.do_rescale
						__magic_name__							=    rescale_factor if rescale_factor is not None else self.rescale_factor
						__magic_name__							=    do_normalize if do_normalize is not None else self.do_normalize
						__magic_name__							=    image_mean if image_mean is not None else self.image_mean
						__magic_name__							=    image_std if image_std is not None else self.image_std
						__magic_name__							=    do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

						__magic_name__							=    make_list_of_images(UpperCamelCase_      )

						if not valid_images(UpperCamelCase_      ):
											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:
											raise ValueError('Size 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.'      )

						# PIL RGBA images are converted to RGB
						if do_convert_rgb:
											__magic_name__							=    [convert_to_rgb(UpperCamelCase_      ) for image in images]

						# All transformations expect numpy arrays.
						__magic_name__							=    [to_numpy_array(UpperCamelCase_      ) for image in images]

						if do_resize:
											__magic_name__							=    [self.resize(image=UpperCamelCase_  , size=UpperCamelCase_  , resample=UpperCamelCase_      ) for image in images]

						if do_center_crop:
											__magic_name__							=    [self.center_crop(image=UpperCamelCase_  , size=UpperCamelCase_      ) for image in images]

						if do_rescale:
											__magic_name__							=    [self.rescale(image=UpperCamelCase_  , scale=UpperCamelCase_      ) for image in images]

						if do_normalize:
											__magic_name__							=    [self.normalize(image=UpperCamelCase_  , mean=UpperCamelCase_  , std=UpperCamelCase_      ) for image in images]

						__magic_name__							=    [to_channel_dimension_format(UpperCamelCase_  , UpperCamelCase_      ) for image in images]

						__magic_name__							=    {'pixel_values': images}
						return BatchFeature(data=UpperCamelCase_  , tensor_type=UpperCamelCase_      )







import torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin

class 				__UpperCamelCase				(					SCREAMING_SNAKE_CASE  ,						SCREAMING_SNAKE_CASE			):







	@register_to_config
	def __init__(   self: Optional[Any]  , *,
	    __UpperCamelCase: int = 4  , __UpperCamelCase: int = 7_68  , __UpperCamelCase: int  , __UpperCamelCase: Any  , ):

						'''simple docstring'''





						super().__init__()

						__magic_name__							=    nn.Parameter(torch.zeros(__UpperCamelCase      )      )

						# parameters for additional clip time embeddings
						__magic_name__							=    nn.Linear(__UpperCamelCase  , __UpperCamelCase      )
						__magic_name__							=    nn.Linear(__UpperCamelCase  , __UpperCamelCase      )

						# parameters for encoder hidden states
						__magic_name__							=    clip_extra_context_tokens
						__magic_name__							=    nn.Linear(
						    __UpperCamelCase  , self.clip_extra_context_tokens * cross_attention_dim      )
						__magic_name__							=    nn.Linear(__UpperCamelCase  , __UpperCamelCase      )
						__magic_name__							=    nn.LayerNorm(__UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: List[str]  , *, __UpperCamelCase: Union[str, Any]  , __UpperCamelCase: str  , __UpperCamelCase: Optional[Any]  , __UpperCamelCase: Any      ):

						'''simple docstring'''





						if do_classifier_free_guidance:
											# Add the classifier free guidance embeddings to the image embeddings
											__magic_name__							=    image_embeddings.shape[0]
											__magic_name__							=    self.learned_classifier_free_guidance_embeddings.unsqueeze(0      )
											__magic_name__							=    classifier_free_guidance_embeddings.expand(
											    __UpperCamelCase  , -1      )
											__magic_name__							=    torch.cat([classifier_free_guidance_embeddings, image_embeddings]  , dim=0      )

						# The image embeddings batch size and the text embeddings batch size are equal
						assert image_embeddings.shape[0] == prompt_embeds.shape[0]

						__magic_name__							=    prompt_embeds.shape[0]

						# "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and
						# adding CLIP embeddings to the existing timestep embedding, ...
						__magic_name__							=    self.embedding_proj(__UpperCamelCase      )
						__magic_name__							=    self.clip_image_embeddings_project_to_time_embeddings(__UpperCamelCase      )
						__magic_name__							=    time_projected_image_embeddings + time_projected_prompt_embeds

						# ... and by projecting CLIP embeddings into four
						# extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder"
						__magic_name__							=    self.clip_extra_context_tokens_proj(__UpperCamelCase      )
						__magic_name__							=    clip_extra_context_tokens.reshape(__UpperCamelCase  , -1  , self.clip_extra_context_tokens      )
						__magic_name__							=    clip_extra_context_tokens.permute(0  , 2  , 1      )

						__magic_name__							=    self.encoder_hidden_states_proj(__UpperCamelCase      )
						__magic_name__							=    self.text_encoder_hidden_states_norm(__UpperCamelCase      )
						__magic_name__							=    torch.cat([clip_extra_context_tokens, text_encoder_hidden_states]  , dim=1      )

						return text_encoder_hidden_states, additive_clip_time_embeddings




