
'''simple docstring'''







import torch
from torch import nn

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






class 			lowerCAmelCase__       (  lowerCamelCase_   ,						lowerCamelCase_					):






     @register_to_config
     def __init__(       self    ,							*,
         __SCREAMING_SNAKE_CASE = 4    ,							__SCREAMING_SNAKE_CASE = 7_68    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							):


           """simple docstring"""





           super().__init__()

           lowercase_     :					Optional[int]													=	nn.Parameter(torch.zeros(__UpperCAmelCase		)		)

           # parameters for additional clip time embeddings
           lowercase_     :					str													=	nn.Linear(__UpperCAmelCase    ,							__UpperCAmelCase		)
           lowercase_     :					Union[str, Any]													=	nn.Linear(__UpperCAmelCase    ,							__UpperCAmelCase		)

           # parameters for encoder hidden states
           lowercase_     :					Optional[Any]													=	clip_extra_context_tokens
           lowercase_     :					List[str]													=	nn.Linear(
               __UpperCAmelCase    ,							self.clip_extra_context_tokens * cross_attention_dim		)
           lowercase_     :					Dict													=	nn.Linear(__UpperCAmelCase    ,							__UpperCAmelCase		)
           lowercase_     :					List[Any]													=	nn.LayerNorm(__UpperCAmelCase		)






     def      _snake_case   (       self    ,							*, __SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		):


           """simple docstring"""





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

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

           lowercase_     :					List[Any]													=	self.encoder_hidden_states_proj(__UpperCAmelCase		)
           lowercase_     :					Tuple													=	self.text_encoder_hidden_states_norm(__UpperCAmelCase		)
           lowercase_     :					List[Any]													=	torch.cat([clip_extra_context_tokens, text_encoder_hidden_states]    ,							dim=1		)

           return text_encoder_hidden_states, additive_clip_time_embeddings





'''simple docstring'''







import logging
import os

from .state import PartialState






class 			lowerCAmelCase__       (  logging.LoggerAdapter					):






					@staticmethod
					def      _snake_case   (       __SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											lowercase_     :					Optional[Any]													=	PartialState()
											return not main_process_only or (main_process_only and state.is_main_process)






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							*__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											if PartialState._shared_state == {}:
																	raise RuntimeError(
																	    '''You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility.'''		)
											lowercase_     :					Tuple													=	kwargs.pop('''main_process_only'''    ,							__SCREAMING_SNAKE_CASE		)
											lowercase_     :					Optional[int]													=	kwargs.pop('''in_order'''    ,							__SCREAMING_SNAKE_CASE		)

											if self.isEnabledFor(__SCREAMING_SNAKE_CASE		):
																	if self._should_log(__SCREAMING_SNAKE_CASE		):
																							lowercase_						,			lowercase_     :					Optional[Any]													=	self.process(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)
																							self.logger.log(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							*__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE		)

																	elif in_order:
																							lowercase_     :					Optional[Any]													=	PartialState()
																							for i in range(state.num_processes		):
																													if i == state.process_index:
																																			lowercase_						,			lowercase_     :					Optional[int]													=	self.process(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)
																																			self.logger.log(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE    ,							*__SCREAMING_SNAKE_CASE    ,							**__SCREAMING_SNAKE_CASE		)
																													state.wait_for_everyone()







def  snake_case_		(    __SCREAMING_SNAKE_CASE :			str				,				__SCREAMING_SNAKE_CASE :			str = None							):



						"""simple docstring"""





						if log_level is None:
												lowercase_     :					Any													=	os.environ.get('''ACCELERATE_LOG_LEVEL'''				,				__SCREAMING_SNAKE_CASE							)
						lowercase_     :					List[str]													=	logging.getLogger(__SCREAMING_SNAKE_CASE							)
						if log_level is not None:
												logger.setLevel(log_level.upper()							)
												logger.root.setLevel(log_level.upper()							)
						return MultiProcessAdapter(__SCREAMING_SNAKE_CASE				,				{}							)



