



"""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							):




	'''simple docstring'''





	@register_to_config
	def __init__(     self:		Dict       ,			*,
	    _SCREAMING_SNAKE_CASE:		int = 4       ,			_SCREAMING_SNAKE_CASE:		int = 768       ,			_SCREAMING_SNAKE_CASE:		int       ,			_SCREAMING_SNAKE_CASE:		List[str]       ,			)				->       Tuple:

				"""simple docstring"""

				super().__init__()

				__lowerCAmelCase      :  Optional[Any]												=       nn.Parameter(torch.zeros(_SCREAMING_SNAKE_CASE))

				# parameters for additional clip time embeddings
				__lowerCAmelCase      :  int												=       nn.Linear(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase      :  Dict												=       nn.Linear(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)

				# parameters for encoder hidden states
				__lowerCAmelCase      :  Any												=       clip_extra_context_tokens
				__lowerCAmelCase      :  Optional[int]												=       nn.Linear(
				    _SCREAMING_SNAKE_CASE       ,			self.clip_extra_context_tokens * cross_attention_dim)
				__lowerCAmelCase      :  List[Any]												=       nn.Linear(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase      :  Tuple												=       nn.LayerNorm(_SCREAMING_SNAKE_CASE)







	def 							_SCREAMING_SNAKE_CASE   (     self:		Optional[int]       ,			*, _SCREAMING_SNAKE_CASE:		List[str]       ,			_SCREAMING_SNAKE_CASE:		str       ,			_SCREAMING_SNAKE_CASE:		Union[str, Any]       ,			_SCREAMING_SNAKE_CASE:		Tuple)				->       Any:

				"""simple docstring"""

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

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

				__lowerCAmelCase      :  Optional[int]												=       self.encoder_hidden_states_proj(_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase      :  Optional[Any]												=       self.text_encoder_hidden_states_norm(_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase      :  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 ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


__snake_case							:   Optional[int]      =    logging.get_logger(__name__)



class 			A__   (  __SCREAMING_SNAKE_CASE  ,		__SCREAMING_SNAKE_CASE							):




	'''simple docstring'''





	SCREAMING_SNAKE_CASE          =	'maskformer-swin'

	SCREAMING_SNAKE_CASE          =	{
	    'num_attention_heads': 'num_heads',
	    'num_hidden_layers': 'num_layers',
	}

	def __init__(     self:		Optional[int]       ,			_SCREAMING_SNAKE_CASE:		int=224       ,			_SCREAMING_SNAKE_CASE:		Tuple=4       ,			_SCREAMING_SNAKE_CASE:		int=3       ,			_SCREAMING_SNAKE_CASE:		List[Any]=96       ,			_SCREAMING_SNAKE_CASE:		Union[str, Any]=[2, 2, 6, 2]       ,			_SCREAMING_SNAKE_CASE:		Any=[3, 6, 12, 24]       ,			_SCREAMING_SNAKE_CASE:		List[str]=7       ,			_SCREAMING_SNAKE_CASE:		List[str]=4.0       ,			_SCREAMING_SNAKE_CASE:		Optional[int]=True       ,			_SCREAMING_SNAKE_CASE:		Tuple=0.0       ,			_SCREAMING_SNAKE_CASE:		Any=0.0       ,			_SCREAMING_SNAKE_CASE:		Any=0.1       ,			_SCREAMING_SNAKE_CASE:		str="gelu"       ,			_SCREAMING_SNAKE_CASE:		Tuple=False       ,			_SCREAMING_SNAKE_CASE:		Union[str, Any]=0.02       ,			_SCREAMING_SNAKE_CASE:		str=1e-5       ,			_SCREAMING_SNAKE_CASE:		Optional[int]=None       ,			_SCREAMING_SNAKE_CASE:		str=None       ,			**_SCREAMING_SNAKE_CASE:		Union[str, Any]       ,			)				->       List[str]:

				"""simple docstring"""

				super().__init__(**_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase      :  int												=       image_size
				__lowerCAmelCase      :  Any												=       patch_size
				__lowerCAmelCase      :  Tuple												=       num_channels
				__lowerCAmelCase      :  Any												=       embed_dim
				__lowerCAmelCase      :  Any												=       depths
				__lowerCAmelCase      :  Dict												=       len(_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase      :  List[Any]												=       num_heads
				__lowerCAmelCase      :  Tuple												=       window_size
				__lowerCAmelCase      :  Dict												=       mlp_ratio
				__lowerCAmelCase      :  Any												=       qkv_bias
				__lowerCAmelCase      :  Union[str, Any]												=       hidden_dropout_prob
				__lowerCAmelCase      :  int												=       attention_probs_dropout_prob
				__lowerCAmelCase      :  Tuple												=       drop_path_rate
				__lowerCAmelCase      :  int												=       hidden_act
				__lowerCAmelCase      :  Optional[int]												=       use_absolute_embeddings
				__lowerCAmelCase      :  List[str]												=       layer_norm_eps
				__lowerCAmelCase      :  Any												=       initializer_range
				# we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel
				# this indicates the channel dimension after the last stage of the model
				__lowerCAmelCase      :  Optional[Any]												=       int(embed_dim * 2 ** (len(_SCREAMING_SNAKE_CASE) - 1))
				__lowerCAmelCase      :  Any												=       ["stem"] + [F"""stage{idx}""" for idx in range(1       ,			len(_SCREAMING_SNAKE_CASE) + 1)]
				__lowerCAmelCase						,	__lowerCAmelCase      :  List[str]												=       get_aligned_output_features_output_indices(
				    out_features=_SCREAMING_SNAKE_CASE       ,			out_indices=_SCREAMING_SNAKE_CASE       ,			stage_names=self.stage_names)