






'''simple docstring'''







import copy
import random

from transformers import CLIPTokenizer

class _lowercase	(				_lowercase		):




							def __init__(							self:				List[str]			,							*UpperCamelCase__:				List[Any]			,							**UpperCamelCase__:				List[Any]     ):
														super().__init__(*UpperCamelCase__			,							**UpperCamelCase__     )
														lowerCamelCase__					:	Any         =       {}




							def 		lowerCamelCase_				(							self:				Union[str, Any]			,							UpperCamelCase__:				Optional[Any]			,							*UpperCamelCase__:				Union[str, Any]			,							**UpperCamelCase__:				Union[str, Any]     ):
														lowerCamelCase__					:	str         =       super().add_tokens(UpperCamelCase__			,							*UpperCamelCase__			,							**UpperCamelCase__     )
														if num_added_tokens == 0:
																					raise ValueError(
																					    F'''The tokenizer already contains the token {placeholder_token}. Please pass a different'''
																					    """ `placeholder_token` that is not already in the tokenizer."""     )




							def 		lowerCamelCase_				(							self:				Optional[Any]			,							UpperCamelCase__:				Optional[Any]			,							*UpperCamelCase__:				List[Any]			,							UpperCamelCase__:				Dict=1			,							**UpperCamelCase__:				int     ):
														lowerCamelCase__					:	Dict         =       []
														if num_vec_per_token == 1:
																					self.try_adding_tokens(UpperCamelCase__			,							*UpperCamelCase__			,							**UpperCamelCase__     )
																					output.append(UpperCamelCase__     )
														else:
																					lowerCamelCase__					:	Any         =       []
																					for i in range(UpperCamelCase__     ):
																												lowerCamelCase__					:	Dict         =       placeholder_token + F'''_{i}'''
																												self.try_adding_tokens(UpperCamelCase__			,							*UpperCamelCase__			,							**UpperCamelCase__     )
																												output.append(UpperCamelCase__     )
        # handle cases where there is a new placeholder token that contains the current placeholder token but is larger
														for token in self.token_map:
																					if token in placeholder_token:
																												raise ValueError(
																												    F'''The tokenizer already has placeholder token {token} that can get confused with'''
																												    F''' {placeholder_token}keep placeholder tokens independent'''     )
														lowerCamelCase__					:	Tuple         =       output




							def 		lowerCamelCase_				(							self:				Optional[Any]			,							UpperCamelCase__:				Tuple			,							UpperCamelCase__:				Optional[Any]=False			,							UpperCamelCase__:				Any=1.0     ):
														if isinstance(UpperCamelCase__			,							UpperCamelCase__     ):
																					lowerCamelCase__					:	int         =       []
																					for i in range(len(UpperCamelCase__     )     ):
																												output.append(self.replace_placeholder_tokens_in_text(text[i]			,							vector_shuffle=UpperCamelCase__     )     )
																					return output
														for placeholder_token in self.token_map:
																					if placeholder_token in text:
																												lowerCamelCase__					:	Optional[Any]         =       self.token_map[placeholder_token]
																												lowerCamelCase__					:	Tuple         =       tokens[: 1 + int(len(UpperCamelCase__     ) * prop_tokens_to_load     )]
																												if vector_shuffle:
																																			lowerCamelCase__					:	List[str]         =       copy.copy(UpperCamelCase__     )
																																			random.shuffle(UpperCamelCase__     )
																												lowerCamelCase__					:	Optional[int]         =       text.replace(UpperCamelCase__			,							""" """.join(UpperCamelCase__     )     )
														return text




							def __call__(							self:				str			,							UpperCamelCase__:				Optional[Any]			,							*UpperCamelCase__:				List[Any]			,							UpperCamelCase__:				str=False			,							UpperCamelCase__:				Optional[int]=1.0			,							**UpperCamelCase__:				Optional[Any]     ):
														return super().__call__(
														    self.replace_placeholder_tokens_in_text(
														        UpperCamelCase__			,							vector_shuffle=UpperCamelCase__			,							prop_tokens_to_load=UpperCamelCase__     )			,							*UpperCamelCase__			,							**UpperCamelCase__			,							)




							def 		lowerCamelCase_				(							self:				Dict			,							UpperCamelCase__:				Union[str, Any]			,							*UpperCamelCase__:				str			,							UpperCamelCase__:				Tuple=False			,							UpperCamelCase__:				Tuple=1.0			,							**UpperCamelCase__:				List[Any]     ):
														return super().encode(
														    self.replace_placeholder_tokens_in_text(
														        UpperCamelCase__			,							vector_shuffle=UpperCamelCase__			,							prop_tokens_to_load=UpperCamelCase__     )			,							*UpperCamelCase__			,							**UpperCamelCase__			,							)








"""simple docstring"""




import torch
from torch import nn

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


class        __UpperCamelCase				(						a__		,       a__						):






							@register_to_config
							def __init__(   self      ,					*,
							    lowerCAmelCase__ = 4      ,					lowerCAmelCase__ = 768      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					)	-> Optional[Any]:
								super().__init__()

								a      :		Tuple	=				nn.Parameter(torch.zeros(lowerCAmelCase__      )      )

								# parameters for additional clip time embeddings
								a      :		str	=				nn.Linear(lowerCAmelCase__      ,					lowerCAmelCase__      )
								a      :		Any	=				nn.Linear(lowerCAmelCase__      ,					lowerCAmelCase__      )

								# parameters for encoder hidden states
								a      :		int	=				clip_extra_context_tokens
								a      :		int	=				nn.Linear(
								    lowerCAmelCase__      ,					self.clip_extra_context_tokens * cross_attention_dim      )
								a      :		Any	=				nn.Linear(lowerCAmelCase__      ,					lowerCAmelCase__      )
								a      :		str	=				nn.LayerNorm(lowerCAmelCase__      )






							def     __a   (   self      ,					*, lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__      ,					lowerCAmelCase__      )	-> Union[str, Any]:
								if do_classifier_free_guidance:
									# Add the classifier free guidance embeddings to the image embeddings
									a      :		str	=				image_embeddings.shape[0]
									a      :		Optional[int]	=				self.learned_classifier_free_guidance_embeddings.unsqueeze(0      )
									a      :		Any	=				classifier_free_guidance_embeddings.expand(
									    lowerCAmelCase__      ,					-1      )
									a      :		Any	=				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]

								a      :		List[str]	=				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, ...
								a      :		Dict	=				self.embedding_proj(lowerCAmelCase__      )
								a      :		List[str]	=				self.clip_image_embeddings_project_to_time_embeddings(lowerCAmelCase__      )
								a      :		Dict	=				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"
								a      :		Union[str, Any]	=				self.clip_extra_context_tokens_proj(lowerCAmelCase__      )
								a      :		List[str]	=				clip_extra_context_tokens.reshape(lowerCAmelCase__      ,					-1      ,					self.clip_extra_context_tokens      )
								a      :		Optional[Any]	=				clip_extra_context_tokens.permute(0      ,					2      ,					1      )

								a      :		Optional[int]	=				self.encoder_hidden_states_proj(lowerCAmelCase__      )
								a      :		str	=				self.text_encoder_hidden_states_norm(lowerCAmelCase__      )
								a      :		List[str]	=				torch.cat([clip_extra_context_tokens, text_encoder_hidden_states]      ,					dim=1      )

								return text_encoder_hidden_states, additive_clip_time_embeddings


