



"""simple docstring"""





import torch
import torch.nn as nn
from transformers.modeling_utils import ModuleUtilsMixin
from transformers.models.ta.modeling_ta import TaBlock, TaConfig, TaLayerNorm

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



class    _lowerCAmelCase    (				SCREAMING_SNAKE_CASE__  ,				SCREAMING_SNAKE_CASE__  ,				SCREAMING_SNAKE_CASE__		):





		"""simple docstring"""






		@register_to_config
		def __init__(					self							,    _lowercase							,    _lowercase							,    _lowercase							,    _lowercase							,    _lowercase							,    _lowercase							,    _lowercase							,    _lowercase							,    _lowercase							,    _lowercase = False							,    ) ->						Dict:







								'''simple docstring'''

								super().__init__()

								snake_case_   : List[Any]						=			nn.Embedding(_lowercase							,    _lowercase   )

								snake_case_   : str						=			nn.Embedding(_lowercase							,    _lowercase   )
								snake_case_   : Dict						=			False

								snake_case_   : Optional[int]						=			nn.Dropout(p=_lowercase   )

								snake_case_   : Any						=			TaConfig(
								    vocab_size=_lowercase							,    d_model=_lowercase							,    num_heads=_lowercase							,    d_kv=_lowercase							,    d_ff=_lowercase							,    dropout_rate=_lowercase							,    feed_forward_proj=_lowercase							,    is_decoder=_lowercase							,    is_encoder_decoder=_lowercase							,    )

								snake_case_   : Optional[Any]						=			nn.ModuleList()
								for lyr_num in range(_lowercase   ):
														snake_case_   : Dict						=			TaBlock(_lowercase   )
														self.encoders.append(_lowercase   )

								snake_case_   : Tuple						=			TaLayerNorm(_lowercase   )
								snake_case_   : List[str]						=			nn.Dropout(p=_lowercase   )





		def    UpperCAmelCase__							(					self							,    _lowercase							,    _lowercase   ) ->						str:







								'''simple docstring'''

								snake_case_   : List[str]						=			self.token_embedder(_lowercase   )

								snake_case_   : Optional[Any]						=			encoder_input_tokens.shape[1]
								snake_case_   : str						=			torch.arange(_lowercase							,    device=encoder_input_tokens.device   )
								x += self.position_encoding(_lowercase   )

								snake_case_   : Optional[Any]						=			self.dropout_pre(_lowercase   )

								# inverted the attention mask
								snake_case_   : Optional[int]						=			encoder_input_tokens.size()
								snake_case_   : Optional[int]						=			self.get_extended_attention_mask(_lowercase							,    _lowercase   )

								for lyr in self.encoders:
														snake_case_   : Union[str, Any]						=			lyr(_lowercase							,    _lowercase   )[0]
								snake_case_   : str						=			self.layer_norm(_lowercase   )

								return self.dropout_post(_lowercase   ), encoder_inputs_mask






"""simple docstring"""





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


__lowerCAmelCase       :					str      					=					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    (				SCREAMING_SNAKE_CASE__  ,				SCREAMING_SNAKE_CASE__		):





		"""simple docstring"""






		_lowerCamelCase			  =       '''nat'''

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





		def __init__(					self							,    _lowercase=4							,    _lowercase=3							,    _lowercase=6_4							,    _lowercase=[3, 4, 6, 5]							,    _lowercase=[2, 4, 8, 1_6]							,    _lowercase=7							,    _lowercase=3.0							,    _lowercase=True							,    _lowercase=0.0							,    _lowercase=0.0							,    _lowercase=0.1							,    _lowercase="gelu"							,    _lowercase=0.02							,    _lowercase=1E-5							,    _lowercase=0.0							,    _lowercase=None							,    _lowercase=None							,    **_lowercase							,    ) ->						Optional[Any]:







								'''simple docstring'''

								super().__init__(**_lowercase   )

								snake_case_   : Any						=			patch_size
								snake_case_   : Optional[Any]						=			num_channels
								snake_case_   : Optional[Any]						=			embed_dim
								snake_case_   : Tuple						=			depths
								snake_case_   : int						=			len(_lowercase   )
								snake_case_   : Optional[int]						=			num_heads
								snake_case_   : List[str]						=			kernel_size
								snake_case_   : str						=			mlp_ratio
								snake_case_   : str						=			qkv_bias
								snake_case_   : str						=			hidden_dropout_prob
								snake_case_   : Tuple						=			attention_probs_dropout_prob
								snake_case_   : Tuple						=			drop_path_rate
								snake_case_   : Dict						=			hidden_act
								snake_case_   : Union[str, Any]						=			layer_norm_eps
								snake_case_   : Tuple						=			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
								snake_case_   : Union[str, Any]						=			int(embed_dim * 2 ** (len(_lowercase   ) - 1)   )
								snake_case_   : Union[str, Any]						=			layer_scale_init_value
								snake_case_   : Optional[Any]						=			["""stem"""] + [f'stage{idx}' for idx in range(1							,    len(_lowercase   ) + 1   )]
								snake_case_			,		snake_case_   : Any						=			get_aligned_output_features_output_indices(
								    out_features=_lowercase							,    out_indices=_lowercase							,    stage_names=self.stage_names   )

