






"""simple docstring"""





import torch
from torch import nn

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



class       __A		(   SCREAMING_SNAKE_CASE_     ,SCREAMING_SNAKE_CASE_			):

					@register_to_config
					def __init__(			self  :      Union[str, Any]     ,		*,
					    __snake_case  :      int = 4     ,		__snake_case  :      int = 7_6_8     ,		__snake_case  :      int     ,		__snake_case  :      List[Any]     ,		)			->							str:
											super().__init__()

											__magic_name__:	Tuple							=       nn.Parameter(torch.zeros(__snake_case					)					)

											# parameters for additional clip time embeddings
											__magic_name__:	Optional[int]							=       nn.Linear(__snake_case     ,		__snake_case					)
											__magic_name__:	int							=       nn.Linear(__snake_case     ,		__snake_case					)

											# parameters for encoder hidden states
											__magic_name__:	Tuple							=       clip_extra_context_tokens
											__magic_name__:	Union[str, Any]							=       nn.Linear(
											    __snake_case     ,		self.clip_extra_context_tokens * cross_attention_dim					)
											__magic_name__:	Optional[int]							=       nn.Linear(__snake_case     ,		__snake_case					)
											__magic_name__:	Union[str, Any]							=       nn.LayerNorm(__snake_case					)






					def   lowerCamelCase__   (			self  :      Dict     ,		*, __snake_case  :      Any     ,		__snake_case  :      List[Any]     ,		__snake_case  :      str     ,		__snake_case  :      List[str]					)			->							Optional[Any]:
											if do_classifier_free_guidance:
																	# Add the classifier free guidance embeddings to the image embeddings
																	__magic_name__:	Tuple							=       image_embeddings.shape[0]
																	__magic_name__:	Tuple							=       self.learned_classifier_free_guidance_embeddings.unsqueeze(0					)
																	__magic_name__:	List[Any]							=       classifier_free_guidance_embeddings.expand(
																	    __snake_case     ,		-1					)
																	__magic_name__:	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]

											__magic_name__:	Tuple							=       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__:	Optional[Any]							=       self.embedding_proj(__snake_case					)
											__magic_name__:	str							=       self.clip_image_embeddings_project_to_time_embeddings(__snake_case					)
											__magic_name__:	List[str]							=       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__:	str							=       self.clip_extra_context_tokens_proj(__snake_case					)
											__magic_name__:	Dict							=       clip_extra_context_tokens.reshape(__snake_case     ,		-1     ,		self.clip_extra_context_tokens					)
											__magic_name__:	Tuple							=       clip_extra_context_tokens.permute(0     ,		2     ,		1					)

											__magic_name__:	Optional[int]							=       self.encoder_hidden_states_proj(__snake_case					)
											__magic_name__:	int							=       self.text_encoder_hidden_states_norm(__snake_case					)
											__magic_name__:	Optional[int]							=       torch.cat([clip_extra_context_tokens, text_encoder_hidden_states]     ,		dim=1					)

											return text_encoder_hidden_states, additive_clip_time_embeddings











"""simple docstring"""





from typing import List

from .keymap import KEYMAP, get_character



def   a    (     __UpperCAmelCase      :    str  )    ->							Optional[int]:

						def decorator(__UpperCAmelCase      :    List[str]  ):
												__magic_name__:	Union[str, Any]							=       getattr(__UpperCAmelCase      ,   """handle_key"""      ,   []  )
												handle += [key]
												setattr(__UpperCAmelCase      ,   """handle_key"""      ,   __UpperCAmelCase  )
												return func

						return decorator



def   a    (     *__UpperCAmelCase      :    List[str]  )    ->							Union[str, Any]:

						def decorator(__UpperCAmelCase      :    Tuple  ):
												__magic_name__:	Union[str, Any]							=       getattr(__UpperCAmelCase      ,   """handle_key"""      ,   []  )
												handle += keys
												setattr(__UpperCAmelCase      ,   """handle_key"""      ,   __UpperCAmelCase  )
												return func

						return decorator



class       __A		(   SCREAMING_SNAKE_CASE_			):

					def __new__(			cls  :      Optional[Any]     ,		__snake_case  :      List[str]     ,		__snake_case  :      Optional[Any]     ,		__snake_case  :      Any					)			->							List[Any]:
											__magic_name__:	str							=       super().__new__(cls     ,		__snake_case     ,		__snake_case     ,		__snake_case					)
											if not hasattr(__snake_case     ,		"""key_handler"""					):
																	setattr(__snake_case     ,		"""key_handler"""     ,		{}					)
											setattr(__snake_case     ,		"""handle_input"""     ,		KeyHandler.handle_input					)

											for value in attrs.values():
																	__magic_name__:	List[str]							=       getattr(__snake_case     ,		"""handle_key"""     ,		[]					)
																	for key in handled_keys:
																							__magic_name__:	Any							=       value
											return new_cls






					@staticmethod
					def   lowerCamelCase__   (			cls  :      List[Any]					)			->							Optional[Any]:
											__magic_name__:	List[str]							=       get_character()
											if char != KEYMAP["undefined"]:
																	__magic_name__:	Union[str, Any]							=       ord(__snake_case					)
											__magic_name__:	Dict							=       cls.key_handler.get(__snake_case					)
											if handler:
																	__magic_name__:	List[str]							=       char
																	return handler(cls					)
											else:
																	return None



def   a    (     cls      :    List[Any]  )    ->							int:
						return KeyHandler(cls.__name__      ,   cls.__bases__      ,   cls.__dict__.copy()  )



