




import torch
from torch import nn

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


class    lowerCamelCase    (		_lowerCamelCase				,_lowerCamelCase						):






			'''simple docstring'''





			@register_to_config
			def __init__( self			:   Tuple       ,						*,
			    lowerCamelCase_			:   int = 4       ,						lowerCamelCase_			:   int = 768       ,						lowerCamelCase_			:   int       ,						lowerCamelCase_			:   List[Any]       ,						)						->   Dict:
								super().__init__()

								__magic_name__							:	Optional[int]    							=							nn.Parameter(torch.zeros(lowerCamelCase_  )  )

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

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


			def 							UpperCAmelCase__       ( self			:   List[Any]       ,						*, lowerCamelCase_			:   Tuple       ,						lowerCamelCase_			:   Dict       ,						lowerCamelCase_			:   Union[str, Any]       ,						lowerCamelCase_			:   List[str]  )						->   Dict:
								if do_classifier_free_guidance:
													# Add the classifier free guidance embeddings to the image embeddings
													__magic_name__							:	str    							=							image_embeddings.shape[0]
													__magic_name__							:	Optional[Any]    							=							self.learned_classifier_free_guidance_embeddings.unsqueeze(0  )
													__magic_name__							:	Dict    							=							classifier_free_guidance_embeddings.expand(
													    lowerCamelCase_       ,						-1  )
													__magic_name__							:	Optional[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[int]    							=							self.embedding_proj(lowerCamelCase_  )
								__magic_name__							:	Tuple    							=							self.clip_image_embeddings_project_to_time_embeddings(lowerCamelCase_  )
								__magic_name__							:	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__							:	Optional[int]    							=							self.clip_extra_context_tokens_proj(lowerCamelCase_  )
								__magic_name__							:	Dict    							=							clip_extra_context_tokens.reshape(lowerCamelCase_       ,						-1       ,						self.clip_extra_context_tokens  )
								__magic_name__							:	int    							=							clip_extra_context_tokens.permute(0       ,						2       ,						1  )

								__magic_name__							:	Dict    							=							self.encoder_hidden_states_proj(lowerCamelCase_  )
								__magic_name__							:	int    							=							self.text_encoder_hidden_states_norm(lowerCamelCase_  )
								__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









from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


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

__lowerCamelCase   :						Tuple				=			{
    '''shi-labs/nat-mini-in1k-224''': '''https://huggingface.co/shi-labs/nat-mini-in1k-224/resolve/main/config.json''',
    # See all Nat models at https://huggingface.co/models?filter=nat
}


class    lowerCamelCase    (		_lowerCamelCase				,_lowerCamelCase						):






			'''simple docstring'''





			UpperCamelCase__								='''nat'''

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

			def __init__( self			:   Union[str, Any]       ,						lowerCamelCase_			:   Dict=4       ,						lowerCamelCase_			:   Union[str, Any]=3       ,						lowerCamelCase_			:   List[str]=64       ,						lowerCamelCase_			:   Union[str, Any]=[3, 4, 6, 5]       ,						lowerCamelCase_			:   List[Any]=[2, 4, 8, 16]       ,						lowerCamelCase_			:   List[Any]=7       ,						lowerCamelCase_			:   Union[str, Any]=3.0       ,						lowerCamelCase_			:   int=True       ,						lowerCamelCase_			:   str=0.0       ,						lowerCamelCase_			:   int=0.0       ,						lowerCamelCase_			:   Dict=0.1       ,						lowerCamelCase_			:   Optional[int]="gelu"       ,						lowerCamelCase_			:   Tuple=0.0_2       ,						lowerCamelCase_			:   Tuple=1E-5       ,						lowerCamelCase_			:   List[Any]=0.0       ,						lowerCamelCase_			:   str=None       ,						lowerCamelCase_			:   Tuple=None       ,						**lowerCamelCase_			:   Any       ,						)						->   Any:
								super().__init__(**lowerCamelCase_  )

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



