






"""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   :					str      ,      *,
	    lowerCAmelCase_   :					int = 4      ,      lowerCAmelCase_   :					int = 768      ,      lowerCAmelCase_   :					int      ,      lowerCAmelCase_   :					Tuple      ,      )		->     Tuple:
				super().__init__()

				UpperCAmelCase_	:					List[Any]     	=   nn.Parameter(torch.zeros(SCREAMING_SNAKE_CASE_		)		)

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

				# parameters for encoder hidden states
				UpperCAmelCase_	:					Tuple     	=   clip_extra_context_tokens
				UpperCAmelCase_	:					Dict     	=   nn.Linear(
				    SCREAMING_SNAKE_CASE_      ,      self.clip_extra_context_tokens * cross_attention_dim		)
				UpperCAmelCase_	:					Union[str, Any]     	=   nn.Linear(SCREAMING_SNAKE_CASE_      ,      SCREAMING_SNAKE_CASE_		)
				UpperCAmelCase_	:					str     	=   nn.LayerNorm(SCREAMING_SNAKE_CASE_		)





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

				UpperCAmelCase_	:					Any     	=   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, ...
				UpperCAmelCase_	:					List[Any]     	=   self.embedding_proj(SCREAMING_SNAKE_CASE_		)
				UpperCAmelCase_	:					List[Any]     	=   self.clip_image_embeddings_project_to_time_embeddings(SCREAMING_SNAKE_CASE_		)
				UpperCAmelCase_	:					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"
				UpperCAmelCase_	:					Tuple     	=   self.clip_extra_context_tokens_proj(SCREAMING_SNAKE_CASE_		)
				UpperCAmelCase_	:					List[str]     	=   clip_extra_context_tokens.reshape(SCREAMING_SNAKE_CASE_      ,      -1      ,      self.clip_extra_context_tokens		)
				UpperCAmelCase_	:					int     	=   clip_extra_context_tokens.permute(0      ,      2      ,      1		)

				UpperCAmelCase_	:					Any     	=   self.encoder_hidden_states_proj(SCREAMING_SNAKE_CASE_		)
				UpperCAmelCase_	:					List[str]     	=   self.text_encoder_hidden_states_norm(SCREAMING_SNAKE_CASE_		)
				UpperCAmelCase_	:					Dict     	=   torch.cat([clip_extra_context_tokens, text_encoder_hidden_states]      ,      dim=1		)

				return text_encoder_hidden_states, additive_clip_time_embeddings





import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging


__a         =   logging.get_logger(__name__)
class 					lowercase__(							UpperCAmelCase				):



	"""simple docstring"""




	a     :List[str]       	=      'encoder-decoder'
	a     :Any       	=      True
	def __init__(			self    :     List[Any]			,				**SCREAMING_SNAKE_CASE_    :     List[Any]			)			->	Optional[int]:
					super().__init__(**SCREAMING_SNAKE_CASE_			)
					assert (
					    "encoder" in kwargs and "decoder" in kwargs
					), "Config has to be initialized with encoder and decoder config"
					lowercase_	    =   kwargs.pop('''encoder'''			)
					lowercase_	    =   encoder_config.pop('''model_type'''			)
					lowercase_	    =   kwargs.pop('''decoder'''			)
					lowercase_	    =   decoder_config.pop('''model_type'''			)

					from ..auto.configuration_auto import AutoConfig

					lowercase_	    =   AutoConfig.for_model(SCREAMING_SNAKE_CASE_			,				**SCREAMING_SNAKE_CASE_			)
					lowercase_	    =   AutoConfig.for_model(SCREAMING_SNAKE_CASE_			,				**SCREAMING_SNAKE_CASE_			)
					lowercase_	    =   True
	@classmethod
	def 		_lowercase    (			cls    :     Optional[int]			,				SCREAMING_SNAKE_CASE_    :     PretrainedConfig			,				SCREAMING_SNAKE_CASE_    :     PretrainedConfig			,				**SCREAMING_SNAKE_CASE_    :     Optional[int]			)			->	PretrainedConfig:
					logger.info('''Set `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config'''			)
					lowercase_	    =   True
					lowercase_	    =   True

					return cls(encoder=encoder_config.to_dict()			,				decoder=decoder_config.to_dict()			,				**SCREAMING_SNAKE_CASE_			)




	def 		_lowercase    (			self    :     Tuple			)			->	List[Any]:
					lowercase_	    =   copy.deepcopy(self.__dict__			)
					lowercase_	    =   self.encoder.to_dict()
					lowercase_	    =   self.decoder.to_dict()
					lowercase_	    =   self.__class__.model_type
					return output




